Skip to contentSkip to conversation

udf: Use kvzalloc() in udf_sb_alloc_bitmap()

PATCH Superseded Denis Efremov 4 participants 9 messages Aug 27, 2020 – Aug 28, 2020 UDF filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 udf: Use kvzalloc() in udf_sb_alloc_bitmap()

Use kvzalloc() in udf_sb_alloc_bitmap() instead of open-coding it.

---

I'm not sure about TODO comment, through.

 fs/udf/super.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
fs/udf/super.c +1 -4
index 1c42f544096d..c7cd15219b7c 100644
@@ -1013,10 +1013,7 @@ static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
 	size = sizeof(struct udf_bitmap) +
 		(sizeof(struct buffer_head *) * nr_groups);
 
-	if (size <= PAGE_SIZE)
-		bitmap = kzalloc(size, GFP_KERNEL);
-	else
-		bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
+	bitmap = kvzalloc(size, GFP_KERNEL);
 
 	if (!bitmap)
 		return NULL;
Signature · 1 line
2.26.2
#1 lore raw

fs/udf/super.c static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index) · line 1014

Quoted 4 lines
fs/udf/super.c +0 -0
@@ -1013,10 +1013,7 @@ static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
 	size = sizeof(struct udf_bitmap) +
 		(sizeof(struct buffer_head *) * nr_groups);

I missed that this size is a good place to use struct_size for overflow checking. I will send v2 instead.

#2 lore raw
Quoted 6 lines · Denis Efremov

fs/udf/super.c static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index) · line 1014

Quoted 3 lines
fs/udf/super.c +0 -0
@@ -1013,10 +1013,7 @@ static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
 	size = sizeof(struct udf_bitmap) +
 		(sizeof(struct buffer_head *) * nr_groups);

I missed that this size is a good place to use struct_size for overflow checking. I will send v2 instead.

And you could cuddle the

	if (!bitmap)
		return NULL;

by removing the blank line between the alloc and test.

#3 lore raw

PATCH v2 1/1 udf: Use kvzalloc() in udf_sb_alloc_bitmap()

In reply to Denis Efremov

Use kvzalloc() in udf_sb_alloc_bitmap() instead of open-coding it. Size computation wrapped in struct_size() macro to prevent potential integer overflows.

---
 fs/udf/super.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
fs/udf/super.c +2 -7
index 1c42f544096d..bdf51bea54f3 100644
@@ -1010,14 +1010,9 @@ static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
 	int size;
 
 	nr_groups = udf_compute_nr_groups(sb, index);
-	size = sizeof(struct udf_bitmap) +
-		(sizeof(struct buffer_head *) * nr_groups);
-
-	if (size <= PAGE_SIZE)
-		bitmap = kzalloc(size, GFP_KERNEL);
-	else
-		bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
+	size = struct_size(bitmap, s_block_bitmap, nr_groups);
 
+	bitmap = kvzalloc(size, GFP_KERNEL);
 	if (!bitmap)
 		return NULL;
 
Signature · 1 line
2.26.2
#5 lore raw
In reply to Gustavo A. R. Silva
Quoted 7 lines · Gustavo A. R. Silva
Quoted 4 lines · Denis Efremov

Use kvzalloc() in udf_sb_alloc_bitmap() instead of open-coding it. Size computation wrapped in struct_size() macro to prevent potential integer overflows.

Quoted 8 lines
Quoted 2 lines

---

Please, comment here what changed in v2, vn... e.g.:

Changes in v2:
- Use struct_size() helper.

Ah, thanks. I added this initially and accidentally regenerated the patch file with format-patch.

Quoted 8 lines

Why not this:

bitmap = kvzalloc(struct_size(bitmap, s_block_bitmap, nr_groups),

		  GFP_KERNEL);

and you can also get rid of _size_ entirely.

My bad, I missed that only nr_groups is used down the code.

Thanks, I will resend it as v3.

Denis

#6 lore raw

PATCH v3 1/1 udf: Use kvzalloc() in udf_sb_alloc_bitmap()

In reply to Denis Efremov

Use kvzalloc() in udf_sb_alloc_bitmap() instead of open-coding it. Size computation wrapped in struct_size() macro to prevent potential integer overflows.

---
Changes in v2:
- size computation wrapped in struct_size()
Changes in v3:
- int size dropped

 fs/udf/super.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
fs/udf/super.c +3 -11
index 1c42f544096d..d9eabbe368ff 100644
@@ -1006,18 +1006,10 @@ int udf_compute_nr_groups(struct super_block *sb, u32 partition)
 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
 {
 	struct udf_bitmap *bitmap;
-	int nr_groups;
-	int size;
-
-	nr_groups = udf_compute_nr_groups(sb, index);
-	size = sizeof(struct udf_bitmap) +
-		(sizeof(struct buffer_head *) * nr_groups);
-
-	if (size <= PAGE_SIZE)
-		bitmap = kzalloc(size, GFP_KERNEL);
-	else
-		bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
+	int nr_groups = udf_compute_nr_groups(sb, index);
 
+	bitmap = kvzalloc(struct_size(bitmap, s_block_bitmap, nr_groups),
+			  GFP_KERNEL);
 	if (!bitmap)
 		return NULL;
 
Signature · 1 line
2.26.2
#4 lore raw
In reply to Denis Efremov
Quoted 6 lines · Denis Efremov

Use kvzalloc() in udf_sb_alloc_bitmap() instead of open-coding it. Size computation wrapped in struct_size() macro to prevent potential integer overflows.

---

Please, comment here what changed in v2, vn... e.g.:

Changes in v2:
- Use struct_size() helper.

Quoted 11 lines
 fs/udf/super.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
fs/udf/super.c +0 -0
index 1c42f544096d..bdf51bea54f3 100644
@@ -1010,14 +1010,9 @@ static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
 	int size;
 
 	nr_groups = udf_compute_nr_groups(sb, index);

fs/udf/super.c static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index) · line 1015

Quoted 10 lines
fs/udf/super.c +2 -7
-	size = sizeof(struct udf_bitmap) +
-		(sizeof(struct buffer_head *) * nr_groups);
-
-	if (size <= PAGE_SIZE)
-		bitmap = kzalloc(size, GFP_KERNEL);
-	else
-		bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
+	size = struct_size(bitmap, s_block_bitmap, nr_groups);
 
+	bitmap = kvzalloc(size, GFP_KERNEL);

Why not this:

bitmap = kvzalloc(struct_size(bitmap, s_block_bitmap, nr_groups),

		  GFP_KERNEL);

and you can also get rid of _size_ entirely.

Thanks
--
Gustavo

Quoted 4 lines
fs/udf/super.c +0 -0
 	if (!bitmap)
 		return NULL;
 
Reviewed-by on v3 1/1
#7 lore raw
In reply to Denis Efremov
Quoted 5 lines · Denis Efremov

Use kvzalloc() in udf_sb_alloc_bitmap() instead of open-coding it. Size computation wrapped in struct_size() macro to prevent potential integer overflows.

Looks good. :)

Thanks
--
Gustavo

Quoted 36 lines

---
Changes in v2:
- size computation wrapped in struct_size()
Changes in v3:
- int size dropped

 fs/udf/super.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
fs/udf/super.c +3 -11
index 1c42f544096d..d9eabbe368ff 100644
@@ -1006,18 +1006,10 @@ int udf_compute_nr_groups(struct super_block *sb, u32 partition)
 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
 {
 	struct udf_bitmap *bitmap;
-	int nr_groups;
-	int size;
-
-	nr_groups = udf_compute_nr_groups(sb, index);
-	size = sizeof(struct udf_bitmap) +
-		(sizeof(struct buffer_head *) * nr_groups);
-
-	if (size <= PAGE_SIZE)
-		bitmap = kzalloc(size, GFP_KERNEL);
-	else
-		bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
+	int nr_groups = udf_compute_nr_groups(sb, index);
 
+	bitmap = kvzalloc(struct_size(bitmap, s_block_bitmap, nr_groups),
+			  GFP_KERNEL);
 	if (!bitmap)
 		return NULL;
 
#8 lore raw
Quoted 7 lines · Gustavo A. R. Silva
Quoted 4 lines · Denis Efremov

Use kvzalloc() in udf_sb_alloc_bitmap() instead of open-coding it. Size computation wrapped in struct_size() macro to prevent potential integer overflows.

Quoted 5 lines
Quoted 1 line

Looks good. :)

Thank you both. I've added the patch to my tree.

Honza

Quoted 42 lines

Thanks
--
Gustavo

Quoted 36 lines

---
Changes in v2:
- size computation wrapped in struct_size()
Changes in v3:
- int size dropped

 fs/udf/super.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
fs/udf/super.c +3 -11
index 1c42f544096d..d9eabbe368ff 100644
@@ -1006,18 +1006,10 @@ int udf_compute_nr_groups(struct super_block *sb, u32 partition)
 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
 {
 	struct udf_bitmap *bitmap;
-	int nr_groups;
-	int size;
-
-	nr_groups = udf_compute_nr_groups(sb, index);
-	size = sizeof(struct udf_bitmap) +
-		(sizeof(struct buffer_head *) * nr_groups);
-
-	if (size <= PAGE_SIZE)
-		bitmap = kzalloc(size, GFP_KERNEL);
-	else
-		bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
+	int nr_groups = udf_compute_nr_groups(sb, index);
 
+	bitmap = kvzalloc(struct_size(bitmap, s_block_bitmap, nr_groups),
+			  GFP_KERNEL);
 	if (!bitmap)
 		return NULL;
 
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR