From nobody Wed Feb 5 14:07:23 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 312C87D07D for ; Thu, 16 Jan 2025 02:03:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736992989; cv=none; b=an2mwDLpF38eAhLGwGCYGqTqoPKnUjTLl53SNWPW7TVVApm5sc6Lej4nxvTpCQnT4He87cKmvDmjYPhmEnSwELTCji9ruGUhb0DWghk2YzxBqWdLbOoFpv2vwmgzX9oF6m1ijGeMGlF6IX37rRLZZt5gmEUD9cXrIM+XiUCaUYA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736992989; c=relaxed/simple; bh=tRg63609RF5TS5HvrZjbKt5iencAmrplLBLmJo3O8Vs=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=FqTFn0LbL5K+WPMv4+ymTWZRUgMMrxlXcXVbA+WzUhWkuJIMGgrVAoyGkjf294W1irB29WvEy3FZvwv39FdlzFGLSKh8RTk1/U/Thn4qKBtqi1iS1e60d3IeNmAh39Dz1u3XaXV449AYx16DYh+piEp3VHjF2wrjUC/fFHvkgfk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AFB9DC4CEE3; Thu, 16 Jan 2025 02:03:08 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1tYFE1-00000000iGl-2neZ; Wed, 15 Jan 2025 21:03:13 -0500 Message-ID: <20250116020313.448931991@goodmis.org> User-Agent: quilt/0.68 Date: Wed, 15 Jan 2025 21:02:56 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Vincent Donnefort Subject: [for-next][PATCH 1/2] ring-buffer: Check for empty ring-buffer with rb_num_of_entries() References: <20250116020255.952254121@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vincent Donnefort Currently there are two ways of identifying an empty ring-buffer. One relying on the current status of the commit / reader page (rb_per_cpu_empty()) and the other on the write and read counters (rb_num_of_entries() used in rb_get_reader_page()). with rb_num_of_entries(). This intends to ease later introduction of ring-buffer writers which are out of the kernel control and with whom, the only information available is through the meta-page counters. Link: https://lore.kernel.org/20250108114536.627715-2-vdonnefort@google.com Signed-off-by: Vincent Donnefort Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 59 +++++++++----------------------------- 1 file changed, 14 insertions(+), 45 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 60210fb5b211..479bbbcbf61b 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -4682,40 +4682,22 @@ int ring_buffer_write(struct trace_buffer *buffer, } EXPORT_SYMBOL_GPL(ring_buffer_write); =20 -static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer) +/* + * The total entries in the ring buffer is the running counter + * of entries entered into the ring buffer, minus the sum of + * the entries read from the ring buffer and the number of + * entries that were overwritten. + */ +static inline unsigned long +rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer) { - struct buffer_page *reader =3D cpu_buffer->reader_page; - struct buffer_page *head =3D rb_set_head_page(cpu_buffer); - struct buffer_page *commit =3D cpu_buffer->commit_page; - - /* In case of error, head will be NULL */ - if (unlikely(!head)) - return true; - - /* Reader should exhaust content in reader page */ - if (reader->read !=3D rb_page_size(reader)) - return false; - - /* - * If writers are committing on the reader page, knowing all - * committed content has been read, the ring buffer is empty. - */ - if (commit =3D=3D reader) - return true; - - /* - * If writers are committing on a page other than reader page - * and head page, there should always be content to read. - */ - if (commit !=3D head) - return false; + return local_read(&cpu_buffer->entries) - + (local_read(&cpu_buffer->overrun) + cpu_buffer->read); +} =20 - /* - * Writers are committing on the head page, we just need - * to care about there're committed data, and the reader will - * swap reader page with head page when it is to read data. - */ - return rb_page_commit(commit) =3D=3D 0; +static bool rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer) +{ + return !rb_num_of_entries(cpu_buffer); } =20 /** @@ -4861,19 +4843,6 @@ void ring_buffer_record_enable_cpu(struct trace_buff= er *buffer, int cpu) } EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu); =20 -/* - * The total entries in the ring buffer is the running counter - * of entries entered into the ring buffer, minus the sum of - * the entries read from the ring buffer and the number of - * entries that were overwritten. - */ -static inline unsigned long -rb_num_of_entries(struct ring_buffer_per_cpu *cpu_buffer) -{ - return local_read(&cpu_buffer->entries) - - (local_read(&cpu_buffer->overrun) + cpu_buffer->read); -} - /** * ring_buffer_oldest_event_ts - get the oldest event timestamp from the b= uffer * @buffer: The ring buffer --=20 2.45.2 From nobody Wed Feb 5 14:07:23 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9078A86329 for ; Thu, 16 Jan 2025 02:03:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736992989; cv=none; b=OriqpoP5o+euP2psPJ0qHjEDS0eQKZjeKaeEZxoszoR4JTHtZ9RYPVwQ15O2DktVPBSmME4yTPlJPvoj07YGPOxKhAGGnqgCfAIGfdAKmxqErTD9tIl3l+p68QdwiCGPiRWn3b0KVKS39xgd+3QgJoTxrXh15fMYX+IgYJKIQmo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736992989; c=relaxed/simple; bh=OdFlSVNIYxPvUnqmO5BlJRboVL9uBDp6nr7SVWKOh8k=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=ukwtivCpjbLF2ywTH76Ym+Gp5Fgxx34AkG2FF1NxP5F18fY/bf5QvQD6enKf4YPT1+bPMr2aXR5eBpSXu1kRcky7aQT/IxtFME9xSW9iHgbB5wjkqAnGzGg9trcypYDvMou9ezVAxkMayFZ+wPuS435Sw73JypYiIVVhjhpplXc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 04DF1C4CEE0; Thu, 16 Jan 2025 02:03:09 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1tYFE1-00000000iHF-42uT; Wed, 15 Jan 2025 21:03:13 -0500 Message-ID: <20250116020313.739097483@goodmis.org> User-Agent: quilt/0.68 Date: Wed, 15 Jan 2025 21:02:57 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Jeongjun Park Subject: [for-next][PATCH 2/2] ring-buffer: Make reading page consistent with the code logic References: <20250116020255.952254121@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jeongjun Park In the loop of __rb_map_vma(), the 's' variable is calculated from the same logic that nr_pages is and they both come from nr_subbufs. But the relationship is not obvious and there's a WARN_ON_ONCE() around the 's' variable to make sure it never becomes equal to nr_subbufs within the loop. If that happens, then the code is buggy and needs to be fixed. The 'page' variable is calculated from cpu_buffer->subbuf_ids[s] which is an array of 'nr_subbufs' entries. If the code becomes buggy and 's' becomes equal to or greater than 'nr_subbufs' then this will be an out of bounds hit before the WARN_ON() is triggered and the code exiting safely. Make the 'page' initialization consistent with the code logic and assign it after the out of bounds check. Link: https://lore.kernel.org/20250110162612.13983-1-aha310510@gmail.com Signed-off-by: Jeongjun Park [ sdr: rewrote change log ] Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ring_buffer.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 479bbbcbf61b..6d61ff78926b 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -7028,7 +7028,7 @@ static int __rb_map_vma(struct ring_buffer_per_cpu *c= pu_buffer, } =20 while (p < nr_pages) { - struct page *page =3D virt_to_page((void *)cpu_buffer->subbuf_ids[s]); + struct page *page; int off =3D 0; =20 if (WARN_ON_ONCE(s >=3D nr_subbufs)) { @@ -7036,6 +7036,8 @@ static int __rb_map_vma(struct ring_buffer_per_cpu *c= pu_buffer, goto out; } =20 + page =3D virt_to_page((void *)cpu_buffer->subbuf_ids[s]); + for (; off < (1 << (subbuf_order)); off++, page++) { if (p >=3D nr_pages) break; --=20 2.45.2