Skip to contentSkip to conversation

xfs: simplify kmem_{zone_}zalloc

PATCH Superseded Gu Zheng 2 participants 3 messages Nov 1, 2013 – Nov 4, 2013 XFS filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gu Zheng

author
#0 lore raw

PATCH 1/1 xfs: simplify kmem_{zone_}zalloc

Introduce flag KM_ZERO which is used to alloc zeroed entry, and convert kmem_{zone_}zalloc to call kmem_{zone_}alloc() with KM_ZERO directly, in order to avoid the setting to zero step.

---
 fs/xfs/kmem.c |   14 ++------------
 fs/xfs/kmem.h |    7 ++++++-
 2 files changed, 8 insertions(+), 13 deletions(-)
fs/xfs/kmem.c +2 -12
index a02cfb9..d56fcc9 100644
@@ -65,12 +65,7 @@ kmem_alloc(size_t size, xfs_km_flags_t flags)
 void *
 kmem_zalloc(size_t size, xfs_km_flags_t flags)
 {
-	void	*ptr;
-
-	ptr = kmem_alloc(size, flags);
-	if (ptr)
-		memset((char *)ptr, 0, (int)size);
-	return ptr;
+	return kmem_alloc(size, flags | KM_ZERO);
 }
 
 void *
@@ -132,10 +127,5 @@ kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
 void *
 kmem_zone_zalloc(kmem_zone_t *zone, xfs_km_flags_t flags)
 {
-	void	*ptr;
-
-	ptr = kmem_zone_alloc(zone, flags);
-	if (ptr)
-		memset((char *)ptr, 0, kmem_cache_size(zone));
-	return ptr;
+	return kmem_zone_alloc(zone, flags | KM_ZERO);
 }
fs/xfs/kmem.h +6 -1
index 3a7371c..8ea21fd 100644
@@ -32,6 +32,7 @@ typedef unsigned __bitwise xfs_km_flags_t;
 #define KM_NOSLEEP	((__force xfs_km_flags_t)0x0002u)
 #define KM_NOFS		((__force xfs_km_flags_t)0x0004u)
 #define KM_MAYFAIL	((__force xfs_km_flags_t)0x0008u)
+#define KM_ZERO		((__force xfs_km_flags_t)0x0010u)
 
 /*
  * We use a special process flag to avoid recursive callbacks into
@@ -43,7 +44,7 @@ kmem_flags_convert(xfs_km_flags_t flags)
 {
 	gfp_t	lflags;
 
-	BUG_ON(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL));
+	BUG_ON(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL|KM_ZERO));
 
 	if (flags & KM_NOSLEEP) {
 		lflags = GFP_ATOMIC | __GFP_NOWARN;
@@ -52,6 +53,10 @@ kmem_flags_convert(xfs_km_flags_t flags)
 		if ((current->flags & PF_FSTRANS) || (flags & KM_NOFS))
 			lflags &= ~__GFP_FS;
 	}
+
+	if (flags & KM_ZERO)
+		lflags |= __GFP_ZERO;
+
 	return lflags;
 }
 
Signature · 1 line
1.7.7
#1 lore raw
Quoted 31 lines · Gu Zheng

Introduce flag KM_ZERO which is used to alloc zeroed entry, and convert kmem_{zone_}zalloc to call kmem_{zone_}alloc() with KM_ZERO directly, in order to avoid the setting to zero step.

---
 fs/xfs/kmem.c |   14 ++------------
 fs/xfs/kmem.h |    7 ++++++-
 2 files changed, 8 insertions(+), 13 deletions(-)
fs/xfs/kmem.c +1 -6
index a02cfb9..d56fcc9 100644
@@ -65,12 +65,7 @@ kmem_alloc(size_t size, xfs_km_flags_t flags)
 void *
 kmem_zalloc(size_t size, xfs_km_flags_t flags)
 {
-	void	*ptr;
-
-	ptr = kmem_alloc(size, flags);
-	if (ptr)
-		memset((char *)ptr, 0, (int)size);
-	return ptr;
+	return kmem_alloc(size, flags | KM_ZERO);
 }
 
 void *
@@ -132,10 +127,5 @@ kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
 void *

fs/xfs/kmem.c kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags) · line 131

Quoted 10 lines
fs/xfs/kmem.c +1 -6
 kmem_zone_zalloc(kmem_zone_t *zone, xfs_km_flags_t flags)
 {
-	void	*ptr;
-
-	ptr = kmem_zone_alloc(zone, flags);
-	if (ptr)
-		memset((char *)ptr, 0, kmem_cache_size(zone));
-	return ptr;
+	return kmem_zone_alloc(zone, flags | KM_ZERO);
 }

These functions should be made static inline functions in kmem.h seeing as they are now just a simple wrapper.

Otherwise it's a nice cleanup.

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
author
#2 lore raw

Hi Dave,

Quoted 29 lines · Dave Chinner
Quoted 28 lines · Gu Zheng

Introduce flag KM_ZERO which is used to alloc zeroed entry, and convert kmem_{zone_}zalloc to call kmem_{zone_}alloc() with KM_ZERO directly, in order to avoid the setting to zero step.

---
 fs/xfs/kmem.c |   14 ++------------
 fs/xfs/kmem.h |    7 ++++++-
 2 files changed, 8 insertions(+), 13 deletions(-)
fs/xfs/kmem.c +1 -6
index a02cfb9..d56fcc9 100644
@@ -65,12 +65,7 @@ kmem_alloc(size_t size, xfs_km_flags_t flags)
 void *
 kmem_zalloc(size_t size, xfs_km_flags_t flags)
 {
-	void	*ptr;
-
-	ptr = kmem_alloc(size, flags);
-	if (ptr)
-		memset((char *)ptr, 0, (int)size);
-	return ptr;
+	return kmem_alloc(size, flags | KM_ZERO);
 }
 
Quoted 16 lines

fs/xfs/kmem.c kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags) · line 131

Quoted 13 lines
fs/xfs/kmem.c +1 -6
 void *
@@ -132,10 +127,5 @@ kmem_zone_alloc(kmem_zone_t *zone, xfs_km_flags_t flags)
 void *
 kmem_zone_zalloc(kmem_zone_t *zone, xfs_km_flags_t flags)
 {
-	void	*ptr;
-
-	ptr = kmem_zone_alloc(zone, flags);
-	if (ptr)
-		memset((char *)ptr, 0, kmem_cache_size(zone));
-	return ptr;
+	return kmem_zone_alloc(zone, flags | KM_ZERO);
 }

These functions should be made static inline functions in kmem.h seeing as they are now just a simple wrapper.

Agree. Thanks for your suggestion, I'll follow it.

Quoted 2 lines

Otherwise it's a nice cleanup.

Thanks:)

Regards,
Gu

Quoted 4 lines

Cheers,

Dave.