Received: by 10.192.165.148 with SMTP id m20csp2524618imm; Sun, 22 Apr 2018 08:50:37 -0700 (PDT) X-Google-Smtp-Source: AIpwx48UF9ZtOB6yfOzXvilSIHSVpgtFECvw7M5Cv9Gr1u7roKwTdtWhjiUBWIByGjQBuvaCNQVq X-Received: by 10.99.117.93 with SMTP id f29mr14069907pgn.401.1524412237103; Sun, 22 Apr 2018 08:50:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524412237; cv=none; d=google.com; s=arc-20160816; b=oiTIJBTTkcADJjajmuopDipXRdhFWDPNJSmqouvw4VRcZFpeisB/zphKXoJxHKhNQn scrJdsgtF62kv+EAtfHj1NMcuwGOfb70OvxMonieijARDJGkw1b1atE6ylO5w/kAZWhQ zl7G2vq5dtJQ/sSi44E/8MqvwJ6mAho5XiaXkcnS0dbG00TDfyuDwVJgoIL1TmsYYk+6 lItdd4/C/HI9XuJU/esQwdW3Wa01AGbHozZd0i6XUfUj+XJ0htm9SV/jw4WO92OWXR9w Kc4sssvhXa64H2Rgnkpi7gVXFbFthvHQpqBdWED0pqGBm17xfcqMTLvqV3RQWHSYAHwq 15YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=C8Phd96pLPVozBLr7sSXsulV/nCFdn7jCRY8sKm8jxc=; b=UpNjWz7IDIEPMl8Urgw4P1kjrO0CHph6+bN+7KtiK77SMdxMjm7JoF7OZPzN1JCp38 t/FuaPYcmM07SBD+kqAYF7+YN1nOJUfgMoazBudEoIU5ocmbIue0Jcdd9/M0ABqVnLck EnYaodh8Ror4UImZ80fMOqk5SIaV6P+su0akUPp58dbLFSRTs7CsL8CCSDH/PxKIueZb AXrpZwNI88liKrtX3hr79TCnVpzGLu5AKpj2no6W1aSZUQ831BTqnPOehMv//yukPkZd gk9DRMlMzD2jvMd7DOrmvLpwAjobgijaHK5Sc0B0SHlt37edw8UDBCICe/gjXIKzEwQf IHxQ== 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 5-v6si4090604plc.203.2018.04.22.08.50.22; Sun, 22 Apr 2018 08:50:37 -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 S1755030AbeDVPsY (ORCPT + 99 others); Sun, 22 Apr 2018 11:48:24 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:48048 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754261AbeDVOBk (ORCPT ); Sun, 22 Apr 2018 10:01:40 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 71F54BC8; Sun, 22 Apr 2018 14:01:39 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, linux-mm@kvack.org, Zhaoyang Huang , Joel Fernandes , "Steven Rostedt (VMware)" Subject: [PATCH 4.16 127/196] ring-buffer: Check if memory is available before allocation Date: Sun, 22 Apr 2018 15:52:27 +0200 Message-Id: <20180422135110.834264099@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180422135104.278511750@linuxfoundation.org> References: <20180422135104.278511750@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Steven Rostedt (VMware) commit 2a872fa4e9c8adc79c830e4009e1cc0c013a9d8a upstream. The ring buffer is made up of a link list of pages. When making the ring buffer bigger, it will allocate all the pages it needs before adding to the ring buffer, and if it fails, it frees them and returns an error. This makes increasing the ring buffer size an all or nothing action. When this was first created, the pages were allocated with "NORETRY". This was to not cause any Out-Of-Memory (OOM) actions from allocating the ring buffer. But NORETRY was too strict, as the ring buffer would fail to expand even when there's memory available, but was taken up in the page cache. Commit 848618857d253 ("tracing/ring_buffer: Try harder to allocate") changed the allocating from NORETRY to RETRY_MAYFAIL. The RETRY_MAYFAIL would allocate from the page cache, but if there was no memory available, it would simple fail the allocation and not trigger an OOM. This worked fine, but had one problem. As the ring buffer would allocate one page at a time, it could take up all memory in the system before it failed to allocate and free that memory. If the allocation is happening and the ring buffer allocates all memory and then tries to take more than available, its allocation will not trigger an OOM, but if there's any allocation that happens someplace else, that could trigger an OOM, even though once the ring buffer's allocation fails, it would free up all the previous memory it tried to allocate, and allow other memory allocations to succeed. Commit d02bd27bd33dd ("mm/page_alloc.c: calculate 'available' memory in a separate function") separated out si_mem_availble() as a separate function that could be used to see how much memory is available in the system. Using this function to make sure that the ring buffer could be allocated before it tries to allocate pages we can avoid allocating all memory in the system and making it vulnerable to OOMs if other allocations are taking place. Link: http://lkml.kernel.org/r/1522320104-6573-1-git-send-email-zhaoyang.huang@spreadtrum.com CC: stable@vger.kernel.org Cc: linux-mm@kvack.org Fixes: 848618857d253 ("tracing/ring_buffer: Try harder to allocate") Requires: d02bd27bd33dd ("mm/page_alloc.c: calculate 'available' memory in a separate function") Reported-by: Zhaoyang Huang Tested-by: Joel Fernandes Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Greg Kroah-Hartman --- kernel/trace/ring_buffer.c | 5 +++++ 1 file changed, 5 insertions(+) --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -1136,6 +1136,11 @@ static int __rb_allocate_pages(long nr_p struct buffer_page *bpage, *tmp; long i; + /* Check if the available memory is there first */ + i = si_mem_available(); + if (i < nr_pages) + return -ENOMEM; + for (i = 0; i < nr_pages; i++) { struct page *page; /*