* [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; 9+ 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 related [flat|nested] 9+ 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; 9+ 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] 9+ 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
2026-12-04 9:13 ` Andy Shevchenko
0 siblings, 1 reply; 9+ 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] 9+ messages in thread
* Re: [PATCH] devres: update node debug info on in-place devm_krealloc()
2026-12-04 0:39 ` Eric Yun Lin Chiang
@ 2026-12-04 9:13 ` Andy Shevchenko
2026-12-04 9:28 ` Johan Hovold
0 siblings, 1 reply; 9+ messages in thread
From: Andy Shevchenko @ 2026-12-04 9:13 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 Fri, Dec 04, 2026 at 08:39:19AM +0800, Eric Yun Lin Chiang wrote:
> 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.
Doesn't matter. We should not lie to the users of the traces. If size was not
shrunk, it is not changed based on what the caller of krealloc() asked for.
If you want to add that to the trace, it should be an additional field or
so, not the lie. OTOH you can introduce memory shrinking support to krealloc()
but I have no idea what will be a perception of any of these approaches.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] devres: update node debug info on in-place devm_krealloc()
2026-12-04 9:13 ` Andy Shevchenko
@ 2026-12-04 9:28 ` Johan Hovold
2026-12-04 11:09 ` Andy Shevchenko
0 siblings, 1 reply; 9+ messages in thread
From: Johan Hovold @ 2026-12-04 9:28 UTC (permalink / raw)
To: Andy Shevchenko
Cc: Eric Yun Lin Chiang, Greg Kroah-Hartman, Rafael J . Wysocki,
Danilo Krummrich, Bartosz Golaszewski, driver-core, linux-kernel
On Fri, Dec 04, 2026 at 11:13:28AM +0200, Andy Shevchenko wrote:
> On Fri, Dec 04, 2026 at 08:39:19AM +0800, Eric Yun Lin Chiang wrote:
> > 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.
>
> Doesn't matter. We should not lie to the users of the traces. If size was not
> shrunk, it is not changed based on what the caller of krealloc() asked for.
> If you want to add that to the trace, it should be an additional field or
> so, not the lie. OTOH you can introduce memory shrinking support to krealloc()
> but I have no idea what will be a perception of any of these approaches.
I believe krealloc() can shrink, unlike devm_krealloc().
Johan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] devres: update node debug info on in-place devm_krealloc()
2026-12-04 9:28 ` Johan Hovold
@ 2026-12-04 11:09 ` Andy Shevchenko
2026-12-04 11:18 ` Andy Shevchenko
0 siblings, 1 reply; 9+ messages in thread
From: Andy Shevchenko @ 2026-12-04 11:09 UTC (permalink / raw)
To: Johan Hovold
Cc: Eric Yun Lin Chiang, Greg Kroah-Hartman, Rafael J . Wysocki,
Danilo Krummrich, Bartosz Golaszewski, driver-core, linux-kernel
On Fri, Dec 04, 2026 at 10:28:46AM +0100, Johan Hovold wrote:
> On Fri, Dec 04, 2026 at 11:13:28AM +0200, Andy Shevchenko wrote:
> > On Fri, Dec 04, 2026 at 08:39:19AM +0800, Eric Yun Lin Chiang wrote:
> > > 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.
> >
> > Doesn't matter. We should not lie to the users of the traces. If size was not
> > shrunk, it is not changed based on what the caller of krealloc() asked for.
> > If you want to add that to the trace, it should be an additional field or
> > so, not the lie. OTOH you can introduce memory shrinking support to krealloc()
> > but I have no idea what will be a perception of any of these approaches.
>
> I believe krealloc() can shrink, unlike devm_krealloc().
Do we have a test case for that? Because as I read the __do_krealloc()
I see that *sometimes* (if some conditions happen)it might allocate a new
buffer with a smaller size. kasan_krealloc() in all three cases returns
the same address back without any reallocation done.
Where am I wrong?
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] devres: update node debug info on in-place devm_krealloc()
2026-12-04 11:09 ` Andy Shevchenko
@ 2026-12-04 11:18 ` Andy Shevchenko
2026-12-04 12:50 ` Johan Hovold
0 siblings, 1 reply; 9+ messages in thread
From: Andy Shevchenko @ 2026-12-04 11:18 UTC (permalink / raw)
To: Johan Hovold
Cc: Eric Yun Lin Chiang, Greg Kroah-Hartman, Rafael J . Wysocki,
Danilo Krummrich, Bartosz Golaszewski, driver-core, linux-kernel
On Fri, Dec 04, 2026 at 01:09:07PM +0200, Andy Shevchenko wrote:
> On Fri, Dec 04, 2026 at 10:28:46AM +0100, Johan Hovold wrote:
> > On Fri, Dec 04, 2026 at 11:13:28AM +0200, Andy Shevchenko wrote:
> > > On Fri, Dec 04, 2026 at 08:39:19AM +0800, Eric Yun Lin Chiang wrote:
> > > > 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.
> > >
> > > Doesn't matter. We should not lie to the users of the traces. If size was not
> > > shrunk, it is not changed based on what the caller of krealloc() asked for.
> > > If you want to add that to the trace, it should be an additional field or
> > > so, not the lie. OTOH you can introduce memory shrinking support to krealloc()
> > > but I have no idea what will be a perception of any of these approaches.
> >
> > I believe krealloc() can shrink, unlike devm_krealloc().
>
> Do we have a test case for that? Because as I read the __do_krealloc()
> I see that *sometimes* (if some conditions happen)it might allocate a new
> buffer with a smaller size. kasan_krealloc() in all three cases returns
> the same address back without any reallocation done.
>
> Where am I wrong?
So, for krealloc() required alignment is hard coded to 1, so the only way to
shrink is to use __GFP_THISNODE, but node is also hardcoded to NUMA_NO_NODE,
so no, there is no way to shrink memory with krealloc().
Please, correct me, if I'm wrong.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] devres: update node debug info on in-place devm_krealloc()
2026-12-04 11:18 ` Andy Shevchenko
@ 2026-12-04 12:50 ` Johan Hovold
2026-12-04 14:24 ` Andy Shevchenko
0 siblings, 1 reply; 9+ messages in thread
From: Johan Hovold @ 2026-12-04 12:50 UTC (permalink / raw)
To: Andy Shevchenko
Cc: Eric Yun Lin Chiang, Greg Kroah-Hartman, Rafael J . Wysocki,
Danilo Krummrich, Bartosz Golaszewski, driver-core, linux-kernel
On Fri, Dec 04, 2026 at 01:18:06PM +0200, Andy Shevchenko wrote:
> On Fri, Dec 04, 2026 at 01:09:07PM +0200, Andy Shevchenko wrote:
> > On Fri, Dec 04, 2026 at 10:28:46AM +0100, Johan Hovold wrote:
> > > On Fri, Dec 04, 2026 at 11:13:28AM +0200, Andy Shevchenko wrote:
> > > > Doesn't matter. We should not lie to the users of the traces. If size was not
> > > > shrunk, it is not changed based on what the caller of krealloc() asked for.
> > > > If you want to add that to the trace, it should be an additional field or
> > > > so, not the lie. OTOH you can introduce memory shrinking support to krealloc()
> > > > but I have no idea what will be a perception of any of these approaches.
> > >
> > > I believe krealloc() can shrink, unlike devm_krealloc().
> >
> > Do we have a test case for that? Because as I read the __do_krealloc()
> > I see that *sometimes* (if some conditions happen)it might allocate a new
> > buffer with a smaller size. kasan_krealloc() in all three cases returns
> > the same address back without any reallocation done.
> >
> > Where am I wrong?
>
> So, for krealloc() required alignment is hard coded to 1, so the only way to
> shrink is to use __GFP_THISNODE, but node is also hardcoded to NUMA_NO_NODE,
> so no, there is no way to shrink memory with krealloc().
>
> Please, correct me, if I'm wrong.
You're right about reallocation, but it seems devm_krealloc() do fail to
honour __GFP_ZERO when shrinking.
Johan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] devres: update node debug info on in-place devm_krealloc()
2026-12-04 12:50 ` Johan Hovold
@ 2026-12-04 14:24 ` Andy Shevchenko
0 siblings, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2026-12-04 14:24 UTC (permalink / raw)
To: Johan Hovold
Cc: Eric Yun Lin Chiang, Greg Kroah-Hartman, Rafael J . Wysocki,
Danilo Krummrich, Bartosz Golaszewski, driver-core, linux-kernel
On Fri, Dec 04, 2026 at 01:50:56PM +0100, Johan Hovold wrote:
> On Fri, Dec 04, 2026 at 01:18:06PM +0200, Andy Shevchenko wrote:
> > On Fri, Dec 04, 2026 at 01:09:07PM +0200, Andy Shevchenko wrote:
> > > On Fri, Dec 04, 2026 at 10:28:46AM +0100, Johan Hovold wrote:
> > > > On Fri, Dec 04, 2026 at 11:13:28AM +0200, Andy Shevchenko wrote:
...
> > > > > Doesn't matter. We should not lie to the users of the traces. If size was not
> > > > > shrunk, it is not changed based on what the caller of krealloc() asked for.
> > > > > If you want to add that to the trace, it should be an additional field or
> > > > > so, not the lie. OTOH you can introduce memory shrinking support to krealloc()
> > > > > but I have no idea what will be a perception of any of these approaches.
> > > > I believe krealloc() can shrink, unlike devm_krealloc().
> > >
> > > Do we have a test case for that? Because as I read the __do_krealloc()
> > > I see that *sometimes* (if some conditions happen)it might allocate a new
> > > buffer with a smaller size. kasan_krealloc() in all three cases returns
> > > the same address back without any reallocation done.
> > >
> > > Where am I wrong?
> >
> > So, for krealloc() required alignment is hard coded to 1, so the only way to
> > shrink is to use __GFP_THISNODE, but node is also hardcoded to NUMA_NO_NODE,
> > so no, there is no way to shrink memory with krealloc().
> >
> > Please, correct me, if I'm wrong.
>
> You're right about reallocation, but it seems devm_krealloc() do fail to
> honour __GFP_ZERO when shrinking.
It might be true. Bart, who is the author of devm variant, if I'm not mistaken,
is already in the Cc list to comment.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-12-04 14:24 UTC | newest]
Thread overview: 9+ 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
2026-12-04 9:13 ` Andy Shevchenko
2026-12-04 9:28 ` Johan Hovold
2026-12-04 11:09 ` Andy Shevchenko
2026-12-04 11:18 ` Andy Shevchenko
2026-12-04 12:50 ` Johan Hovold
2026-12-04 14:24 ` Andy Shevchenko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox