Skip to contentSkip to conversation

gfs2: Use kvmalloc instead of opencoded kmalloc/vmalloc

PATCH In review Denis Efremov 3 participants 4 messages Jul 31, 2020 – Aug 3, 2020 GFS2 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gfs2: Use kvmalloc instead of opencoded kmalloc/vmalloc

Use kvmalloc instead of opencoded kmalloc/vmalloc condition.

---
 fs/gfs2/dir.c   | 23 ++++-------------------
 fs/gfs2/quota.c |  5 +----
 2 files changed, 5 insertions(+), 23 deletions(-)
fs/gfs2/dir.c +4 -19
index c0f2875c946c..5d2a708fae9c 100644
@@ -352,9 +352,7 @@ static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
 		return ERR_PTR(-EIO);
 	}
 
-	hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN);
-	if (hc == NULL)
-		hc = __vmalloc(hsize, GFP_NOFS);
+	hc = kvmalloc(hsize, GFP_NOFS);
 
 	if (hc == NULL)
 		return ERR_PTR(-ENOMEM);
@@ -1320,18 +1318,6 @@ static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
 	return 0;
 }
 
-static void *gfs2_alloc_sort_buffer(unsigned size)
-{
-	void *ptr = NULL;
-
-	if (size < KMALLOC_MAX_SIZE)
-		ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
-	if (!ptr)
-		ptr = __vmalloc(size, GFP_NOFS);
-	return ptr;
-}
-
-
 static int gfs2_set_cookies(struct gfs2_sbd *sdp, struct buffer_head *bh,
 			    unsigned leaf_nr, struct gfs2_dirent **darr,
 			    unsigned entries)
@@ -1409,7 +1395,8 @@ static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
 	 * 99 is the maximum number of entries that can fit in a single
 	 * leaf block.
 	 */
-	larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
+	larr = kvmalloc_array(leaves + entries + 99,
+			      sizeof(void *), GFP_NOFS);
 	if (!larr)
 		goto out;
 	darr = (struct gfs2_dirent **)(larr + leaves);
@@ -1985,9 +1972,7 @@ static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
 
 	memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
 
-	ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
-	if (ht == NULL)
-		ht = __vmalloc(size, GFP_NOFS | __GFP_NOWARN | __GFP_ZERO);
+	ht = kvzalloc(size, GFP_NOFS);
 	if (!ht)
 		return -ENOMEM;
 
fs/gfs2/quota.c +1 -4
index 4b67d47a7e00..204b34f38e5c 100644
@@ -1362,10 +1362,7 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 	bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long));
 	bm_size *= sizeof(unsigned long);
 	error = -ENOMEM;
-	sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN);
-	if (sdp->sd_quota_bitmap == NULL)
-		sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS |
-						 __GFP_ZERO);
+	sdp->sd_quota_bitmap = kvzalloc(bm_size, GFP_NOFS);
 	if (!sdp->sd_quota_bitmap)
 		return error;
 
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/1 gfs2: Use kvmalloc instead of opencoded kmalloc/vmalloc

Please, skip this patch. I missed that kvmalloc accepts (flags & GFP_KERNEL) == GFP_KERNEL for calling vmalloc.

Thanks,
Denis

Quoted 88 lines · Denis Efremov

Use kvmalloc instead of opencoded kmalloc/vmalloc condition.

---
fs/gfs2/dir.c | 23 ++++-------------------
fs/gfs2/quota.c | 5 +----

2 files changed, 5 insertions(+), 23 deletions(-)
fs/gfs2/dir.c +1 -3
index c0f2875c946c..5d2a708fae9c 100644
@@ -352,9 +352,7 @@ static __be64 *gfs2_dir_get_hash_table(struct
 gfs2_inode *ip)
 		return ERR_PTR(-EIO);
 	}
 
-	hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN);
-	if (hc == NULL)
-		hc = __vmalloc(hsize, GFP_NOFS);
+	hc = kvmalloc(hsize, GFP_NOFS);
 
 	if (hc == NULL)
		return ERR_PTR(-ENOMEM);
fs/gfs2/dir.c +0 -12
@@ -1320,18 +1318,6 @@ static int do_filldir_main(struct gfs2_inode
 *dip, struct dir_context *ctx,
 	return 0;
 }
 
-static void *gfs2_alloc_sort_buffer(unsigned size)
-{
-	void *ptr = NULL;
-
-	if (size < KMALLOC_MAX_SIZE)
-		ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
-	if (!ptr)
-		ptr = __vmalloc(size, GFP_NOFS);
-	return ptr;
-}
-
-

static int gfs2_set_cookies(struct gfs2_sbd *sdp, struct buffer_head *bh,
unsigned leaf_nr, struct gfs2_dirent **darr, unsigned entries)

fs/gfs2/dir.c +2 -1
@@ -1409,7 +1395,8 @@ static int gfs2_dir_read_leaf(struct inode
 *inode, struct dir_context *ctx,
 	 * 99 is the maximum number of entries that can fit in a single
 	 * leaf block.
 	 */
-	larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void
 *));
+	larr = kvmalloc_array(leaves + entries + 99,
+			      sizeof(void *), GFP_NOFS);
 	if (!larr)
		goto out;
	darr = (struct gfs2_dirent **)(larr + leaves);
fs/gfs2/dir.c +1 -3
@@ -1985,9 +1972,7 @@ static int leaf_dealloc(struct gfs2_inode *dip,
 u32 index, u32 len,
 
 	memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
 
-	ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
-	if (ht == NULL)
-		ht = __vmalloc(size, GFP_NOFS | __GFP_NOWARN | __GFP_ZERO);
+	ht = kvzalloc(size, GFP_NOFS);
 	if (!ht)
 		return -ENOMEM;
fs/gfs2/quota.c +1 -4
index 4b67d47a7e00..204b34f38e5c 100644
@@ -1362,10 +1362,7 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 	bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned
 long));
 	bm_size *= sizeof(unsigned long);
 	error = -ENOMEM;
-	sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN);
-	if (sdp->sd_quota_bitmap == NULL)
-		sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS |
-						 __GFP_ZERO);
+	sdp->sd_quota_bitmap = kvzalloc(bm_size, GFP_NOFS);
 	if (!sdp->sd_quota_bitmap)
 		return error;
Signature · 2 lines
-- 
2.26.2

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/cluster-devel/attachments/20200801/481ad606/attachment.htm>

#2 lore raw
In reply to Denis Efremov

Please, skip this patch. I missed that kvmalloc checks (flags & GFP_KERNEL) == GFP_KERNEL before calling vmalloc.

P.S.: previous mail was filtered because of html tags.

Thanks,
Denis

Quoted 80 lines · Denis Efremov

Use kvmalloc instead of opencoded kmalloc/vmalloc condition.

---
 fs/gfs2/dir.c   | 23 ++++-------------------
 fs/gfs2/quota.c |  5 +----
 2 files changed, 5 insertions(+), 23 deletions(-)
fs/gfs2/dir.c +4 -19
index c0f2875c946c..5d2a708fae9c 100644
@@ -352,9 +352,7 @@ static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip)
 		return ERR_PTR(-EIO);
 	}
 
-	hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN);
-	if (hc == NULL)
-		hc = __vmalloc(hsize, GFP_NOFS);
+	hc = kvmalloc(hsize, GFP_NOFS);
 
 	if (hc == NULL)
 		return ERR_PTR(-ENOMEM);
@@ -1320,18 +1318,6 @@ static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx,
 	return 0;
 }
 
-static void *gfs2_alloc_sort_buffer(unsigned size)
-{
-	void *ptr = NULL;
-
-	if (size < KMALLOC_MAX_SIZE)
-		ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN);
-	if (!ptr)
-		ptr = __vmalloc(size, GFP_NOFS);
-	return ptr;
-}
-
-
 static int gfs2_set_cookies(struct gfs2_sbd *sdp, struct buffer_head *bh,
 			    unsigned leaf_nr, struct gfs2_dirent **darr,
 			    unsigned entries)
@@ -1409,7 +1395,8 @@ static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx,
 	 * 99 is the maximum number of entries that can fit in a single
 	 * leaf block.
 	 */
-	larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *));
+	larr = kvmalloc_array(leaves + entries + 99,
+			      sizeof(void *), GFP_NOFS);
 	if (!larr)
 		goto out;
 	darr = (struct gfs2_dirent **)(larr + leaves);
@@ -1985,9 +1972,7 @@ static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
 
 	memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
 
-	ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN);
-	if (ht == NULL)
-		ht = __vmalloc(size, GFP_NOFS | __GFP_NOWARN | __GFP_ZERO);
+	ht = kvzalloc(size, GFP_NOFS);
 	if (!ht)
 		return -ENOMEM;
 
fs/gfs2/quota.c +1 -4
index 4b67d47a7e00..204b34f38e5c 100644
@@ -1362,10 +1362,7 @@ int gfs2_quota_init(struct gfs2_sbd *sdp)
 	bm_size = DIV_ROUND_UP(sdp->sd_quota_slots, 8 * sizeof(unsigned long));
 	bm_size *= sizeof(unsigned long);
 	error = -ENOMEM;
-	sdp->sd_quota_bitmap = kzalloc(bm_size, GFP_NOFS | __GFP_NOWARN);
-	if (sdp->sd_quota_bitmap == NULL)
-		sdp->sd_quota_bitmap = __vmalloc(bm_size, GFP_NOFS |
-						 __GFP_ZERO);
+	sdp->sd_quota_bitmap = kvzalloc(bm_size, GFP_NOFS);
 	if (!sdp->sd_quota_bitmap)
 		return error;
 
#3 lore raw

Hello,

Quoted 2 lines · Denis Efremov

Please, skip this patch. I missed that kvmalloc checks (flags & GFP_KERNEL) == GFP_KERNEL before calling vmalloc.

okay. Assuming that you'll follow up with a fixed version, please also mention that this change supplements commit 3cdcf63ed2d1 ("GFS2: use kvfree() instead of open-coding it").

Thanks,
Andreas