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(