linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: [for-next][PATCH 10/21] ring-buffer: Make resize disable per cpu buffer instead of total buffer
Date: Sun, 29 Mar 2020 14:43:02 -0400	[thread overview]
Message-ID: <20200329184316.939831283@goodmis.org> (raw)
In-Reply-To: 20200329184252.289087453@goodmis.org

From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

When the ring buffer becomes writable for even when the trace file is read,
it must still not be resized. But since tracers can be activated while the
trace file is being read, the irqsoff tracer can modify the per CPU buffers,
and this can cause the reader of the trace file to update the wrong buffer's
resize disable bit, as the irqsoff tracer swaps out cpu buffers.

By making the resize disable per cpu_buffer, it makes the update follow the
per cpu_buffer even if it's swapped out with the snapshot buffer and keeps
the release of the trace file modifying the same data as the open did.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 kernel/trace/ring_buffer.c | 43 ++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 13 deletions(-)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 5979327254f9..e2de5b448c91 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -441,6 +441,7 @@ enum {
 struct ring_buffer_per_cpu {
 	int				cpu;
 	atomic_t			record_disabled;
+	atomic_t			resize_disabled;
 	struct trace_buffer	*buffer;
 	raw_spinlock_t			reader_lock;	/* serialize readers */
 	arch_spinlock_t			lock;
@@ -484,7 +485,6 @@ struct trace_buffer {
 	unsigned			flags;
 	int				cpus;
 	atomic_t			record_disabled;
-	atomic_t			resize_disabled;
 	cpumask_var_t			cpumask;
 
 	struct lock_class_key		*reader_lock_key;
@@ -1740,18 +1740,24 @@ int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
 
 	size = nr_pages * BUF_PAGE_SIZE;
 
-	/*
-	 * Don't succeed if resizing is disabled, as a reader might be
-	 * manipulating the ring buffer and is expecting a sane state while
-	 * this is true.
-	 */
-	if (atomic_read(&buffer->resize_disabled))
-		return -EBUSY;
-
 	/* prevent another thread from changing buffer sizes */
 	mutex_lock(&buffer->mutex);
 
+
 	if (cpu_id == RING_BUFFER_ALL_CPUS) {
+		/*
+		 * Don't succeed if resizing is disabled, as a reader might be
+		 * manipulating the ring buffer and is expecting a sane state while
+		 * this is true.
+		 */
+		for_each_buffer_cpu(buffer, cpu) {
+			cpu_buffer = buffer->buffers[cpu];
+			if (atomic_read(&cpu_buffer->resize_disabled)) {
+				err = -EBUSY;
+				goto out_err_unlock;
+			}
+		}
+
 		/* calculate the pages to update */
 		for_each_buffer_cpu(buffer, cpu) {
 			cpu_buffer = buffer->buffers[cpu];
@@ -1819,6 +1825,16 @@ int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
 		if (nr_pages == cpu_buffer->nr_pages)
 			goto out;
 
+		/*
+		 * Don't succeed if resizing is disabled, as a reader might be
+		 * manipulating the ring buffer and is expecting a sane state while
+		 * this is true.
+		 */
+		if (atomic_read(&cpu_buffer->resize_disabled)) {
+			err = -EBUSY;
+			goto out_err_unlock;
+		}
+
 		cpu_buffer->nr_pages_to_update = nr_pages -
 						cpu_buffer->nr_pages;
 
@@ -1888,6 +1904,7 @@ int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size,
 			free_buffer_page(bpage);
 		}
 	}
+ out_err_unlock:
 	mutex_unlock(&buffer->mutex);
 	return err;
 }
@@ -4294,7 +4311,7 @@ ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags)
 
 	iter->cpu_buffer = cpu_buffer;
 
-	atomic_inc(&buffer->resize_disabled);
+	atomic_inc(&cpu_buffer->resize_disabled);
 	atomic_inc(&cpu_buffer->record_disabled);
 
 	return iter;
@@ -4369,7 +4386,7 @@ ring_buffer_read_finish(struct ring_buffer_iter *iter)
 	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
 
 	atomic_dec(&cpu_buffer->record_disabled);
-	atomic_dec(&cpu_buffer->buffer->resize_disabled);
+	atomic_dec(&cpu_buffer->resize_disabled);
 	kfree(iter->event);
 	kfree(iter);
 }
@@ -4474,7 +4491,7 @@ void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
 	if (!cpumask_test_cpu(cpu, buffer->cpumask))
 		return;
 
-	atomic_inc(&buffer->resize_disabled);
+	atomic_inc(&cpu_buffer->resize_disabled);
 	atomic_inc(&cpu_buffer->record_disabled);
 
 	/* Make sure all commits have finished */
@@ -4495,7 +4512,7 @@ void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu)
 	raw_spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags);
 
 	atomic_dec(&cpu_buffer->record_disabled);
-	atomic_dec(&buffer->resize_disabled);
+	atomic_dec(&cpu_buffer->resize_disabled);
 }
 EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu);
 
-- 
2.25.1



  parent reply	other threads:[~2020-03-29 18:43 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-29 18:42 [for-next][PATCH 00/21] tracing: Updates for v5.7 Steven Rostedt
2020-03-29 18:42 ` [for-next][PATCH 01/21] tracing: Use address-of operator on section symbols Steven Rostedt
2020-03-29 18:42 ` [for-next][PATCH 02/21] selftest/ftrace: Fix function trigger test to handle trace not disabling the tracer Steven Rostedt
2020-03-29 18:42 ` [for-next][PATCH 03/21] tracing: Save off entry when peeking at next entry Steven Rostedt
2020-03-29 18:42 ` [for-next][PATCH 04/21] ring-buffer: Have ring_buffer_empty() not depend on tracing stopped Steven Rostedt
2020-03-29 18:42 ` [for-next][PATCH 05/21] ring-buffer: Rename ring_buffer_read() to read_buffer_iter_advance() Steven Rostedt
2020-03-29 18:42 ` [for-next][PATCH 06/21] ring-buffer: Add page_stamp to iterator for synchronization Steven Rostedt
2020-03-29 18:42 ` [for-next][PATCH 07/21] ring-buffer: Have rb_iter_head_event() handle concurrent writer Steven Rostedt
2020-03-29 18:43 ` [for-next][PATCH 08/21] ring-buffer: Do not die if rb_iter_peek() fails more than thrice Steven Rostedt
2020-03-29 18:43 ` [for-next][PATCH 09/21] ring-buffer: Optimize rb_iter_head_event() Steven Rostedt
2020-03-29 18:43 ` Steven Rostedt [this message]
2020-03-29 18:43 ` [for-next][PATCH 11/21] ring-buffer: Do not disable recording when there is an iterator Steven Rostedt
2020-03-29 18:43 ` [for-next][PATCH 12/21] tracing: Do not disable tracing when reading the trace file Steven Rostedt
2020-03-29 18:43 ` [for-next][PATCH 13/21] ring-buffer/tracing: Have iterator acknowledge dropped events Steven Rostedt
2020-03-29 18:43 ` [for-next][PATCH 14/21] tracing: Have the document reflect that the trace file keeps tracing enabled Steven Rostedt
2020-03-29 18:43 ` [for-next][PATCH 15/21] ftrace/kprobe: Show the maxactive number on kprobe_events Steven Rostedt
2020-03-31 13:11   ` Sasha Levin
2020-03-29 18:43 ` [for-next][PATCH 16/21] ftrace: Make function trace pid filtering a bit more exact Steven Rostedt
2020-03-29 18:43 ` [for-next][PATCH 17/21] ftrace: Create set_ftrace_notrace_pid to not trace tasks Steven Rostedt
2020-03-29 18:43 ` [for-next][PATCH 18/21] tracing: Create set_event_notrace_pid " Steven Rostedt
2020-03-29 18:43 ` [for-next][PATCH 19/21] selftests/ftrace: Add test to test new set_ftrace_notrace_pid file Steven Rostedt
2020-03-29 18:43 ` [for-next][PATCH 20/21] selftests/ftrace: Add test to test new set_event_notrace_pid file Steven Rostedt
2020-03-29 18:43 ` [for-next][PATCH 21/21] tracing: Add documentation on set_ftrace_notrace_pid and set_event_notrace_pid Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200329184316.939831283@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).