Received: by 10.213.65.68 with SMTP id h4csp887349imn; Wed, 4 Apr 2018 08:55:22 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+P0+ujZCScDwBpqCgCnx0KGnbAjU9OtyFZ7LpBXlzQc36V7hzXNUjQ9qPVWQTiDesHDqkw X-Received: by 10.99.95.22 with SMTP id t22mr12661701pgb.315.1522857321993; Wed, 04 Apr 2018 08:55:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522857321; cv=none; d=google.com; s=arc-20160816; b=YTz8YSvKYAvLJ99puVQX21zE94OzaOP0/MZqarxudbOFv1Nz7K+W8RpqKC5V6WHPy4 kpBCZRDBQdZRvRHnQnW88czjNZFsG3sbwKhLk91cLCH8Skykoj96SzL+d795F7zRSCeL GsuV5ooNxCUpWAK0z2lISL4AarEgWnJxPkiCYsi+d3+KbTFiZBjtWyoiA1MtxD1aIlWI WIJSRK/nOyKDng8Sg1cJpTGPjz9MOMxGk/rF6Rzb1RgY1Tgkv5EqRoMI2TrFQAbIS9V2 RmGoFR8zGOvPh6bw/U0ULmwPRkSgvm3+h428Yj8ijjYKxJtm/HW1mFEGFw8vidZSSCqJ EPrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:subject:cc:to:from:date:dmarc-filter :arc-authentication-results; bh=W8DU2Dx9MX8GQY0fFaBnu63tt1lF8ohXV/BpRs0RnQM=; b=dc6hHylnf43QJNSff7ceN7g6zPpwDLy77HcjttqlC9H6U67jfIhMopJQRdPaoVfRwv ZddHnGJwYlTyRejtREGwRVQXdih+N9b7YUhVY2EwM1GfrCASj3sQzqDiZgGv8GtcD2gP UB94hzGfDF0WbM3FdHUjMmjFiVyxLEOcxDPtiMN+HISOrjtbsYc78XVLVkC02T1Jte8f v4g0mEnJIcdrj86+UxeZetK8SvloZnSm1O76ce0U2X7TVcaQQzSfDwZzJD7unfQO1U6Y KCPv16tUtI1eTVpHrKQH6tMztzH/ZJd4M9+sy683l9LZVN2JFWrHHSF7K7QiV84fHKmb ajNg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h1-v6si3283323pld.222.2018.04.04.08.55.08; Wed, 04 Apr 2018 08:55:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752383AbeDDPxQ (ORCPT + 99 others); Wed, 4 Apr 2018 11:53:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:50696 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752320AbeDDPxN (ORCPT ); Wed, 4 Apr 2018 11:53:13 -0400 Received: from gandalf.local.home (cpe-66-24-56-78.stny.res.rr.com [66.24.56.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1FEF520838; Wed, 4 Apr 2018 15:53:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1FEF520838 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=goodmis.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=rostedt@goodmis.org Date: Wed, 4 Apr 2018 11:53:10 -0400 From: Steven Rostedt To: LKML Cc: Michal Hocko , Zhaoyang Huang , Ingo Molnar , linux-kernel@vger.kernel.org, kernel-patch-test@lists.linaro.org, Andrew Morton , Joel Fernandes , linux-mm@kvack.org, Vlastimil Babka Subject: [PATCH] ring-buffer: Add set/clear_current_oom_origin() during allocations Message-ID: <20180404115310.6c69e7b9@gandalf.local.home> X-Mailer: Claws Mail 3.16.0 (GTK+ 2.24.31; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Steven Rostedt (VMware)" 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 Signed-off-by: Steven Rostedt (VMware) --- 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 #include #include +#include #include @@ -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.13.6