mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hang Zhengyang <hang.zhengyang1010@gmail.com>
To: Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@redhat.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	Dapeng Mi <dapeng1.mi@linux.intel.com>,
	Hanxiang Xu <xuhx@hust.edu.cn>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH] perf/core: Check for a NULL cpc before cancelling its hrtimer
Date: Thu,  3 Dec 2026 15:37:26 +0800	[thread overview]
Message-ID: <20261203073728.2470032-1-hang.zhengyang1010@gmail.com> (raw)

perf_pmu_register() allocates the per-CPU contexts one CPU at a time.
When one of these allocations fails, it returns -ENOMEM and
perf_pmu_free() runs with NULL entries for the CPUs that are not
allocated yet. The loop in perf_pmu_free() skips those entries, but
commit 35fbc5a85891 ("perf/core: Cancel hrtimer on
perf_pmu_unregister()") put the hrtimer_cancel() before the NULL check.
Such a failure then oopses instead of returning -ENOMEM:

  KASAN: null-ptr-deref in range [0x0000000000000108-0x000000000000010f]
  RIP: 0010:hrtimer_active+0xc1/0x350
  Call Trace:
   hrtimer_cancel+0x15/0x40
   perf_pmu_free+0x1cd/0x320
   perf_pmu_register+0x4e1/0xb40

The allocations are small GFP_KERNEL ones, so this needs fault injection
or a registering task that the OOM killer has picked. Found with failslab
fault injection on the allocations in perf_pmu_register().

Cancel the hrtimer after the NULL check.

Fixes: 35fbc5a85891 ("perf/core: Cancel hrtimer on perf_pmu_unregister()")
Cc: stable@vger.kernel.org
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@gmail.com>
---

Notes:
    Tested with a module that sets current->fail_nth before each
    perf_pmu_register() call, in a KVM guest with 4 CPUs, KASAN and failslab,
    on tip/perf/urgent 67b34211a9eb: the oops above without the patch; with
    it, fail_nth 1-4 return -ENOMEM and fail_nth 5 registers the PMU.
    
    I used Claude Code to find the issue, write the patch and run the test.
    I reviewed the patch.

 kernel/events/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 4ab3919ac78e..726ed5538397 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -12999,9 +12999,9 @@ static void perf_pmu_free(struct pmu *pmu)
 			struct perf_cpu_pmu_context *cpc;
 
 			cpc = *per_cpu_ptr(pmu->cpu_pmu_context, cpu);
-			hrtimer_cancel(&cpc->hrtimer);
 			if (!cpc)
 				continue;
+			hrtimer_cancel(&cpc->hrtimer);
 			if (cpc->epc.embedded) {
 				/* refcount managed */
 				put_pmu_ctx(&cpc->epc);

base-commit: 67b34211a9eb103aba474bc179fab3dd2912f07e
-- 
2.53.0


             reply	other threads:[~2026-12-03  7:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  7:37 Hang Zhengyang [this message]
2026-12-03  9:13 ` Mi, Dapeng

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=20261203073728.2470032-1-hang.zhengyang1010@gmail.com \
    --to=hang.zhengyang1010@gmail.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=stable@vger.kernel.org \
    --cc=xuhx@hust.edu.cn \
    /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®