From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796410202; b=P4cWYhOqscsmmP5POeF1oyqOfx/ld/xROyiqjm/Wdes+Jgaq6MJCbg6B0np/bAPo3aEa 3O2dx52wuXHEcIYIZi60+qUIKIwK5z7BFYU+yaG+CH8/QFA3kNXVOj/RT8pQHDjS5Ihjx J5ouHPhKelOYzLovkU6A9gJVdKaWSqrK9en+IwWcgoyMBwVm1uDcxgvlOmkDZYMCOrVIX A/eK6dPZtXZi9RcuFDmWG7sqQ3vZP3mPvzWVpJFIbYo185H0L0Aly/hzleAUVwDIDmYKG TGrTPLWF3i8Jd4iUT8HK/LeMYSeIqShN9pq2t6vYBuGh0jXPHjifeWslvOf9tXmIGag== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796410202; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=KORF/980moZRVX3ue8HS3mlSf+AaIWjy6CUklsL2XZ0=; b=fdeKRuAUVFIbsimwC3UcEqKHLmyz82XFu7s5HJYrwPkcj6wgg46Cpq1rAqsToAiXb0B+ EWAQU+sW9+2Ch8oNhgadJp7MbTVrbeYiAj0jiEHZ3oxxEqq6bJ+uoCp7WPoruHUvS3k1p j1LF2Fm9ik5sQkGnxMGRJxi2QBUkxobI0DSPdH9bP1pz6irTuWUFvXEIsiUBboNn4zsBt 29i43aiLKxOdw8MNn+KSQF50pI+kxAYnLxBGPZSOrXzET0lfWxuJ40AtUNmMG+ruT3bte OAHgsn8dmlovLBqNCPoz2Ti46ndvEg90TuH+ojPnmrg0KTFrZSn8Bb6x2dNmJX0CPcA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=M4CfZeaH; arc=none smtp.remote-ip=74.125.82.41 Received: from mail-dl1-f41.google.com (mail-dl1-f41.google.com [74.125.82.41]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 546D2606A322 for ; Fri, 4 Dec 2026 18:50:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 546D2606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=M4CfZeaH Received: by mail-dl1-f41.google.com with SMTP id a92af1059eb24-1a3c08d6928so1933490c88.0 for ; Fri, 04 Dec 2026 10:50:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796410201; x=1797015001; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=KORF/980moZRVX3ue8HS3mlSf+AaIWjy6CUklsL2XZ0=; b=M4CfZeaHcd5FTdoFkSLyFfHIJioOlm3fCeziz8/oOMj6yDeyxDqRGryw/vldfP/2r1 YrDmtmQlStZ+o60/qvrNl2jdaipN+qlm9xm8SFsL4vmevlBTyUyVgwjRyDuHrrQK5n0N 8D+UOd+fz5wJUCODFqLdHBGt9JHiG6VMtzB8IR8LL7o1pkrv4tGggKs52EAEHaEgDA9S 3BrXqiE9fYC0HWuk1AE/oBWHWhTyjx+S2BwcdHHUJRLF9rTXjTCR9ZaBYElulCoGzir8 Jy/ozBo0k6IiQZux0oyE4XrwH/ya1Gm+pUQ1BwVQ3nwLO9Gpf/5wBWiSCwE4M2qFrLyQ YMng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796410201; x=1797015001; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=KORF/980moZRVX3ue8HS3mlSf+AaIWjy6CUklsL2XZ0=; b=WtypNvFN23HMNiKom9zMXcPYKNB+Z2q9cUboTASFT1Jb57XRg9nBoG76ABIS6rViz/ l9TtL7IUCM0UomAP07jp5CAHJMihYmiqXLOEEWIR8sFBXphIviDsapJLQZaAKT9OUyE8 W8NF7XhreHxlVldII0T8t1xE9EVcXPWwL1Z4tr7hzQ6QRZA6YZ3TugCwIuixVJyvxc+o /LQXvj2SwXMheJ3E5s/G5Ff+8vtt8dX3eOM9yS4XOQDpMaZJ2ohPA6SIis1pUDMflATu /M+kGy+PuDa8t43vkTUAiermRNeE9kyzx9u9oIF6GI8zAeC8E2H07q42ShUO8lrW360M l6Lw== X-Forwarded-Encrypted: i=1; AL+hErFX7VT5c3waBNJ2a7D966P6D6DdYEicElGw+VaCwWTu3H7pkZ5V3ldt52UbEr4oJMOWmUDLMycfDPPoCDXYH/e/xIk=@vger.kernel.org X-Gm-Message-State: APuU4Z+j0UzSVQXPx5d1ULMVa29Pd4aqya6cnDJAaH+2k+oIc1XIdwh+ tgfqEhlP5EWaVrmN3sHTRGvU6ZbEX6OfrAO0u/HOq8v2OpBffotMeyxU X-Gm-Gg: AZFdTZeCH3e51SHHZQPpvAdv0wIfTdBzYdu5HDU76wYePMqsruckZwC21nkNNdEXLTe kWhzciCBmg1b1W75uQpiFO3nOaS0eZCASkAQ2Idv55brcN+0Zej9gOwk5oz0AOdK3rkGTHP9FY4 b1xamg/HjaQ9yd+5DIZBu70gBACgciJrIxGPJh+Js9C3T5/4LOd3qUrrXGRUCzDbiE/ocsCV/Dc 8VS1xnnUTw89ysA5m0sRRVPlGrDF4BuhrP4RC4H5vZXdeC223jF2LPx8IhGzV0sb86cdZVBHc2n OT8l5TMRCBwNo5D3denobaqwg5nFO6XkAq9vg7R3VStbTfvFXpob9PTHn5cCUG63xeF+WWut/Iw Fk78Gzu9OWCh6kUZ8lCPklTsoIkZWyIa/3dv7QNm2x9O55JCOJInvbyuGip6X8G4B6RsF6eaKpJ j3qcvSowaEcWgAdw/+yb4qYORIdmyzR15hOFdjc9UrNwONEeqqgNfC94sbanfyDjYmyK4a7N390 ybpEd3a9ENA3yqsKAE8j7JH12STPlaMUkML8ihnyYFc9p4MYU+ovaP6VHOiN7stF4zpbUUrmPZn KAFi8nQnfID+xTEiGnS/MpxhQwNxHGYJZSrOtOwnbA0XhZdWwSQhFkatYvpmdvrTFwuTxmHCSUm tWW3efOPtd97I X-Received: by 2002:a05:701b:220f:b0:1b5:330f:6232 with SMTP id a92af1059eb24-1b5d7104ae6mr2403728c88.12.1796410201448; Fri, 04 Dec 2026 10:50:01 -0800 (PST) Received: from localhost.localdomain (wood.seclab.cs.ucsb.edu. [128.111.49.37]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5d642de41sm8412279c88.3.2026.12.04.10.50.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 10:50:01 -0800 (PST) From: Praneeth Balasubramanian To: Steven Rostedt , Masami Hiramatsu Cc: Mathieu Desnoyers , linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, Praneeth Balasubramanian , stable@vger.kernel.org Subject: [PATCH 1/2] tracing: Keep an instance alive while ftrace_dump() dumps it Date: Fri, 4 Dec 2026 10:49:57 -0800 Message-Id: <20261204184958.1687218-1-praneethb2002@gmail.com> X-Mailer: git-send-email 2.25.1 X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit ftrace_dump_by_param() looks up the instances named in ftrace_dump_on_oops with trace_array_find(), whose comment says that trace_types_lock must be held, and then dumps them with ftrace_dump_one(). ftrace_dump() runs from the oops, panic and SysRq paths and cannot take that mutex, so it holds neither the lock nor a reference. If the instance is removed meanwhile (instance_rmdir() -> __remove_instance()), the dump can follow a list entry that list_del() has poisoned, or use a trace_array and buffers that have been freed. ftrace_dump_one() does not sleep, so the dump can stay in an RCU read-side critical section. Look the instance up with a new trace_array_find_rcu() under rcu_read_lock() and stay in the critical section until the instance has been dumped. On the update side, use list_add_rcu() and list_del_rcu() for ftrace_trace_arrays, and always wait for a grace period in __remove_instance() before the instance is torn down. It already waited when the trace marker copy was enabled. The unlocked lookup was found by a static check that turns the locking rule in a function's comment into a Clang context-analysis requirement on its callers. Fixes: 19f0423fd55c ("tracing: Support to dump instance traces by ftrace_dump_on_oops") Cc: stable@vger.kernel.org Signed-off-by: Praneeth Balasubramanian --- Notes: This was found by a compiler-based check, part of research at UCSB SecLab. The locking rule stated in the kernel-doc comment of the called function was turned into a Clang context-analysis requirement (__must_hold) on that function, and Clang 23 with CONFIG_WARN_CONTEXT_ANALYSIS then checked every caller. An LLM was used only to turn the comment into a machine-readable rule; the report itself comes from the compiler. The patch and changelog were drafted with Claude Opus 5.5. I reviewed the patch and take responsibility for it. Testing: builds without new warnings with W=1 (x86_64, clang 23, allmodconfig). Not runtime-tested, and I have not observed the race myself. kernel/trace/trace.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 4ccdefb5c3ef..faa17589df70 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -8765,7 +8765,7 @@ trace_array_create_systems(const char *name, const char *systems, } else __trace_early_add_events(tr); - list_add(&tr->list, &ftrace_trace_arrays); + list_add_rcu(&tr->list, &ftrace_trace_arrays); tr->ref++; @@ -8890,14 +8890,20 @@ static int __remove_instance(struct trace_array *tr) if (tr->ref > 1 || (tr->current_trace && tr->trace_ref)) return -EBUSY; - list_del(&tr->list); + list_del_rcu(&tr->list); if (printk_trace == tr) update_printk_trace(&global_trace); /* Must be done before disabling all the flags */ - if (update_marker_trace(tr, 0)) - synchronize_rcu(); + update_marker_trace(tr, 0); + + /* + * Wait for RCU readers that may still use tr: the trace marker + * copies, and ftrace_dump(), which finds instances without taking + * trace_types_lock. + */ + synchronize_rcu(); /* Disable all the flags that were enabled coming in */ for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) { @@ -9570,6 +9576,23 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m local_irq_restore(flags); } +/* + * Like trace_array_find(), for ftrace_dump(), which cannot take + * trace_types_lock. Must be called under rcu_read_lock(), and the + * returned trace array may only be used until rcu_read_unlock(). + */ +static struct trace_array *trace_array_find_rcu(const char *instance) +{ + struct trace_array *tr; + + list_for_each_entry_rcu(tr, &ftrace_trace_arrays, list) { + if (tr->name && strcmp(tr->name, instance) == 0) + return tr; + } + + return NULL; +} + static void ftrace_dump_by_param(void) { bool first_param = true; @@ -9597,8 +9620,14 @@ static void ftrace_dump_by_param(void) } inst_name = strsep(&token, "="); - tr = trace_array_find(inst_name); + /* + * trace_types_lock cannot be taken here; RCU keeps the + * instance from being freed until the dump is done. + */ + rcu_read_lock(); + tr = trace_array_find_rcu(inst_name); if (!tr) { + rcu_read_unlock(); printk(KERN_TRACE "Instance %s not found\n", inst_name); continue; } @@ -9608,6 +9637,7 @@ static void ftrace_dump_by_param(void) ftrace_dump_one(tr, DUMP_ORIG); else ftrace_dump_one(tr, DUMP_ALL); + rcu_read_unlock(); } } -- 2.25.1