Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2512226imm; Thu, 7 Jun 2018 11:55:23 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIzU2/T31IESkayl2rTWskPVOJS3vifVbXN0Wu9e3cfBS9B6rcSVrwW0hmgOy+kVwxp2ggK X-Received: by 2002:a63:6096:: with SMTP id u144-v6mr2513982pgb.433.1528397723537; Thu, 07 Jun 2018 11:55:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528397723; cv=none; d=google.com; s=arc-20160816; b=tiXU3JUh56suHOZaTNZ+/q/Tew3yvr+33zhPwouNpH+Yzt3tSXO5WDU5PiOTavgQHo Cz9fGD56yJk6GG6FjYrbExgfETUneFJOtoOm/h0V0S9aIo4L7fXn/QWkDdQ7CAnLFWcG 0lbkRLh4w+A9TppcTG8BxAvP0/EzgKuAKMppeRAyyqNzLnnWVHTNWDWQNv3DSvQNee0j WIW2jnkZ/O0/MZRMuKVAvGYXKVbJxRprr9dRhUBXxJwLMwRxQYBdwqrm9YuyrMWqVUnN g7dNcJbzEsudZxAJ/iPjSE5OVGCMLZ64opoy9cLuDUn8WJ0D4/U8sT6FwVeR27mxIJ4q rkNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=YTuJHOSh8xNrPjIrjwg9qvrcZIaJp8HuC1TNYS6ft8g=; b=1GwYxcLtHs+R+uMa1l5PUUiFSAeLyhwPP99uwhH8ClAPyaeGqQZDCS6InlDSWllWZt /rkRStIFevO6DQHJcIs5uM/q6jepegpemFTBEzLlEa4ppz5TPq59nH8SqJfl6ATtaczk DrNofohyQjWaciuizvRY0Z+IpMNKdleUNY7DBREYXTPhHzo0aPAPR29YOawCFyc+/dQ7 XChVycm52PqdlE6esWIkke0DfodD8fV/DE723aNR11xZfwPBBbvE/bAhppc6MQpDb8dv NODSEUaV/CRrkSwKAiF0JkVeFdIs6aiYPk5wjftuWDGCH8NHBPs5IZR+FQBhUmjBwUWo 4AGg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b34-v6si55358647pld.272.2018.06.07.11.55.09; Thu, 07 Jun 2018 11:55:23 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934414AbeFGPuf (ORCPT + 99 others); Thu, 7 Jun 2018 11:50:35 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:40524 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933775AbeFGOj5 (ORCPT ); Thu, 7 Jun 2018 10:39:57 -0400 Received: from [148.252.241.226] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1fQvbj-0005Zo-9d; Thu, 07 Jun 2018 15:09:43 +0100 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1fQvb5-0002y5-1p; Thu, 07 Jun 2018 15:09:03 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Linus Torvalds" , "Mel Gorman" , "Jan Kara" , "Minchan Kim" , "Huang, Ying" Date: Thu, 07 Jun 2018 15:05:21 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 183/410] mm: pin address_space before dereferencing it while isolating an LRU page In-Reply-To: X-SA-Exim-Connect-IP: 148.252.241.226 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.57-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Mel Gorman commit 69d763fc6d3aee787a3e8c8c35092b4f4960fa5d upstream. Minchan Kim asked the following question -- what locks protects address_space destroying when race happens between inode trauncation and __isolate_lru_page? Jan Kara clarified by describing the race as follows CPU1 CPU2 truncate(inode) __isolate_lru_page() ... truncate_inode_page(mapping, page); delete_from_page_cache(page) spin_lock_irqsave(&mapping->tree_lock, flags); __delete_from_page_cache(page, NULL) page_cache_tree_delete(..) ... mapping = page_mapping(page); page->mapping = NULL; ... spin_unlock_irqrestore(&mapping->tree_lock, flags); page_cache_free_page(mapping, page) put_page(page) if (put_page_testzero(page)) -> false - inode now has no pages and can be freed including embedded address_space if (mapping && !mapping->a_ops->migratepage) - we've dereferenced mapping which is potentially already free. The race is theoretically possible but unlikely. Before the delete_from_page_cache, truncate_cleanup_page is called so the page is likely to be !PageDirty or PageWriteback which gets skipped by the only caller that checks the mappping in __isolate_lru_page. Even if the race occurs, a substantial amount of work has to happen during a tiny window with no preemption but it could potentially be done using a virtual machine to artifically slow one CPU or halt it during the critical window. This patch should eliminate the race with truncation by try-locking the page before derefencing mapping and aborting if the lock was not acquired. There was a suggestion from Huang Ying to use RCU as a side-effect to prevent mapping being freed. However, I do not like the solution as it's an unconventional means of preserving a mapping and it's not a context where rcu_read_lock is obviously protecting rcu data. Link: http://lkml.kernel.org/r/20180104102512.2qos3h5vqzeisrek@techsingularity.net Fixes: c82449352854 ("mm: compaction: make isolate_lru_page() filter-aware again") Signed-off-by: Mel Gorman Acked-by: Minchan Kim Cc: "Huang, Ying" Cc: Jan Kara Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds [bwh: Backported to 3.16: adjust context] Signed-off-by: Ben Hutchings --- mm/vmscan.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1206,6 +1206,7 @@ int __isolate_lru_page(struct page *page if (PageDirty(page)) { struct address_space *mapping; + bool migrate_dirty; /* ISOLATE_CLEAN means only clean pages */ if (mode & ISOLATE_CLEAN) @@ -1214,10 +1215,19 @@ int __isolate_lru_page(struct page *page /* * Only pages without mappings or that have a * ->migratepage callback are possible to migrate - * without blocking + * without blocking. However, we can be racing with + * truncation so it's necessary to lock the page + * to stabilise the mapping as truncation holds + * the page lock until after the page is removed + * from the page cache. */ + if (!trylock_page(page)) + return ret; + mapping = page_mapping(page); - if (mapping && !mapping->a_ops->migratepage) + migrate_dirty = mapping && mapping->a_ops->migratepage; + unlock_page(page); + if (!migrate_dirty) return ret; } }