Received: by 10.213.65.68 with SMTP id h4csp2121480imn; Sun, 8 Apr 2018 20:14:05 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+yOx6jFuGneyirb3vHuIu3n8SBYYc/vTp4lZ1GKj7oFm8RAg9/o5SBD/UaebwOTqwYN6Rn X-Received: by 10.101.97.130 with SMTP id c2mr7359299pgv.219.1523243645528; Sun, 08 Apr 2018 20:14:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523243645; cv=none; d=google.com; s=arc-20160816; b=t2V+s7qX5WOa7rE0eHBXk8lEMv2KuSCFX0dqgK8sWX0k3W3Rfgybh7ixuiZPMS2K1W coGZSB9O24iRkOcITPka0w4O/liiWG1pv6xR4fyxHtlRAqisACrUuQez7aK683pmcmUD fLnyFcJbTQRkYlsOJLrB8aAb9JzQwDrLpHMKnUynqdsLeCmrJGs1M89QzpBGAFHfoKcC YnswsBANliFANlPwTgi+GmK+41rn8yUkZmKUTbL7crOrl8e0ccAykKY2ebpO0OCVGIQZ WtzVEe+d/g4mlxyqpBaMO7GcemuDhGXnNTdGFYOq/Vrbn0Xi+NXQaEZkuZ+UubdJUhTJ DD3w== 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=n9qRUAnadByiU+/I9C5X3tz+XI4BeI2PtKIn59+pdZ0=; b=w+J8iTub9xEtjeNKVbSy7pBvP+z0FDVhQxqKPGzjr1+WnuRuuH67+YNl1OshBSNUaZ Cm+UQRO7Tidi989BRmDXfLWyeuIsxtH7P9Nmj/tN8zxo/I2X/81NHENgy8CFDWk1q3KU +gl/pf1+v/bhaJzmjIEM+y4C9jlHPaeIBoW4KA0TxurnTh7PyTsTbdGi7b4Ob4V7/7ys Qz+kOr+R17IXBExvk95n3UN6tSna/RFm1ypLCCJbxk3JqCSAlrIHkZAOS0HGPiOtRK63 lZnd0Bu0PEFKpafEDuNIxTI+sJMXDlH/JimDeS8oYIsnIatthp0touKWZETwQRqxWk4H hmtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=Q/LPJIZq; 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 o2-v6si12988547plk.264.2018.04.08.20.13.28; Sun, 08 Apr 2018 20:14:05 -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=Q/LPJIZq; 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 S1752810AbeDIDEh (ORCPT + 99 others); Sun, 8 Apr 2018 23:04:37 -0400 Received: from mail-sn1nam02on0136.outbound.protection.outlook.com ([104.47.36.136]:19528 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932122AbeDIAYu (ORCPT ); Sun, 8 Apr 2018 20:24:50 -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=n9qRUAnadByiU+/I9C5X3tz+XI4BeI2PtKIn59+pdZ0=; b=Q/LPJIZqIvPSj+g39IppVxeIc6tNJVpH3A2BLjSu93ICYj2SPzkcXDkAeJ9n87v1mLnOFDOJa9/+F0YCm3kNVBXPqrgvXmcmzBfbjhl1v2Z39J0qeSf4PY1uZ37imo6VknDaSpFCS66+r2O872K/ygzTLBdlgEjhy6IsbtFmVIc= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB0983.namprd21.prod.outlook.com (52.132.133.29) 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:24:44 +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:24:44 +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.14 104/161] mm: pin address_space before dereferencing it while isolating an LRU page Thread-Topic: [PATCH AUTOSEL for 4.14 104/161] mm: pin address_space before dereferencing it while isolating an LRU page Thread-Index: AQHTz5itkDLTllA0qU+lwlqvfrqahw== Date: Mon, 9 Apr 2018 00:21:17 +0000 Message-ID: <20180409001936.162706-104-alexander.levin@microsoft.com> References: <20180409001936.162706-1-alexander.levin@microsoft.com> In-Reply-To: <20180409001936.162706-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;DM5PR2101MB0983;7:yudqWVuPOapyffXTJnR0I6i3ffop99HOSMr1R7FpqujUJhC001OhL0lQQsvcHpAUScrB4j6GqfxSP1RnZPckittiTqClR2nOnOlndazwibvDl3oerRdu7qB+hC9CSdVSlgbmTxvGXvynNdI2RHt67zskQdBU4ugfxmWNSGDWlTH41M+uT47Zz6+8brlz99XsVsX4CycG7pBqH7Xa/gQItiDZV6pTP6ku6//pHyq41hwRE5fYBrmbeeVezQO4lDgb;20:kMDntM0qUbBisGbIFIy08c2evjqs84SN2/PJPoMfwV4zkBAAqobErNxpn8grOXWbO/WpGuoJ8DR3YbGU56BPM1GBXXxXI8fC4QWlIiWdhSy71CTSDaYhQWIPFOpk16Vi26K2hv38DYt4ydcElXZ88zL0+2ImD7V77KikXPPeojc= x-ms-office365-filtering-ht: Tenant X-MS-Office365-Filtering-Correlation-Id: fb841f74-4e17-4676-21b6-08d59db04b6a x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB0983; x-ms-traffictypediagnostic: DM5PR2101MB0983: 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)(8121501046)(5005006)(93006095)(93001095)(3231221)(944501327)(52105095)(3002001)(10201501046)(6055026)(61426038)(61427038)(6041310)(20161123562045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123564045)(20161123560045)(6072148)(201708071742011);SRVR:DM5PR2101MB0983;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB0983; x-forefront-prvs: 0637FCE711 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(396003)(39860400002)(346002)(376002)(39380400002)(366004)(199004)(189003)(478600001)(59450400001)(72206003)(10290500003)(3846002)(102836004)(6506007)(6116002)(25786009)(6436002)(66066001)(99286004)(54906003)(86612001)(6666003)(105586002)(53936002)(76176011)(316002)(486006)(7736002)(22452003)(3660700001)(86362001)(97736004)(106356001)(8936002)(110136005)(5660300001)(11346002)(446003)(81166006)(81156014)(2906002)(8676002)(2900100001)(1076002)(476003)(2616005)(10090500001)(6486002)(107886003)(36756003)(4326008)(966005)(2501003)(68736007)(186003)(305945005)(3280700002)(5250100002)(6306002)(6512007)(26005)(14454004)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB0983;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: fzdgTkfFcuFsnURLgClHDKqwxc3FeGzIVFjVHFOuz+QWbogofSkqV5aWFByyhs/WQYKY/E2NR6LPYoEuzhW8cDw9jY9/xt6FimYUMAblK0bywghLELTlcgcEUQHbCzRkKCW4bk595e9YEjqUli1SZNVAUkIyFT6tEmr/h6hONEet9ikAR3FyJMyH2+aYYlXUvn7BMeDpUwF6eWvYqcoz3agISxcKDCcIwFNF4V/nRNHcyiPdcOmcFejIGw9zWjmRiblxxTITFlL5NGADIG2Z0OoV1TIDSoNp8JRrG01E2ss/4qIezsvD3VDhtGNpZt1GS/sDnd6ALOe39EOxacrP7z2TaAU8colwqpag8gvxYnBGbR2C2XQcxrkoH6NyCm1NM9glc5WHzhwWZRkoGK6VAfqRfK4d8835qtjUcjdwI0s= 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: fb841f74-4e17-4676-21b6-08d59db04b6a X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Apr 2018 00:21:17.4406 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0983 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 a8a3729bfaa9..b3f5e337b64a 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