mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nickolai Zeldovich <nickolai@csail.mit.edu>
To: Stephen Boyd <sboyd@kernel.org>,
	Brian Masney <bmasney+clk@redhat.com>,
	Jerome Brunet <jbrunet+clk@baylibre.com>
Cc: linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org,
	Nickolai Zeldovich <nickolai@csail.mit.edu>
Subject: [PATCH v2] clk: Free the parent map only in __clk_release()
Date: Fri, 23 Oct 2026 23:39:04 -0400	[thread overview]
Message-ID: <20261024033904.830766-1-nickolai@csail.mit.edu> (raw)
In-Reply-To: <20261016164050.790594-1-nickolai@csail.mit.edu>

Since commit 9d05ae531c2c ("clk: Initialize struct clk_core kref
earlier"), every failure in __clk_register() frees the parent map
through __clk_release().  clk_core_populate_parent_map() still cleans up
after itself on failure, so an allocation failure while registering a
clock with parents either dereferences a NULL map (core->parents is NULL
while core->num_parents is set) or frees the parent names and the map a
second time.

Leave the cleanup to __clk_release(): return the error without freeing
anything, and have clk_core_free_parent_map() check for a missing map
instead of num_parents == 0.  The map is zero-allocated and
kfree_const(NULL) is a no-op, so a partially filled map is freed
correctly.

Fixes: 9d05ae531c2c ("clk: Initialize struct clk_core kref earlier")
Cc: stable@vger.kernel.org
Reviewed-by: Jerome Brunet <jbrunet@baylibre.com>
Signed-off-by: Nickolai Zeldovich <nickolai@csail.mit.edu>
---

Notes:
    Changes in v2:
    - Shortened the commit message (Jerome, Brian).
    - Added Cc: stable (Brian) and Jerome's Reviewed-by.
    - No code change; rebased onto current master.
    
    v1: https://lore.kernel.org/r/20261016164050.790594-1-nickolai@csail.mit.edu
    
    Developed with the assistance of an LLM.
    
    Reproduced on riscv64 under QEMU by rebinding a fixed-factor clock
    provider through its sysfs bind file with failslab aimed at the parent
    map allocation: without the patch the kernel oopses with a NULL pointer
    dereference in __clk_release(); with it the bind fails with -ENOMEM and
    "failed to register clk" is logged.  The second failure path (a parent
    name copy failing) was reproduced only in a userspace transcription of
    the four functions under AddressSanitizer.  The clk KUnit suites pass on
    x86_64 and riscv64.

 drivers/clk/clk.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index fef87167a60b5..c32c3e9d62bdb 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -4353,15 +4353,8 @@ static int clk_core_populate_parent_map(struct clk_core *core,
 			WARN(1, "Must specify parents if num_parents > 0\n");
 		}
 
-		if (ret) {
-			do {
-				kfree_const(parents[i].name);
-				kfree_const(parents[i].fw_name);
-			} while (--i >= 0);
-			kfree(parents);
-
+		if (ret)
 			return ret;
-		}
 	}
 
 	return 0;
@@ -4371,7 +4364,7 @@ static void clk_core_free_parent_map(struct clk_core *core)
 {
 	int i = core->num_parents;
 
-	if (!core->num_parents)
+	if (!core->parents)
 		return;
 
 	while (--i >= 0) {

base-commit: 4da9dedf3fc59137fd86d664693776aa38cb97f5
-- 
2.53.0


      parent reply	other threads:[~2026-10-24  3:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-16 16:40 [PATCH v1] " Nickolai Zeldovich
2026-10-19  8:59 ` Jerome Brunet
2026-10-19 22:16 ` Brian Masney
2026-10-24  3:39 ` Nickolai Zeldovich [this message]

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=20261024033904.830766-1-nickolai@csail.mit.edu \
    --to=nickolai@csail.mit.edu \
    --cc=bmasney+clk@redhat.com \
    --cc=jbrunet+clk@baylibre.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sboyd@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

all inboxes | Powered by JetHome®