Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1628674ybv; Sun, 23 Feb 2020 11:09:39 -0800 (PST) X-Google-Smtp-Source: APXvYqzDqKgKqXgsOPKcy3eG09ZeAN1IQiaskS2n6T/KYm0uIODh5saj9lvSvZp1SOrdAkpdUgtk X-Received: by 2002:aca:3354:: with SMTP id z81mr10089775oiz.129.1582484979332; Sun, 23 Feb 2020 11:09:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582484979; cv=none; d=google.com; s=arc-20160816; b=XBE0+3sYDRKrC583TSYkyJRDNJox6TqQSc/29ivKfFrQb6opZYkivssYGSV1VY2ntP X3NxjRVg9TPkxGAJhZkstBcw4IlQb1TI5Rzn89J/ECasCIpP2lQ+7TZIwNfWgtCGwp4q q+LfbwcEdxKRkemDNehNzBlqfsQ/GhCGb1tLeQ2quxlnwitsO7Gh1/p4UtcAv1VdhbFJ 5trYPUJwzLpmjDJVScVlY2JegK5FYH/6dwrzdhR4ftSBwdblXNCQqM38ibM4bTygERSQ VW3qYFAuWdmT42uJ55Vn6KyeWBMPuehwpxpb4eCRRCtY0PRpqExDO1UMwfV4f0ZnjYC2 fWXg== 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 :message-id:in-reply-to:subject:cc:to:from:date:dkim-signature; bh=htLN1mwGdJtRnyqYUCPJp+OPGglRrhjiWXYVtDt/NKM=; b=mcBC9rBAaTFXOq6JZzWp5zrnflsFWGwYl2k6dnw96XVK6bTwYrqf9JDpQrnl+f/xoi 7Dm01QVzQufUw3EQp50qo0VHso+zhzvBl8qoaHyCgkcEy1MSsQqSManJFmRM0bTyTj/A WP4NElAn90eLScRXYpRHfihy81k4tpRJAHoENFdu0ANRKq1MjIBF+jUUwaZBbIt13zjc gJUDvcyTPF6t7jKlutg9riUGdQ5QTmKZKHV64YzR73XrMdbgdBa94qXA8cboKO0anUQA QfxVcHgfvZQzSCSX/2lx/+bgVnBqfCjB+MuhwfHpP4dqh6wvbavWficIanyidiu4nV+h 0Jdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=JSZ7Tv7F; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r12si3995207oij.113.2020.02.23.11.09.26; Sun, 23 Feb 2020 11:09:39 -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=@google.com header.s=20161025 header.b=JSZ7Tv7F; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727033AbgBWTJS (ORCPT + 99 others); Sun, 23 Feb 2020 14:09:18 -0500 Received: from mail-ot1-f68.google.com ([209.85.210.68]:34276 "EHLO mail-ot1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726302AbgBWTJS (ORCPT ); Sun, 23 Feb 2020 14:09:18 -0500 Received: by mail-ot1-f68.google.com with SMTP id j16so6812518otl.1 for ; Sun, 23 Feb 2020 11:09:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=htLN1mwGdJtRnyqYUCPJp+OPGglRrhjiWXYVtDt/NKM=; b=JSZ7Tv7F6nQ3YH2jP3HFLdPAhZhy2Y/RM9Au69XuF7TR0GzciTC0aj+0Tv4uRV+9XJ S4Wu6jcRiaRdgV38hOL3giTmwn97IzhRnjQRKVCoYK+wbKxwIa24ektK26DJ43CkLtwN ZT9IiL8y9//RQ99zTTNcqBJXNSogX2r+AwY/uH8cqaruxnp5gzlWpx+9El5byimG0jnA gwXKIGTFa3/cNEDFtAyjXJOHNCZOiszkR89nBIjCVQRfXSF+Sr/HRB0GuxP8BmqtZijN wiRvUQvVfqVjVfedH3LtBOG9DpYU+DUoB1Db0YG0GDCRe4xQFuaaRpiYbX5PICYUsE9L PPqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=htLN1mwGdJtRnyqYUCPJp+OPGglRrhjiWXYVtDt/NKM=; b=HaYERqVwd+Nr4HuSGiv4pUFaBHoSaZjtFOjFyPIY2z/xC9lqoL/D2B+jDeeppFDa3q +LBGPxtlYMpLEZ3Q9sfXNTbp3UyRZQMt/oEVaVamQ81d5+QfM5c7GXFQf5VM/gvKL1qs 19WHnhdPgtLQqbTlTqHqTgRkJtb/r422hEcESTeBlCAEkMLEqZ7cHZxi8QylgH4NQMJl ywFD7btIyJ8IFRdLPaVIOaxO2Fge/KAUuSV3beiy8VGbUH7uQFz4hbputNajUYfL9kSF +DEN7W0TaD72N6nj5XohwBbWsFv1DcEivjZiTp0TqTvX6C1RtFGp8ck9gy7eYKWIh0US pWMg== X-Gm-Message-State: APjAAAXYTw7+POcqATXhfVGipiehP55svjaWBnjBLCBEMeG9Tv263+eU r4nvPmEOy6zG87IW9dZeR7MIFA== X-Received: by 2002:a9d:5786:: with SMTP id q6mr36062007oth.164.1582484957514; Sun, 23 Feb 2020 11:09:17 -0800 (PST) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id i6sm3575702oto.62.2020.02.23.11.09.15 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 23 Feb 2020 11:09:16 -0800 (PST) Date: Sun, 23 Feb 2020 11:08:56 -0800 (PST) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Dan Schatzberg cc: 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 , "open list:BLOCK LAYER" , open list , "open list:CONTROL GROUP (CGROUP)" , "open list:CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)" Subject: Re: [PATCH v3 2/3] mm: Charge active memcg when no mm is set In-Reply-To: <0a27b6fcbd1f7af104d7f4cf0adc6a31e0e7dd19.1582216294.git.schatzberg.dan@gmail.com> Message-ID: References: <0a27b6fcbd1f7af104d7f4cf0adc6a31e0e7dd19.1582216294.git.schatzberg.dan@gmail.com> User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 20 Feb 2020, Dan Schatzberg wrote: > 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: Hugh Dickins Yes, internally we have some further not-yet-upstreamed complications here (mainly, the "memcg=" mount option for all charges on a tmpfs to be charged to that memcg); but what you're doing here does not obstruct adding that later, they fit in well with the hierarchy that you (and Johannes) mapped out above, and it's really an improvement for shmem not to be referring to current there - thanks. > --- > 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)) { > -- > 2.17.1