Received: by 10.223.164.202 with SMTP id h10csp385256wrb; Fri, 17 Nov 2017 02:13:05 -0800 (PST) X-Google-Smtp-Source: AGs4zMY7O98MNkDRqfDFWldodk4q9fyhjbax7tbZbhH/6HqxFtpst0J9Jb05N/YKzR61r5Vv3YDR X-Received: by 10.101.82.130 with SMTP id y2mr4592347pgp.65.1510913585123; Fri, 17 Nov 2017 02:13:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510913585; cv=none; d=google.com; s=arc-20160816; b=QvZzXs8XmRmh6I3tgj8JcKFhIzHdnySYgpH8w+01yrkxpaAOq2yGAtykbv50SzX7bK c9YuDo8zab42tCzP1XoKwGlqiRoIdDH6afke52fe7Gvw7J4jahxBd1O2nuv6jdFpJxtD ED0aGoO61rZgN2AO6eYEasK14vyDN61urBKgNwNzPmHFSM8B13vJ63cUNZ9jmVMXV+iv S6h4D9zNY7c/pwB0Y9tr/BLeFfzqr3ZVUKmQsxjOcPYMlSPAn5NzW7wqTNYoYdgXevHm 0KW/PZAOmpcMGHWzCdYq+Oah1F6zoOz6ekR2xnG8EOWFIkom2wGr86XTNQ57rR66HksP n1gA== 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:cc:to:from :dkim-signature:arc-authentication-results; bh=52bt+Zxw0Bdm6Jyhfur1mjFpF4gEblymnQogxCM9vkI=; b=vHW4hFC4Qn/QMIq/EydyYyOGxA3gSiTrxyXjSiwAgZ4znn4lbxFF96vebAbftXD/Fz t8Ld0Prvibate4wOsLDGw1pcTjwjUk6CyBoLHO6ZefrkqcyCnlHkAKPk3rggfzkfobuz RsW0pdC3FaV7jfN0eBXBYUtBbIO/3VvmIpZspefswztk6P1ZReAylqgFdn0epSqPwNC+ VPIngFgRGcgfqBVhXCusOSzcAQQ9TA+G/gYcJT2NXIeO34YUUpvLA34ppzrJK+/bQPxI dtB/2DyJDwExLc02X/LxzVn7+4ouiqRlcFOPPRT5KzYztXyPSjsrYfF13Mnlgk6iAFea jaAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HaL2SX2W; 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=NONE 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 n28si2737901pfg.347.2017.11.17.02.12.52; Fri, 17 Nov 2017 02:13:05 -0800 (PST) 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=HaL2SX2W; 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=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756318AbdKQDKU (ORCPT + 91 others); Thu, 16 Nov 2017 22:10:20 -0500 Received: from mail-pf0-f194.google.com ([209.85.192.194]:46371 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755775AbdKQDKL (ORCPT ); Thu, 16 Nov 2017 22:10:11 -0500 Received: by mail-pf0-f194.google.com with SMTP id q4so877920pfg.13; Thu, 16 Nov 2017 19:10:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=52bt+Zxw0Bdm6Jyhfur1mjFpF4gEblymnQogxCM9vkI=; b=HaL2SX2WNqoMkOoFW4Sd7kDLm14z6VzHU9dhryOpVN9C96kNI2NTbTTU2kweWk+L9r 51sWQdlmhPWo9/C2xrPSidDY2NfSr9zXmxeCUkCJQQMPDFnBhaFG64zp/BzVfyG37AP0 BJLKouDSoU0rKWzwyUHMQrD2RNNsoYafiq2rzqYLf7q6pmBrIzRDVXUYDiqihhOX+l9F 2eAJgMc+iFnT3F/fpNOyXLnW3GZV3DIIoSt+MmLvIlaDmJPS3rJXDOL5rwk3Z9OQGPGX p/dLYoNjDl/+tYA3CGQg+wiS5WkDMcCXFVIYDJJEOChyJGy/Ag0Hzh+ZkF2i2T+gzmgM eZGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=52bt+Zxw0Bdm6Jyhfur1mjFpF4gEblymnQogxCM9vkI=; b=gnh8+uDgSTpy1LezceN5Gc2mTYzspGZoQTZsRQ68isWkFmskdm76rWf0enS2gmnyjR 9IBk4IodXHlz7kYJByvg9uIyp2JZ0OVBGn9wICtIFbT6ce6jjRYOGfrHQmcBig8kzXD1 Lcw3uDDvgboo96S9nWb7SfWxnHelblYEpaoU1Q1Ii/ewq7n2ANy1bZQOdm9V16i2Chi1 UxPx8z2EulL6f8iq/BASJQF4iWGIrzEen0aThbp+c9tEXMus740FHqZruDtQhGyn0/Ow f19nPKkH4UxcqldBGGl5ukRGLJBm88oE+D6zOHm/rGBl+/LVX5JNSTVnVtw1gjZN+2E+ 6OoQ== X-Gm-Message-State: AJaThX4+uArvJR0npZVguxAoAsrvK9LkZc0TVvYL4yEcy+GhvIQFgWTZ kKg/uLSloYkdSGacI2Y5Jgw= X-Received: by 10.99.124.89 with SMTP id l25mr3642256pgn.86.1510888211339; Thu, 16 Nov 2017 19:10:11 -0800 (PST) Received: from li1588-6.members.linode.com (li1588-6.members.linode.com. [139.162.104.6]) by smtp.gmail.com with ESMTPSA id m87sm5470843pfi.134.2017.11.16.19.10.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 Nov 2017 19:10:10 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org, hannes@cmpxchg.org, vdavydov.dev@gmail.com, mhocko@suse.com, tj@kernel.org, guro@fb.com, khlebnikov@yandex-team.ru, mka@chromium.org, hughd@google.com Cc: cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH] mm/shmem: set default tmpfs size according to memcg limit Date: Fri, 17 Nov 2017 03:09:59 +0000 Message-Id: <1510888199-5886-1-git-send-email-laoar.shao@gmail.com> X-Mailer: git-send-email 1.8.3.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently the default tmpfs size is totalram_pages / 2 if mount tmpfs without "-o size=XXX". When we mount tmpfs in a container(i.e. docker), it is also totalram_pages / 2 regardless of the memory limit on this container. That may easily cause OOM if tmpfs occupied too much memory when swap is off. So when we mount tmpfs in a memcg, the default size should be limited by the memcg memory.limit. Signed-off-by: Yafang Shao --- include/linux/memcontrol.h | 1 + mm/memcontrol.c | 2 +- mm/shmem.c | 20 +++++++++++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 69966c4..79c6709 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -265,6 +265,7 @@ struct mem_cgroup { /* WARNING: nodeinfo must be the last member here */ }; +extern struct mutex memcg_limit_mutex; extern struct mem_cgroup *root_mem_cgroup; static inline bool mem_cgroup_disabled(void) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 661f046..ad32f3c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2464,7 +2464,7 @@ static inline int mem_cgroup_move_swap_account(swp_entry_t entry, } #endif -static DEFINE_MUTEX(memcg_limit_mutex); +DEFINE_MUTEX(memcg_limit_mutex); static int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long limit) diff --git a/mm/shmem.c b/mm/shmem.c index 07a1d22..1c320dd 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -35,6 +35,7 @@ #include #include #include +#include #include /* for arch/microblaze update_mmu_cache() */ @@ -108,7 +109,24 @@ struct shmem_falloc { #ifdef CONFIG_TMPFS static unsigned long shmem_default_max_blocks(void) { - return totalram_pages / 2; + unsigned long size; + +#ifdef CONFIG_MEMCG + struct mem_cgroup *memcg = mem_cgroup_from_task(current); + + if (memcg == NULL || memcg == root_mem_cgroup) + size = totalram_pages / 2; + else { + mutex_lock(&memcg_limit_mutex); + size = memcg->memory.limit > totalram_pages ? + totalram_pages / 2 : memcg->memory.limit / 2; + mutex_unlock(&memcg_limit_mutex); + } +#else + size = totalram_pages / 2; +#endif + + return size; } static unsigned long shmem_default_max_inodes(void) -- 1.8.3.1 From 1586209552117481580@xxx Fri Dec 08 10:01:50 +0000 2017 X-GM-THRID: 1586209552117481580 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread