* [PATCH] devres: update node debug info on in-place devm_krealloc()
@ 2026-12-03 0:57 Eric Yun Lin Chiang
2026-12-03 11:42 ` Andy Shevchenko
0 siblings, 1 reply; 3+ messages in thread
From: Eric Yun Lin Chiang @ 2026-12-03 0:57 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J . Wysocki, Danilo Krummrich
Cc: Johan Hovold, Bartosz Golaszewski, Andy Shevchenko, driver-core,
linux-kernel, Eric Yun Lin Chiang
When the new size still fits in the existing allocation,
devm_krealloc() returns the old pointer without touching the node's
debug info, so the node keeps the size of the original request.
devres_log() and the devres_log tracepoint then report a stale size
for the resource: a buffer grown in place from 1000 to 1500 bytes, or
shrunk to 600 bytes, is still reported as 1000 bytes.
Commit f813ec9e84b4 ("devres: fix missing node debug info in
devm_krealloc()") added the debug info for nodes newly allocated by
devm_krealloc(); do the same on the in-place path, so the recorded
size always matches the size the caller asked for.
Fixes: f82485722e5d ("devres: provide devm_krealloc()")
Signed-off-by: Eric Yun Lin Chiang <ericyunlinchiang@gmail.com>
---
Notes (not for the changelog):
- Tool use: the problem was found, and this change and its test
drafted, with the help of an AI coding assistant (Claude).
- Reproducer and test (UML, KUnit test written for this purpose and not
part of this patch; base commit below):
devm_kzalloc(dev, 1000) -> devm_krealloc(1500) in place
-> devm_krealloc(600) in place -> devm_krealloc(3000) new node
node size read back after each step:
without this patch: 1000, 1000, 3000 (test fails)
with this patch: 1500, 600, 3000 (test passes)
Data in [0, 600) is preserved across the final copy in both cases.
W=1 build of drivers/base/devres.o: no new warnings.
Not tested on real hardware.
- Locking: the node's debug info is updated without taking
dev->devres_lock and without find_dr(), matching how this path
already trusts @ptr for the ksize() call above. If preferred, the
update can be done under devres_lock after find_dr(), like the
new-node path does.
- Stable: before v7.1 the helper is still called set_node_dbginfo()
(renamed by ba424bc2c7bb ("devres: rename and export
set_node_dbginfo()")). On 6.18.66 the change applies cleanly but
needs that rename to build; with it, the same KUnit test gives node
sizes 1000, 1000, 3000 without the change and 1500, 600, 3000 with
it (UML). Older trees not checked. Happy to send the backport.
drivers/base/devres.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index 9d9842fc5a19..d1baf7c44435 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -1010,10 +1010,14 @@ 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.
+ * allocated previously - just update the node's debug info and
+ * return the same pointer.
*/
- if (total_new_size <= total_old_size)
+ if (total_new_size <= total_old_size) {
+ devres_set_node_dbginfo(&container_of(ptr, struct devres, data)->node,
+ "devm_krealloc_release", new_size);
return ptr;
+ }
/*
* Otherwise: allocate new, larger chunk. We need to allocate before
base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] devres: update node debug info on in-place devm_krealloc()
2026-12-03 0:57 [PATCH] devres: update node debug info on in-place devm_krealloc() Eric Yun Lin Chiang
@ 2026-12-03 11:42 ` Andy Shevchenko
2026-12-04 0:39 ` Eric Yun Lin Chiang
0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2026-12-03 11:42 UTC (permalink / raw)
To: Eric Yun Lin Chiang
Cc: Greg Kroah-Hartman, Rafael J . Wysocki, Danilo Krummrich,
Johan Hovold, Bartosz Golaszewski, driver-core, linux-kernel
On Thu, Dec 03, 2026 at 08:57:04AM +0800, Eric Yun Lin Chiang wrote:
> When the new size still fits in the existing allocation,
> devm_krealloc() returns the old pointer without touching the node's
> debug info, so the node keeps the size of the original request.
> devres_log() and the devres_log tracepoint then report a stale size
> for the resource: a buffer grown in place from 1000 to 1500 bytes, or
> shrunk to 600 bytes, is still reported as 1000 bytes.
Do we really support shrinking for realloc() in kernel?
I am under the impression that we don't, so in such a case info should
not be updated. Please, check this and confirm.
> Commit f813ec9e84b4 ("devres: fix missing node debug info in
> devm_krealloc()") added the debug info for nodes newly allocated by
> devm_krealloc(); do the same on the in-place path, so the recorded
> size always matches the size the caller asked for.
...
> /*
> * If new size is smaller or equal to the actual number of bytes
> - * allocated previously - just return the same pointer.
> + * allocated previously - just update the node's debug info and
> + * return the same pointer.
> */
> - if (total_new_size <= total_old_size)
> + if (total_new_size <= total_old_size) {
> + devres_set_node_dbginfo(&container_of(ptr, struct devres, data)->node,
> + "devm_krealloc_release", new_size);
> return ptr;
> + }
If I'm right, this patch is bogus.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] devres: update node debug info on in-place devm_krealloc()
2026-12-03 11:42 ` Andy Shevchenko
@ 2026-12-04 0:39 ` Eric Yun Lin Chiang
0 siblings, 0 replies; 3+ messages in thread
From: Eric Yun Lin Chiang @ 2026-12-04 0:39 UTC (permalink / raw)
To: Andy Shevchenko
Cc: Greg Kroah-Hartman, Rafael J . Wysocki, Danilo Krummrich,
Johan Hovold, Bartosz Golaszewski, driver-core, linux-kernel
On Thu, Dec 03, 2026 at 01:42:35PM +0200, Andy Shevchenko wrote:
> Do we really support shrinking for realloc() in kernel?
> I am under the impression that we don't, so in such a case info should
> not be updated. Please, check this and confirm.
devm_krealloc() accepts a smaller size and returns the same pointer,
like krealloc() does, but no memory is given back. My changelog said
"shrunk", which made it sound otherwise.
The patch only changes the size recorded in the node's debug info, so
on its own it only changes debug output, which is not much of a reason.
I wrote it as groundwork for letting KASAN check devres allocations
against the requested size rather than the rounded-up bucket, and I
should not have sent it without that context. Please consider this one
withdrawn. I will come back with the KASAN part as one RFC series,
together with a concrete case it catches.
Thanks,
Eric
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-04 0:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03 0:57 [PATCH] devres: update node debug info on in-place devm_krealloc() Eric Yun Lin Chiang
2026-12-03 11:42 ` Andy Shevchenko
2026-12-04 0:39 ` Eric Yun Lin Chiang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®