Kaynağa Gözat

[iobuf] Allocate I/O buffer descriptor separately to conserve aligned memory

I/O buffers are allocated on aligned boundaries.  The I/O buffer
descriptor (the struct io_buffer) is currently attached to the end of
the I/O buffer.  When the size of the buffer is close to its
alignment, this can waste large amounts of aligned memory.

For example, a network card using 2048-byte receive buffers will end
up allocating 2072 bytes on a 2048-byte boundary.  This effectively
wastes 50% of the available memory.

Improve the situation by allocating the descriptor separately from the
main I/O buffer if inline allocation would cause the total allocated
size to cross the alignment boundary.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 yıl önce
ebeveyn
işleme
4a8a7bd91a
1 değiştirilmiş dosya ile 53 ekleme ve 13 silme
  1. 53
    13
      src/core/iobuf.c

+ 53
- 13
src/core/iobuf.c Dosyayı Görüntüle

@@ -40,7 +40,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
40 40
  * @c IOBUF_SIZE.
41 41
  */
42 42
 struct io_buffer * alloc_iob ( size_t len ) {
43
-	struct io_buffer *iobuf = NULL;
43
+	struct io_buffer *iobuf;
44 44
 	size_t align;
45 45
 	void *data;
46 46
 
@@ -57,14 +57,37 @@ struct io_buffer * alloc_iob ( size_t len ) {
57 57
 	 */
58 58
 	align = ( 1 << fls ( len - 1 ) );
59 59
 
60
-	/* Allocate memory for buffer plus descriptor */
61
-	data = malloc_dma ( len + sizeof ( *iobuf ), align );
62
-	if ( ! data )
63
-		return NULL;
60
+	/* Allocate buffer plus descriptor as a single unit, unless
61
+	 * doing so will push the total size over the alignment
62
+	 * boundary.
63
+	 */
64
+	if ( ( len + sizeof ( *iobuf ) ) <= align ) {
65
+
66
+		/* Allocate memory for buffer plus descriptor */
67
+		data = malloc_dma ( len + sizeof ( *iobuf ), align );
68
+		if ( ! data )
69
+			return NULL;
70
+		iobuf = ( data + len );
71
+
72
+	} else {
73
+
74
+		/* Allocate memory for buffer */
75
+		data = malloc_dma ( len, align );
76
+		if ( ! data )
77
+			return NULL;
64 78
 
65
-	iobuf = ( struct io_buffer * ) ( data + len );
79
+		/* Allocate memory for descriptor */
80
+		iobuf = malloc ( sizeof ( *iobuf ) );
81
+		if ( ! iobuf ) {
82
+			free_dma ( data, len );
83
+			return NULL;
84
+		}
85
+	}
86
+
87
+	/* Populate descriptor */
66 88
 	iobuf->head = iobuf->data = iobuf->tail = data;
67
-	iobuf->end = iobuf;
89
+	iobuf->end = ( data + len );
90
+
68 91
 	return iobuf;
69 92
 }
70 93
 
@@ -75,12 +98,29 @@ struct io_buffer * alloc_iob ( size_t len ) {
75 98
  * @v iobuf	I/O buffer
76 99
  */
77 100
 void free_iob ( struct io_buffer *iobuf ) {
78
-	if ( iobuf ) {
79
-		assert ( iobuf->head <= iobuf->data );
80
-		assert ( iobuf->data <= iobuf->tail );
81
-		assert ( iobuf->tail <= iobuf->end );
82
-		free_dma ( iobuf->head,
83
-			   ( iobuf->end - iobuf->head ) + sizeof ( *iobuf ) );
101
+	size_t len;
102
+
103
+	/* Allow free_iob(NULL) to be valid */
104
+	if ( ! iobuf )
105
+		return;
106
+
107
+	/* Sanity checks */
108
+	assert ( iobuf->head <= iobuf->data );
109
+	assert ( iobuf->data <= iobuf->tail );
110
+	assert ( iobuf->tail <= iobuf->end );
111
+
112
+	/* Free buffer */
113
+	len = ( iobuf->end - iobuf->head );
114
+	if ( iobuf->end == iobuf ) {
115
+
116
+		/* Descriptor is inline */
117
+		free_dma ( iobuf->head, ( len + sizeof ( *iobuf ) ) );
118
+
119
+	} else {
120
+
121
+		/* Descriptor is detached */
122
+		free_dma ( iobuf->head, len );
123
+		free ( iobuf );
84 124
 	}
85 125
 }
86 126
 

Loading…
İptal
Kaydet