public inbox for linux-kernel@vger.kernel.org
 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>,
	Michal Hocko <mhocko@kernel.org>
Subject: [for-next][PATCH 14/18] ring-buffer: Add set/clear_current_oom_origin() during allocations
Date: Fri, 06 Apr 2018 09:00:49 -0400	[thread overview]
Message-ID: <20180406130113.884986123@goodmis.org> (raw)
In-Reply-To: 20180406130035.400292196@goodmis.org

[-- Attachment #1: 0014-ring-buffer-Add-set-clear_current_oom_origin-during-.patch --]
[-- Type: text/plain, Size: 3761 bytes --]

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

As si_mem_available() can say there is enough memory even though the memory
available is not useable by the ring buffer, it is best to not kill innocent
applications because the ring buffer is taking up all the memory while it is
trying to allocate a great deal of memory.

If the allocator is user space (because kernel threads can also increase the
size of the kernel ring buffer on boot up), then after si_mem_available()
says there is enough memory, set the OOM killer to kill the current task if
an OOM triggers during the allocation.

Link: http://lkml.kernel.org/r/20180404062340.GD6312@dhcp22.suse.cz

Suggested-by: Michal Hocko <mhocko@kernel.org>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 kernel/trace/ring_buffer.c | 48 ++++++++++++++++++++++++++++++++++++----------
 1 file changed, 38 insertions(+), 10 deletions(-)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 966128f02121..c9cb9767d49b 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -22,6 +22,7 @@
 #include <linux/hash.h>
 #include <linux/list.h>
 #include <linux/cpu.h>
+#include <linux/oom.h>
 
 #include <asm/local.h>
 
@@ -1162,35 +1163,60 @@ static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer)
 static int __rb_allocate_pages(long nr_pages, struct list_head *pages, int cpu)
 {
 	struct buffer_page *bpage, *tmp;
+	bool user_thread = current->mm != NULL;
+	gfp_t mflags;
 	long i;
 
-	/* Check if the available memory is there first */
+	/*
+	 * Check if the available memory is there first.
+	 * Note, si_mem_available() only gives us a rough estimate of available
+	 * memory. It may not be accurate. But we don't care, we just want
+	 * to prevent doing any allocation when it is obvious that it is
+	 * not going to succeed.
+	 */
 	i = si_mem_available();
 	if (i < nr_pages)
 		return -ENOMEM;
 
+	/*
+	 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
+	 * gracefully without invoking oom-killer and the system is not
+	 * destabilized.
+	 */
+	mflags = GFP_KERNEL | __GFP_RETRY_MAYFAIL;
+
+	/*
+	 * If a user thread allocates too much, and si_mem_available()
+	 * reports there's enough memory, even though there is not.
+	 * Make sure the OOM killer kills this thread. This can happen
+	 * even with RETRY_MAYFAIL because another task may be doing
+	 * an allocation after this task has taken all memory.
+	 * This is the task the OOM killer needs to take out during this
+	 * loop, even if it was triggered by an allocation somewhere else.
+	 */
+	if (user_thread)
+		set_current_oom_origin();
 	for (i = 0; i < nr_pages; i++) {
 		struct page *page;
-		/*
-		 * __GFP_RETRY_MAYFAIL flag makes sure that the allocation fails
-		 * gracefully without invoking oom-killer and the system is not
-		 * destabilized.
-		 */
+
 		bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()),
-				    GFP_KERNEL | __GFP_RETRY_MAYFAIL,
-				    cpu_to_node(cpu));
+				    mflags, cpu_to_node(cpu));
 		if (!bpage)
 			goto free_pages;
 
 		list_add(&bpage->list, pages);
 
-		page = alloc_pages_node(cpu_to_node(cpu),
-					GFP_KERNEL | __GFP_RETRY_MAYFAIL, 0);
+		page = alloc_pages_node(cpu_to_node(cpu), mflags, 0);
 		if (!page)
 			goto free_pages;
 		bpage->page = page_address(page);
 		rb_init_page(bpage->page);
+
+		if (user_thread && fatal_signal_pending(current))
+			goto free_pages;
 	}
+	if (user_thread)
+		clear_current_oom_origin();
 
 	return 0;
 
@@ -1199,6 +1225,8 @@ static int __rb_allocate_pages(long nr_pages, struct list_head *pages, int cpu)
 		list_del_init(&bpage->list);
 		free_buffer_page(bpage);
 	}
+	if (user_thread)
+		clear_current_oom_origin();
 
 	return -ENOMEM;
 }
-- 
2.15.1

  parent reply	other threads:[~2018-04-06 13:02 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-06 13:00 [for-next][PATCH 00/18] tracing: Last minute updates before pushing to Linus Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 01/18] tracing: Fix a potential NULL dereference Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 02/18] init: Fix initcall0 name as it is "pure" not "early" Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 03/18] tracing: Default to using trace_global_clock if sched_clock is unstable Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 04/18] tracing: Mention trace_clock=global when warning about unstable clocks Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 05/18] ftrace: Drop a VLA in module_exists() Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 06/18] tracing: Fix display of hist trigger expressions containing timestamps Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 07/18] tracing: Dont add flag strings when displaying variable references Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 08/18] tracing: Add action comparisons when testing matching hist triggers Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 09/18] tracing: Make sure variable string fields are NULL-terminated Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 10/18] tracing: Uninitialized variable in create_tracing_map_fields() Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 11/18] vsprintf: Do not preprocess non-dereferenced pointers for bprintf (%px and %pK) Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 12/18] lockdep: Add print_irqtrace_events() to __warn Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 13/18] ring-buffer: Check if memory is available before allocation Steven Rostedt
2018-04-06 13:00 ` Steven Rostedt [this message]
2018-04-06 13:00 ` [for-next][PATCH 15/18] tracing: Hide global trace clock from lockdep Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 16/18] tracing: Fixup logic inversion on setting trace_global_clock defaults Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 17/18] tracing: Add rcu dereference annotation for filter->prog Steven Rostedt
2018-04-06 13:00 ` [for-next][PATCH 18/18] tracing: Add rcu dereference annotation for test func that touches filter->prog 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=20180406130113.884986123@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhocko@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