From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796410202; b=CYnVI6x5WNhzQNAPQenB1goUu+PhzrdPHC2M6sVsZpIgrgf9ApuSai8hmeMt6+f+xg1X XwN7HAsqT1wQMULUfFPrI1aI2RZrK8sMge91l8Pa+5YYLJhMJYMme7z9Z6ilPn8LPp1bt TJxi+ipPiSL6E8iZSCDOjhJbJzqs6yl/tYn9pTX5pn+W6hLbAIl+bH+l10m7LJvQVFwyz bD83hGXwSs231L8lrANL/b5YZr+sQ7qOKaZOGjzY72b3cUqAXDJwq82d7rYtrpv9arXEe nM3bGsFpK47YU3Uw7Drh4akLnHABiDHnJGDlmuiGfKUPAeqDfBSFz2tG6WSJcoMELXg== 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=hvZYx5VlQjWBdyzAcs4+zdPWnZPRNnkVYYa473LMWP5cD8nsavcC/Cqlfw08ICk9leBX KQiJek0QTMaV2n9nQE6zvD/CwDrDHC+GmVjDhOw6CEJ4q304YNnt7bi83s6fjDbrEKpV8 ZWYsEA/ypBJ00V8o12SLc/ssYrRx8yl1cTWQz4Rfs/C8SPTIOAcLk0j6r7PfLit7TXq4L 8WvGY76g9FmlB2WjqaGKwB99rsn0ZVqBuEvKoKP2D3xiYJpsBwXHS3J1UbwLfoU6iUOpk Bjv5tPBp0tqI5tFMTuUkhC+GCzqVd5V3KCLRS/BZm5DF+10k2UY+sG+uff2nSAYRcsQ== 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.42 Received: from mail-dl1-f42.google.com (mail-dl1-f42.google.com [74.125.82.42]) (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 656CD606A32C for ; Fri, 4 Dec 2026 18:50:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 656CD606A32C 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-f42.google.com with SMTP id a92af1059eb24-1a3c08d6928so1933491c88.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=JoMDXnLdgtLQrk5eoCxFyjiPqEXrdBj2sP+fAh94p83sfeTA+LuXe0eM6V33SF/0f8 C889o9OrhyAi8X5ImbUimUq9JrA6Ptu9pqXdm+3OuL9khJJPxScGSSB3FSObSqgpfl5i Nt7aBtmSG9mhtGEkxAjtwrGJ6oqSVG1K8FoUMz0kQTQQKpkPPudsXdCmHa/PWXJoLxYJ eh53ZSUU0TF5q1RYPBdRuHn9ehcdC2zPqB52p2l54BJvbQ939zKjEfL4DC6HqyNW83im 59P/4ZaD6c3IbFrghWk6Eev7rHrKCz759RlRhi1ngLGxCpGqvsB1SlLtHNpkbznWSKmy cB+g== X-Forwarded-Encrypted: i=1; AL+hErGXNkhhpEwvbaQ65BEc2r9q3KICgs3eEldhW9Vk26rgDdzooCTzjEqZ9fkF3N0R2awYmw1yBjc=@vger.kernel.org X-Gm-Message-State: APuU4Z/pHvPtuntWVJVShQsnDkm7a6kODyiAGkj/DCWGLxLz37FCqLzJ g2ziLdxxy+H9JdR0D2iCPpFvbrulTeZ8+S2q55/zgy3n7mWQcgxLar4EhIgO4qhI X-Gm-Gg: AZFdTZcozKKO7VUssHSejBDFBJBONQAcgwRG5AYTNWenDVZKAVwreT4VGd6eeRwZr9W UbhK/CEZoY7WH5Z6Cv36MlGAoDL6XW3r+1PWKFS5F2OL/Rn4SWYFP/G47YwXVpL+BQhV15F5pql 49fU23tDglWkoKbITHb726vdFujcPpZcfJKWVh/wX0HnspKtGxk+TsIOlH29Vj1tVluhFlRV3B5 F1J2LzqQo68l7j74Cu1xSBQTK8unirWXav50OXEUyKpQEPzAr+4fVEx+yPMvjFoifLo0hU5Nz69 OwsgrrUEis1afBJ/kPnv1LHCb8W/OfUOAPXhNmY8IHVs0AJKH5WtRwx3KHpY/ikrDaDsIJR7OzQ Dr9fY9hogs1/TSKDFhNy+W2GxQweT06XKSETHtM+KsywFh9gb8OAjGcZ2sT8zleHnexb5FMTcRs 81Y2Kk0Bu+m0p3MJfdbxU3+UZHKJC7J+E+8yLRkDGC2FLmzrYxGohjDg5Lsu0EU2OLRbPBYqfDE 5oJG1ef+64e8ATKgkuCUJ7UvZSRPSddTyst/4m33DnU2u3d8VTJMfXodUV4M0TtYlD5TRBr4h0T Htz/TjShWffOHDiocm6FK6vb/rsuO8iOMBvtA6tr8ThmKL/jOtpr7sPhLjTtTtIREsUWDgi7kaA dAGzZ4+VTRRPQ 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: stable@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