Linux driver-core infrastructure
 help / color / mirror / Atom feed
From: Eric Yun Lin Chiang <ericyunlinchiang@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	Danilo Krummrich <dakr@kernel.org>
Cc: Johan Hovold <johan@kernel.org>,
	Bartosz Golaszewski <brgl@kernel.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	driver-core@lists.linux.dev, linux-kernel@vger.kernel.org,
	Eric Yun Lin Chiang <ericyunlinchiang@gmail.com>
Subject: [PATCH] devres: update node debug info on in-place devm_krealloc()
Date: Thu,  3 Dec 2026 08:57:04 +0800	[thread overview]
Message-ID: <20261203005704.3066960-1-ericyunlinchiang@gmail.com> (raw)

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


             reply	other threads:[~2026-12-03  0:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  0:57 Eric Yun Lin Chiang [this message]
2026-12-03 11:42 ` [PATCH] devres: update node debug info on in-place devm_krealloc() 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203005704.3066960-1-ericyunlinchiang@gmail.com \
    --to=ericyunlinchiang@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=brgl@kernel.org \
    --cc=dakr@kernel.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=johan@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox