From patchwork Mon Jun 20 09:05:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12887150 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33B44C43334 for ; Mon, 20 Jun 2022 09:05:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C39636B007E; Mon, 20 Jun 2022 05:05:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BC3548E0006; Mon, 20 Jun 2022 05:05:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A14C98E0005; Mon, 20 Jun 2022 05:05:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8B16B6B007E for ; Mon, 20 Jun 2022 05:05:44 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 671B434306 for ; Mon, 20 Jun 2022 09:05:44 +0000 (UTC) X-FDA: 79598031408.25.7AC38F6 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2048.outbound.protection.outlook.com [40.107.244.48]) by imf02.hostedemail.com (Postfix) with ESMTP id EA5E58009F for ; Mon, 20 Jun 2022 09:05:43 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XYnDaRkIohaRPwFScE9QvAY6XOXlJsuvwMmhQTOlPxkbuNO1eD97pPwUZJPrxtW9qutVpDCF4dbU+EolKpUQraLq10ZtRpcltiTI1O/V9ZuD5D+9fn1k2EfHhW4G6tuKL6Yvy6+MdzSDY9hPmAya3bddpe6uO7YHjTnyPvaVZ0sWm6EAzdIlOSVn5POZzKXDUu0ISd8fhX9qXStxiLCYMePxJkzLg0zGrfSv5YCBwcxotXWMOXubTUPFQoOrBqJnpjY7fvnWePnghzait/0oi4FmfZSEkKCOafYstfZn+qVNx5nB5CWdp2jRRaBkTSjaiFVjoFTV6kEt1uLe5jqWeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=H8nYjpfQtXoJdL5Ibq0k+1DkGQvEsXFbtgCIAHQIrto=; b=FzLHKA/yWKb3S466OTbgSC8oOCSyizQuUj5y+7fZpvPsXD7foJCB21xJ8Rp74bC7fVp5+vm+4fmIkCgFLYTKD6kXiwXev7AeohPkSCYJiHZGjiboVcRKQoQ6N2YbJecfXf8GY+SBDrQS3m4R4TNVwOuhKmGx6H7jTbFk/7244eFOKFTpxXOjsvvgehRAvnNVGx871xKl2KWlOu05+6IvKJce36EwU8HHo9IDKA39ADrw9JGxXWm15BG8GTSTjTF4+z318RvdjDss4c6xmBasOrJcRSTJthAQ/seafQeYzoFeCDzRy8N18AL98Ig6J1lcymm0Krrw6PALlvedj5bxEA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=H8nYjpfQtXoJdL5Ibq0k+1DkGQvEsXFbtgCIAHQIrto=; b=S8oQI0C7hLLJiUHHDIgRMk0k5rbLS8mbXX7MwysboTLX19afMKXN9Bw3hff07O+ECFj9LyQRTyFHGzhmJL2IRzVD0qKleGRjXhXrf5aQGji5GkeK6/LYFafDOy6N83UO36sl/ZGAEQ/yCEkfwbzr5nz2nIR+q2M8jn95gX4M8P6wJ23qJdQSOMJ4g8/zZp46uGlrkX0BznAUKkE8buTbYgmEgJRymBaCKGLhuzRRjuBGK5RbNmCSeDJlDH3x7m7SLX2s1hOQdfJKJ8J+S4FSOO0FsjoQeY2G/A5hiTlgheOWiOWMO7rrUqi56qEU+RmPbKXmgW6Uy5aS5YFuBzN/RQ== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by BL1PR12MB5335.namprd12.prod.outlook.com (2603:10b6:208:317::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5353.14; Mon, 20 Jun 2022 09:05:41 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::59cf:d090:5d26:6e7b]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::59cf:d090:5d26:6e7b%5]) with mapi id 15.20.5353.022; Mon, 20 Jun 2022 09:05:41 +0000 From: Alistair Popple To: akpm@linux-foundation.org Cc: willy@infradead.org, linux-mm@kvack.org, jack@suse.cz, david@fromorbit.com, jhubbard@nvidia.com, Alistair Popple Subject: [PATCH] mm: Fix serialization adding transparent huge pages to page cache Date: Mon, 20 Jun 2022 19:05:36 +1000 Message-Id: <20220620090536.1474002-1-apopple@nvidia.com> X-Mailer: git-send-email 2.35.1 X-ClientProxiedBy: SJ0PR03CA0220.namprd03.prod.outlook.com (2603:10b6:a03:39f::15) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e279a85a-949a-4c6f-56e3-08da529c0d10 X-MS-TrafficTypeDiagnostic: BL1PR12MB5335:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sJFM+ieB4TSY5qIh/VDGimG0Il1m7WSRvCwuenZzPmd9BrLVNP2WXUFIP9VnK2eK7DjBlJoNDkBWMmQP8IQGJFnXbN9NFDddMKcvLtx7QiS+pYNkyqtxdWjwjqLMoXu/hZsL42FIT3LS7wJFRcEGbk8DIg1SIuwKKM6tsNtBTd5HYuzwyeh7PqgYlpiGLS5SHpbfLA8qAx2xVILwgGEK7KNBg/2S0r8lJOcQeIxZxYl8UScAiZwbZkmKL8c5SwslpkwGEv5/NobVLdwYF7F9hOChSgyBcNyoRbinH9nkoqqoUGgaoS9e5l2ZtY0U9Lp0VUWnaYgBKXiszsVotDyMQ749XeuSP6n0IBG24KqUkO9/tj1Yeii/sPc65kloNCgyJ3+E6btVEpUGeLPkUEU3ZC+uvKFRBm650ZJfbh6nnNEILwoTxaYDuD5rXJwSstBVBpO9FKG/Z9Buvsqfne+3ndDEVoIjgv+7TjfFDPvfScNUcJKq0H8lzyrt4x2nDqZfbtZEyTGnf60i1VVjNWyMunxH1bF143TPH7YNkVXj/y/7zt6fGQhsZxfoqHeqqjMdTsiks4+hs9Jt/MDyyP6jUt9WeAncrrCPRc7fTlgTGrqN0aXpglfnuTOJjTKZUtzEbuaCFAru7SiHLnFkiM91HgzExMRrlXzDhBn3XT57NsLXCERe52oWD/aSoZxJhhXSEKgtymzyWsI+vVcG8xBXSA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(4636009)(366004)(376002)(346002)(39860400002)(136003)(396003)(8936002)(66556008)(8676002)(4326008)(66946007)(5660300002)(2616005)(2906002)(6486002)(66476007)(6916009)(478600001)(36756003)(6512007)(316002)(26005)(6506007)(86362001)(1076003)(83380400001)(186003)(41300700001)(6666004)(38100700002)(107886003)(14583001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 4V9fKMPBLwq4sZIdnXZ4d1mH0YChl94o9EWEB215iUglcZKH4ylCl99pkt1Me4yXjVP8hhQB2GXIuT9dyAHELPFGOG+b4p3O08/WjsMCOcxO6nTzHJ8nVTEyJ6M6oYlCSm2aPrTDdm44TPjeLJo04gLwOKtC1aKEDNt8mATFqZe6VJX0Ok7P5ZmbskTDOZkDHWVAG6yhwAVq2MxOX7cfNtNo8rR9aA+NDAiRQUGtCenCbK4vCTiZmdIjAskFKSYup1SCvJezp/AQo/h2xv7TkuM+35sH/HYZf5TRlVQIYqH+K1VY4u7ZM23CDYSjv/rDHtobBv5QC85MkmeEpTtgd3eJftv2viiNtJ0I2fdXFGO+dqIsa8l+0LB56DlzSV58oYv1n7QkjJsdl/c6ECnHxCF1G9n5zoPehIBzCYfaoxNmWQ8mva+rwUedeRAxcCKuJz3CjaUqmrYzbS4YayX6ZrB4EBnpxQPZLx1ivnP/XTqKCUuAWbd8kKB3CKzk6okKNHxmnNJLK7rlyWIaatSx3tzqUUtP0lPoJF39OAUU2Q2oQEnK6A741IrGmain6OAMMnhxB5vFGmlLMyx61MjjyuzPMOnAlHMAcsvQOcwnf265ZnlAhjv1ddG/gxgyJk/YJ00T6OLicKokm62Sr3s7cc8bVsUum/DLt76WtQL52vqxX4gaTGlAAEBQr5yEK16LDiSzCaWmftpwQMxzTx+MWK8bN1nrQrrnrORzjBHFKgAv6Kkt55BVO3DxpAGHte1xf4SXko9bfw4G5Blayr5EljFV3NVRs7bprxbAT5lYiFeEgyb7lCc5FYfWiEDz0vwQnpjK7v6HrdxpHqgPTHBfvoxOsKZUFTK2Rs+6UQnJLhF7l8X1/3SYWV9hEb1nW/QcRyZT0SRt+vEAepTlMiZ//sefRMzA7Z1+lLqpqAQ7c+VV46DINPPQAQaJAflqDU7UtKQYmKM3tqmDgLpoDSDTHPMHrnhQCgcm7Qpqu5y/qgA3hliNPM/W1nKOS+pstOzRiPjYKCQzy3FQoS/e4a2Sd/EzPiUyi5bFxtY/UYvQm1de+FHRc1oDBs1WHMHTDl62BiajpD7csz636qdXR/1YDY1acaie2HnOwhzcyfYmIfw6AWu0ZC3m2//4+ApnGMY8Mzpqt9RVohkJMu7iC2jOlTvDnENeyZuUUmSfhLCjU3/iAwLMNbT8K2NmY6B0r9nDbNGCRUq6Y3vcvMvuNafQmcOe5G7Y5KzUrK94E94pPHolLv11OnTkIgn+EaV28Gt26BUs3jnoEQrf+KAem9LXlxcznxkJsU5n8zBoUzULkyoQK8LwpjVm2FJ3/EU6fwfIgS3tm3c8AS+SR1aXPMZqNrTzO0Qdh3SDcmj7f6+cJ34Y5DZj3ItFMaGd4ODYa9aygpLWnf315HrZur9Ci64tM6sImWMFhK7ahQQ8cpZGxkzKKI3wKvi2zNnG3ixR2woitiY0lNMp63uJWBPheKhPbASMv7wbXo2jEZRlaVTwj71JkOeAt4HmVujCzdjdwoXnxIa66RDuEZIwt/ZTc5DOQeYRrATyIMo7MYD4BGSfcnpEg0QyIBwFEvoSRLB4el1Jx7t+urDNhEdrjoKF2KrdgvBEdqPTQ/bHkq6XtyUsflh25BxJgq8s6z0ZRCWc3X8So3Z9sdWNM3ohUgr2vB79nQJ4q/cPqWGmu80PyNkxs6RB6vPKWacl5ZhIr4GYMqf6s7G0OZHbSa/Kqag6G7QF6Q== X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e279a85a-949a-4c6f-56e3-08da529c0d10 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Jun 2022 09:05:41.5146 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: exp66cU67e5o4j3OeT/2NngIC5H8zSEg30J+yrZiplLKzWLOhF3hVwMBzWRjIi5gpX8WRqR/hYv7jg5QemkVIQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5335 ARC-Authentication-Results: i=2; imf02.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=S8oQI0C7; dmarc=pass (policy=reject) header.from=nvidia.com; spf=none (imf02.hostedemail.com: domain of apopple@nvidia.com has no SPF policy when checking 40.107.244.48) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655715944; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=H8nYjpfQtXoJdL5Ibq0k+1DkGQvEsXFbtgCIAHQIrto=; b=rD1OJbfFxnXxq5ViOq1VPN+2fF9kbBK/kHey6jppCTNpPWcKUlDsVo6SEh9CA+VPAh6u54 /PbFNIc3CPu5dVKoH5IbVz/U6sEbfJjx+1wgIdTD0cULEsqt9Zyiuz5l/M0HNihd8lv9So ji9Ga60AB5rpLbs1DMLgEhRkwAzRRZU= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1655715944; a=rsa-sha256; cv=pass; b=eY0LUF4asScVzLCJ0zpwAMIEbYE1hjfIxDoNbxx30MeqcH9O+2s03v+Lv9SBNJlHOJK7lW uLxN2a052CrSzTuvtWNv/4z7T7ARN+GI6ycvkUIzg1w4xwwdhveZtb0VSYE9MgFKRpYkol FCdGctMs31/1i4RPYBRy0t3j0T52s8k= X-Stat-Signature: 6b3qypx85isxz3hektxkzibipxcqd31t X-Rspamd-Queue-Id: EA5E58009F X-Rspam-User: Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=S8oQI0C7; dmarc=pass (policy=reject) header.from=nvidia.com; spf=none (imf02.hostedemail.com: domain of apopple@nvidia.com has no SPF policy when checking 40.107.244.48) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") X-Rspamd-Server: rspam10 X-HE-Tag: 1655715943-689406 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Commit 793917d997df ("mm/readahead: Add large folio readahead") introduced support for using large folios for filebacked pages if the filesystem supports it. page_cache_ra_order() was introduced to allocate and add these large folios to the page cache. However adding pages to the page cache should be serialized against truncation and hole punching by taking invalidate_lock. Not doing so can lead to data races resulting in stale data getting added to the page cache and marked up-to-date. See commit 730633f0b7f9 ("mm: Protect operations adding pages to page cache with invalidate_lock") for more details. This issue was found by inspection but a testcase revealed it was possible to observe in practice on XFS. Fix this by taking invalidate_lock in page_cache_ra_order(), to mirror what is done for the non-thp case in page_cache_ra_unbounded(). Signed-off-by: Alistair Popple Fixes: 793917d997df ("mm/readahead: Add large folio readahead") Reviewed-by: Jan Kara --- mm/readahead.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/readahead.c b/mm/readahead.c index 4a60cdb64262..38635af5bab7 100644 --- a/mm/readahead.c +++ b/mm/readahead.c @@ -508,6 +508,7 @@ void page_cache_ra_order(struct readahead_control *ractl, new_order--; } + filemap_invalidate_lock_shared(mapping); while (index <= limit) { unsigned int order = new_order; @@ -534,6 +535,7 @@ void page_cache_ra_order(struct readahead_control *ractl, } read_pages(ractl); + filemap_invalidate_unlock_shared(mapping); /* * If there were already pages in the page cache, then we may have