Skip to contentSkip to conversation

xfs: Use kmem_zalloc for bp->b_pages.

PATCH In review Sean Fu 3 participants 4 messages Mar 9, 2019 – Mar 10, 2019 XFS filesystem

lore
Share & tools
lore

Sean Fu

author
#0 lore raw

PATCH 1/1 xfs: Use kmem_zalloc for bp->b_pages.

Change the allocation of bp->b_pages to use kmem_zalloc instead of kmem_alloc.
Remove unnecessary memset for bp->b_pages.

This reduces text size by 42 bytes.
Before:
text data bss dec hex filename
23335 588 8 23931 5d7b ./fs/xfs/xfs_buf.o
After:
text data bss dec hex filename
23293 588 8 23889 5d51 ./fs/xfs/xfs_buf.o

---
 fs/xfs/xfs_buf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/xfs/xfs_buf.c +1 -2
index 4f5f2ff3f70f..be4f740b97c1 100644
@@ -289,12 +289,11 @@ _xfs_buf_get_pages(
 		if (page_count <= XB_PAGES) {
 			bp->b_pages = bp->b_page_array;
 		} else {
-			bp->b_pages = kmem_alloc(sizeof(struct page *) *
+			bp->b_pages = kmem_zalloc(sizeof(struct page *) *
 						 page_count, KM_NOFS);
 			if (bp->b_pages == NULL)
 				return -ENOMEM;
 		}
-		memset(bp->b_pages, 0, sizeof(struct page *) * page_count);
 	}
 	return 0;
 }
Signature · 1 line
2.16.4
#1 lore raw
Quoted 22 lines · Sean Fu

Change the allocation of bp->b_pages to use kmem_zalloc instead of kmem_alloc.
Remove unnecessary memset for bp->b_pages.

This reduces text size by 42 bytes.
Before:
text data bss dec hex filename
23335 588 8 23931 5d7b ./fs/xfs/xfs_buf.o
After:
text data bss dec hex filename
23293 588 8 23889 5d51 ./fs/xfs/xfs_buf.o

---
 fs/xfs/xfs_buf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/xfs/xfs_buf.c +0 -0
index 4f5f2ff3f70f..be4f740b97c1 100644

fs/xfs/xfs_buf.c _xfs_buf_get_pages( · line 297

Quoted 10 lines
fs/xfs/xfs_buf.c +1 -2
@@ -289,12 +289,11 @@ _xfs_buf_get_pages(
 		if (page_count <= XB_PAGES) {
 			bp->b_pages = bp->b_page_array;
 		} else {
-			bp->b_pages = kmem_alloc(sizeof(struct page *) *
+			bp->b_pages = kmem_zalloc(sizeof(struct page *) *
 						 page_count, KM_NOFS);
 			if (bp->b_pages == NULL)
 				return -ENOMEM;
 		}
-		memset(bp->b_pages, 0, sizeof(struct page *) * page_count);

Does this leave b_pages uninitialized in the page_count <= XB_PAGES case?

--D

Quoted 6 lines
fs/xfs/xfs_buf.c +0 -0
 	}
 	return 0;
 }
Signature · 1 line
2.16.4
author
#2 lore raw
Quoted 28 lines · Darrick J. Wong
Quoted 27 lines · Sean Fu

Change the allocation of bp->b_pages to use kmem_zalloc instead of kmem_alloc.
Remove unnecessary memset for bp->b_pages.

This reduces text size by 42 bytes.
Before:
text data bss dec hex filename
23335 588 8 23931 5d7b ./fs/xfs/xfs_buf.o
After:
text data bss dec hex filename
23293 588 8 23889 5d51 ./fs/xfs/xfs_buf.o

---
 fs/xfs/xfs_buf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/xfs/xfs_buf.c +1 -1
index 4f5f2ff3f70f..be4f740b97c1 100644
@@ -289,12 +289,11 @@ _xfs_buf_get_pages(
 		if (page_count <= XB_PAGES) {
 			bp->b_pages = bp->b_page_array;
 		} else {
-			bp->b_pages = kmem_alloc(sizeof(struct page *) *
+			bp->b_pages = kmem_zalloc(sizeof(struct page *) *
Quoted 8 lines

fs/xfs/xfs_buf.c _xfs_buf_get_pages( · line 297

Quoted 5 lines
fs/xfs/xfs_buf.c +0 -1
 						 page_count, KM_NOFS);
 			if (bp->b_pages == NULL)
 				return -ENOMEM;
 		}
-		memset(bp->b_pages, 0, sizeof(struct page *) * page_count);

Does this leave b_pages uninitialized in the page_count <= XB_PAGES case?

bp is allocated by kmem_zone_zalloc, But i will take a deep look at xfs_buf_associate_memory.

Quoted 9 lines

--D

Quoted 6 lines
fs/xfs/xfs_buf.c +0 -1
 	}
 	return 0;
 }
--

2.16.4

#3 lore raw
Quoted 37 lines · Sean Fu
Quoted 28 lines · Darrick J. Wong
Quoted 27 lines · Sean Fu

Change the allocation of bp->b_pages to use kmem_zalloc instead of kmem_alloc.
Remove unnecessary memset for bp->b_pages.

This reduces text size by 42 bytes.
Before:
text data bss dec hex filename
23335 588 8 23931 5d7b ./fs/xfs/xfs_buf.o
After:
text data bss dec hex filename
23293 588 8 23889 5d51 ./fs/xfs/xfs_buf.o

---
 fs/xfs/xfs_buf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/xfs/xfs_buf.c +1 -1
index 4f5f2ff3f70f..be4f740b97c1 100644
@@ -289,12 +289,11 @@ _xfs_buf_get_pages(
 		if (page_count <= XB_PAGES) {
 			bp->b_pages = bp->b_page_array;
 		} else {
-			bp->b_pages = kmem_alloc(sizeof(struct page *) *
+			bp->b_pages = kmem_zalloc(sizeof(struct page *) *
Quoted 8 lines

fs/xfs/xfs_buf.c _xfs_buf_get_pages( · line 297

Quoted 5 lines
fs/xfs/xfs_buf.c +0 -1
 						 page_count, KM_NOFS);
 			if (bp->b_pages == NULL)
 				return -ENOMEM;
 		}
-		memset(bp->b_pages, 0, sizeof(struct page *) * page_count);

Does this leave b_pages uninitialized in the page_count <= XB_PAGES case?

Quoted 1 line

bp is allocated by kmem_zone_zalloc, But i will take a deep look at xfs_buf_associate_memory.

We can (and do) reuse buffers with different memory buffers via xfs_buf_associate_memory() (e.g. see the split log buffer code in xlog_sync()].

I'd prefer that we leave the explicit memset here for this re-use case - leaving the page pointer array with stale page pointers (even though beyond b_page_count) means any page array overrun turns into a memory corruption (via access of a stale page) rather than a NULL pointer dereference and immediate fail.

IMO, a small decrease in binary size is not worth changing the failure behaviour from a definite, immediate fail to a random memory corruption failure some time in the future...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]