From patchwork Wed Aug 24 03:03:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12952814 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 A02F8C32792 for ; Wed, 24 Aug 2022 03:03:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CDB12940008; Tue, 23 Aug 2022 23:03:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C890E940007; Tue, 23 Aug 2022 23:03:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ADC06940008; Tue, 23 Aug 2022 23:03:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9EA3F940007 for ; Tue, 23 Aug 2022 23:03:47 -0400 (EDT) Received: from smtpin31.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 67BB6140AD7 for ; Wed, 24 Aug 2022 03:03:47 +0000 (UTC) X-FDA: 79832991294.31.33EC350 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2058.outbound.protection.outlook.com [40.107.237.58]) by imf19.hostedemail.com (Postfix) with ESMTP id ECE4B1A000B for ; Wed, 24 Aug 2022 03:03:45 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AdW0DkYsEOIg77//mxJwQGddBK6wMqquM2o/CFUVKcyovVCKw1ARNChirp2hnFEpb1rUf6xmAJKky4vD6vFb6MT95gqB/k89UnSJ3JhFh+puXuHVlNlw+e/TCSvBKuWolooY+5gRubrEtYWdWHC5dVpgx6GTDN20uQui0DqskhwNvSSLXkLHpNOx5kMeKr8/MHOonrZOxwHmaVMnGz/FYHNzVTmvu9goBoIMvsFZr75Llc2mvhYIyK0HIZUzTqGtKZpYnYVhkUT1GSROA0p5wejrQPQYeG4OleC6/ScVaSDeVyzAsTFX3eIaDrapVgoJyifd9rWOIhXdaRU8N1HG5A== 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=xHYL5xlQbzG7KP6atpHLxTbv06P0UZ7cEpmWgty0z1Y=; b=FOTkkOLFM275t6xyOQR6Zj4l2VwPJhw29SDc6ZaBryDQXJRiOqt0x+6cbgJ4xyVFMCp1oXzo2LuPFTKyHkOpQi2o0XtztIz6jXExkS0chi1rtd7bsm+mgde4eoprmXHXclF8S1rBnKuT/ZWedTV9b4UPgp2McJQ8uBLOBGE9/U7PETG9wIEoYAbiru++Lib6c3iApit/yFPDCbqIzr24VAi9LOtulLTMWKUNgwVjzmRrXo4qyAqWjX6oXHU6FjQZesNXh+5x2JaE7BmMCKZNXkI1SiGPWAlqycUTOsNqQGMJUe4oEwDFnR/NLunnvU9CB9WvvFoHkFJpPTf7G0NMNA== 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=xHYL5xlQbzG7KP6atpHLxTbv06P0UZ7cEpmWgty0z1Y=; b=siXFdNoAoPsDHz4/7EAKtkQumGPTA5QOYrSwakiDAH01+kXjOef0QmrQ5duBoUvpbPIu9FP33xmhMIMmq6SLLFv0VbWIurogWwRhW+glN6zk3yGeOfNx5VzwOlzf1Jkgf6oHL2wbFpMSLvwNcEhWYhV/Na7H6JWHabqPYPLLLXM0WCAo2wlWtfs1OBamCXd1puGDvVajtnYstfnI1PyTMAHRnU2J9po88Xqz19XVnFhfvGmbgy3HqHOtwtpd3WjGiUZGp5CDQ7e8K+o0tFF4IFPrgSrJ0efgoRIcVMpxSccgtS1VJpDZDvC8y7Zbz50fk9H1XhIysdGX/EfIn89N3A== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by LV2PR12MB5774.namprd12.prod.outlook.com (2603:10b6:408:17a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5546.23; Wed, 24 Aug 2022 03:03:44 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::7432:2749:aa27:722c]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::7432:2749:aa27:722c%7]) with mapi id 15.20.5546.022; Wed, 24 Aug 2022 03:03:43 +0000 From: Alistair Popple To: linux-mm@kvack.org, akpm@linux-foundation.org Cc: Peter Xu , Nadav Amit , Alistair Popple , huang ying , LKML , "Sierra Guiza, Alejandro (Alex)" , Felix Kuehling , Jason Gunthorpe , John Hubbard , David Hildenbrand , Ralph Campbell , Matthew Wilcox , Karol Herbst , Lyude Paul , Ben Skeggs , Logan Gunthorpe , paulus@ozlabs.org, linuxppc-dev@lists.ozlabs.org, stable@vger.kernel.org Subject: [PATCH v3 1/3] mm/migrate_device.c: Flush TLB while holding PTL Date: Wed, 24 Aug 2022 13:03:37 +1000 Message-Id: <3b01af093515ce2960ac39bb16ff77473150d179.1661309831.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.35.1 X-ClientProxiedBy: BYAPR01CA0051.prod.exchangelabs.com (2603:10b6:a03:94::28) 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: 08f87d09-fb37-4ef0-77b5-08da857d4121 X-MS-TrafficTypeDiagnostic: LV2PR12MB5774:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: d1bLfB3VMOtxkzQTyYajEB5kio3ZmMlhvFfUilRhhrts4Bg1hLOkzBW+EEJN6wqJlIvngisgZLrS6bRPOA14ng+82v+Hvop3/a3dOugZF2Ned9Ydrtm6vp5u10Pox038ujo9NPzxLBfzh0O0FD7Et0tF2+kM23pFZSHvVodqlXA+lbu1/umu42Ufjslvz5o4VgxX0dea0pVi33vBZXIQHinEg1Wr2HKf48N2ax098hMRmNqtELlMK6KYobjvAN7KASx8hSHakj11Q9n/TZXiE1i7EM7JpTeToid4hOaPTb+Nku87Rlb59jyAnmJP2adMW9/eiJ6B4Wd3PjkEf9R/2s/+XiNDPsBG/VZFammbBWi8PJi798hKa1FZDz4tEoEpHp7UXSR+3kzJyRf+eTlQpYgvtSDKCCwhramFkbn9ZibjxOqQORAgn09umDfVt2kKJ8AK6/qQ1c7HgQo7IvCKOZEqig8MEHVonMqB/5xRS7TVNcoJZFzncbu74PxqS8EW7Q2s+uCSiIdb0eDMNjlDY+KAaDZNGGH5YZlEPFxg2Bc+15paOVd2aYoS19/e01CXqCFoHm9FNIq71GysLWlY95oXHL9iWtHA3uildg2Fl5Z/+uTBpUhOW0PKl9WY/IKwWCLxDP5glpQ5l2oVVXhHFfKJk3eRVSc+T0KJIf3gmsVndW0b5a71Q9UnAZNO3w3OgSZhAwIIV3B0r7TUxMvvvQ== 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)(136003)(396003)(346002)(39860400002)(83380400001)(478600001)(66556008)(66476007)(66946007)(2616005)(4326008)(186003)(8676002)(36756003)(8936002)(41300700001)(6666004)(6512007)(2906002)(6486002)(6506007)(26005)(86362001)(7416002)(5660300002)(316002)(54906003)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Keez3Uw1PgtXxZMrJrj4m3GtkuiTQwbvwlstuOSRhPhqgFqdPB2sgTl954/QgW+jG9b1+39MF4z5face1WanZKd9YxebTgRxa8YJsGuwLDGlt0ZVjEVCzn627aY7iRiiHndp22teRBXXQgKgfLHeMlvq3GLon5tlPSzbwpr3rEPz4vlyUDRcie4PyXMoYNNKxdxjoQceruNXAzJ/HUrrir98+A16/nRlDui0lvgirSrFsG2Mx92M5FnhYTN1bCGVPJsKc4jRkzvIj34RiiMyIXjFhBR4dL9qHbBGhmb0Pnf3gcQ45DZv4i9i6F1XjWUs7wQ94EYA3pW86T5ZfWE5L1JI14sHdAwa+eGgWZyhdZPiDnm9fdSbxTEvgrHPDf4Bwiu+jGX8dr7A79gtl8AAtQQkopj4r5V7D5TSf5JRE66mHXWefgD+MiTChMRt96suko6WuUqV/F4Y90GtdeVaE6Uy7x5s/vhYFKO6BDOPrZLg+lZRI4oTxHAZJttUN0ymXQuQbgbqXVYuqzRfXNWKnr7K37Fqgugm4NjHW0s7RcoS5h6pRlur+Joo+F19mWJ4l5p5HHtlONZYz5NSTYbPDfsFm0TOLjZg+D+7kYtm+A4Q0LcQp3IcwPD480wbfB/KwVq0TAR2TiCJz5MYSZyu9wWPUqlZaj8/JoDTOPTckw/OkbPKUckP46dF52uzCZjS7QzvNBPAPBNB4cyULwAFu5JLHJ39oJgT3u8iCXBzMMqYSyxJi2dibwMkTrR3Fgt0EWazNpOW+QSIXt9IgN91Aje5/cp+rawkpMdH0tx/8Ty3h+guXHCJozLN6bJFRAyOFwAPSL9eMombugePL4gULN+vzXCY+pPYapKufNVA2C2d7qAJNTothaTUapwjk5NEhV/U9A4hM+4jPtwYL1cBpsW4WCLS+cDKOoQ/Ws5kkHMy+aXWhYg71F7Q8AALRwdHMO1UTt+3qzt0RhEdts/k76uezmRcUy8wbAnO5u+ryWHGpru8dnPjZ20KCiocw+9JovRUUxlqTNoL9m8Qb7XsdWs3NAgzVCdbttADoE7iQBI0ko5XYOB0KFz6kj4731Yrq5lOdeSpHDJVLY2vyA2TbPuN/FymxAYtpF8KXTWDG0vBxe1bJLbaqUEOzhMyNNZ5IE1UKCkxtml0JfC7Lc5QxLg1XY+9A70qckYJIyAS65HfV2L1tivg5usP/b1w9JlbIar2CiXOQ3ZKPt6q+SF77y/L35MElrVMGsaoztPlNLJvXI111+319ms/oUpiRoP67MYsNbAgEHNhQzN+Hr4XvV5NxKGXJVDqlw8OsVqDadFZokOeBgog+HIKwF3fzJ6rcTRjDaPu4iS6pl4LjnDGuT/qWcMMLsjnYJUJ190hqkcS/p10xzyvp0VnNMcQjkA39672Y1GOXWZTXkLboXG0Lyn6E9F6srgrkh1jnNVfxGi0jBoPb8nJULIC5yDP2BFEJYzGL6mYaDmm93Cczy+8bPu7jqav82zm/VFipv6VpoGS5inR7kSB3qTyRggVH330Sy0ymEND6DPUw/2aBIm3vThMECkvw5CVz8JeHwh8Z6a3yDmV6GZx/73oHlRX87XU X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 08f87d09-fb37-4ef0-77b5-08da857d4121 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Aug 2022 03:03:43.8652 (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: Ou4Q8qPVeCW9mvvbVbeOjjoXhRH3C4R2GIBiRywMYu+XdC3hx3W+k5SnZ8J9j486RlLlsqGZh4JiqckMLQwMag== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5774 ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=siXFdNoA; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf19.hostedemail.com: domain of apopple@nvidia.com designates 40.107.237.58 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1661310226; a=rsa-sha256; cv=pass; b=2XmyoRXVdOtM8rEnOtJJvZM9BUjZBZNAobqDX2zBhtNWfpbxZT3H6N1PzAaNYr7g09UOZJ SBc7ZEBP24B2HNUb9g6PRCt+rSamGmdwSr5AbdFG45ed9kkfwZpz4K6YfSGR3b7HF2QBc7 YtWKz1gy66Wgzptr6GXniXbqEkiaXOc= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661310226; 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=xHYL5xlQbzG7KP6atpHLxTbv06P0UZ7cEpmWgty0z1Y=; b=1JdHDc4ROGd+t1s8+1LgbvdUW8LK3QkVvhm4xp9805pl0UuZQONaUahkEkpcxT7Pzw+iux AWug2W8LjyrtxNqjXfIfiy9PDBPyfjObyrX/lOfVMHrHaHw92AbN5DUIJGV8cb7YPygURC pIcd4qzLUJAMX/Uk1BXDCV/myAJJpmA= X-Stat-Signature: q494rhgmoser65b611rhyqkkh4ppsrms X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: ECE4B1A000B Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=siXFdNoA; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf19.hostedemail.com: domain of apopple@nvidia.com designates 40.107.237.58 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com X-HE-Tag: 1661310225-714962 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: When clearing a PTE the TLB should be flushed whilst still holding the PTL to avoid a potential race with madvise/munmap/etc. For example consider the following sequence: CPU0 CPU1 ---- ---- migrate_vma_collect_pmd() pte_unmap_unlock() madvise(MADV_DONTNEED) -> zap_pte_range() pte_offset_map_lock() [ PTE not present, TLB not flushed ] pte_unmap_unlock() [ page is still accessible via stale TLB ] flush_tlb_range() In this case the page may still be accessed via the stale TLB entry after madvise returns. Fix this by flushing the TLB while holding the PTL. Signed-off-by: Alistair Popple Reported-by: Nadav Amit Fixes: 8c3328f1f36a ("mm/migrate: migrate_vma() unmap page from vma while collecting pages") Cc: stable@vger.kernel.org Acked-by: David Hildenbrand Reviewed-by: "Huang, Ying" --- Changes for v3: - New for v3 --- mm/migrate_device.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) base-commit: ffcf9c5700e49c0aee42dcba9a12ba21338e8136 diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 27fb37d..6a5ef9f 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -254,13 +254,14 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, migrate->dst[migrate->npages] = 0; migrate->src[migrate->npages++] = mpfn; } - arch_leave_lazy_mmu_mode(); - pte_unmap_unlock(ptep - 1, ptl); /* Only flush the TLB if we actually modified any entries */ if (unmapped) flush_tlb_range(walk->vma, start, end); + arch_leave_lazy_mmu_mode(); + pte_unmap_unlock(ptep - 1, ptl); + return 0; } From patchwork Wed Aug 24 03:03:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12952815 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 62DC8C32793 for ; Wed, 24 Aug 2022 03:03:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B2F04940009; Tue, 23 Aug 2022 23:03:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ADBB9940007; Tue, 23 Aug 2022 23:03:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8E025940009; Tue, 23 Aug 2022 23:03:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 813C4940007 for ; Tue, 23 Aug 2022 23:03:49 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 59D061A0B2A for ; Wed, 24 Aug 2022 03:03:49 +0000 (UTC) X-FDA: 79832991378.14.5356A6F Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2043.outbound.protection.outlook.com [40.107.237.43]) by imf24.hostedemail.com (Postfix) with ESMTP id E2DAA180060 for ; Wed, 24 Aug 2022 03:03:48 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lvrhnmJ6FNTPQbCslVVU2j/0iw+JUFf4gWQsVHCsNQeTH/xTGO6qf98I+pryxoKxx9eYoKoXX6q98HMaDAu1mPa6l2GVzpaNk5wVzGwp3y+1rZvIq3LaG6YCY3k/3sPdAFUDBfONdnl/O7oCHRbmprGWfos9lC7PVXeaawOOJ/mQVIksAceZdygeDVotA+sR5StVhMqptamoz/EiDd8qZ0dxiF1NbAzh8lKHGJ5e8ysfwoMGxX9VCSdo7YIq+0wwmjYy+GfMHWhZp01gPimfSjI6zqv3iId51fXLLIMwwk8zhZlx0HlUQoidUrONFFOJgXA28MXdubDf0yw0KENz/Q== 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=bbxR+OCYU2G/04rFdWWgpbEcYTHFk46Ar6Hr4DZBQf4=; b=H1YQUgvxNBOG2c82Ai3GhdJk/c5GtrLEOO1d/sfqW1PA1BYpWa9IVYjVSJtprJUwNxNxySEvp5HzSWZBvBJWlKek/2i0NQu61eZnI55PnhIgC1DVSAFy+t4bxBAAAqdmKjYRqEKVw0ue36Fq2Y6VTso8wR5kJkI9dlHafG6eJAmBgQgFzGdtCpPv5nma8Fc/0n1AcwMYfGi+9EpGXij3NA1FGkGCy4gLKEvq1BxGBpsmmDMVAsG0LOihlul4VdjmyzJfXeLVwa5gAcFbWxMWwSTC1EZ3DRb/42IdNcnI2VjFO0e/yNJDMH8RjsFk72tDqV6kGzJcQxVsO3HCJzkp2Q== 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=bbxR+OCYU2G/04rFdWWgpbEcYTHFk46Ar6Hr4DZBQf4=; b=gEvu/Dl56U9nMwHvmxxVdo3BXjnlGBXHhgWiGa5FuV1/7jym1emr8qFAt4Cxnbn8YL2eL2SgAoNTI0w9cZt9DzjyhYOzfh1c7aoJezhidx4GcSrzMU966GyN1a+PUi1sYONxcuCWyT8Cr1Aqf+cGVoAOs0LQEdEY7IVu1HwIrZTBShHzTwgwbOC0KQECMnR+E0g9b2oqo6ydMUez9lwY9gm7USCJCQMGWC0PSJuRMsCaZjDQXqZ+jdCeiSDcJXhN84J2V+L3ytPVIhDFX4/+XDu+ee0NcOKb9QW4/q7NStg/DmHKxJv/G4Ufs8EBbtBbktilTEKJcELX3lx4A660RQ== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by LV2PR12MB5774.namprd12.prod.outlook.com (2603:10b6:408:17a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5546.23; Wed, 24 Aug 2022 03:03:47 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::7432:2749:aa27:722c]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::7432:2749:aa27:722c%7]) with mapi id 15.20.5546.022; Wed, 24 Aug 2022 03:03:47 +0000 From: Alistair Popple To: linux-mm@kvack.org, akpm@linux-foundation.org Cc: Peter Xu , Nadav Amit , Alistair Popple , huang ying , LKML , "Sierra Guiza, Alejandro (Alex)" , Felix Kuehling , Jason Gunthorpe , John Hubbard , David Hildenbrand , Ralph Campbell , Matthew Wilcox , Karol Herbst , Lyude Paul , Ben Skeggs , Logan Gunthorpe , paulus@ozlabs.org, linuxppc-dev@lists.ozlabs.org, stable@vger.kernel.org, Huang Ying Subject: [PATCH v3 2/3] mm/migrate_device.c: Copy pte dirty bit to page Date: Wed, 24 Aug 2022 13:03:38 +1000 Message-Id: X-Mailer: git-send-email 2.35.1 In-Reply-To: <3b01af093515ce2960ac39bb16ff77473150d179.1661309831.git-series.apopple@nvidia.com> References: <3b01af093515ce2960ac39bb16ff77473150d179.1661309831.git-series.apopple@nvidia.com> X-ClientProxiedBy: SJ0PR03CA0032.namprd03.prod.outlook.com (2603:10b6:a03:33e::7) 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: 3ccdb7f2-72f7-4c0f-301f-08da857d430a X-MS-TrafficTypeDiagnostic: LV2PR12MB5774:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MZWgOnCDZpyEhnPPQeuL5XCEWKrcOcEDEARbMQf1CvtZmwmGm6FsIODMTt43Zwln2zXSyZSjQoOrrV9uMKuEq8/ZXSa4kFi+Is94GaVBuMKUUkAYFQrmoxkfJofuW8cHkWQndF+MjZFEuWNHEN1pMxo/C6VzWR3TZVFgL+7yTu3240YvzzyvP2g4LwMs2nC7tWCSUb7J+Uh4dIzIWxsXedCJJSkk9aoQBr8OFiy7lv9BR1XAtG2liEsACb906j5TMuGpukat60DYfAFCnaECWcBBF8i+B6XsPvIyKXbW8XnucW2ioij0oOMGwXz7Icrrq7evsBzlNR+KaoCHygL+/VwhX624sL52HO6rttT1db6pZmghoawWNRxxAnoRgN0tcV2zK5GFtwZkHzXvWKsNkWzaJTxILknFb4cgkKvPxEbkkPrF7HYl/ljrBPKmFUay4TGV6esjpuk5SHuuj+M5A44fuAn9bwWPXjPHNYa7/ac9viIba4SEtI628ASSTNywWG0znSOgc934A6Efe5F28zi16qgM6HZWpqDzw5tizvy2QPuUMe0seJVIZEzbrfWR9+Bkp+/cc2CLwnzCObd6NPR9uGC2gfaGetiyPc7CFLB6+ix1gDx+6cBBLft0StyCyhuqxl5eVw3GPJ7wlLew96mvYkjV9c6WaRS+oHGjpmWmzJdU/lG6SDguDbKNe3X2LowREvYqiZuMgX3y3z1yv8Kkt/VXu+tu+jNHCbFaFoE/YgaoEJHhmhNIieaVKurJYDbPHpkZffSN07WTYDql0w== 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)(136003)(396003)(346002)(39860400002)(83380400001)(478600001)(66556008)(66476007)(66946007)(2616005)(4326008)(186003)(8676002)(36756003)(8936002)(41300700001)(6666004)(6512007)(2906002)(6486002)(6506007)(26005)(86362001)(7416002)(5660300002)(316002)(54906003)(38100700002)(14143004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Wl5KCcNKEwPWlwnqBs30F+D+TJEqdhGP0ITdzW9wpSEWDmrmxmqabXJwq64TO3zD7nVVfydBb84+qJYmNtLyHqipykVykAN+JBwaRZpAl8unYubB2R4RvjefMWCjkvmZ/EoFp6Bk3aN2+YzWAshnCabpGtLWnlaKVW6VINAf1bq8txXwgO4CZUdLYec14Vy+R4spe6bf7Apc8cNbR+BbUiHGGFh5SaxxqkI9COqibH45jL7YXSl9ZnsyYVgiYssFgWz9JoSHHo+s5yl/Zwea0HECuN3V+nZViOC70lhitW9bVCfmxTQoX94Vnyk+pnq+yDzvrnDMeJKpifTqml6mcbVB+tIFJPDORFbGVzUqPrGBfdyLurK7Z2fVxC2aP/cNeVG4STxA4HQQ6fvKRICgSICG3nUWE8tuHsX72HOE5QNzCxD9q195FdRbLyfgNs++DxEfQth1VHWKCwDazCUOZt1r0g2kzIdDqJx227LroLX5QP9vk5L7dmqapfrD+nhHU5WiOQOYhS0vgVKXeL9fyY3t/D30R5u4QhfdllbEactyEXCfPHyTQqN831gFzJF98DUCAfqqZ0Gz3O5clPnKikw81e2P1SE3mP7ovvvKJzVKLXKSCOf0NTX8hbrGBXS8TkpoZwvHbbQZRQdYGx7my06jbY9SKOBgISYPOgzh/m02O3LP90ixWxASj3dKGL2E3J8hbLoBWMMiidyGK6S9RX46Yu+S8UAWAOCu0LdgZhm8uRl5xtjG/nuq7HLQj0HtFnR7jslM/aaeaRChccZGPhHi/3MLlaFRYYBZyvOFmZH3xcNWHPpezhaA+/MBEMN9J699/TAGbzZ3iuieuD+jv776IQvhEyiij5uu4Wmk+0AsiTIUN0rE59V2ptLmeV6+IvZQagBy+XE97mNFHRbmvqP1am9CTR87Lu7q44YG4ahPZ/V8DO6DJj+9weLDKeVPn4wFpodsygzcPG091X1DJLk4SXJIuPdI4fky6bAxt7WUxRf/JO+f2YDi71E+nff07rLthww7yJuS5dJFBiWMt87AS9oj23Kj9HKpBl6YaX18f5Kqr0EXbBKIJ01FHEHcnbvXXtIIYuACaOk8WneaKSlAEDw0bpNaWS+2ezf8hbhtTR7nTW6JazcbP/ubO4p53nxVHeQ8VbRgzFPdMrQlbJnqAgx1qGhlvpmkU+iiGO7qm80NPRXemqO+Al5eA3NEmSFfxzBp2goXKy4FJ89C02pDB2O4y3UEktdpN+xXAnLmC6kwa9UP+TChf8pSWIjTcu+JvWfnEpMhUayzLH5WFJP3s8D7F3/6qD0dJBZ29cF61KIkrudk8g1Wx6yQ59reIu9sTTaqr9ZQPJUanRBzHsww6c8i8Lou41SykzGs2wILg34iAZbXVW91ZHblcSPeFWcuoAknyQFQl5cBgl+WWXii1fN7v5r+r6HiJ7Dz1bQ8K5HcJK5tc9Pg8UIWoJ3LC8tHTmWELxLy5koUsKBDwzxwnCUyHCqyFOLuW8WHthi9j+3cWC5coODKXAyjRNK4C76UuZQ65msJqwYA5j9PlHAMTGJyGkkc16NymwDBJBFVZAIRg8vGLTlTNXSmkpcL X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3ccdb7f2-72f7-4c0f-301f-08da857d430a X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Aug 2022 03:03:47.0837 (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: csHTlJV/69iQt7MWQfo2oXOdJiWyqOC1jq7AUPl2m8IBwdiGEyWbAD0duRtfn0ZF0hhkAcxbe/6hLa1oylXdAg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5774 ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1661310229; a=rsa-sha256; cv=pass; b=MD5+pW9iI8Y2/za9d4az+JhY4SoIVJbiZoS+roZHmQ5deFbWjP8TsbSwJPNkuV5PJMNtVr sgjAb2pWu1ukCTnqKGbnqDBpDPpZzL1j6POODvQQ7cWSGgabSlsdiSPBFn9fBF6WbpHGFn MUeTbZ+ZC2quuqkFzlzk1uQvuhPIEDo= ARC-Authentication-Results: i=2; imf24.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b="gEvu/Dl5"; spf=pass (imf24.hostedemail.com: domain of apopple@nvidia.com designates 40.107.237.43 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=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=1661310228; 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:in-reply-to:references:references:dkim-signature; bh=bbxR+OCYU2G/04rFdWWgpbEcYTHFk46Ar6Hr4DZBQf4=; b=zmhnYWK/6l7bSs/y+MWrW6X2E6I66eTpLV8nZdRBC1+3Rsc1M5uG8z3ALkrXHDy7I/zNAF R+NCbns9qmml0bDIFT44wsU4gEy3UZLDfPpmyiitDQXYm73MzTt4EAi3gy7CDiuvjUq8QW Pi98hQqkSvnKdK5NK0LAypuPWiE3xzg= X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E2DAA180060 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b="gEvu/Dl5"; spf=pass (imf24.hostedemail.com: domain of apopple@nvidia.com designates 40.107.237.43 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") X-Stat-Signature: b13fbewdb54pqiseas3x6wwgw7afod3k X-HE-Tag: 1661310228-602690 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: migrate_vma_setup() has a fast path in migrate_vma_collect_pmd() that installs migration entries directly if it can lock the migrating page. When removing a dirty pte the dirty bit is supposed to be carried over to the underlying page to prevent it being lost. Currently migrate_vma_*() can only be used for private anonymous mappings. That means loss of the dirty bit usually doesn't result in data loss because these pages are typically not file-backed. However pages may be backed by swap storage which can result in data loss if an attempt is made to migrate a dirty page that doesn't yet have the PageDirty flag set. In this case migration will fail due to unexpected references but the dirty pte bit will be lost. If the page is subsequently reclaimed data won't be written back to swap storage as it is considered uptodate, resulting in data loss if the page is subsequently accessed. Prevent this by copying the dirty bit to the page when removing the pte to match what try_to_migrate_one() does. Signed-off-by: Alistair Popple Acked-by: Peter Xu Reported-by: Huang Ying Fixes: 8c3328f1f36a ("mm/migrate: migrate_vma() unmap page from vma while collecting pages") Cc: stable@vger.kernel.org Reviewed-by: "Huang, Ying" --- Changes for v3: - Defer TLB flushing - Split a TLB flushing fix into a separate change. Changes for v2: - Fixed up Reported-by tag. - Added Peter's Acked-by. - Atomically read and clear the pte to prevent the dirty bit getting set after reading it. - Added fixes tag --- mm/migrate_device.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 6a5ef9f..51d9afa 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -196,7 +197,7 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, anon_exclusive = PageAnon(page) && PageAnonExclusive(page); if (anon_exclusive) { flush_cache_page(vma, addr, pte_pfn(*ptep)); - ptep_clear_flush(vma, addr, ptep); + pte = ptep_clear_flush(vma, addr, ptep); if (page_try_share_anon_rmap(page)) { set_pte_at(mm, addr, ptep, pte); @@ -206,11 +207,15 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, goto next; } } else { - ptep_get_and_clear(mm, addr, ptep); + pte = ptep_get_and_clear(mm, addr, ptep); } migrate->cpages++; + /* Set the dirty flag on the folio now the pte is gone. */ + if (pte_dirty(pte)) + folio_mark_dirty(page_folio(page)); + /* Setup special migration page table entry */ if (mpfn & MIGRATE_PFN_WRITE) entry = make_writable_migration_entry( From patchwork Wed Aug 24 03:03:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12952816 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 A7B4FC32774 for ; Wed, 24 Aug 2022 03:03:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3D99294000A; Tue, 23 Aug 2022 23:03:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 388C4940007; Tue, 23 Aug 2022 23:03:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DB8694000A; Tue, 23 Aug 2022 23:03:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 10F61940007 for ; Tue, 23 Aug 2022 23:03:53 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CEFF71C712F for ; Wed, 24 Aug 2022 03:03:52 +0000 (UTC) X-FDA: 79832991504.17.8474049 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2085.outbound.protection.outlook.com [40.107.237.85]) by imf09.hostedemail.com (Postfix) with ESMTP id 534D814003B for ; Wed, 24 Aug 2022 03:03:52 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=B/E+l+T6Q4YzJKTWUeeeofNAz8wQeEil8WUM19cgy7Z59WFbQEsc2YYIwbamIknnkwvQgRmj7H5jvDNLju2wvlnPXrnsF/mxb9hM+P8BdH0h7TA6/GISrKNo90rnVNLWmNnGY49a7v5uqx3ofhpBcCILeMKJBmkEfOH1YfBUVzbFFOd3ZDIYB9jdq/0V7L/zhtYDHMso2c0XXuC8wRsUB6FCwEjSjO6GyVuwGzovCVlOjg2wu5maK35tNuIUZkhitaFwIlfYV1nbm7feRts7ZaKKFIhqUi2MkLQtXNZnmp0Jsi8UUrr1y1iR17ViLAxeuA0YcFvu1UEdd/glJBMy3Q== 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=snVPulbAKBAi9Wd3KFPYZ5rYVHb8mY3ptG8l5yLntdE=; b=Vs5Pu9sDiwrhW3JeGDZv5wZj7aIHscwNUD+B1yFaENCF9lLTEC3xliYgbdEw40ocd/Be73XLialRLem8PKYa/nY5si97kPAUWG5lAlOExL+0WI6cFUXemGWY6vHwK7H90tsiF5FzdoPKH66I3EQ+aLwWDdQ1R0zgmXxdS26+57vRJ2/KSAKX+6PPHerOj5KgfUvo4B2qYLgKNHJdmUxbnMz+R9Cirqvcw7/r1ZQIxC4egBZ5cUiU7GQTIEOpJNADHgvSD1Dt/Bs5TsVOYsmjX1bEkVVOnH/iATWxXbffjeuQ9MD+vpSswVlXQqgZhhUHQLFeo3L7tRM5vZAJF/EG6g== 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=snVPulbAKBAi9Wd3KFPYZ5rYVHb8mY3ptG8l5yLntdE=; b=N9r3BBevrOsLQ8P5gSvHSSuOS0fUYjYp8SiyuvUIpmAok3JdCAHK3STK6cCQpRn9ZefzOFMIMNU8AQYXALt+G2u/eq3zWc/7yrvaA9aQIlM++g8a/J4A3mcd+nTmyMN4dOlk+k2O6nkFSwb4l2ZN6pa3LTte5lKnLNbv3c4ZITIcXY3XSFjYL+tfpPprsdzmHwz+DTb0GSLEgBqeGT+pesKU9hW5wdorbxpiBTHfrXKpobjK36arsI+fOHS64rAhfKyM1eibU0BFnym4lpno7gtlFBzePNFpBJ47RBkLcxo00SFy03CDETS0jrm2+Pz5+Ix1v5aoSBkBkTpnNuWGjA== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by LV2PR12MB5774.namprd12.prod.outlook.com (2603:10b6:408:17a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5546.23; Wed, 24 Aug 2022 03:03:50 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::7432:2749:aa27:722c]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::7432:2749:aa27:722c%7]) with mapi id 15.20.5546.022; Wed, 24 Aug 2022 03:03:50 +0000 From: Alistair Popple To: linux-mm@kvack.org, akpm@linux-foundation.org Cc: Peter Xu , Nadav Amit , Alistair Popple , huang ying , LKML , "Sierra Guiza, Alejandro (Alex)" , Felix Kuehling , Jason Gunthorpe , John Hubbard , David Hildenbrand , Ralph Campbell , Matthew Wilcox , Karol Herbst , Lyude Paul , Ben Skeggs , Logan Gunthorpe , paulus@ozlabs.org, linuxppc-dev@lists.ozlabs.org, stable@vger.kernel.org Subject: [PATCH v3 3/3] selftests/hmm-tests: Add test for dirty bits Date: Wed, 24 Aug 2022 13:03:39 +1000 Message-Id: <8e425a8ec191682fa2036260c575f065eeb56a53.1661309831.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <3b01af093515ce2960ac39bb16ff77473150d179.1661309831.git-series.apopple@nvidia.com> References: <3b01af093515ce2960ac39bb16ff77473150d179.1661309831.git-series.apopple@nvidia.com> X-ClientProxiedBy: SJ0PR05CA0113.namprd05.prod.outlook.com (2603:10b6:a03:334::28) 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: 8f693d62-88fc-4a34-6fc3-08da857d451b X-MS-TrafficTypeDiagnostic: LV2PR12MB5774:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bZmzRAFqR2cu83C/EuVCfmxhlHyqrLyPS3M/bUwnQHD7qdvSTkAEUdKNJpogGeDWjDfedcmOYikq5zwy6aMTRlXQoHIxRlwj7naji0D2vkFNFV0OtP585OWdzC7Q4cafdDfxd333+ZzmLxkgRM61z/RUouKMGRGXlqxo896/Ye6GQrz0jeSUC7RxJbEttijST2jzoL/tLNkYQM8N29TwKHbjLZdXxlJeONGptHdqhBxH4+tqHQ8TNlpc89l6hLqXri6cqJpvTn6QQpGqnFxIM94loQER7E8Ynus/a8uiuslJxyiKyE86q0cZ/8nK2JXVOCrXA6ahO/SyCMhcDZa3yblZfSSRdvLCMm/k2j3sl9rzydZHv4tKgtkLxLXxEtnoob1hzqNZkmoQfSbW6yxsbsQEGBhjejPerch6KmWZY994cUOBhhidVu8tmMrVUJ52rGl+GvD68qJ3qZhV0xAFTApW1pvmgodj3UXhXBR3+gz66Ig/fISAyEFoWoG+6HFR21DyXGzCA0sFbj1X2zsmVcZPJHQVN2IhgXM0JF7KZJBKC710Hs/Wbaq7JiB1jfuMXRVlFEd9uv5rK12jrPHr0Mk3rxhqawxF3/NFf6Sw96OwBSog6TSq7c9toF1jv/C7OJKufrXypKA3zvg/aPuOVztLLypxNxkB7Ed20WzQ9qI3DuJFH3BhdFWXk2p7fupBkkaYB6W2a3WhseK2p/Q2tz7vjz8USrZdJ8xEj1Qai1MON1X38ZvF1TjNlN+XRP3XQXOpN8yx5P6iYgYOZYmldA== 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)(136003)(396003)(346002)(39860400002)(83380400001)(478600001)(66556008)(66476007)(66946007)(2616005)(4326008)(186003)(8676002)(36756003)(8936002)(41300700001)(6666004)(6512007)(2906002)(6486002)(6506007)(26005)(86362001)(7416002)(5660300002)(316002)(54906003)(38100700002)(14143004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Vi06MaBhg7VPeMAIg2aX3mngHgnPkB/9LTIrlWZfM2I61oFXUT5QxwneylgtvQOHI26GDs03DoRAxeYCQqfdV8oiN0elQAynVxAlhR0ZfP7Mq2gw5MskwJsx/9hsDyO14zSv6tlfrjgfRbmNpZYNxz5BCtDr27mVkkpHpUkRBL/hNsQGXSnc9qHxtyMnUJcPu4Di/g9corFfcjTKUAeEVeVcHQ5pNRd72CKt/Fh4tB0zDylGuFLLOMzmNaq4rzS9nxWWBoHtEZqR+R7s5rEH1Xn52JR7rKO8nDj8b8+DpEUpCYIiwMRsfi88Dsb6pcXnEeSxxOIc+ZAgDbIi9eJLFuAVCULlhGe+3GpFeKw/2RE+0sB4s6R+miTqdNi7zIViI0TBccUVNdc7W9YOQyTpMaWzzMnJek4okLhmSuiZyrv/Wr4hNKE5fdQE8/gVrZPAjPhXaxtV/ZMSc5BpGWu7SKs8tM0//KDkkHnmEwXzf+nVEr43uRwlRsmA+FmnYw5UIPjEhu1a/YMn8ssWaAv4h2ryvQDouucv2MzNEyc5h/rx/P5CJ2sr2y1LZoV+V8k6epyDp+gj1Wo1ECL9s7Sk+T++HsajAauEkhXofJ3AfE+ykgHeIczKKg2mUFkzn73Bny4VfAKPOCiA7N+MjTXqw1/ev8YD8zvqx6+eFp6PeMjCQIprDUrvLc8h1ODdmXpmzfilLAz2AboHZwFlU1Nw3bActgE8hWzinbZRmnDbiezJXniJlfooca5xpknf5ZZAGqBSUzsOgbUsUdgfKr6nYtSBMiTQRCEGg2jnV8UnKqvbUTmbG/NigXER/TCsZuNmxByKxRi0FetJ+ET0aBCregDzahMhBmxQASbVmP8SDW4FWjsR0ifkdNHeAjNBVIDBeawmUl4nAGr0Z2v55DnqdN1s9aP86POBepwVkcliVQD/N249QtPJuKZKm6kgJ0zV5uGDt9O2+6hUPWxpNYJolnQEnhCqRUGrGe1uQIwTETsJXDOTBK9FV+G7r+IkW+B5IBCNxP8MlR4Iemk1Ybzc36ldSpNP1KLIHaSzMxvR0LviymXHU9gZrHbSy0l9wEe4sT5NR++E1+nVpTAbdNYcUG2jX6jmt1wdZy7kWAQSeOQIGcwoY6HOd5TQoHtuQH/3QW/YyphoMnjzSC9422rvoiNkhzMyw9VA9ZLyZXzbEkkTve0O7zQ82tBo68szMjbfFVerbkRGHuDqwHIXzwYJY8Hi0bMPPSp6H+u13PBskjt1r08mGDYO69T6JWflk++7pMsJKpwQB5rcDw9FzF/KI5131/99FwXuUcyVxGfvj0LGJq5ukSrBfeVQRpbg3eKSf7DSpxV43yrXxIzR9Hjl0Zr2RwBRfCgJPCeLULMqpNy4baYvBIjF0tkE1AEFBP0ayVFKHXF5UlJjl6ekvx8n7Xkzz+s8HcfCG2E2uHFJYEbh3es2czSnVunpFSxd369pdIejGa/LBa9weTRgaltTm7veA5KyrXgKyjFRmJ8sm8N+A1G6lLV4GeulZ+v0H7DKr0ZIYEfEYN2fpfh0p0sA1PsU38ay9/KDVe+/bDzUixb9TFx5l7tnx+sKBrBUo5J5 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8f693d62-88fc-4a34-6fc3-08da857d451b X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Aug 2022 03:03:50.4741 (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: F4qkStYyDB6rG+wwdhwWs4iDxmijQGEFtsaBY1wyFVB+h2S0kyyU7Z/4rJw97hh2qranG7Ql0XPPfljL+Ea2YQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR12MB5774 ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661310232; 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:in-reply-to:references:references:dkim-signature; bh=snVPulbAKBAi9Wd3KFPYZ5rYVHb8mY3ptG8l5yLntdE=; b=dRCsUX94zji40eMjF6d5v5yNLXsdpSHvBao0FT3hJXh0bAhAhEkjZEAMpB+9Jfi2nF9rIV qaKHghiaqVfqLfCW574Bda4ZEBKXIUxRaiA/AejoVZUvllcdlTM7gZFpghfBHRlDJwSFW0 yrqO+JyXmTgQ4alozTz0DVdkm9fFYvI= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=N9r3BBev; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf09.hostedemail.com: domain of apopple@nvidia.com designates 40.107.237.85 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1661310232; a=rsa-sha256; cv=pass; b=Ui2Yrhr14OI7MIOb9TGAz9peano9EG5pP0eh/S4+oqk8fKQBu0nQIeS31F3RZXLSfFc4t7 NEb77o+ijPrxnNZ+0JVkAaSFJfBHj/qixGKp0BTKlyOQlR4oXieO18T5w0ziCC9fgzZNHS Mt/eskJYW5FRxmDNSICvGOWxSLvwcu0= X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 534D814003B X-Rspam-User: Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=N9r3BBev; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf09.hostedemail.com: domain of apopple@nvidia.com designates 40.107.237.85 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com X-Stat-Signature: ajw1pfu9919xusaiwpoofkpfk461cz8d X-HE-Tag: 1661310232-558577 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: We were not correctly copying PTE dirty bits to pages during migrate_vma_setup() calls. This could potentially lead to data loss, so add a test for this. Signed-off-by: Alistair Popple --- tools/testing/selftests/vm/hmm-tests.c | 124 ++++++++++++++++++++++++++- 1 file changed, 124 insertions(+) diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c index 529f53b..70fdb49 100644 --- a/tools/testing/selftests/vm/hmm-tests.c +++ b/tools/testing/selftests/vm/hmm-tests.c @@ -1200,6 +1200,130 @@ TEST_F(hmm, migrate_multiple) } } +static char cgroup[] = "/sys/fs/cgroup/hmm-test-XXXXXX"; +static int write_cgroup_param(char *cgroup_path, char *param, long value) +{ + int ret; + FILE *f; + char *filename; + + if (asprintf(&filename, "%s/%s", cgroup_path, param) < 0) + return -1; + + f = fopen(filename, "w"); + if (!f) { + ret = -1; + goto out; + } + + ret = fprintf(f, "%ld\n", value); + if (ret < 0) + goto out1; + + ret = 0; + +out1: + fclose(f); +out: + free(filename); + + return ret; +} + +static int setup_cgroup(void) +{ + pid_t pid = getpid(); + int ret; + + if (!mkdtemp(cgroup)) + return -1; + + ret = write_cgroup_param(cgroup, "cgroup.procs", pid); + if (ret) + return ret; + + return 0; +} + +static int destroy_cgroup(void) +{ + pid_t pid = getpid(); + int ret; + + ret = write_cgroup_param("/sys/fs/cgroup/cgroup.procs", + "cgroup.proc", pid); + if (ret) + return ret; + + if (rmdir(cgroup)) + return -1; + + return 0; +} + +/* + * Try and migrate a dirty page that has previously been swapped to disk. This + * checks that we don't loose dirty bits. + */ +TEST_F(hmm, migrate_dirty_page) +{ + struct hmm_buffer *buffer; + unsigned long npages; + unsigned long size; + unsigned long i; + int *ptr; + int tmp = 0; + + npages = ALIGN(HMM_BUFFER_SIZE, self->page_size) >> self->page_shift; + ASSERT_NE(npages, 0); + size = npages << self->page_shift; + + buffer = malloc(sizeof(*buffer)); + ASSERT_NE(buffer, NULL); + + buffer->fd = -1; + buffer->size = size; + buffer->mirror = malloc(size); + ASSERT_NE(buffer->mirror, NULL); + + ASSERT_EQ(setup_cgroup(), 0); + + buffer->ptr = mmap(NULL, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, + buffer->fd, 0); + ASSERT_NE(buffer->ptr, MAP_FAILED); + + /* Initialize buffer in system memory. */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + ptr[i] = 0; + + ASSERT_FALSE(write_cgroup_param(cgroup, "memory.reclaim", 1UL<<30)); + + /* Fault pages back in from swap as clean pages */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + tmp += ptr[i]; + + /* Dirty the pte */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + ptr[i] = i; + + /* + * Attempt to migrate memory to device, which should fail because + * hopefully some pages are backed by swap storage. + */ + ASSERT_TRUE(hmm_migrate_sys_to_dev(self->fd, buffer, npages)); + + ASSERT_FALSE(write_cgroup_param(cgroup, "memory.reclaim", 1UL<<30)); + + /* Check we still see the updated data after restoring from swap. */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + ASSERT_EQ(ptr[i], i); + + hmm_buffer_free(buffer); + destroy_cgroup(); +} + /* * Read anonymous memory multiple times. */