Received: by 10.213.65.68 with SMTP id h4csp1376988imn; Thu, 29 Mar 2018 03:43:54 -0700 (PDT) X-Google-Smtp-Source: AIpwx49K1lpO7X1x6cqQGNR0PIhq0fW5R4ogmtuZSfD5tyTrJditpJN1kRNbwsOXphmjyJ/wOnXp X-Received: by 10.167.130.71 with SMTP id e7mr5962816pfn.22.1522320234337; Thu, 29 Mar 2018 03:43:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522320234; cv=none; d=google.com; s=arc-20160816; b=ARee0HKtqzhD39qo3icdl2SXOqqaS176IRZK8pLww7ydtpKsS95+M/eXbYvTSD/UwO rx0ixAeY7eYDg9hgsglewMLG05gAESOhYHFwSRaB22OZVf27SkFTjmTxXfjqDZXobTuB 1i5709rKH6jjxZuEi8Ttr81BjJGAdAs3jE29pus65RdIAnAxxmV5ZT1jJ8NDeQMmQXfZ QanfhdSKqishiw9fDqQfG/sRtYUG6EfcdPCs0mHFA+H/clID9pTLV9Fx7HLaKkOo2xnK WIJBOgNb/VngK+QlQtw/e9KwdSNSPAErNnTWC1MEL0I8vQSMAABztJvaV7V8i2RCfit/ zd7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:to:from :dkim-signature:arc-authentication-results; bh=4wasyHtRTduz/4MXOWp6R5lA8oXBVkgSnFwA/+60Bbg=; b=PIXRX3aueN57s7+cSQwhMrCt7JUlTX2Mww9FhLWWy4qn5okroTeVEMSwjoXr8cHunS Qg7d5xf2VrwCHpP+E70TfIRm2ya32mPqFB/rH0WSBqHYc6vem77/B3/lXae/cj2NTwW+ jWsmDKHl6HKYw8/aNnyvkZqIyt69hQkLvKQhpQbcGs+lSl7Ao+hop8CAOL/JUCyr3Ti9 5x6PYkQQ+V1YdWg07aOBH9YInQuxVqyrcjyXn7Sj+kWW0l6VjlYFqk2swORHX+sGsSn4 KwL4DBc3DzuzCJRcbiM5jdwClsCxONtYli+rLvBWL+7xsoHVyyjDCjv4LCqKICQybTSs zpsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=iOInS8IX; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y2si276396pgp.486.2018.03.29.03.43.29; Thu, 29 Mar 2018 03:43:54 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=iOInS8IX; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751213AbeC2Klz (ORCPT + 99 others); Thu, 29 Mar 2018 06:41:55 -0400 Received: from mail-pg0-f66.google.com ([74.125.83.66]:44899 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750735AbeC2Kly (ORCPT ); Thu, 29 Mar 2018 06:41:54 -0400 Received: by mail-pg0-f66.google.com with SMTP id v26so2780801pge.11 for ; Thu, 29 Mar 2018 03:41:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=4wasyHtRTduz/4MXOWp6R5lA8oXBVkgSnFwA/+60Bbg=; b=iOInS8IXR4U3S8NwNlLgUiSrFoVoXlC96JZ9YdPpw1YOKYAb7TaYzptUEsjwR67GzA aKuocctqmzh3GJMKZHDknUqM1/HAHhd7xWbftoTRY4Xgi1QWnkyVWr1fw0cp/yo4tR5C DhTE6/GTjce7NfIpbSe9xBH4z++zqG7kKV0dTao8faKv9mXPXnn9cBkRPEVLQDiGFFua hJOuuP7poiyXQlg7wirn6OC0NtgD2j77c7S6lfNMwhvTM4r9qLGIqyQGPR+1Vhhvr9HC UE7Yjx/N03KUejFwuU4ItsTet8+KTVc7FI7W1IEysGxo8Kr/ESFFys0D/hDNnaIzWnFj L1xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=4wasyHtRTduz/4MXOWp6R5lA8oXBVkgSnFwA/+60Bbg=; b=lY6NsXYr9w3u4Wqx2FxY/yZaOEX/V12Kdnt7keyCTzSScXtCK+ryTz+jb8x8u+5IKj HRU6qukkMVvJPX2B1NiYv/F+XAU6mspC6kTHQ526Z8x60L9k3bjrFT0MhZ8EslNn7MY/ 7q18ynOYQUv5rQA4bL7Bgy4LjoMCW/czEX+XEY5KjMTbjFW89eEr2shapJr8vGe3LNCq NBoHPJmwzuo2Hj0LgeSkAByN9xxUd97/UTIRMD6D6Y4efF8SfKU5JJ67Dg5rIadr6fLv VnZhEYO32KbXfd+AOearUkg/yewybuxTcjWvW/pyt0D0xO8b7+c8g9y9OgSHM9Q2K6Uz 4ZrQ== X-Gm-Message-State: AElRT7ExoRJPc+p/OPV+sIIJFVhtvHFFo0guM/MLYWvovPhmAehze9fO bOli0ESwZQiz/iGbp+CBIqE= X-Received: by 10.98.130.140 with SMTP id w134mr5933818pfd.127.1522320113977; Thu, 29 Mar 2018 03:41:53 -0700 (PDT) Received: from bj03382pcu.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id h2sm12720017pfh.86.2018.03.29.03.41.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 29 Mar 2018 03:41:52 -0700 (PDT) From: Zhaoyang Huang X-Google-Original-From: Zhaoyang Huang To: Steven Rostedt , Ingo Molnar , linux-kernel@vger.kernel.org, kernel-patch-test@lists.linaro.org Subject: [PATCH v1] kernel/trace:check the val against the available mem Date: Thu, 29 Mar 2018 18:41:44 +0800 Message-Id: <1522320104-6573-1-git-send-email-zhaoyang.huang@spreadtrum.com> X-Mailer: git-send-email 1.7.9.5 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It is reported that some user app would like to echo a huge number to "/sys/kernel/debug/tracing/buffer_size_kb" regardless of the available memory, which will cause the coinstantaneous page allocation failed and introduce OOM. The commit checking the val against the available mem first to avoid the consequence allocation. Signed-off-by: Zhaoyang Huang --- kernel/trace/trace.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 2d0ffcc..a4a4237 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -43,6 +43,8 @@ #include #include +#include +#include #include "trace.h" #include "trace_output.h" @@ -5967,6 +5969,39 @@ static ssize_t tracing_splice_read_pipe(struct file *filp, return ret; } +static long get_available_mem(void) +{ + struct sysinfo i; + long available; + unsigned long pagecache; + unsigned long wmark_low = 0; + unsigned long pages[NR_LRU_LISTS]; + struct zone *zone; + int lru; + + si_meminfo(&i); + si_swapinfo(&i); + + for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++) + pages[lru] = global_page_state(NR_LRU_BASE + lru); + + for_each_zone(zone) + wmark_low += zone->watermark[WMARK_LOW]; + + available = i.freeram - wmark_low; + + pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE]; + pagecache -= min(pagecache / 2, wmark_low); + available += pagecache; + + available += global_page_state(NR_SLAB_RECLAIMABLE) - + min(global_page_state(NR_SLAB_RECLAIMABLE) / 2, wmark_low); + + if (available < 0) + available = 0; + return available; +} + static ssize_t tracing_entries_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos) @@ -5975,13 +6010,15 @@ static ssize_t tracing_splice_read_pipe(struct file *filp, struct trace_array *tr = inode->i_private; unsigned long val; int ret; + long available; + available = get_available_mem(); ret = kstrtoul_from_user(ubuf, cnt, 10, &val); if (ret) return ret; /* must have at least 1 entry */ - if (!val) + if (!val || (val > available)) return -EINVAL; /* value is in KB */ -- 1.9.1