From nobody Sun Feb 8 23:11:09 2026 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0FDCC19D8AC for ; Fri, 14 Nov 2025 03:34:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763091260; cv=none; b=pYIUrE+kF+qg/d8sHTwgvl29jKkLo4EWqaoTgmsZrhSS0cDHGYOIvCp4ys+LccD7Hoi1HDENdo/LdrFlRVskQWIFqoqfIO0iT76knFF6Vsmki6IYKmEKHWNyK3Qk3bzemiCdLSKi/Dlnr6Hk2Km2b6sPDcLHFHOwzkUuggFTQIE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763091260; c=relaxed/simple; bh=eypeEOGS4DTZUcPlaH9EVqH033dMbd2jWX5a0gNfGyI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZgbeU0M68UpAcy+IW8Q+XVXOvK5WbthAV76+C7KyFkmGb/vs57uc5W7nW+W/4RgJSxJIPpPCfEFEpNqnUxhgFRY05ReHeAOBzc1ArbwrqmXTOiKsJSQPeZ2J4dYdyiKN9yTIooFgqhlKblS96kxIruZWYoQmEYKsZS30SBRO32A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=diTjSPFn; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="diTjSPFn" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7b852bb31d9so1801369b3a.0 for ; Thu, 13 Nov 2025 19:34:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1763091258; x=1763696058; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=06rxs18soTzDGgvX4jMIEpe66BQCyuI5RVVlwhCORYg=; b=diTjSPFn+bKUbjdnhppqtIrEfdXuvv0VHaXUAaXL5Z7h+fyZmG5Q4PMzYwxDm1GLoL z6JOqAl/tYr2tk/XQWxv33JDgjiHTjDVMMPq8qpfLOrxF5WSdIrU/TIlUOTgnUtFexq6 CjTbpra+dD6srQlqR5b97WcNgqPzgJHC2hSAL0munrKB2pt30H1EdM8lEB8X6pohOXY5 8AX1dcFE1r8DGIeiklOsTy/WkYx+B7LQrUzZZI3ISnsRHkYn7iqhxpLLp+nTKm1QeRty UtPN7ietfSw/zpECd4V9Sh0qjNECWD7FXGEucOj1/i/xeEBdIa7h46tGconFNJw9Kq8H IhOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763091258; x=1763696058; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=06rxs18soTzDGgvX4jMIEpe66BQCyuI5RVVlwhCORYg=; b=pnqD28yN3drs98V4WTT2BTWe/pRapzO4ZgBnOVp/G8wmSpEdAgB09ljAtvab2sAJf8 7jCUapkIxVnPN3Xu58Afqqt5ZjwI52t7lXieiMk7CpAuz7neRpAxPJIZxCp6DG2Ed0zZ 6Wo1uWyL5y3EdAlH2ffmBrgBYr3wnKQ5YdavUKltIrcLvL+KOtpUWQQ7HduhmARIEEnd iPsKLzNHc+d/8pDdBkBGcHtFXHp02ra0QaMXPhnoLCXqf1vJMrofwAFdx/rohOmMO6Ee DCvGNsEr7vu62s2zr3u2qRHVPjCRzvtM76IN7L41O3qSaNvOfV9w7phaKvyi46U4cHUF kLAQ== X-Forwarded-Encrypted: i=1; AJvYcCXndM9KICxi7+LlVSoBwkvaOQLXS6Az9ZNhHXtEvY1Z+Ut7sKJBZEupTz8xJYrulV/aacypCbPBRiNv/s8=@vger.kernel.org X-Gm-Message-State: AOJu0YzRWYvTeFgi/GE5JyFYcRyEU/HdADP83TiZGtOY9Ojk0HPtaIWO MqGkknPAxVWQ4e0SfCZyQEqdO4JELq6VVEhwqQxginNrsxXuKp82KKqxeijrRUrH/4Q= X-Gm-Gg: ASbGncuYNz19Aj94jK8Jne915s3o4QFwSVfUusYoHQ71BeGrUcngS6puokyHpKp5Wzr 8TsO8IHxEGOAbcOQHdxrsmidT3FMg+NlWUePPDDD7SQSdPjxOi8MhSZ+IKj8MzdZl4atg/HEgVy a/FTWYBFqVRYIe6JkrORJauDCDNFbWqhjrkGrBlz7OrCg/Nzz2wqK77W3Tw8dBIiyLncw+PhyCq O5Jmea64nZ0207yNeM6crxc+0LwT8nnzWKxaFmc/Ol8l1AbzQJJrsK/2Wl1aJCIfGzvuD8Gcufw cTEH1QyCGbdcNp6rdtKeGX9cvdNxKnz7hTU1TaKykpGrddkHW/JdCZWsHs7xgvNRzAVuhhFhsrf Xq3liesli8Yxs24xmjmFnHqWxeZdaD2cf8c0Wp9DQY0y0NPU3w2N1O8q0Mrlkwo8dQC6n9ncZqk jAM2EoU0FFflTTqdtWZA6+VZZS+UTPUYndq6y8Yg== X-Google-Smtp-Source: AGHT+IHxKhWOLK7ixe3MOL0Ju5wRKPpe/wyqhOmYIo/Pknu5a7BOoAwSwSzFp5FswHA7DxNLm8+Y3Q== X-Received: by 2002:a05:6a00:12c5:b0:772:8101:870c with SMTP id d2e1a72fcca58-7ba39bc0759mr2295198b3a.11.1763091258072; Thu, 13 Nov 2025 19:34:18 -0800 (PST) Received: from T179DVVMRY.bytedance.net ([2001:c10:ff04:0:1000::7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b92782d39bsm3671231b3a.63.2025.11.13.19.34.12 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 13 Nov 2025 19:34:17 -0800 (PST) From: Liangyan To: peterz@infradead.org, mingo@redhat.com, bigeasy@linutronix.de Cc: acme@kernel.org, namhyung@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, james.clark@linaro.org, zengxianjun@bytedance.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Liangyan Subject: [PATCH v3] perf/core: Fix pending work re-queued in __perf_event_overflow Date: Fri, 14 Nov 2025 11:33:49 +0800 Message-Id: <20251114033349.3048-1-liangyan.peng@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20251109164122.80138-1-liangyan.peng@bytedance.com> References: <20251109164122.80138-1-liangyan.peng@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We got warning below during perf test. [ 467.100914] [ T1] WARNING: CPU: 0 PID: 1 at kernel/events/core.c:51= 47 put_pmu_ctx+0x2ef/0x3c0 [ 467.107702] [ T1] CPU: 0 UID: 0 PID: 1 Comm: systemd Kdump: loaded = Tainted: G E 6.18.0-rc4-dirty #114 PREEMPT(voluntary) [ 467.109835] [ T1] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009= ), BIOS 1.16.2-debian-1.16.2-1 [ 467.111027] [ T1] RIP: 0010:put_pmu_ctx+0x2ef/0x3c0 [ 467.122081] [ T1] Call Trace: [ 467.122463] [ T1] [ 467.124822] [ T1] __free_event+0x337/0xa50 [ 467.125306] [ T1] perf_pending_task+0x10f/0x3b0 [ 467.125824] [ T1] task_work_run+0x140/0x210 [ 467.127413] [ T1] exit_to_user_mode_loop+0x10e/0x130 [ 467.127965] [ T1] do_syscall_64+0x26d/0x2e0 [ 467.128453] [ T1] entry_SYSCALL_64_after_hwframe+0x76/0x7e [ 467.129025] [ T1] RIP: 0033:0x7f01d22349ca [ 467.135157] [ T1] A race condition occurs between task context and IRQ context when handling sigtrap tracepoint event overflows: 1. In task context, an event is overflowed and its pending work is queued to task->task_works 2. Before pending_work is set, the same event overflows in IRQ context 3. Both contexts queue the same perf pending work to task->task_works This double queuing causes: - task_work_run() enters infinite loop calling perf_pending_task() - Potential warnings and use-after-free when event is freed in perf_pending_task() Fix the race by disabling interrupts during queuing of perf pending work. Fixes: c5d93d23a260 ("perf: Enqueue SIGTRAP always via task_work.") Reported-by: Xianjun Zeng Signed-off-by: Liangyan --- v3: Refine commit log suggested by Sebastian. --- kernel/events/core.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kernel/events/core.c b/kernel/events/core.c index cae921f4d137..7c63e5fdd334 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -10433,6 +10433,16 @@ static int __perf_event_overflow(struct perf_event= *event, =20 notify_mode =3D in_nmi() ? TWA_NMI_CURRENT : TWA_RESUME; =20 + /* + * Task context queues the work via task_work_add() but has not yet + * set event->pending_work when the same event overflows in + * IRQ context. The IRQ path, seeing !event->pending_work, + * queues the work again. + * The double queuing causes corruption in task->task_works. + * Prevent this by disabling interrupts around the critical section. + */ + guard(irqsave)(); + if (!event->pending_work && !task_work_add(current, &event->pending_task, notify_mode)) { event->pending_work =3D pending_id; --=20 2.39.3 (Apple Git-145)