From patchwork Thu Feb 20 16:51:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Schatzberg X-Patchwork-Id: 11394615 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9CAB213A4 for ; Thu, 20 Feb 2020 16:52:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5FBE32467A for ; Thu, 20 Feb 2020 16:52:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dNg0IiRn" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5FBE32467A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 8EA2A6B0073; Thu, 20 Feb 2020 11:52:40 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8BFE66B007B; Thu, 20 Feb 2020 11:52:40 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7D60E6B007D; Thu, 20 Feb 2020 11:52:40 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0210.hostedemail.com [216.40.44.210]) by kanga.kvack.org (Postfix) with ESMTP id 640566B0073 for ; Thu, 20 Feb 2020 11:52:40 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 09DBD8248D52 for ; Thu, 20 Feb 2020 16:52:40 +0000 (UTC) X-FDA: 76511099280.24.force45_516c0243d9f2a X-Spam-Summary: 2,0,0,2e9323e08726c3f8,d41d8cd98f00b204,schatzberg.dan@gmail.com,:schatzberg.dan@gmail.com:axboe@kernel.dk:tj@kernel.org:lizefan@huawei.com:hannes@cmpxchg.org:mhocko@kernel.org:vdavydov.dev@gmail.com:akpm@linux-foundation.org:hughd@google.com:guro@fb.com:shakeelb@google.com:chris@chrisdown.name:yang.shi@linux.alibaba.com:tglx@linutronix.de:linux-block@vger.kernel.org:linux-kernel@vger.kernel.org:cgroups@vger.kernel.org:,RULES_HIT:41:355:379:387:541:800:960:973:988:989:1260:1311:1314:1345:1359:1437:1515:1535:1542:1711:1730:1747:1777:1792:2198:2199:2393:2559:2562:2731:3138:3139:3140:3141:3142:3354:3865:3867:3870:3871:3872:3874:4117:4250:4321:4605:5007:6119:6261:6653:6742:7514:7901:7903:9413:10004:11026:11658:11914:12043:12296:12297:12438:12517:12519:12555:12895:12986:13053:13894:14096:14181:14394:14687:14721:21324:21433:21444:21451:21627:21666:21740:21795:21990:30012:30051:30054,0,RBL:209.85.222.194:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100,Cache IP:none, X-HE-Tag: force45_516c0243d9f2a X-Filterd-Recvd-Size: 6076 Received: from mail-qk1-f194.google.com (mail-qk1-f194.google.com [209.85.222.194]) by imf47.hostedemail.com (Postfix) with ESMTP for ; Thu, 20 Feb 2020 16:52:39 +0000 (UTC) Received: by mail-qk1-f194.google.com with SMTP id p7so4159806qkh.10 for ; Thu, 20 Feb 2020 08:52:39 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=I8J4e+RKsO0vBs9ulW2qejTzM3UP4IUucx45iZgHGDk=; b=dNg0IiRn+Gk9q6KF+hcyLl60wq0erTW0EXJiWVJ6o7dlHxQ/E0J0TEw5dFNUcTKu6e becjiDbCVRRn2+wyS6bnxSDyuVvWnC9V6xOfbWHnYmoD4PKhjTlaDzO/qZIcvObgzOog 3LlvWuPv6L0ZcZd3Gqc9Ta57OzRhgW+XI3KbpDT/uiekeqDgK1OLt8r6BHQsUfpW17Sb MOaGL2lrRUYXhWErY2Hdimj/tnrFkcz9B9QEqZrvjeSN0BiR/Yp3ZZ+akBkuZ7LmThrG uJuD9QxOo+NHbUOf5HWYe8qAAkmZnc4v84NIm4DSush/HbfARDvIR6r2yuqD8yFP7HX9 oyMg== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=I8J4e+RKsO0vBs9ulW2qejTzM3UP4IUucx45iZgHGDk=; b=uGsdJG026xMgOKO231hdVv3QcJ5fxVTGtV+72Udd8Xz0v03L5EtTIo9tNkr0Npdntp zbqwC6Cr9GJWlRj2p9T7tt385dHC7pbGXzkYpVX56hD65pWOPdLucS8sgDTeKymHnrjL Haph1kgmIAt58zv4A4XNy4ohz3pBNwpv6No4FH+4rnfrlwEBdFwWF1eO2nRTOlq9GuQ1 UUyOLbGH04vapZ/xpU4lxJp77u//uf1PjpxTCzsYMQPGuq56Hum/SRbMfyfY/9PWIIlt v1b+t4wsr2+3+lyRNruxA5AiXYLHtEsP/TmBRK1Wwp3bhsEkRFmQagMDPQ79aZL2+5vL sw8A== X-Gm-Message-State: APjAAAUgAZEx6qm3C4QINNE2x2XH2XMNRFtKdWF6DBzTTHMWu51u6QID T7p22dYqckj/OTaF0YPWCDk= X-Google-Smtp-Source: APXvYqwuXQgzUa7PIPUi2PPZnRvaq1VkmjKxS8nsMsEuTd0zTGjx5hX6n+BdmU44K9ufjVnvYUNujw== X-Received: by 2002:a37:e55:: with SMTP id 82mr29857378qko.370.1582217558779; Thu, 20 Feb 2020 08:52:38 -0800 (PST) Received: from dschatzberg-fedora-PC0Y6AEN.thefacebook.com ([2620:10d:c091:500::3:edb0]) by smtp.gmail.com with ESMTPSA id t55sm43567qte.24.2020.02.20.08.52.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2020 08:52:38 -0800 (PST) From: Dan Schatzberg To: Cc: Dan Schatzberg , Jens Axboe , Tejun Heo , Li Zefan , Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Hugh Dickins , Roman Gushchin , Shakeel Butt , Chris Down , Yang Shi , Thomas Gleixner , linux-block@vger.kernel.org (open list:BLOCK LAYER), linux-kernel@vger.kernel.org (open list), cgroups@vger.kernel.org (open list:CONTROL GROUP (CGROUP)), linux-mm@kvack.org (open list:CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)) Subject: [PATCH v3 2/3] mm: Charge active memcg when no mm is set Date: Thu, 20 Feb 2020 11:51:52 -0500 Message-Id: <0a27b6fcbd1f7af104d7f4cf0adc6a31e0e7dd19.1582216294.git.schatzberg.dan@gmail.com> X-Mailer: git-send-email 2.21.1 In-Reply-To: References: MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: memalloc_use_memcg() worked for kernel allocations but was silently ignored for user pages. This patch establishes a precedence order for who gets charged: 1. If there is a memcg associated with the page already, that memcg is charged. This happens during swapin. 2. If an explicit mm is passed, mm->memcg is charged. This happens during page faults, which can be triggered in remote VMs (eg gup). 3. Otherwise consult the current process context. If it has configured a current->active_memcg, use that. Otherwise, current->mm->memcg. Previously, if a NULL mm was passed to mem_cgroup_try_charge (case 3) it would always charge the root cgroup. Now it looks up the current active_memcg first (falling back to charging the root cgroup if not set). Signed-off-by: Dan Schatzberg Acked-by: Johannes Weiner Acked-by: Tejun Heo Acked-by: Chris Down Reviewed-by: Shakeel Butt Acked-by: Hugh Dickins --- mm/memcontrol.c | 11 ++++++++--- mm/shmem.c | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 6f6dc8712e39..b174aff4f069 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6317,7 +6317,8 @@ enum mem_cgroup_protection mem_cgroup_protected(struct mem_cgroup *root, * @compound: charge the page as compound or small page * * Try to charge @page to the memcg that @mm belongs to, reclaiming - * pages according to @gfp_mask if necessary. + * pages according to @gfp_mask if necessary. If @mm is NULL, try to + * charge to the active memcg. * * Returns 0 on success, with *@memcgp pointing to the charged memcg. * Otherwise, an error code is returned. @@ -6361,8 +6362,12 @@ int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm, } } - if (!memcg) - memcg = get_mem_cgroup_from_mm(mm); + if (!memcg) { + if (!mm) + memcg = get_mem_cgroup_from_current(); + else + memcg = get_mem_cgroup_from_mm(mm); + } ret = try_charge(memcg, gfp_mask, nr_pages); diff --git a/mm/shmem.c b/mm/shmem.c index c8f7540ef048..7c7f5acf89d6 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1766,7 +1766,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, } sbinfo = SHMEM_SB(inode->i_sb); - charge_mm = vma ? vma->vm_mm : current->mm; + charge_mm = vma ? vma->vm_mm : NULL; page = find_lock_entry(mapping, index); if (xa_is_value(page)) {