From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796307478; b=VvNwiYXHwJpmSj9ShgR6AtGtVCI3roT7DZ954gTJBARW2t+nrHZp/HANXYrQetVROsq7 iD3bSm3xd2wbUradzko4m3MKWwF2MrGqWsVVx290XrllLkLmkcm9KIaFzK8UKWa3yY8C8 w7gQd6pQz5dXqAg77KctJzHD76BxYLuEma3stzDmpZGEKg61QDopEYnp8RjmCc53FDw9S SnQLvPS7sxvD6qwmrb18QEqpXb5lXOiluOem7hyzvRyEUwf77GWJTY3W8p6h1xoHHKMBN EAIqXKCaQIo3DbkeUC2nemfYpCa6yM24XNW+mUJxWzXD0SirCDdyhrxoYweoRnyBTeQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796307478; h=Received:DKIM-Signature:Received:Message-ID:User-Agent:Date:From:To: Cc:Subject:References:MIME-Version:Content-Type; bh=0ODB2kru858XQqZoVz9DdmRS+iunOiQ5BAi2eAb0Pz0=; b=1LFOKZpIvtZGeJMqmFOy+FgGLExn5iRFRWXrEoBMZUQUe1VHTYG8fj8Z+0QCboYGQwyi JDowi7VDShyZd+p3P3FqXdZcKmvOt3lHWG0YeSjPQlc256/8H77yH9LmmBFo1UbaUnqws u8ywm7WFdQHbuCD5YA4DJlZa3jDeZUrS0llDz6w0dNsk46CFU+iVRKty0TAAoA72yNAMm YlwRAqbqHfrPypJ1OxbI63pYRfRyjvTaA+b09OLfPyi+KycqLsCEFeUuCgxk1HMJ49Y+M FupxQiA1UNZtLSWom0bhNzvUmAZENITdNy+OaHTYRwhYL8CfeI2KzX70ro3V+HGNiOQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=noO866yx; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88C22606A69B; Thu, 3 Dec 2026 14:17:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=noO866yx Received: by smtp.kernel.org (Postfix) with ESMTPSA id 65CF11F0089A; Thu, 3 Dec 2026 14:17:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796307478; bh=0ODB2kru858XQqZoVz9DdmRS+iunOiQ5BAi2eAb0Pz0=; h=Date:From:To:Cc:Subject:References; b=noO866yxctr5wg9kNdnXQb2vmRjL6GNzEjpDn6OINr3mDjVV3ayRJIIfARUXLHzuY JRvMTzH9rCt1ec9lFZKiIobNs9LOb4R0iYd7CDsI0ukQ3ur7aCO7C0IfOepFVUDLzo wIzZXw4XzXa0VWj+9QdcXrQKkZd8mFwvjcZd89GNUiT2ePTOfdydSL6gpYDNquWHT9 DnNZMPGPyVQqGrpdsWW3WVwKju9o1pWVeeCt4uq0eLok4NMutrSKTfGzdsPHsF7H39 M89JlGu6vuU4jVk90nsEZWNeg+pPCwOucx8cAi++LIMqUgfB5qiCiNuJOJIa9RAJvJ cIy5u1vG8CFYg== Received: from rostedt by gandalf with local (Exim 4.100.1) (envelope-from ) id 1xZ7dF-0000000C25q-3F4H; Thu, 03 Dec 2026 09:17:57 -0500 Message-ID: <20261203141757.594416625@kernel.org> User-Agent: quilt/0.69 Date: Thu, 03 Dec 2026 09:17:33 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , stable@vger.kernel.org Subject: [for-linus][PATCH 8/8] tracing: Reject backup instance opens only after autoremove is queued References: <20261203141725.845506016@kernel.org> 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-Type: text/plain; charset=UTF-8 From: "Masami Hiramatsu (Google)" Commit eca33fdab47b ("tracing: Remove the backup instance automatically after read") sets tr->free_on_close in update_last_data() once all previous-boot data has been read out or cleared, and queues autoremove_work in __trace_array_put() when the last user reference is dropped (tr->ref == 1). However, __trace_array_get() rejects new references with -ENODEV as soon as tr->free_on_close is set, even while existing file descriptors still hold tr->ref > 1 and autoremove has not yet been queued. This breaks userspace consumers that keep multiple file descriptors open on the backup instance while finishing a dump. For example, a reader that keeps per_cpu/cpu*/trace_pipe_raw open across CPUs and drains them via read() triggers update_last_data_if_empty() on the final empty read, setting tr->free_on_close while those pipe file descriptors are still open. Any subsequent open of another file in the same instance before closing the pipes -- such as reading per_cpu/cpu*/stats or opening trace with O_WRONLY | O_TRUNC to clear the buffer -- then fails with -ENODEV. Introduce a tr->autoremove_queued flag set when autoremove_work is queued, and check it in __trace_array_get() instead of tr->free_on_close. This allows opening files while existing references remain, while still blocking new opens once autoremove is queued. Cc: stable@vger.kernel.org Fixes: eca33fdab47b ("tracing: Remove the backup instance automatically after read") Link: https://patch.msgid.link/179622393332.9100.10906884385392450114.stgit@mhiramat.c.googlers.com Signed-off-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt --- kernel/trace/trace.c | 12 +++++++++--- kernel/trace/trace.h | 4 ++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index fa65ef8d7fbe..f153ffedb9d2 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -585,8 +585,12 @@ static struct workqueue_struct *autoremove_wq; static void trace_array_kick_autoremove(struct trace_array *tr) { - if (autoremove_wq) + lockdep_assert_held(&trace_types_lock); + + if (autoremove_wq && !tr->autoremove_queued) { + tr->autoremove_queued = true; queue_work(autoremove_wq, &tr->autoremove_work); + } } static void trace_array_cancel_autoremove(struct trace_array *tr) @@ -619,8 +623,10 @@ LIST_HEAD(ftrace_trace_arrays); static int __trace_array_get(struct trace_array *this_tr) { - /* When free_on_close is set, this is not available anymore. */ - if (autoremove_wq && this_tr->free_on_close) + lockdep_assert_held(&trace_types_lock); + + /* Once autoremove is queued, this is not available anymore. */ + if (this_tr->autoremove_queued) return -ENODEV; this_tr->ref++; diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 45971e4be77e..0211db62d6a1 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -462,8 +462,12 @@ struct trace_array { /* * If the ring buffer is a read only backup instance, it will be * removed after dumping all data via pipe, because no readable data. + * @free_on_close is set once the buffer is drained or cleared, and + * @autoremove_queued is set when the last reference is released and + * @autoremove_work is queued. */ bool free_on_close; + bool autoremove_queued; struct work_struct autoremove_work; }; -- 2.53.0