From nobody Fri Apr 3 10:19:01 2026 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (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 966B1314A65 for ; Wed, 1 Apr 2026 05:37:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775021852; cv=none; b=iQgtgh3ok3UI6cL+SBQimj1FlPY+wOhUODunz2V8y347bWX4vCivNtcCrGa3IkkIvxMYk12yANqk952NjDVFL38Iql6XRjOcukNQBHkbZ+An2OvzftoQxZOUSLDIjVH7CiV5AlGJjcA19ZFt65xVEsEeAyroBVRsiAp0gMWgkC0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775021852; c=relaxed/simple; bh=g7Ed5EWs6irGU3euUlogWHAf8XlbAn4lOy8PpKGTXJ8=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=PhrFajo70bXIT2TV0QEA8d1GHzMsMOCrGi8vu/+vWlIpwXFfT9OeUfPAyxDEly707VruDshMio1AlO1GOriUYeg0XAUL+nPl9np8K2+KT/NsDn0UN1kgEDmeuURReBV3zeo4iR7Yi6q9bW4BEp9s9C+IOrOrA9VNde007t8YubY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fW0pWqpn; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--vdonnefort.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fW0pWqpn" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-43b9a86b9b0so6505278f8f.0 for ; Tue, 31 Mar 2026 22:37:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775021849; x=1775626649; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=jBLhbC6YBGjXwUzmIRUHH3YF/N1tH9WoVcrhDPGIIHw=; b=fW0pWqpnNxGobl+hmwt+jaG5DrbH1oONqXM30ldhscnVq/5kasZrBSftdhTYdktU7a QRSlGMGJOM7gLcRCsyPn7mZKzSJWzfUHYZesU9X8vUzIXXwF8zTmI6Kf4wcYnOX5HsUv BjmWjKIKR7tGeeZq6Sk6H7u6L6Tq7gzyS+k4zDGjHoY+JDTtDkWjrMEVPLnFKByWccF1 o2qF8hTg4UV6ZAMQcKTGVhNZ4Kklt/0WIcW3MZrk8VfoZ5vVdFl5n1eilra6TAgxC7S2 Lermm+SznM5VimAJXg/6nj4DzNb86q5eVy/fXLBKHpzAIvtQAxEwgQxKnHKEEqQz41Qo h/rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775021849; x=1775626649; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=jBLhbC6YBGjXwUzmIRUHH3YF/N1tH9WoVcrhDPGIIHw=; b=El0U4aDKVuxBw7OhLq+gObSRv+e05GyJEQCb4KVxbaxU5hmgiDvsDI5O8g9+KIl42e ccMY2bbrPwWiPvG5/ITahgenQqFOSpd0Xh8NU4WLP2kVTb6sonm9uVzHHT4J/41AfxEB SLIGW1LGDDjbMCkwbDShGvp3w5rkL4hOXers/OCWLGNZplR39G099SQrZC41VGDtceDC X3iXyo24j4qEpon6Ec9aItc6LaB1PuYS/6AfBAmBFffv+Z5kTrPXbzSuSMHpGf+visuE nVNIr9BFkEa1/f8nQ3F0+08OphN2XYmK6aF8ViBsfT5XjwcXI+z6shwPGg+Aq0bZdY5w RJfA== X-Forwarded-Encrypted: i=1; AJvYcCW8DK0XDYNTKai0cenD6/Xxz6eR0ipa0NLJtmGowiJFXyhcNPtfWV4nl2HY13VrnCVKsG8/2Lejs8j5M5U=@vger.kernel.org X-Gm-Message-State: AOJu0YxzfbX61HFY+nbSfnK4Z5IjbsF3CKZa0ft95D5ZTNodevtmsrqa OVvNHPr5Ah+0ZlglVG0ft5PaTCBHNVEyJV9LnVF3bKjnTA99nD1kEctm0XQFLcB/ogXNuRSGNYi f/+K5McicF0GwwkkDIMVQtA== X-Received: from wmby19.prod.google.com ([2002:a05:600c:c053:b0:486:7a8:4f4e]) (user=vdonnefort job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:818f:b0:485:3423:727d with SMTP id 5b1f17b1804b1-48883595de9mr30905495e9.26.1775021848954; Tue, 31 Mar 2026 22:37:28 -0700 (PDT) Date: Wed, 1 Apr 2026 06:36:59 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.53.0.1118.gaef5881109-goog Message-ID: <20260401053659.3458961-1-vdonnefort@google.com> Subject: [PATCH] ring-buffer: Enforce read ordering of trace_buffer cpumask and buffers From: Vincent Donnefort To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, linux-trace-kernel@vger.kernel.org Cc: kernel-team@android.com, linux-kernel@vger.kernel.org, Vincent Donnefort Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" On CPU hotplug, if it is the first time a trace_buffer sees a CPU, a ring_buffer_per_cpu will be allocated and its corresponding bit toggled in the cpumask. Many readers check this cpumask to know if they can safely read the ring_buffer_per_cpu but they are doing so without memory ordering and may observe the cpumask bit set while having NULL buffer pointer. Enforce the memory read ordering by sending an IPI to all online CPUs. The hotplug path is a slow-path anyway and it saves us from adding read barriers in numerous call sites. Signed-off-by: Vincent Donnefort Suggested-by: Steven Rostedt (Google) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 170170bd83bd..10d2d0404434 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -7468,6 +7468,12 @@ int ring_buffer_map_get_reader(struct trace_buffer *= buffer, int cpu) return 0; } =20 +static void rb_cpu_sync(void *data) +{ + /* Not really needed, but documents what is happening */ + smp_rmb(); +} + /* * We only allocate new buffers, never free them if the CPU goes down. * If we were to free the buffer, then the user would lose any trace that = was in @@ -7506,7 +7512,18 @@ int trace_rb_cpu_prepare(unsigned int cpu, struct hl= ist_node *node) cpu); return -ENOMEM; } - smp_wmb(); + + /* + * Ensure trace_buffer readers observe the newly allocated + * ring_buffer_per_cpu before they check the cpumask. Instead of using a + * read barrier for all readers, send an IPI. + */ + if (unlikely(system_state =3D=3D SYSTEM_RUNNING)) { + on_each_cpu(rb_cpu_sync, NULL, 1); + /* Not really needed, but documents what is happening */ + smp_wmb(); + } + cpumask_set_cpu(cpu, buffer->cpumask); return 0; } base-commit: 7aaa8047eafd0bd628065b15757d9b48c5f9c07d --=20 2.53.0.1118.gaef5881109-goog