Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp2152979ybc; Wed, 20 Nov 2019 09:38:39 -0800 (PST) X-Google-Smtp-Source: APXvYqxamz7DrQvLSpGJUW0+D4CsVJ/SM0dcchez55qD2cfvn7tOxrJSIz6R48I+3Ishb/QuYCu8 X-Received: by 2002:a7b:c08f:: with SMTP id r15mr4607200wmh.45.1574271519554; Wed, 20 Nov 2019 09:38:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574271519; cv=none; d=google.com; s=arc-20160816; b=aXyxWRbq9iQk9yQJJ/74XSpsnkC7nzBcZ/sOOfWXS0MOqVe8JtxS3/jowjNf4rBbkg lRsh8KShV1YFIrS2w0T7gv1QlVR2DMKRqTEbXZxRtEw+ZXk3JZBlyZ4Y2yEz2musg0js b+10Q/2zdNwL9lgIEoy9Vu9kThGZQ1Kw41agpRmWi7gVOZuqrbUvW2p2EH0kfxJrpbmE Ujxa+W0b7YK+dRpWuDop4Zq0UTtxlANgRD8aWHkiq9e5JoozOHps+hDddKpcAT5Ds/BX e+fc5ricIzon0MLs1weIDy8aiYrFO4u1IVJgcNEN4OaVmqh+Ivt7PIeSJBm/DRMdPJ7/ CaFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=hpIA+rvc+Ub15DwARGwCPsLQo64idg/OwWT6O3mpE6w=; b=A/i9gsYTHxZ1eIrZMdP04sIbevxDZtowsGYi9gGDOiyVWeAMl6HoGfgTrF1epViJd/ xmEr7pzhAT+Af9S/Dc2JsKqLHo0p17dfLhF0fL+ISDnfKafiO8hIyz9RHVsoaWqZwD1H ZWgIgrKq51a2rzZ/0svvCZ9L1qXg2x3SES1kI51IC+Kth+gV/olqt4XA2MP6hzuvvQtW SBPKuiktxAnhbSgLuw3unZE+/LcXlYmaKAV5VnXJCgPoDK1mmL+PS3gyK+cSpuvnI/sq 0oI/myiiQzvgivJZohc5L8+yLVWcYQ+rPC8FGFlHfhYpSu80qq7+ujFlz43QVm3TT2Ju RgIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=JWQOya2U; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f21si28853edf.277.2019.11.20.09.38.15; Wed, 20 Nov 2019 09:38: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=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=JWQOya2U; 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=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730041AbfKTQ6u (ORCPT + 99 others); Wed, 20 Nov 2019 11:58:50 -0500 Received: from mail-qv1-f66.google.com ([209.85.219.66]:44389 "EHLO mail-qv1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728580AbfKTQ6u (ORCPT ); Wed, 20 Nov 2019 11:58:50 -0500 Received: by mail-qv1-f66.google.com with SMTP id d3so167999qvs.11 for ; Wed, 20 Nov 2019 08:58:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=hpIA+rvc+Ub15DwARGwCPsLQo64idg/OwWT6O3mpE6w=; b=JWQOya2UTVmtO3YTI3J++veMgOo/VuDvIh5JkQNpTTc5CoZly1XR0w+K3s2/9nah4Z Lf4udbLEoNM5yRNDtigxpPqCVFNUr3ddOdXT3J4WRH326Cl1KZVwMCvo/0or/mAXbbJc 7ndDVIb5LaaBTvFcVp9tiGBhnT8zG4zAG+GDR4hka42LtEM9yjtvplacvk12jKQlfcTr riLsiA8iJtP9ZK6LbWmV8IYv+4jyuxHurD+NKQDg9aC7WjCIe4eFpDGjxUL/Kiqzgtwy p9zgdkh9CrE3imotLw33zZnAacTznJz8BMsbPk67PT4MqDz8fy+XGUqTzA4f3LkSg68T nVAA== 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:mime-version :content-transfer-encoding; bh=hpIA+rvc+Ub15DwARGwCPsLQo64idg/OwWT6O3mpE6w=; b=CIiAEmJ2WtjdKxqPP/C6plh49+M225BFKpfEtKxb8IXcoM1Gi6+uvSX+V2Dy2ZL/2u N4Jvx5wDtKQqm/A2GjVyovvBDD2qO6NCFYht5WM5Swg9qql6/hictv+XnZy0N5Al8QJ5 46yg9XWmui8md/UFrR1Q1rnbHXXzeqXgNIDnhTEtxVJNTTpO+p9w0YJtF3diAQ1Dp2EU d0uQ0pmhDzr7Gg5CKqXEJDVlN078fql4AbLLxqN7l1wXV5TsbuEjDjntA9ScS/neJ7H9 fCowougnwvf4RUqIz5ff40TQ49/qFMmxbRPZt/A+QBQ/wmqO8/Jy3GFFKLMtcZS7GZrK /OdA== X-Gm-Message-State: APjAAAX3yM7JYgfJwL1Inghq2H0pgAcropWeLYBFLCDpxmaN63vQO1C7 XyY10VXS+hDtQM0yyo5722mkag== X-Received: by 2002:a0c:d4ba:: with SMTP id u55mr3539945qvh.40.1574269128605; Wed, 20 Nov 2019 08:58:48 -0800 (PST) Received: from localhost ([2620:10d:c091:500::6900]) by smtp.gmail.com with ESMTPSA id 11sm12082528qkv.131.2019.11.20.08.58.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Nov 2019 08:58:47 -0800 (PST) From: Johannes Weiner To: Andrew Morton Cc: Hugh Dickins , Shakeel Butt , Michal Hocko , Alex Shi , Roman Gushchin , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: [PATCH] mm: fix unsafe page -> lruvec lookups with cgroup charge migration Date: Wed, 20 Nov 2019 11:58:47 -0500 Message-Id: <20191120165847.423540-1-hannes@cmpxchg.org> X-Mailer: git-send-email 2.24.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org While reviewing the "per lruvec lru_lock for memcg" series, Hugh and I noticed two places in the existing code where the page -> memcg -> lruvec lookup can result in a use-after-free bug. This affects cgroup1 setups that have charge migration enabled. To pin page->mem_cgroup, callers need to either have the page locked, an exclusive refcount (0), or hold the lru_lock and "own" PageLRU (either ensure it's set, or be the one to hold the page in isolation) to make cgroup migration fail the isolation step. Failure to follow this can result in the page moving out of the memcg and freeing it, along with its lruvecs, while the observer is dereferencing them. 1. isolate_lru_page() calls mem_cgroup_page_lruvec() with the lru_lock held but before testing PageLRU. It doesn't dereference the returned lruvec before testing PageLRU, giving the impression that it might just be safe ordering after all - but mem_cgroup_page_lruvec() itself touches the lruvec to lazily initialize the pgdat back pointer. This one is easy to fix, just move the lookup into the PageLRU branch. 2. pagevec_lru_move_fn() conveniently looks up the lruvec for all the callbacks it might get invoked on. Unfortunately, it's the callbacks that first check PageLRU under the lru_lock, which makes this order equally unsafe as isolate_lru_page(). Remove the lruvec argument from the move callbacks and let them do it inside their PageLRU branches. Reported-by: Hugh Dickins Signed-off-by: Johannes Weiner --- mm/swap.c | 48 +++++++++++++++++++++++++++++------------------- mm/vmscan.c | 8 ++++---- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/mm/swap.c b/mm/swap.c index 5341ae93861f..6b015e9532fb 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -188,12 +188,11 @@ int get_kernel_page(unsigned long start, int write, struct page **pages) EXPORT_SYMBOL_GPL(get_kernel_page); static void pagevec_lru_move_fn(struct pagevec *pvec, - void (*move_fn)(struct page *page, struct lruvec *lruvec, void *arg), + void (*move_fn)(struct page *page, void *arg), void *arg) { int i; struct pglist_data *pgdat = NULL; - struct lruvec *lruvec; unsigned long flags = 0; for (i = 0; i < pagevec_count(pvec); i++) { @@ -207,8 +206,7 @@ static void pagevec_lru_move_fn(struct pagevec *pvec, spin_lock_irqsave(&pgdat->lru_lock, flags); } - lruvec = mem_cgroup_page_lruvec(page, pgdat); - (*move_fn)(page, lruvec, arg); + (*move_fn)(page, arg); } if (pgdat) spin_unlock_irqrestore(&pgdat->lru_lock, flags); @@ -216,12 +214,14 @@ static void pagevec_lru_move_fn(struct pagevec *pvec, pagevec_reinit(pvec); } -static void pagevec_move_tail_fn(struct page *page, struct lruvec *lruvec, - void *arg) +static void pagevec_move_tail_fn(struct page *page, void *arg) { int *pgmoved = arg; if (PageLRU(page) && !PageUnevictable(page)) { + struct lruvec *lruvec; + + lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); del_page_from_lru_list(page, lruvec, page_lru(page)); ClearPageActive(page); add_page_to_lru_list_tail(page, lruvec, page_lru(page)); @@ -272,12 +272,14 @@ static void update_page_reclaim_stat(struct lruvec *lruvec, reclaim_stat->recent_rotated[file]++; } -static void __activate_page(struct page *page, struct lruvec *lruvec, - void *arg) +static void __activate_page(struct page *page, void *arg) { if (PageLRU(page) && !PageActive(page) && !PageUnevictable(page)) { int file = page_is_file_cache(page); int lru = page_lru_base_type(page); + struct lruvec *lruvec; + + lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); del_page_from_lru_list(page, lruvec, lru); SetPageActive(page); @@ -328,7 +330,7 @@ void activate_page(struct page *page) page = compound_head(page); spin_lock_irq(&pgdat->lru_lock); - __activate_page(page, mem_cgroup_page_lruvec(page, pgdat), NULL); + __activate_page(page, NULL); spin_unlock_irq(&pgdat->lru_lock); } #endif @@ -498,9 +500,9 @@ void lru_cache_add_active_or_unevictable(struct page *page, * be write it out by flusher threads as this is much more effective * than the single-page writeout from reclaim. */ -static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec, - void *arg) +static void lru_deactivate_file_fn(struct page *page, void *arg) { + struct lruvec *lruvec; int lru, file; bool active; @@ -518,6 +520,8 @@ static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec, file = page_is_file_cache(page); lru = page_lru_base_type(page); + lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); + del_page_from_lru_list(page, lruvec, lru + active); ClearPageActive(page); ClearPageReferenced(page); @@ -544,12 +548,14 @@ static void lru_deactivate_file_fn(struct page *page, struct lruvec *lruvec, update_page_reclaim_stat(lruvec, file, 0); } -static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec, - void *arg) +static void lru_deactivate_fn(struct page *page, void *arg) { if (PageLRU(page) && PageActive(page) && !PageUnevictable(page)) { int file = page_is_file_cache(page); int lru = page_lru_base_type(page); + struct lruvec *lruvec; + + lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); del_page_from_lru_list(page, lruvec, lru + LRU_ACTIVE); ClearPageActive(page); @@ -561,12 +567,14 @@ static void lru_deactivate_fn(struct page *page, struct lruvec *lruvec, } } -static void lru_lazyfree_fn(struct page *page, struct lruvec *lruvec, - void *arg) +static void lru_lazyfree_fn(struct page *page, void *arg) { if (PageLRU(page) && PageAnon(page) && PageSwapBacked(page) && !PageSwapCache(page) && !PageUnevictable(page)) { bool active = PageActive(page); + struct lruvec *lruvec; + + lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); del_page_from_lru_list(page, lruvec, LRU_INACTIVE_ANON + active); @@ -921,15 +929,17 @@ void lru_add_page_tail(struct page *page, struct page *page_tail, } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ -static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec, - void *arg) +static void __pagevec_lru_add_fn(struct page *page, void *arg) { - enum lru_list lru; int was_unevictable = TestClearPageUnevictable(page); + struct lruvec *lruvec; + enum lru_list lru; VM_BUG_ON_PAGE(PageLRU(page), page); - SetPageLRU(page); + + lruvec = mem_cgroup_page_lruvec(page, page_pgdat(page)); + /* * Page becomes evictable in two ways: * 1) Within LRU lock [munlock_vma_page() and __munlock_pagevec()]. diff --git a/mm/vmscan.c b/mm/vmscan.c index df859b1d583c..3c8b81990146 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1767,15 +1767,15 @@ int isolate_lru_page(struct page *page) if (PageLRU(page)) { pg_data_t *pgdat = page_pgdat(page); - struct lruvec *lruvec; spin_lock_irq(&pgdat->lru_lock); - lruvec = mem_cgroup_page_lruvec(page, pgdat); if (PageLRU(page)) { - int lru = page_lru(page); + struct lruvec *lruvec; + + lruvec = mem_cgroup_page_lruvec(page, pgdat); get_page(page); ClearPageLRU(page); - del_page_from_lru_list(page, lruvec, lru); + del_page_from_lru_list(page, lruvec, page_lru(page)); ret = 0; } spin_unlock_irq(&pgdat->lru_lock); -- 2.24.0