Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2827023pxb; Sun, 24 Jan 2021 22:39:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJyPT5sc/mMf84Bo2Ig8nFmjsYUvDxMWPOTiJgV8wAA/rZRRvCvRt8aSzp5+04RQIZvC5mAK X-Received: by 2002:a50:e008:: with SMTP id e8mr105496edl.339.1611556743095; Sun, 24 Jan 2021 22:39:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611556743; cv=none; d=google.com; s=arc-20160816; b=s1EaGhqoTqCcx0u0aWd+yTwWEkRuaLFvhmQfrrQ6OU45kWCD9Vl63EjtvoKzkXIRBc ZS++U0sjbJSO7ZQnr3oRkfZKxidVxzEl+geMVLcgXATkkAO4My9IHkijRXb1GHdIKwen u/oPoYA8EO+viBMdJA7hBeAu2UJdccO6TGDx336Fx7kH1uqDtbV/CFvQclW2ydqvwAWg jA71i5/DX2PJqdypW0rcuioFwTYnbYlFFkXRFXyDf9vRBknVv1lJj+Aioln5hMiY0XCA 0JCAzHMVMnGMt0d+B0FvK1bY+85ibBbCWt4f581aoObCW/5iwV44VuexjzwmhjGhyxCA Mr/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=FzmeP0yHZEPMZ7o7VAUIxGVgYAz0RDXquFi1BRsKX74=; b=uHnABdrnC+K7aw5Xe43fymK6e8csLEX8pDkMYs0Km+wE2S45In8KkPfh4EJpIJK3uT XC8fnMBhWYn6qmvtcqyIMrHuF4M34c8lfk4XwHfc94r2sv52VyBwfvXJuHmDysgLOjiV l3C4Ww6CoVP8DzUz3r9KUOymFdX9GheUMv34J/FUZqAH1m2fCw/iH3tDCsHvEtP05U7r pOwrJOl8fhShN2YgjtY2XVH2YfjuoQtlDT85ftiZIq56Zh/44IReDrjXbzss3FgaJZdS g3+EEwvj2ukxbKob7Hf//oLgVDCQDBHbJ/NvRHkrni0d4gdDHO4dKZ/+b/oU42l7eU68 rRLw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id sd11si5662558ejb.584.2021.01.24.22.38.39; Sun, 24 Jan 2021 22:39:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727155AbhAYGgZ (ORCPT + 99 others); Mon, 25 Jan 2021 01:36:25 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:11486 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727149AbhAYGbn (ORCPT ); Mon, 25 Jan 2021 01:31:43 -0500 Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4DPKhv6yk1zjCyQ; Mon, 25 Jan 2021 14:29:35 +0800 (CST) Received: from [10.174.179.117] (10.174.179.117) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.498.0; Mon, 25 Jan 2021 14:30:42 +0800 Subject: Re: [PATCH] mm/filemap: Adding missing mem_cgroup_uncharge() to __add_to_page_cache_locked() To: Waiman Long CC: , , Andrew Morton , Johannes Weiner , Alex Shi References: <20210125042441.20030-1-longman@redhat.com> From: Miaohe Lin Message-ID: <3b3f2b56-e66a-db5e-8d8e-95f3812c6838@huawei.com> Date: Mon, 25 Jan 2021 14:30:42 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <20210125042441.20030-1-longman@redhat.com> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.179.117] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021/1/25 12:24, Waiman Long wrote: > The commit 3fea5a499d57 ("mm: memcontrol: convert page > cache to a new mem_cgroup_charge() API") introduced a bug in > __add_to_page_cache_locked() causing the following splat: > > [ 1570.068330] page dumped because: VM_BUG_ON_PAGE(page_memcg(page)) > [ 1570.068333] pages's memcg:ffff8889a4116000 > [ 1570.068343] ------------[ cut here ]------------ > [ 1570.068346] kernel BUG at mm/memcontrol.c:2924! > [ 1570.068355] invalid opcode: 0000 [#1] SMP KASAN PTI > [ 1570.068359] CPU: 35 PID: 12345 Comm: cat Tainted: G S W I 5.11.0-rc4-debug+ #1 > [ 1570.068363] Hardware name: HP HP Z8 G4 Workstation/81C7, BIOS P60 v01.25 12/06/2017 > [ 1570.068365] RIP: 0010:commit_charge+0xf4/0x130 > : > [ 1570.068375] RSP: 0018:ffff8881b38d70e8 EFLAGS: 00010286 > [ 1570.068379] RAX: 0000000000000000 RBX: ffffea00260ddd00 RCX: 0000000000000027 > [ 1570.068382] RDX: 0000000000000000 RSI: 0000000000000004 RDI: ffff88907ebe05a8 > [ 1570.068384] RBP: ffffea00260ddd00 R08: ffffed120fd7c0b6 R09: ffffed120fd7c0b6 > [ 1570.068386] R10: ffff88907ebe05ab R11: ffffed120fd7c0b5 R12: ffffea00260ddd38 > [ 1570.068389] R13: ffff8889a4116000 R14: ffff8889a4116000 R15: 0000000000000001 > [ 1570.068391] FS: 00007ff039638680(0000) GS:ffff88907ea00000(0000) knlGS:0000000000000000 > [ 1570.068394] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > [ 1570.068396] CR2: 00007f36f354cc20 CR3: 00000008a0126006 CR4: 00000000007706e0 > [ 1570.068398] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > [ 1570.068400] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 > [ 1570.068402] PKRU: 55555554 > [ 1570.068404] Call Trace: > [ 1570.068407] mem_cgroup_charge+0x175/0x770 > [ 1570.068413] __add_to_page_cache_locked+0x712/0xad0 > [ 1570.068439] add_to_page_cache_lru+0xc5/0x1f0 > [ 1570.068461] cachefiles_read_or_alloc_pages+0x895/0x2e10 [cachefiles] > [ 1570.068524] __fscache_read_or_alloc_pages+0x6c0/0xa00 [fscache] > [ 1570.068540] __nfs_readpages_from_fscache+0x16d/0x630 [nfs] > [ 1570.068585] nfs_readpages+0x24e/0x540 [nfs] > [ 1570.068693] read_pages+0x5b1/0xc40 > [ 1570.068711] page_cache_ra_unbounded+0x460/0x750 > [ 1570.068729] generic_file_buffered_read_get_pages+0x290/0x1710 > [ 1570.068756] generic_file_buffered_read+0x2a9/0xc30 > [ 1570.068832] nfs_file_read+0x13f/0x230 [nfs] > [ 1570.068872] new_sync_read+0x3af/0x610 > [ 1570.068901] vfs_read+0x339/0x4b0 > [ 1570.068909] ksys_read+0xf1/0x1c0 > [ 1570.068920] do_syscall_64+0x33/0x40 > [ 1570.068926] entry_SYSCALL_64_after_hwframe+0x44/0xa9 > [ 1570.068930] RIP: 0033:0x7ff039135595 > > Before that commit, there was a try_charge() and commit_charge() > in __add_to_page_cache_locked(). These 2 separated charge functions > were replaced by a single mem_cgroup_charge(). However, it forgot > to add a matching mem_cgroup_uncharge() when the xarray insertion > failed with the page released back to the pool. Fix this by adding a > mem_cgroup_uncharge() call when insertion error happens. > > Fixes: 3fea5a499d57 ("mm: memcontrol: convert page cache to a new mem_cgroup_charge() API") > Signed-off-by: Waiman Long Cc stable should be needed. > --- > mm/filemap.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/mm/filemap.c b/mm/filemap.c > index 5c9d564317a5..aa0e0fb04670 100644 > --- a/mm/filemap.c > +++ b/mm/filemap.c > @@ -835,6 +835,7 @@ noinline int __add_to_page_cache_locked(struct page *page, > XA_STATE(xas, &mapping->i_pages, offset); > int huge = PageHuge(page); > int error; > + bool charged = false; > > VM_BUG_ON_PAGE(!PageLocked(page), page); > VM_BUG_ON_PAGE(PageSwapBacked(page), page); > @@ -848,6 +849,7 @@ noinline int __add_to_page_cache_locked(struct page *page, > error = mem_cgroup_charge(page, current->mm, gfp); > if (error) > goto error; > + charged = true; > } > > gfp &= GFP_RECLAIM_MASK; > @@ -896,6 +898,8 @@ noinline int __add_to_page_cache_locked(struct page *page, > > if (xas_error(&xas)) { > error = xas_error(&xas); > + if (charged) > + mem_cgroup_uncharge(page); > goto error; > } > > Looks good to me. Thanks. Reviewed-by: Miaohe Lin