Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp955473ybv; Thu, 20 Feb 2020 10:16:32 -0800 (PST) X-Google-Smtp-Source: APXvYqxChAetCUzZdpTmykB0JJZnz3C7nCL8hhrdbjokSfRQ6w+nR9P9Xj4K1A/aVeHb2g67hp4K X-Received: by 2002:a9d:6b84:: with SMTP id b4mr24956993otq.190.1582222591822; Thu, 20 Feb 2020 10:16:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582222591; cv=none; d=google.com; s=arc-20160816; b=rvtHwoGxBsZBlofCJT/tSo+hfAe2jlDQCdYCbNVcrERm8Kk/uNWzMKNCsVWmNNN581 KlFcczrYRbWs+i4Bxjj29zK1Tc9tb+oCcF3UjNh5ZISCZvbp8r+dURs0Ilb8WjT9C3Gk QhrMimrjzoDz1bF9zHYW72oQ+Eny33A2stSAMVLTMpnNEJ7KQOTwWXl5ybXLwfisS7Uc wS0t0i3+8It5xNGekTcm8/QMw2zO678E2Fz0TBuYMo7co0CAXx3DhmO8FsE4a7rKYleU zf2ubKVdWmIBzI9Fkn7q2P0Ffw2NUA4K68wkox62JdekVSdrlixRf5J4ETLRKgOCqLib 5Wrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=uUKdgkaufxLmLYOt3TduZTiePIKaOkl8sejxXEeQJug=; b=HXyO1Dh+UxC1fN9TJYxNbFEOGlxs50yPjxcabd76ToHR4ITS+Y5uiXTW3bIiSg1utX FcOZ0KGGCjfFHadaY8Zpkw8j1gbtBpwHF52qIKtWOx1Logzflv95rux1G+GztFA9IrWS yjIR6PJF6PhycQU2VHBKmhlQ4t4oLh06gXn/GpFMd85C2C94mgl5lwoPiO9Q+ofd5ERH AZ8IXMN5j+WYqUsMPC7PRDf6nB7VwvDuf1h7N2gocXl9c4KWIe8VPrGfwueaMJy4PffJ DXZT0tTmOkxmu8t+VEOolYfrEXP0Ynq3v/ikHYMFVuaxerLFR7hmT8g+9BR9TnLcRveg MWUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=uEXDpSnv; 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 b8si89684otf.32.2020.02.20.10.16.19; Thu, 20 Feb 2020 10:16:31 -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=uEXDpSnv; 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 S1728739AbgBTSO5 (ORCPT + 99 others); Thu, 20 Feb 2020 13:14:57 -0500 Received: from mail-ot1-f67.google.com ([209.85.210.67]:44060 "EHLO mail-ot1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728512AbgBTSO5 (ORCPT ); Thu, 20 Feb 2020 13:14:57 -0500 Received: by mail-ot1-f67.google.com with SMTP id h9so4532076otj.11 for ; Thu, 20 Feb 2020 10:14:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=uUKdgkaufxLmLYOt3TduZTiePIKaOkl8sejxXEeQJug=; b=uEXDpSnvxNGx/1g8in8h2Lps6UF2cwI0Ci30lm3NqVXZaApTkzuDK+720dgSH9vb6k KPzL7duyHh9m0BqEDV45tazOognY/d0scMRdl2OcG1JPE/lSbuZQK0NgSRFEE6JZ1ocE bqLEMcVwBuFDRo16e9ImN9DFgGBbY4Rh3WKWen7Cz5Tan7f1NIIGTAN0UdCsrXlhKb2x xp82sXN5MddaQ9Fo3jlxRRZOvEG5C3dmzkLPX1is1Moe0S017U8Ajd4vA0J68kr+ivdJ DDHGyxImOHKFItW4eCZUYeAX2LdHOyh93fukcpNbkzLACAa56EL81Jnugc/MS1xxyks7 aVSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=uUKdgkaufxLmLYOt3TduZTiePIKaOkl8sejxXEeQJug=; b=mDgo7bipT/BBc4dgsJkozwq7yOX91Ad9baDNnnsNff0JakXwnTr3ongtKh8QmBt+z+ r89NxHCNR31/E4bwNFysNW30wVSf2nPHQ9vjOiv7ZRzTPyVIsMhUnD74Qu661ZXN3di8 cRROye21B3vEVLHjPcqSaolKaDg1D6Fh3K6S02bFDtZmkrXR7udJCm+Il3LqR6GWRHLd N8IfFlpn05HnzGz1TphgTMnP+OXRgCGuiPuEQ1LL3ucTgnok6bH53zJAEyWoupYCum6n PGiTA1XdTNBHdrD3+ncNA1TloUOcr6e0XyOdX8kh43bJVLnuHBTkgsBbRU9+lalyomEn QuTw== X-Gm-Message-State: APjAAAXJjiJfERmF0UD13I0gyiH6esMwxO202nG3oeQ6+luKa8AuTvhj ox1NAyFQ6gV3cOINH7ma1ja9rBayzRzryb8C2A36rw== X-Received: by 2002:a05:6830:11:: with SMTP id c17mr24335278otp.360.1582222496058; Thu, 20 Feb 2020 10:14:56 -0800 (PST) MIME-Version: 1.0 References: <0a27b6fcbd1f7af104d7f4cf0adc6a31e0e7dd19.1582216294.git.schatzberg.dan@gmail.com> In-Reply-To: <0a27b6fcbd1f7af104d7f4cf0adc6a31e0e7dd19.1582216294.git.schatzberg.dan@gmail.com> From: Shakeel Butt Date: Thu, 20 Feb 2020 10:14:45 -0800 Message-ID: Subject: Re: [PATCH v3 2/3] mm: Charge active memcg when no mm is set To: Dan Schatzberg Cc: Jens Axboe , Tejun Heo , Li Zefan , Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Hugh Dickins , Roman Gushchin , 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)" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 20, 2020 at 8:52 AM 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. What if css_tryget_online(current->active_memcg) in get_mem_cgroup_from_current() fails? Do we want to change this to css_tryget() and even if that fails should we fallback to root_mem_cgroup or current->mm->memcg? > 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 > --- > 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 >