From 8a9dc231850787a2a49dc131d2487439e83959a0 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Wed, 3 Jan 2024 11:18:30 +0100 Subject: [PATCH] 5.4-stable patches added patches: ring-buffer-fix-wake-ups-when-buffer_percent-is-set-to-100.patch --- ...ps-when-buffer_percent-is-set-to-100.patch | 73 +++++++++++++++++++ queue-5.4/series | 1 + 2 files changed, 74 insertions(+) create mode 100644 queue-5.4/ring-buffer-fix-wake-ups-when-buffer_percent-is-set-to-100.patch diff --git a/queue-5.4/ring-buffer-fix-wake-ups-when-buffer_percent-is-set-to-100.patch b/queue-5.4/ring-buffer-fix-wake-ups-when-buffer_percent-is-set-to-100.patch new file mode 100644 index 00000000000..7980dc4e48b --- /dev/null +++ b/queue-5.4/ring-buffer-fix-wake-ups-when-buffer_percent-is-set-to-100.patch @@ -0,0 +1,73 @@ +From 623b1f896fa8a669a277ee5a258307a16c7377a3 Mon Sep 17 00:00:00 2001 +From: "Steven Rostedt (Google)" +Date: Tue, 26 Dec 2023 12:59:02 -0500 +Subject: ring-buffer: Fix wake ups when buffer_percent is set to 100 + +From: Steven Rostedt (Google) + +commit 623b1f896fa8a669a277ee5a258307a16c7377a3 upstream. + +The tracefs file "buffer_percent" is to allow user space to set a +water-mark on how much of the tracing ring buffer needs to be filled in +order to wake up a blocked reader. + + 0 - is to wait until any data is in the buffer + 1 - is to wait for 1% of the sub buffers to be filled + 50 - would be half of the sub buffers are filled with data + 100 - is not to wake the waiter until the ring buffer is completely full + +Unfortunately the test for being full was: + + dirty = ring_buffer_nr_dirty_pages(buffer, cpu); + return (dirty * 100) > (full * nr_pages); + +Where "full" is the value for "buffer_percent". + +There is two issues with the above when full == 100. + +1. dirty * 100 > 100 * nr_pages will never be true + That is, the above is basically saying that if the user sets + buffer_percent to 100, more pages need to be dirty than exist in the + ring buffer! + +2. The page that the writer is on is never considered dirty, as dirty + pages are only those that are full. When the writer goes to a new + sub-buffer, it clears the contents of that sub-buffer. + +That is, even if the check was ">=" it would still not be equal as the +most pages that can be considered "dirty" is nr_pages - 1. + +To fix this, add one to dirty and use ">=" in the compare. + +Link: https://lore.kernel.org/linux-trace-kernel/20231226125902.4a057f1d@gandalf.local.home + +Cc: stable@vger.kernel.org +Cc: Mark Rutland +Cc: Mathieu Desnoyers +Acked-by: Masami Hiramatsu (Google) +Fixes: 03329f9939781 ("tracing: Add tracefs file buffer_percentage") +Signed-off-by: Steven Rostedt (Google) +Signed-off-by: Greg Kroah-Hartman +--- + kernel/trace/ring_buffer.c | 9 +++++++-- + 1 file changed, 7 insertions(+), 2 deletions(-) + +--- a/kernel/trace/ring_buffer.c ++++ b/kernel/trace/ring_buffer.c +@@ -579,9 +579,14 @@ static __always_inline bool full_hit(str + if (!nr_pages || !full) + return true; + +- dirty = ring_buffer_nr_dirty_pages(buffer, cpu); ++ /* ++ * Add one as dirty will never equal nr_pages, as the sub-buffer ++ * that the writer is on is not counted as dirty. ++ * This is needed if "buffer_percent" is set to 100. ++ */ ++ dirty = ring_buffer_nr_dirty_pages(buffer, cpu) + 1; + +- return (dirty * 100) > (full * nr_pages); ++ return (dirty * 100) >= (full * nr_pages); + } + + /* diff --git a/queue-5.4/series b/queue-5.4/series index 5a091c30cb3..9bfe5014fe9 100644 --- a/queue-5.4/series +++ b/queue-5.4/series @@ -43,3 +43,4 @@ usb-fotg210-hcd-delete-an-incorrect-bounds-test.patch smb-client-fix-oob-in-smbcalcsize.patch bus-ti-sysc-use-fsleep-instead-of-usleep_range-in-sy.patch bus-ti-sysc-flush-posted-write-only-after-srst_udela.patch +ring-buffer-fix-wake-ups-when-buffer_percent-is-set-to-100.patch -- 2.47.3