Skip to contentSkip to conversation

devres: zero the memory in devm_krealloc() if needed

PATCH In review Bartosz Golaszewski 3 participants 7 messages Oct 26, 2020 – Oct 30, 2020 Driver core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 devres: zero the memory in devm_krealloc() if needed

From: Bartosz Golaszewski <[email protected]>

If we're returning the same pointer (when new size is smaller or equal to the old size) we need to check if the user wants the memory zeroed and memset() it manually if so.

Fixes f82485722e5d devres: provide devm_krealloc()
---
 drivers/base/devres.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/base/devres.c +5 -1
index 586e9a75c840..e522ad5f8342 100644
@@ -895,8 +895,12 @@ void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp)
 	 * If new size is smaller or equal to the actual number of bytes
 	 * allocated previously - just return the same pointer.
 	 */
-	if (total_new_size <= total_old_size)
+	if (total_new_size <= total_old_size) {
+		if (gfp & __GFP_ZERO)
+			memset(ptr, 0, new_size);
+
 		return ptr;
+	}
 
 	/*
 	 * Otherwise: allocate new, larger chunk. We need to allocate before
Signature · 1 line
2.29.1
#1 lore raw
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If we're returning the same pointer (when new size is smaller or equal to the old size) we need to check if the user wants the memory zeroed and memset() it manually if so.

Any use case? Because to me it sounds contradictory to the whole idea of [k]realloc().

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If we're returning the same pointer (when new size is smaller or equal to the old size) we need to check if the user wants the memory zeroed and memset() it manually if so.

Any use case? Because to me it sounds contradictory to the whole idea of [k]realloc().

This is kind of a gray area in original krealloc() too and I want to submit a patch for mm too. Right now krealloc ignores the __GFP_ZERO flag if new_size <= old_size but zeroes the memory if new_size > old_size. This should be consistent - either ignore __GFP_ZERO or don't ignore it in both cases. I think that not ignoring it is better
- if user passes it then it's for a reason.

Bartosz

#3 lore raw
Quoted 12 lines · Bartosz Golaszewski
Quoted 9 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If we're returning the same pointer (when new size is smaller or equal to the old size) we need to check if the user wants the memory zeroed and memset() it manually if so.

Any use case? Because to me it sounds contradictory to the whole idea of [k]realloc().

Quoted 8 lines

This is kind of a gray area in original krealloc() too and I want to submit a patch for mm too. Right now krealloc ignores the __GFP_ZERO flag if new_size <= old_size but zeroes the memory if new_size > old_size.

This should be consistent - either ignore __GFP_ZERO or don't ignore it in both cases. I think that not ignoring it is better
- if user passes it then it's for a reason.

Sorry, but I consider in these two choices the best is the former one, i.e. ignoring, because non-ignoring for sizes less than current is counter the REalloc() by definition.

Reading realloc(3):

"If the new size is larger than the old size, the added memory will not be initialized."

So, supports my choice over yours.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 26 lines · Andy Shevchenko
Quoted 12 lines · Bartosz Golaszewski
Quoted 9 lines · Andy Shevchenko
Quoted 5 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

If we're returning the same pointer (when new size is smaller or equal to the old size) we need to check if the user wants the memory zeroed and memset() it manually if so.

Any use case? Because to me it sounds contradictory to the whole idea of [k]realloc().

Quoted 8 lines

This is kind of a gray area in original krealloc() too and I want to submit a patch for mm too. Right now krealloc ignores the __GFP_ZERO flag if new_size <= old_size but zeroes the memory if new_size > old_size.

This should be consistent - either ignore __GFP_ZERO or don't ignore it in both cases. I think that not ignoring it is better
- if user passes it then it's for a reason.

Sorry, but I consider in these two choices the best is the former one, i.e. ignoring, because non-ignoring for sizes less than current is counter the REalloc() by definition.

Quoted 6 lines

Reading realloc(3):

"If the new size is larger than the old size, the added memory will not be initialized."

So, supports my choice over yours.

Two notes:
- perhaps kzrealloc() for what you want
- there is a library call reallocarray() which supports your idea about krealloc_array() API in kernel.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 1 line · Andy Shevchenko

[snip]

Quoted 16 lines
Quoted 11 lines
Quoted 2 lines

Any use case? Because to me it sounds contradictory to the whole idea of [k]realloc().

This is kind of a gray area in original krealloc() too and I want to submit a patch for mm too. Right now krealloc ignores the __GFP_ZERO flag if new_size <= old_size but zeroes the memory if new_size > old_size.

This should be consistent - either ignore __GFP_ZERO or don't ignore it in both cases. I think that not ignoring it is better
- if user passes it then it's for a reason.

Sorry, but I consider in these two choices the best is the former one, i.e. ignoring, because non-ignoring for sizes less than current is counter the REalloc() by definition.

Quoted 6 lines

Reading realloc(3):

"If the new size is larger than the old size, the added memory will not be initialized."

So, supports my choice over yours.

Kernel memory management API is not really orthogonal to the one in user-space. For example: kmalloc() takes the gfp parameter and if you pass __GFP_ZERO to it, it zeroes the memory even if user-space malloc() never does.

Quoted 1 line · Andy Shevchenko

[snip]

Quoted 3 lines

Two notes:
- perhaps kzrealloc() for what you want

It could be used as a helper wrapper around krealloc() but kzalloc() is already a simple kmalloc() with additional __GFP_ZERO flag passed. This is why I think krealloc() should honor __GFP_ZERO.

Quoted 3 lines

- there is a library call reallocarray() which supports your idea about krealloc_array() API in kernel.

reallocarray() is a bsd extension. I'd stick to krealloc_array() naming in the kernel.

Bartosz

#6 lore raw
Quoted 28 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

[snip]

Quoted 21 lines
Quoted 11 lines
Quoted 2 lines

Any use case? Because to me it sounds contradictory to the whole idea of [k]realloc().

This is kind of a gray area in original krealloc() too and I want to submit a patch for mm too. Right now krealloc ignores the __GFP_ZERO flag if new_size <= old_size but zeroes the memory if new_size > old_size.

This should be consistent - either ignore __GFP_ZERO or don't ignore it in both cases. I think that not ignoring it is better
- if user passes it then it's for a reason.

Sorry, but I consider in these two choices the best is the former one, i.e. ignoring, because non-ignoring for sizes less than current is counter the REalloc() by definition.

Reading realloc(3):

"If the new size is larger than the old size, the added memory will not be initialized."

Quoted 7 lines
Quoted 1 line

So, supports my choice over yours.

Kernel memory management API is not really orthogonal to the one in user-space. For example: kmalloc() takes the gfp parameter and if you pass __GFP_ZERO to it, it zeroes the memory even if user-space malloc() never does.

Ok so I was wrong - it turns out that krealloc() is consistent in ignoring __GFP_ZERO after all. In that case this patch can be dropped.

Bartosz