Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4174474pxb; Mon, 8 Feb 2021 09:37:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJwZUK/aSiO0mrSOjiwrmz8eI/0BJBntouRA0VvK1vXQKQQ9XplLKAUZDGugz4B2AhSL6/TV X-Received: by 2002:a05:6402:1153:: with SMTP id g19mr13540783edw.292.1612805825473; Mon, 08 Feb 2021 09:37:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612805825; cv=none; d=google.com; s=arc-20160816; b=W90lTz4RV7hMWIFyZFosF7uKyOw9IKMisOgXdq/fj51wsU+NRNQ9j+PDNLYHjd3C/X gyDMa5iX/m03Rvnuy3LiqcUgRNaIImMh0t64FmAsu26hK1ube63GKT2Gd81GF0Xqf78H f+FxqecOhTOhOM3CRpRamTvJrPlOWLr7UKO4TTBhtoBgv0Z9yBDt4Qvx/feU4VUk+8O2 1C+7JaqDrFatvcYgbNM/gdiBqB/vN4+z6HyFJOZYFyMBvS3meuQgI1SvuEcXxjx7Vuue 0JT/LA3LxcxFuiaUpNXOu8eHwPl9HDvUUaGHMkLAxOZQWbOYF+7KgdwMHwxZxoCfTgMQ Fhvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=mdvKdy1eJczzh+IeU5+fhk+hy6zJbQNjryLuWL5CDc8=; b=dykxRKKoC8O3mtC4Wac/j16lRVsk0u0m5o/aG2MHQt70fHGrfL2CCVM/5RyKen7gP7 8LpzERnz6q8tM+1g1BeYC8nf5OMoveQv262UnJN9rQjKad1M32AGJ/YYzRU+5tsYvQeu d6LsM3t1M+1FIGc9bNBC1NAyOseRMEhANI6xR5+k5WezcHPvvByNUfDCN+DeM0+V9gW+ a/olhmTKrH2/ea8gwLYXGDHnbqOyzaqBwEFVWY8lHHej4NPAgtdZcIlnADTfOMKfMnfr R4FUX8CbYYki4mSZvkFGdzm5JrXXsuO2pbCl3xPgN5S1tA544QbCY73PWS1hFxtF/ulV 7Gwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wqTbRKUV; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k21si12128598edo.563.2021.02.08.09.36.40; Mon, 08 Feb 2021 09:37:05 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wqTbRKUV; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232499AbhBHRgH (ORCPT + 99 others); Mon, 8 Feb 2021 12:36:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:37068 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233754AbhBHPaB (ORCPT ); Mon, 8 Feb 2021 10:30:01 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 63B3264ECD; Mon, 8 Feb 2021 15:17:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1612797422; bh=KgOTo85NRv/jeWrQtnUac5CCENu8eNKCvVq7HV4fbXA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wqTbRKUVXBdKSJkVpcqpJpgVwIniEuluodjQBOUuVT/Ge0zBTymfD94GeCIp8R2VU 35mf6Pl56f7rH6Payl3UzghoLroHUhUWqUtNr1XHFvkaVclbva2qz7EnCwdXx3Vu79 w/o6NzUlwsT8OxOJ3t3hRa+kPGy33QwnnvXf2a/Y= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Waiman Long , Alex Shi , Johannes Weiner , Matthew Wilcox , Miaohe Lin , Muchun Song , Michal Hocko , Andrew Morton , Linus Torvalds Subject: [PATCH 5.10 105/120] mm/filemap: add missing mem_cgroup_uncharge() to __add_to_page_cache_locked() Date: Mon, 8 Feb 2021 16:01:32 +0100 Message-Id: <20210208145822.572875718@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210208145818.395353822@linuxfoundation.org> References: <20210208145818.395353822@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Waiman Long commit da74240eb3fcd806edb1643874363e954d9e948b upstream. 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: page dumped because: VM_BUG_ON_PAGE(page_memcg(page)) pages's memcg:ffff8889a4116000 ------------[ cut here ]------------ kernel BUG at mm/memcontrol.c:2924! invalid opcode: 0000 [#1] SMP KASAN PTI CPU: 35 PID: 12345 Comm: cat Tainted: G S W I 5.11.0-rc4-debug+ #1 Hardware name: HP HP Z8 G4 Workstation/81C7, BIOS P60 v01.25 12/06/2017 RIP: commit_charge+0xf4/0x130 Call Trace: mem_cgroup_charge+0x175/0x770 __add_to_page_cache_locked+0x712/0xad0 add_to_page_cache_lru+0xc5/0x1f0 cachefiles_read_or_alloc_pages+0x895/0x2e10 [cachefiles] __fscache_read_or_alloc_pages+0x6c0/0xa00 [fscache] __nfs_readpages_from_fscache+0x16d/0x630 [nfs] nfs_readpages+0x24e/0x540 [nfs] read_pages+0x5b1/0xc40 page_cache_ra_unbounded+0x460/0x750 generic_file_buffered_read_get_pages+0x290/0x1710 generic_file_buffered_read+0x2a9/0xc30 nfs_file_read+0x13f/0x230 [nfs] new_sync_read+0x3af/0x610 vfs_read+0x339/0x4b0 ksys_read+0xf1/0x1c0 do_syscall_64+0x33/0x40 entry_SYSCALL_64_after_hwframe+0x44/0xa9 Before that commit, there was a try_charge() and commit_charge() in __add_to_page_cache_locked(). These two 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. Link: https://lkml.kernel.org/r/20210125042441.20030-1-longman@redhat.com Fixes: 3fea5a499d57 ("mm: memcontrol: convert page cache to a new mem_cgroup_charge() API") Signed-off-by: Waiman Long Reviewed-by: Alex Shi Acked-by: Johannes Weiner Cc: Matthew Wilcox Cc: Miaohe Lin Cc: Muchun Song Cc: Michal Hocko Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/filemap.c | 4 ++++ 1 file changed, 4 insertions(+) --- a/mm/filemap.c +++ b/mm/filemap.c @@ -835,6 +835,7 @@ noinline int __add_to_page_cache_locked( 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( error = mem_cgroup_charge(page, current->mm, gfp); if (error) goto error; + charged = true; } gfp &= GFP_RECLAIM_MASK; @@ -896,6 +898,8 @@ unlock: if (xas_error(&xas)) { error = xas_error(&xas); + if (charged) + mem_cgroup_uncharge(page); goto error; }