Received: by 10.213.65.68 with SMTP id h4csp2117128imn; Sun, 8 Apr 2018 20:08:08 -0700 (PDT) X-Google-Smtp-Source: AIpwx48e9+9JKKGB//IL3zKHPzy2i+MD4vKaEZhrAR6PqPbkIcyNbfsF8V9qVs4EpzJJ0GOwHsCu X-Received: by 2002:a17:902:51ce:: with SMTP id y72-v6mr36375096plh.157.1523243288000; Sun, 08 Apr 2018 20:08:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523243287; cv=none; d=google.com; s=arc-20160816; b=TAXmzS26icMCK7TXLMtK6y+Hxl6y+PskDMkocbMeXnoqqeYBB4ZdRdAx5LyssOok8p +3t4+6XR0OyPP/Mv/SaQDr2uPMWoFyxqHeu1P0JkngK2Uv12WpieqmTpLqUgUElTysiu EGAadwhmLG2vP/cfgMxWyGwLgBBmkC8Cm0a1vQDvCTFBr6jcqQZghv90D4t8lt06HqpW YUezQPzDsX/8Z1NmNFOdr5gB01gK/cYsEhgH70O5jUbAVgDd7YvhlZyuUe3uJJeSedfk CFBBRzpovREZ4mSO5yaglFIZQEI9FGNatgSmdigUwafG560/K5R2W3DPqMMnZbdi+ltb kP9g== 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=6u55OahYOn2zmjF9cmMN0mwuDiajT2gQXfNRJF1HDmM=; b=HASf6ZR+qLftmN28WjRISU2WYUSLy1VzrM9GhLuZeH6qb0SKxDiHjco4tt6uPhY8BS Tpx3/Jg0puVzO/Uob56pss/84c7GnMK5OKZzH8w/4lwSPmi36xqIFPwY9ANl9AcnKC1I Kk6/Ybp7uTqylhr2lqFERA+J1nLqeqFJhxnXeKBdDIqDysLjVpE0Kln7OoiVaxWNDCvx 1eJZG8cOncY5joxx9A4IP2JcgD7DU0n5MtbejH8gkS//urdZ1c01V0m/GRCecW0gsA3H odEgOk+koG5y+jQlGiyY3F0CE+AsAAEyk8wmOlAHON3Y1bVxewgFezyXmGC1TABTfNUq ZkaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=OmZ5WuPg; 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 d90-v6si13454845pld.515.2018.04.08.20.07.31; Sun, 08 Apr 2018 20:08:07 -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=OmZ5WuPg; 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 S932549AbeDIBsK (ORCPT + 99 others); Sun, 8 Apr 2018 21:48:10 -0400 Received: from mail-bl2nam02on0114.outbound.protection.outlook.com ([104.47.38.114]:2685 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932322AbeDIAeB (ORCPT ); Sun, 8 Apr 2018 20:34:01 -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=6u55OahYOn2zmjF9cmMN0mwuDiajT2gQXfNRJF1HDmM=; b=OmZ5WuPgQt/FUvUh6fuHyD6xaVdoKYfiyGwC0Tr64mZktNb7RRdEDlyPoWrdiwGM46dvOvduckVblsgF90uM+Hu2ETH8KVg2ZeoTKciiNl/sJQkoSOXrqGopN6coQxY//ae+oV+iRQdEAkR5mBUsMSpcvYCcqW3sTqa6XyNUpmo= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB0984.namprd21.prod.outlook.com (52.132.133.30) 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:33:57 +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:33:57 +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.9 262/293] mm: pin address_space before dereferencing it while isolating an LRU page Thread-Topic: [PATCH AUTOSEL for 4.9 262/293] mm: pin address_space before dereferencing it while isolating an LRU page Thread-Index: AQHTz5lhj1H0JiIFD0C/sMrEjAABXQ== Date: Mon, 9 Apr 2018 00:26:19 +0000 Message-ID: <20180409002239.163177-262-alexander.levin@microsoft.com> References: <20180409002239.163177-1-alexander.levin@microsoft.com> In-Reply-To: <20180409002239.163177-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;DM5PR2101MB0984;7:sP2Y6LDjXV4IrpSWdF0NcdmxDqwRA12LL9fBF0ez2cWBHsw19WvqUTlO2wrrlyZt2Cus0k/Q99DnLiAeO/FDNYEg2G+QSIn/5Y0p6pzA6q8Gz3fRWf6YehR6UTEb9/Ij8frAGSkXT3Y5VbggE3E5O/nNsgK2TsVtX5qkmyraZPSndu2gx3GvmxSeVuncvCZjFzdc1mwt4C2fVnof34EY3NC8qbdHFldq6o4RRu2Ceyq7GnMRm2rXVGGZfv2NNFf8;20:DxqMBI4kF/FtfwSd1GCm28a2+UlNgMpvJxWelSYCopW9f9Nc2Fw3Tjsy2JGAVDbt+T6B6wI30QvixQ5Xzj/sMiP832JvAS6RZyAs3T09FruBtXSarjpCJRybH1Psnpsh+/58gJAmf5E4QaCstFBpI6XCiukLA4xwPeezjT77uQk= x-ms-office365-filtering-ht: Tenant X-MS-Office365-Filtering-Correlation-Id: ee1775ba-9d4e-4db5-ef4b-08d59db19514 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB0984; x-ms-traffictypediagnostic: DM5PR2101MB0984: 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:DM5PR2101MB0984;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB0984; x-forefront-prvs: 0637FCE711 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(396003)(39860400002)(366004)(39380400002)(346002)(376002)(199004)(189003)(26005)(99286004)(2906002)(14454004)(97736004)(59450400001)(8676002)(81156014)(81166006)(8936002)(6512007)(106356001)(76176011)(186003)(3280700002)(10090500001)(36756003)(6506007)(66066001)(3660700001)(72206003)(478600001)(6666003)(53936002)(2900100001)(105586002)(102836004)(966005)(2501003)(110136005)(54906003)(5250100002)(486006)(1076002)(25786009)(6116002)(6436002)(11346002)(6486002)(22452003)(4326008)(446003)(7736002)(86362001)(305945005)(2616005)(476003)(6306002)(68736007)(3846002)(86612001)(5660300001)(10290500003)(107886003)(316002)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB0984;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: 57UHByP8S8MJsNC5dPIMULmZWIk0NREW1C7WXvpOuvzFvQOnx3JdKwjrC3brLg8FYVYnLzOY/KkuIsG5YwHZji4i2AZXQDxfIgXiFSxt8AeXuJi3jxCk+bPPFtiviwsDZIkEWv5Iff07CmwE9r2hzKRD1m7IwoDtCEvxT8Fu3YMytcJ5ePpZd+Ee0Q4iG5xAUn9aQrdeHlXMeXyCa6qDfmN/w61SkrcCIuI/e1C7en6tms8QYyoNuGQuM+SXdWoZn3YgmRWX+lM6h9QF2zXI8BEPREwHfzeG5trnQ2qZSJKibDviYD/yri2kWpJ7P6roBX2rj8HfSP8ZepuV3na9H3LVrP1PA9pODwkTPZ4nMaeuejz9075leeWSSB4pGejDUGkLD5pNNDMxl0NSaHAI3rxtWR8+auNubPoOtyz5b50= 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: ee1775ba-9d4e-4db5-ef4b-08d59db19514 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Apr 2018 00:26:19.3632 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0984 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 cdd5c3b5c357..d012c13d96f7 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1374,6 +1374,7 @@ int __isolate_lru_page(struct page *page, isolate_mod= e_t mode) =20 if (PageDirty(page)) { struct address_space *mapping; + bool migrate_dirty; =20 /* ISOLATE_CLEAN means only clean pages */ if (mode & ISOLATE_CLEAN) @@ -1382,10 +1383,19 @@ int __isolate_lru_page(struct page *page, isolate_m= ode_t mode) /* * 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