From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 2D0923A544C for ; Wed, 11 Mar 2026 10:16:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773224169; cv=none; b=ZZ7cUG6I3tLuqP+CdnhmYMY2Fy6mJy5gTJpaglr+ibSDSo7hUUOiQAPGBq/x72//IbRYX1yqdPGkMORXXvdM8KwzYA0zOj8IBzCQQ2Pm9B++7t53JbFyzZaoNRbKcc7lJqlnh/3GECK8G5ZKfpNMGewP+NBw4qkewgC08JKOJNI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773224169; c=relaxed/simple; bh=wPOlGJegScfjGXLqN02j6e37cMLeoiGng2UniPA/m/Q=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=dTQ7cjFSHcJBW2Mc7dbs38biCGkLhwvp9uqGpDxP4uSaOeRTRi9atojsdGZoRhsmSKdJP1WsWURi4HfMhux3tz7UeXXlLXXJ7ibrHDQEWco6oPcRPPVvnLWGNKVCD1d5uwWs/L0mVaDXv/Tli+JOC4LeebxviD4Nt9zMkdqkvN4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=POgbHspI; arc=none smtp.client-ip=209.85.221.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="POgbHspI" Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-439afc58ac7so11243047f8f.0 for ; Wed, 11 Mar 2026 03:16:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1773224166; x=1773828966; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=traLbeCphIWDAPRf8wXsen2JdQ2Go7wYWE6BuOL/0nw=; b=POgbHspIGX4XQfy2d2Wdh4cQcjoLeEiTozxq6PIqEagqreaR9ipnXBSw8FM7F5SH6g iXwfLC8QdxUVEpfnIIs5TjBVqRfenHQ10KnL0iCmPoRKhaWa8YG1wpAug50Tisv+iHPg ltET8pumrh1JZ+EM2NZNWM4RYtYL+Ye5Pc+BoPScUPMLu7rdtZQENa0ZgyH/D6EgWdb+ cc9syNp0hbrUWPvmpb87HV6WDApJLATjBGbub6PJWKOwgDalTCyxNzzQWJUeqbPWYc68 DmkGpd0I+rTb48kEtZlxp9s3cmpG84Bdg9SokdB+flQKRKYcz5iOWCALYhAs2R8FGKF1 /Gkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773224166; x=1773828966; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=traLbeCphIWDAPRf8wXsen2JdQ2Go7wYWE6BuOL/0nw=; b=IPzTMn+IHVmh8NH2KEsPsiwJoWt81BcReTjYWoozsNV/MtNa/Keh4zL/fZb9eclCoz IkY1axsv9jFNwRIczku/+xPW/b/lrf4ZwLZqgi/Rnn0uWcx8bC6M0wHGNUvkbrDDEHMa g0fsPAOcEG9dfg80aIIr8u3U2SFeSDTYIU1gz49VhlGS3uHc3TvlrBcwmM0ztBtFcgfD hYA1vrdn9XZGfWixCGa/XxOi622HEQAFXpwTmDA42+SE1Ya5ksWv3dKXgfwhvrNcvW2O tJN/Z8fbaZwDEJk1DnhugZZvfQZ4SKJ6xP3BTWCs0Gpayq3nC5O1LCamb/C6ZVrNmaug FLpg== X-Forwarded-Encrypted: i=1; AJvYcCUDwoJfsZ1ZrlgINrP+zl0FN4UinqvQ3okQKIU0tJ/0JpeU5bB2V5bE+qCaGBPRINPWu1w8Aitxjeb5hns=@vger.kernel.org X-Gm-Message-State: AOJu0YwHS8ZBPQFmBp8M8AiHxPLaVXlqrmdaXciFpY5fPLjUFv4GVib1 sg4irbRtPt1naIR1kvE6Qqf6TNBobowhnFVUA3k8cJZusetXrOHgixh8Tb7F5tLberM= X-Gm-Gg: ATEYQzwgFBcYwtbRRX8MMW3itwVNhyewoFOWKTJddJfcN0524piCBDi44ZMpkELthLa GWOtkTCFMqKzsi+TZ6eaf5PD4CkNYkVxHZrp8ohvpmejFUBH4EEuX/fTML26GQycaFLYpm/5Mgl HwCUbS8oUNYGKsjDsCIuc8Xddt7r4b0m16rlX4jGRkU1VfFV5dCAfrA6rszW/L5rIM3y+iMZCCi zHi3//JJjWfw+6hbOM35f0+rFwd68AFVH+eLsb5ldlzHHnp3GFlnO/Ji1qxA1am9Ms6NRmG5T+X 96j244JUq3ISzBAtDDtVorwRsYYkN9c8Dz8d5caijgdb+u+3C2fN49EhcjUg5v7AoRU0n48Uk9U Ato3ihQFICojznDSBHVfhHlpaj3nP40pjP7V3L8JEXhfZP2HHwzsErwBl6t8FdORTO9PjHqQ8Kb 11ANtTUTzQqh7pBamT9dRUGxlcG+3+ X-Received: by 2002:a05:6000:228a:b0:439:b7c9:2ef1 with SMTP id ffacd0b85a97d-439f81c639bmr3834639f8f.20.1773224165981; Wed, 11 Mar 2026 03:16:05 -0700 (PDT) Received: from [192.168.1.3] ([185.48.77.170]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-439f81a0480sm5386398f8f.12.2026.03.11.03.16.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 11 Mar 2026 03:16:05 -0700 (PDT) Message-ID: <70621066-d327-4206-9be9-28531ad136b0@linaro.org> Date: Wed, 11 Mar 2026 10:16:04 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3] perf/core: Fix missing read event generation on task exit To: Ian Rogers , Peter Zijlstra Cc: Thaumy Cheng , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , Suzuki K Poulose , Leo Yan , Mike Leach References: <20251024170543.11201-1-thaumy.love@gmail.com> <1e6337ec-d4a0-420b-bd7b-0fd2b6fee620@linaro.org> <20260206152907.GQ1395266@noisy.programming.kicks-ass.net> <61e544ab-d9b9-454e-9dd7-65625d9be126@linaro.org> <20260303142948.GA3698720@noisy.programming.kicks-ass.net> Content-Language: en-US From: James Clark In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 10/03/2026 6:25 pm, Ian Rogers wrote: > On Tue, Mar 3, 2026 at 6:29 AM Peter Zijlstra wrote: >> >> I suppose the easiest fix is this, but urgh... >> >> --- >> diff --git a/kernel/events/core.c b/kernel/events/core.c >> index 03ced7aad309..c38acf1e2f43 100644 >> --- a/kernel/events/core.c >> +++ b/kernel/events/core.c >> @@ -3664,6 +3664,9 @@ static int context_equiv(struct perf_event_context *ctx1, >> if (ctx1->pin_count || ctx2->pin_count) >> return 0; >> >> + if (ctx1->nr_stat || ctx2->nr_stat) >> + return 0; >> + > > So, just for fun, I ran the original patch, James's bug description > and this proposed fix through an AI using Chris Mason's kernel review > prompts. I'm sharing the output as the do_exit comments seem > legitimate given the stack trace in James' report. > > Does this introduce a significant performance regression for multithreaded > workloads? By forcing a full sched_out and sched_in of the PMU on every > thread switch instead of just swapping context pointers, it removes the > performance benefits of context_equiv() for inherited stats. inherit_stat isn't default though. I suppose if you ask for more stuff it costs more, and I assume complete data but slightly slower is better than incomplete but faster. > > Furthermore, does this patch actually address the concurrent ring buffer > corruption reported in the bug? No, the top patch fixes that issue, not the bottom one. > > The corruption occurs because perf_event_exit_event() (called during do_exit) > invokes perf_output_begin() on the parent's ring buffer from the exiting child's > CPU. Because ring buffers use local_t operations which are not SMP-safe, > concurrent child exits corrupt the rb->nest tracking. > > Since context_equiv() is only used during context switch > (perf_event_context_sched_out) and not during task exit, modifying it does > not prevent the concurrent ring buffer writes during do_exit. > > Thanks, > Ian > >> /* If ctx1 is the parent of ctx2 */ >> if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) >> return 1; >> @@ -3677,76 +3680,13 @@ static int context_equiv(struct perf_event_context *ctx1, >> * hierarchy, see perf_event_init_context(). >> */ >> if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && >> - ctx1->parent_gen == ctx2->parent_gen) >> + ctx1->parent_gen == ctx2->parent_gen) >> return 1; >> >> /* Unmatched */ >> return 0; >> } >> >> -static void __perf_event_sync_stat(struct perf_event *event, >> - struct perf_event *next_event) >> -{ >> - u64 value; >> - >> - if (!event->attr.inherit_stat) >> - return; >> - >> - /* >> - * Update the event value, we cannot use perf_event_read() >> - * because we're in the middle of a context switch and have IRQs >> - * disabled, which upsets smp_call_function_single(), however >> - * we know the event must be on the current CPU, therefore we >> - * don't need to use it. >> - */ >> - perf_pmu_read(event); >> - >> - perf_event_update_time(event); >> - >> - /* >> - * In order to keep per-task stats reliable we need to flip the event >> - * values when we flip the contexts. >> - */ >> - value = local64_read(&next_event->count); >> - value = local64_xchg(&event->count, value); >> - local64_set(&next_event->count, value); >> - >> - swap(event->total_time_enabled, next_event->total_time_enabled); >> - swap(event->total_time_running, next_event->total_time_running); >> - >> - /* >> - * Since we swizzled the values, update the user visible data too. >> - */ >> - perf_event_update_userpage(event); >> - perf_event_update_userpage(next_event); >> -} >> - >> -static void perf_event_sync_stat(struct perf_event_context *ctx, >> - struct perf_event_context *next_ctx) >> -{ >> - struct perf_event *event, *next_event; >> - >> - if (!ctx->nr_stat) >> - return; >> - >> - update_context_time(ctx); >> - >> - event = list_first_entry(&ctx->event_list, >> - struct perf_event, event_entry); >> - >> - next_event = list_first_entry(&next_ctx->event_list, >> - struct perf_event, event_entry); >> - >> - while (&event->event_entry != &ctx->event_list && >> - &next_event->event_entry != &next_ctx->event_list) { >> - >> - __perf_event_sync_stat(event, next_event); >> - >> - event = list_next_entry(event, event_entry); >> - next_event = list_next_entry(next_event, event_entry); >> - } >> -} >> - >> static void perf_ctx_sched_task_cb(struct perf_event_context *ctx, >> struct task_struct *task, bool sched_in) >> { >> @@ -3835,8 +3775,6 @@ perf_event_context_sched_out(struct task_struct *task, struct task_struct *next) >> RCU_INIT_POINTER(next->perf_event_ctxp, ctx); >> >> do_switch = 0; >> - >> - perf_event_sync_stat(ctx, next_ctx); >> } >> raw_spin_unlock(&next_ctx->lock); >> raw_spin_unlock(&ctx->lock);