Received: by 10.213.65.68 with SMTP id h4csp2145360imn; Sun, 8 Apr 2018 20:53:38 -0700 (PDT) X-Google-Smtp-Source: AIpwx48AzNUxiM1Rk0+qV8O+eKG6zvVTpsUIh6RQYdUlDu70Zof5jt6wJKiBL0aQfTiVoqeSamVZ X-Received: by 10.99.126.71 with SMTP id o7mr15048562pgn.366.1523246018068; Sun, 08 Apr 2018 20:53:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523246018; cv=none; d=google.com; s=arc-20160816; b=M+1M1qLXQnEKWXiR0WV77CafREaA/bK48hhsAgLbEqxBgI5+tURwvXC7N7pscfMWIQ ATlSB3N6byHDx2Pm8V9EPmvp3826xWrEFO30gmRlLeMsZWZXb+NF+punl7Y5cUBKftVs wtW6UgVimIyfZQiaPST2eI6SH446eO/nYrpPGKjvguJfkEQdXFyQvVSWEOxqjkP+Z5rH k0ttx4sAkgFCjj23+RVFuvEQ8t8TwdbQWs08TTQ8nM/YxIOEl1OyhN/pdfIIUvR+T9/e iOfFYNJ/zEsJzZ0K33FDTEa9+Xyi+F8Xt1GviFoWivagP4ko3bq4I3f8GUFpNOiHNCZL h0EA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=T/1guFsy+CQEO3UEFOK+J5u/RJbii1q/ioxyV5XXBvQ=; b=pYCLvkbFdsLfaxuh074NgY8Ak91u/u32O0pCWgzDCKQf0u+jwt9Lhm19iHzHQi5d+J 0BFZ4hWSh53m4KII1FOvSkP7VKmIpJBr4JcYq5CR+OEmQIlBsdhmIOjOVFjwYa32S0Th xmnGf/C4nvwekSPEIwV9G4Xvt/oZfqCUXv8fHOBiW7uCfjKkQ4E3yiT+9lf5LCNZNw7v CK5hK5j0e+LdOXZlIWHcNSrYvqDn8cQgaMbvQy0N2rGIdikX+eNnlwkVYkxeiS3fwcoh AyASu1UsuGBT+mWa43MCcmjFQv5sizr75/uNz4Pq96OwW3SM76kqeISHSZi1/EDAfc3l b4hQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=NO2vZFYE; 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=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w13-v6si14031826plp.149.2018.04.08.20.53.01; Sun, 08 Apr 2018 20:53:38 -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; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=NO2vZFYE; 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=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754277AbeDIDsH (ORCPT + 99 others); Sun, 8 Apr 2018 23:48:07 -0400 Received: from mail-bn3nam01on0125.outbound.protection.outlook.com ([104.47.33.125]:5792 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754086AbeDIAUI (ORCPT ); Sun, 8 Apr 2018 20:20:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=T/1guFsy+CQEO3UEFOK+J5u/RJbii1q/ioxyV5XXBvQ=; b=NO2vZFYEsYK2H3KPbcURS0gd1thQ726mlkInvNJ6WhaKuPfCosEFHJPKdMeIuiQ+dHX6VqLaor3IZpRUbI5aM/oQcfPPtaJzqPJQ8UUI6DGVyJbN831vCEbV+YaEGXaih7M/B1IzFRrrdKDlPjWR6neUuBqVryWt/EA9qkRQblQ= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB0726.namprd21.prod.outlook.com (10.167.108.39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.696.0; Mon, 9 Apr 2018 00:20:05 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059%2]) with mapi id 15.20.0696.003; Mon, 9 Apr 2018 00:20:05 +0000 From: Sasha Levin To: "stable@vger.kernel.org" , "linux-kernel@vger.kernel.org" CC: Mel Gorman , "Huang, Ying" , Jan Kara , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH AUTOSEL for 4.15 124/189] mm: pin address_space before dereferencing it while isolating an LRU page Thread-Topic: [PATCH AUTOSEL for 4.15 124/189] mm: pin address_space before dereferencing it while isolating an LRU page Thread-Index: AQHTz5hHF2F1cF1ajkm/qzD0Z3NMUQ== Date: Mon, 9 Apr 2018 00:18:26 +0000 Message-ID: <20180409001637.162453-124-alexander.levin@microsoft.com> References: <20180409001637.162453-1-alexander.levin@microsoft.com> In-Reply-To: <20180409001637.162453-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB0726;7:dAvB+CXTLDIWQ5q2Yklj5GCkMMY31gATEgqKGJ6stwPJUNH9BZgIcWJecm/NOOyn5eVptbb4si001ieqEtLJC7t9OVHn1S/WHdQWQTO/9N6N/ugkaV9rW6SRKhWIwqZl2kk2JcJaVeeCOiDfKhja9fWisxdVEAt2X6NwIR7npOpxDtvXAfUVryfvQWLcUZUgOzU7kNGQcPeQodlpT5HH0cOmKCupLEmbYTnYQBoQHAHfC1+NSybt7lIff2gZpVu7;20:LyPk13G1DSvVWmu7NqiHp6ttktIAwB2YfDD/wUu6qbj02hXLe1EivmbKvEk3c4HOfUIcYmLr2POCuZHAm5OwrTfieLQhbKLvyddyxuYkCmNzYLr/aOV28LPzJsRmT+OQ2zm4vTbxEi/BpxaUUzVZEswJJlq2m3OiTivOkO4i1vA= x-ms-office365-filtering-ht: Tenant X-MS-Office365-Filtering-Correlation-Id: 9eb5ef80-cd3f-43a6-93ad-08d59dafa4cf x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB0726; x-ms-traffictypediagnostic: DM5PR2101MB0726: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(42068640409301)(228905959029699); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040522)(2401047)(5005006)(8121501046)(93006095)(93001095)(3231221)(944501327)(52105095)(3002001)(10201501046)(6055026)(61426038)(61427038)(6041310)(20161123558120)(20161123562045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(6072148)(201708071742011);SRVR:DM5PR2101MB0726;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB0726; x-forefront-prvs: 0637FCE711 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(366004)(346002)(39380400002)(39860400002)(396003)(376002)(199004)(189003)(6306002)(6486002)(26005)(6512007)(4326008)(81166006)(6666003)(6436002)(81156014)(8676002)(97736004)(186003)(36756003)(72206003)(53936002)(107886003)(2906002)(966005)(86362001)(76176011)(110136005)(305945005)(10090500001)(486006)(3660700001)(102836004)(5660300001)(14454004)(476003)(86612001)(446003)(99286004)(68736007)(66066001)(11346002)(54906003)(2616005)(1076002)(7736002)(5250100002)(25786009)(8936002)(478600001)(2900100001)(2501003)(3280700002)(105586002)(59450400001)(316002)(106356001)(22452003)(6506007)(3846002)(10290500003)(6116002)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB0726;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: E3ig9WAKeub09PYHy2VDXucGrz1OGZjHdTfcFkYwr8oFp3nyaE6dPDmX2EY4oSh0chfoVAn9a7fnlAUimtvXygMCwUi49t4m8zZvroRwuat/0ShaLxD8UkxHQFMAj6PMCxoOHo00VuM4zqkI3Wgy4UF5543iO403EOFZKrZ4M65YdATtnphrqO6Dc1UERRGagpaE9U6fmpuJC3I9OD+3pCg1mdse1/Oax+7SqAhs6WDoXUoDLdeOnp2pQAM/Gky/b7ZDR6/qfo1psPLHJlBP8pKyZAXOBVPfiVY9wMBj3FM4W1Lce+ic2q26n1dS6diIEiFVacfgDJnnYc/Ng66bZe1GuUahn3r9C/SYl1YNNxt3Tn3czNOvkWtinEE4CaLC5t64NTOxNvF32lfMW+A7LBiCTZOgsocRDPbIYBEQurc= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9eb5ef80-cd3f-43a6-93ad-08d59dafa4cf X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Apr 2018 00:18:26.1291 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0726 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mel Gorman [ Upstream commit 69d763fc6d3aee787a3e8c8c35092b4f4960fa5d ] 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 =3D page_mapping(= page); page->mapping =3D 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@techsingular= ity.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 Signed-off-by: Sasha Levin --- mm/vmscan.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 503fa224c6b6..ff9066d096c1 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1436,14 +1436,24 @@ int __isolate_lru_page(struct page *page, isolate_m= ode_t mode) =20 if (PageDirty(page)) { struct address_space *mapping; + bool migrate_dirty; =20 /* * 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 =3D page_mapping(page); - if (mapping && !mapping->a_ops->migratepage) + migrate_dirty =3D mapping && mapping->a_ops->migratepage; + unlock_page(page); + if (!migrate_dirty) return ret; } } --=20 2.15.1