From patchwork Tue Mar 16 15:36:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Schatzberg X-Patchwork-Id: 12142551 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0E0AC433DB for ; Tue, 16 Mar 2021 15:37:31 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5869F650EB for ; Tue, 16 Mar 2021 15:37:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5869F650EB 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 EC1368D0013; Tue, 16 Mar 2021 11:37:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E97F88D0012; Tue, 16 Mar 2021 11:37:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D12248D0013; Tue, 16 Mar 2021 11:37:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0087.hostedemail.com [216.40.44.87]) by kanga.kvack.org (Postfix) with ESMTP id AFF8C8D0012 for ; Tue, 16 Mar 2021 11:37:30 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 6E9868249980 for ; Tue, 16 Mar 2021 15:37:30 +0000 (UTC) X-FDA: 77926141860.17.7804FE1 Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) by imf12.hostedemail.com (Postfix) with ESMTP id E5D53350B for ; Tue, 16 Mar 2021 15:37:26 +0000 (UTC) Received: by mail-qk1-f173.google.com with SMTP id l4so35643635qkl.0 for ; Tue, 16 Mar 2021 08:37:26 -0700 (PDT) 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=Io4/SuSrOnO2lgbZBjKm6Sy30ngmcZaMUy5F+H9fpyg=; b=litRM2T0M677dAg8+HS9+0ucinGZ/dprpSboNx4ygkFJPc0HA7LeXLHFrv7Q9m7k79 aUxF39PH+bvWZtQ0icKUCN/4ShHLXO2+bh72cKKbvhbVjEJDHIL0WCz61My8fhcto88B QvLWU0tlpdH7qaNFpUHjdjkJ7Asl4Hi0b3cRIqZ93FsHXczVR7keUqqqpiMBaIlsoJ5m HokRsWiOoqXWTO98zQWkfe4xQC3jTTfs0+bMx0aKRlA47CnKyFscvmsBxs3Z7jKLx+jd 1HGoBPab+GGGBMBfNjofRaLQVJrgvBq2KfmpJBH1thj4ZTg7KHcg32MgwI4IJ2rSfJh/ 2noQ== 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=Io4/SuSrOnO2lgbZBjKm6Sy30ngmcZaMUy5F+H9fpyg=; b=eqfB+hINZbbbTXDzknC0th3Wan8zGi52RTIQvT0mZrhLtn59/Yb8R8ZYdpTNgGbOwZ 0L7QgQ5s/mR3eJpSQJQDQzR/s3KHJDe7+IooKsDKxhucJpxgbQJeG8XmOrjulY5wdWwL 3anwYygMP0DTERdbM8hqJ2TLL7JfqYD+NYigkppO6fpze/Bd5/NeV6zaR4i8mfwYeOJj OwMpiVmdltCRqPs+ig4TD/79dMNQK51jJ4FC6DGsYL4jdn4eOR20pe/ls/AQioRAXYbR S430kZGOtNARoTWexBLG/1TcPf/xDOFXdkFBdJOXyrIuSggnL1rni0CvDl8FBVPep7vZ GDLg== X-Gm-Message-State: AOAM530FJtsDi3JetC3yE90FB4e1oKWC2a6c90fqTZl4vc9IGA7scU4z osCu0Ohs6U/6OQDojq34ZC8= X-Google-Smtp-Source: ABdhPJzLhmIigbpwLFc93Ups8jNMalmervLIp/mKz6S4aNI/h1o+8g1FB8LgzYpx7UXu9v1tS7X0QA== X-Received: by 2002:a37:6ca:: with SMTP id 193mr125522qkg.405.1615909045580; Tue, 16 Mar 2021 08:37:25 -0700 (PDT) Received: from localhost ([2620:10d:c091:480::1:1ee]) by smtp.gmail.com with ESMTPSA id k24sm13719703qtu.35.2021.03.16.08.37.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Mar 2021 08:37:25 -0700 (PDT) From: Dan Schatzberg To: Cc: Jens Axboe , Tejun Heo , Zefan Li , Johannes Weiner , Andrew Morton , Michal Hocko , Vladimir Davydov , Hugh Dickins , Shakeel Butt , Roman Gushchin , Muchun Song , Alex Shi , Alexander Duyck , Chris Down , Yafang Shao , Wei Yang , 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:MEMORY MANAGEMENT) Subject: [PATCH 2/3] mm: Charge active memcg when no mm is set Date: Tue, 16 Mar 2021 08:36:51 -0700 Message-Id: <20210316153655.500806-3-schatzberg.dan@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210316153655.500806-1-schatzberg.dan@gmail.com> References: <20210316153655.500806-1-schatzberg.dan@gmail.com> MIME-Version: 1.0 X-Stat-Signature: zzz67toku1rmbjnmjzag4gyy51bafmm5 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: E5D53350B Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf12; identity=mailfrom; envelope-from=""; helo=mail-qk1-f173.google.com; client-ip=209.85.222.173 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1615909046-879367 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 --- mm/filemap.c | 2 +- mm/memcontrol.c | 14 +++++++++++--- mm/shmem.c | 4 ++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index 43700480d897..5135f330f05c 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -843,7 +843,7 @@ noinline int __add_to_page_cache_locked(struct page *page, page->index = offset; if (!huge) { - error = mem_cgroup_charge(page, current->mm, gfp); + error = mem_cgroup_charge(page, NULL, gfp); if (error) goto error; charged = true; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e064ac0d850a..9a1b23ed3412 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6690,7 +6690,8 @@ void mem_cgroup_calculate_protection(struct mem_cgroup *root, * @gfp_mask: reclaim mode * * 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. Otherwise, an error code is returned. */ @@ -6726,8 +6727,15 @@ int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask) rcu_read_unlock(); } - if (!memcg) - memcg = get_mem_cgroup_from_mm(mm); + if (!memcg) { + if (!mm) { + memcg = get_mem_cgroup_from_current(); + if (!memcg) + memcg = get_mem_cgroup_from_mm(current->mm); + } else { + memcg = get_mem_cgroup_from_mm(mm); + } + } ret = try_charge(memcg, gfp_mask, nr_pages); if (ret) diff --git a/mm/shmem.c b/mm/shmem.c index b2db4ed0fbc7..353b362c370e 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1695,7 +1695,7 @@ static int shmem_swapin_page(struct inode *inode, pgoff_t index, { struct address_space *mapping = inode->i_mapping; struct shmem_inode_info *info = SHMEM_I(inode); - struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm; + struct mm_struct *charge_mm = vma ? vma->vm_mm : NULL; struct page *page; swp_entry_t swap; int error; @@ -1816,7 +1816,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 = pagecache_get_page(mapping, index, FGP_ENTRY | FGP_HEAD | FGP_LOCK, 0);