From patchwork Fri Aug 12 05:22:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12941906 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 ECFCFC00140 for ; Fri, 12 Aug 2022 05:23:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60D0E8E0001; Fri, 12 Aug 2022 01:23:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5964C6B0075; Fri, 12 Aug 2022 01:23:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E9908E0001; Fri, 12 Aug 2022 01:23:26 -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 29C0C6B0073 for ; Fri, 12 Aug 2022 01:23:26 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id F0D7CAB71A for ; Fri, 12 Aug 2022 05:23:25 +0000 (UTC) X-FDA: 79789797612.25.BDE7FB4 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2085.outbound.protection.outlook.com [40.107.94.85]) by imf09.hostedemail.com (Postfix) with ESMTP id 4F41B14019C for ; Fri, 12 Aug 2022 05:23:25 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Dvy5oBudKnOBiROLKC1x/8Reg59+CaNY4IpKZQAZSGDVRYtN2hkwTFqmJQ4py9+IGK16D+gIhrgi0qsKaCJa4Q/ropfu7+PuDozxpHCT7zXIUwJBlwuQY5cYQi9DW49AR5Yp+crPwsLTJn6HzF3elYDInaE+XOCKgrKA1i4O1kEk0oHsxfSdUQYb0upbqgtc7VaKuBUtJzPoVUBYkVB0MJXFtMf9qLK7cupOfCtEx9hZWLZeHmhvTKW/3xishSxKjpC6IIvlAlXOiBmMOsHSfZ/ZG8dUFImsmyD+AYPNp9FiaaqRABy5n8JL/ObLq3jYupEIYD/h7yti5NPEF7hrLQ== 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=51rvVXCforVG/VV+jnIVuOgd23Hpr3PwEdWaoS4K4Ag=; b=PTPt+wTpDUSpwP2bldMCKH4yyt1/aWuT+4hzf0sDIvVFgwvDUnxYad95ckafB7tr0dAduHbPNbKiZ8mGf5apSt3e+vprYTnOxBYjZ4LWU4Gvk3G6S2zuO72cN3dkGDBvIWLHjDYVJtk1MZV99+nm+ZtFmQqujeSPO9OdwStCHgHaMhDT3YNNSpOvp14y/GctGHNUPSDKozfOHrQ7D/1P754xUdvjd3MHZbZvMfXEkCITzhFQjsaH0GjjUe8tc8sib1rZuktw7+z4jw0B+g9ojHpZvG0J3tdVYtQ7Nj4IHJkwm0oIFYZMrQNIV7tyxUgtBUNJsdE03P23lw/dZ5H0Sg== 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=51rvVXCforVG/VV+jnIVuOgd23Hpr3PwEdWaoS4K4Ag=; b=WyOup/eT8jSp0HsvOo8t4CDibuFbMl7zXDqHLiwS7USFBzqrTeprs67kcgSdly0UNs38C+j0C/XJQj4oN3+NgP4s/vLKe2vSZqWveRoFJGCzntnK0nFbD/8zTV9DvzuofDwT/OzJC9mKmtUO5k5r0J3RtAs23dhWvhBiRO85K1MEK5555WxLKxT/UMtXlhywOF7xFShKOirHz1MPk15lgQpAmc+6s6G3wjB06X1AoDqicL2gymZiIVwQdseo01QEjJDkifnSdMxoOaeYRuwlPplmYLHplnFFBEcIazop5SjWaXqUU+mg2q3Wvgpv9Y/5f23N0D1YooypguhBTjUjMQ== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by DM6PR12MB3978.namprd12.prod.outlook.com (2603:10b6:5:1c5::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5525.10; Fri, 12 Aug 2022 05:23:22 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::eca6:a4a7:e2b2:27e7]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::eca6:a4a7:e2b2:27e7%5]) with mapi id 15.20.5504.025; Fri, 12 Aug 2022 05:23:22 +0000 From: Alistair Popple To: linux-mm@kvack.org, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, "Sierra Guiza, Alejandro (Alex)" , Felix Kuehling , Jason Gunthorpe , John Hubbard , David Hildenbrand , Ralph Campbell , Matthew Wilcox , Karol Herbst , Lyude Paul , Ben Skeggs , Logan Gunthorpe , linuxram@us.ibm.com, paulus@ozlabs.org, Alistair Popple , Peter Xu Subject: [PATCH 1/2] mm/migrate_device.c: Copy pte dirty bit to page Date: Fri, 12 Aug 2022 15:22:30 +1000 Message-Id: X-Mailer: git-send-email 2.35.1 X-ClientProxiedBy: SJ0PR05CA0125.namprd05.prod.outlook.com (2603:10b6:a03:33d::10) 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: 1c049e8a-ea4d-4c64-9c17-08da7c22c646 X-MS-TrafficTypeDiagnostic: DM6PR12MB3978:EE_ X-LD-Processed: 43083d15-7273-40c1-b7db-39efd9ccc17a,ExtAddr X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SEqxDmnTGkCMcmXruL2YwA3gFXLqkbLkJlU3S2HPk+1mKgakGYz0hUdlcCfj19RmX4X32nFRsylqHDFZIzo+dfbuWaYYqQcsptsrKlhVlv5C/qrZkDXa/YWIGXY/91DHeNE6ewiDYxq3V+cgA1HRyLhxsUp5Wv17UL6lCyVxNM9bCV6hLSb7ig+DPzs2VxmZdFsbOx3h9OSR6otIvRd+XqiTNI3dFpuzHCDtC/660UGlucj/CZA/ycC/sPyityWsGTKH8EPdcbQx6X3tdsl1lNNcMhl2YVgykqaSHzbpnrnXyMe2W9faavnIKncBoWiI8gnttj6wz0Pkr3w8fizFS+QtL/TevYmrgEKT+KLHjrzLV+82qBe2HA+G9vLrj+zfcr6Q32TPIw7/X2XM8eGT9+2IktoR3tVflme6URMpEsLsFZ3plfT9Ad/whE4YMhhf05Ct+vNAdEWZO92Qy20R3JeZjid7wJY+uSPND4eNB6/GDH+meh7MHVW21CMjvFjl5fitdB5wroEjVtFshLqHpP+qmC0VLevEaDy3iqcvWxkCJXkXUnOa1aJt0KPx3INNfm6KmhzX4ykOZ8Qd9Rc7y2ydlU3OVM5TwnJDtei621MD1P2e19kS77X/HHoESeUO3VuLLGQC7UMNgQCTtaPnQLVo4NypW3wwCkfnrwE5mrh+JxwZCmKeCCE4vwgaUlQxBGfgMEuI8Yd+DRexlJwSDbRXBWjZvzva5CPSUau/MDmqG8STBjNltUHENXa0h9T65/h6pJFE37fX8XMiz6AfvwldPz2wau5B82+0ImukwhI= 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)(396003)(39860400002)(376002)(136003)(366004)(346002)(5660300002)(6666004)(6506007)(41300700001)(26005)(6512007)(8936002)(86362001)(66946007)(316002)(54906003)(478600001)(66476007)(6486002)(66556008)(4326008)(8676002)(2906002)(36756003)(186003)(83380400001)(7416002)(2616005)(38100700002)(14143004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: p6v+vMlQXzvaJbQolEmbNflt4yfX7aFofHGyhM++QGIFQeaIFv7CSciF+P3u3/7O7ziuxvxVfCuYTqXL19KrrRsa13UV+lVsIylGzc/Ierly5iB/gM/wEDwzQmdYnuspGpLYI1hrg2+x8u+QzS8ndOiETC29HpHHpcUAR48neThnCGgHXIfuYYoJDCxBMtCmKO5i6BcaJrYSYkTGqbhbLGDybC7TJZmPwnBFreGPa7NF358f6rnYos3Hx4NEBuWKz09bKhjT+zMHig0VRJJUGjOoI8Cddf8tbcen3BRIu/Wmz+syZn970WEVDbPEKlq2GI0YnvJTe2Gu+uVvD3IVBcyQwPu4tGPryAg2HpLHMMc7fnZRkkdapz3jPUE/aPZpcYeucu3Bs9s9xCwWxGvs4L11IXwMIPiDhR+WHKIwSwvOLXJSOOlUEcqvIJN5HjVRA6zfmPsU0oDWPhTc3leMOHCkA6vV8/eHr9el79EPHORm8acRpttSZsyA+Zl/OrWjURpll/GnTNpRrUSPYUa7GWkHZthBn3QaJcsPi3PHUNnFwWQxaIMjDHTA9Rz3sn9ckIqyNioZmHTEAIfrHq9b2fYOIXhNc0o9X+gfObi7PiUNDma6nG7BNVbmzD5Xe58d+9UMKtqg/2dcBFekIcdSi5PVmB15yZtRH3OCCv3yBQ4Pgy1chcoNJrDScv7SI1W1x82/WJJwCNeEU0Y8EC2xOUQC3Z8uvebUCCvMC8ZjraiwIM/xZX+V05ty22MpgSjU0AOOyJH3FN4QCb/D2sqQ7MoTpfBn9pF5sJgPqT8O9YuYPh1CxtgB06lw+J6Kpq7Fp8GPWfx3COvH61zFCUOjhjq1iYzaUK1FniZY60THAqjV3rPvQFKB7pcC4MY8kN5PSLpsqSX7lcuuOqq/poOHhqjzwMctiOKEQCevC1kPJpxbaf72buE3IS4A2btkVtdYQ65NhPjalvEPWo21IsP7ukWJMjmz3aLEp2zD4u3xkLyjiOQacOTMwJJ7eu7GzuywhUWsdQ8aJH2SswS78h2Vc99KKJM7Cit83xBKD9yJHO4gbOV3j1L73imgevEymKZn1EAUQaxiQ0UtalDN64ujhxKgxjalofZtEgr1qUk7vR78t6QB3dDyn6INJorN+aVF4v92WM56N2L8suD4fGv8DusI8w4z65xOzDlysB/90HMBh29LQZ8Rp3UlP5aa8ZWUAM5FSCf9Sw6EcWzvtkj+b46GrYxPzhF18C7hjbBb9DdfMYy8WslNPCA1iofcr9tsrMnhAAvA6vUrVfpj8hAm7/H2lAnOug+pPQQs8A6QRyOKiP/R8Zk23aSqnk8YI6wajWOG1npuUxE7XLJrHkkU5HdlrFavaGNyvnlwwCvOYfmo7Vsu3fe7fjke0ZI6S4rPce3XlWK7FGCHFrvaQzbQbQ2hlebsAEyUCV4ZVQ+niPqzG0tGEPYBurTKHQeLAcZ2C8xHj0oa21SnTMctYZBBuu4VxbZyKPw/3M0I3yruqfrajQF0G6VATcuue2eJbHQ1T8hHGIqvN+EkkwBxhx1t1/haCl8yxfNpNb1EAMJUCFKoQsqbg04kLe15rYsCDxT6 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1c049e8a-ea4d-4c64-9c17-08da7c22c646 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Aug 2022 05:23:22.4532 (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: FojY0PWVXMMSgYG5XSWloU6ebwTYWyYRbS+oPpTh6qg5GvFfFM8e4lzn7255JF2EDXpXjkupyi7dVACL1tcpIA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3978 ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660281805; 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=51rvVXCforVG/VV+jnIVuOgd23Hpr3PwEdWaoS4K4Ag=; b=J2ycAN17PynfcAPm7TF0phJwbv3Y3Y1q+ujYBLHEwm12Qn4b6l8Ymrpb0MErwYNaQ+T4kV W+/dHlwTTjcRNgiTxFLkd2op6N91uhGOUIxu5LKK3W71Yn7/OpGvosMzK+4EYdLn9SSWJR eg5d8sgQtpHqFDUfSuLwLvASx/HdiKU= ARC-Authentication-Results: i=2; imf09.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b="WyOup/eT"; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf09.hostedemail.com: domain of apopple@nvidia.com designates 40.107.94.85 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1660281805; a=rsa-sha256; cv=pass; b=T3fTRInqZrvQIghDprGo9bUA+FC4FYnqXAckwn6xyUjKztQy0Rfr8yZtONJazBse/GtO2k 4pPc/rYtleSvcFh9WAeoiSAgY0FfSIZ2a20C23xDer9mcr9qkiWcvJJpS4qwhy6cKw9EKE FgyfPWl7h/sygNPZaEHhKPgUgMxIin0= X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 4F41B14019C X-Rspam-User: X-Stat-Signature: ypte3mg76bscfr4gzqrb1pj3puoek575 Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b="WyOup/eT"; dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf09.hostedemail.com: domain of apopple@nvidia.com designates 40.107.94.85 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") X-HE-Tag: 1660281805-213374 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 Reported-by: Peter Xu Reported-by: Huang Ying Acked-by: Peter Xu --- mm/migrate_device.c | 5 +++++ 1 file changed, 5 insertions(+) base-commit: ffcf9c5700e49c0aee42dcba9a12ba21338e8136 diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 27fb37d..d38f8a6 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -211,6 +212,10 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, 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 Fri Aug 12 05:22:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12941907 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 C230FC00140 for ; Fri, 12 Aug 2022 05:23:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C3D98E0002; Fri, 12 Aug 2022 01:23:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3251E6B0075; Fri, 12 Aug 2022 01:23:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 151658E0002; Fri, 12 Aug 2022 01:23:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id F3CE16B0073 for ; Fri, 12 Aug 2022 01:23:28 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9FB84C068A for ; Fri, 12 Aug 2022 05:23:28 +0000 (UTC) X-FDA: 79789797696.29.C360936 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2074.outbound.protection.outlook.com [40.107.94.74]) by imf29.hostedemail.com (Postfix) with ESMTP id 11203120183 for ; Fri, 12 Aug 2022 05:23:27 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ewX8H85xGiA1mN2sJQCZroK03SQxPxyvSvYOAylv/n7+TzrbLUnfIKf58MkGGKeLSd+8UBvySoWipfgCPUy8bW40jjGgNQdxkUTaWgv94JG9N7h7jdSKrj66+/ImOsu8RBclg3EOyMywCf+GzG/1sx5Oc0ESXm6uiqrpSS/TbOAd3qYTZ7cHWKvoTICM1h9gXR0vJMc+XWiMQgwg/8UIsm5m+S2Ppb8tLQxVmpMLWDAvein75yC2eo0hH7dV6TDEjKV54HjNSOEyZkAMhoQ92Z6x1kxqQjdoA/Q1EzfTog+W6CmCsk1YXhOeF1DmBzEjvvkhlPnOgvgNWXoY69CcSg== 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=ROxhFtybP/MZxfos7JNr1i8X3gW7UfjboEnvoKweaR6zGvsNgbzJlqalYZs40b5TnBv+36CIFkBN/iaIK5IIcpq0xXQUN6KxZ1EyGlqAsaqK1eVgTMIxWc5YWYblNtKUDQQ/D+cpB/1QUC8Kcg4661Gk1mang4Mz5ex2pZsQIz2IF3k6J4G8sZe1WzYsUJcWH5dc3ds9MqaJ4VXGgza8i/bkdqIlNZPfui+m1J2MqrrzDiZEMKRy5RWYCfE0PMfGQpy5GxYAEQ7FfgSFqyr3n+BYOB0CtEEADCmffLPgc0vPv+lQziUr5FYXB1ghCwhYtckV0VQb+qMugl3TL3fBOw== 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=WvsxuMbpBtrdcnRprhJNYN/tn+9cpPDIBh1zAQpbwNkWRVve5EZ1/K3rbo4qWOGdQhMRf25e5JZz3xjNZBrsTBaCppte1axhhLU0vltg43h5++V4GeR1GIRv2BLJjw6qH4q//YvLG4XpBdEdt6nfB/6TnyyU0z8Lr+8uuEWlA6Nkoin+WCm28avyx/ST5d9JylvEIo2w/cCircfHOEzJ8BHtibBHap3dD9fKaxFJ/scpkLZHi1pdo11SoeiF0jgD3JQb2/1jlqvDXg1Lp6I6DN5XD1448zkC9m7D+1NZ8sWmjsORuhTqZotjNiqGKlBpHRs04B60Y/P5mw2iRnV6lw== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by DM6PR12MB3978.namprd12.prod.outlook.com (2603:10b6:5:1c5::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5525.10; Fri, 12 Aug 2022 05:23:25 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::eca6:a4a7:e2b2:27e7]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::eca6:a4a7:e2b2:27e7%5]) with mapi id 15.20.5504.025; Fri, 12 Aug 2022 05:23:25 +0000 From: Alistair Popple To: linux-mm@kvack.org, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, "Sierra Guiza, Alejandro (Alex)" , Felix Kuehling , Jason Gunthorpe , John Hubbard , David Hildenbrand , Ralph Campbell , Matthew Wilcox , Karol Herbst , Lyude Paul , Ben Skeggs , Logan Gunthorpe , linuxram@us.ibm.com, paulus@ozlabs.org, Alistair Popple Subject: [PATCH 2/2] selftests/hmm-tests: Add test for dirty bits Date: Fri, 12 Aug 2022 15:22:31 +1000 Message-Id: <8f19b172d32be2e889b837f88b1ba070bf2c97ee.1660281458.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: References: X-ClientProxiedBy: BY5PR03CA0027.namprd03.prod.outlook.com (2603:10b6:a03:1e0::37) 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: 5e0cf9f9-0ff2-41c8-56e3-08da7c22c810 X-MS-TrafficTypeDiagnostic: DM6PR12MB3978:EE_ X-LD-Processed: 43083d15-7273-40c1-b7db-39efd9ccc17a,ExtAddr X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: BknpDFaWS0vAiU6bh+AAC73T35aXCDrL3m6Ha87v1IHvSAtLXfLpxPkoi2f+IXfybynrKhjOn5wfTAHemuJWrASpLA1s0ny4CvLp38XeYJg5NCnjhHh3e25XnROWagkTgv/tEkFxObVi37tLlF4TZ73GIklIqfaIEs2JRecbgQaBt+aBvztvGUPLSHnBCt7HBxcnzepQBHjmfZ/gKrc3gZZf0fOEb+4uZXcceuMBdC0BehoRpYS67t8HJPeaujS955DNHGwpGSExfUOKccT8PoNlhokuI42mZDipf5XZ0NcLuGwUBqpkhrFRTYiyxrrhBf1PR3RQCVK7DXKLwTr0CPkyWHEZv5PePWvLp4xyg6DVhdUcDSQ7hIqylb7Yb/wodMjNREVNgSS7gS3ik2PIJM8+K/uB2iJ3OEdc+iDZcs9sCsz6e9H/bLgLDKO02BEWCBacfPBBMMxNWZwNDu83WxpmKV5Y2KcJi5xkxdrecet9QeroEpyfW1ORa9UF2be4w+JsGLIomh0iXzUMrHD65goqYEjLUyV0WjUsCaDOMS3rPueeS69EhOYrfkEZmQDNrwrALdo3fKbzZZ6fDcJ5zvwYptXozVMRLXxddgWXOYies0DdzpbsfBHcR9lraYLgDInQzTpkP9xiXfChIaKpBa/J4dEbIRVn9J0Cu8Q8fLLw/YMLmoecNuwzJq6+b6zfNCxKoyBu3Z3DRXDX8SgTtWjHwf7Fl2kCxCxIfD61xXOkbM/5M9wI6lMwa/bYxc2o+5kqLYpneHgRWT8qtcMxWtgnCFaPzkKbFnPoxpvg0VY= 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)(396003)(39860400002)(376002)(136003)(366004)(346002)(5660300002)(6666004)(6506007)(41300700001)(26005)(6512007)(8936002)(86362001)(66946007)(316002)(54906003)(478600001)(66476007)(6486002)(66556008)(4326008)(8676002)(2906002)(36756003)(186003)(83380400001)(107886003)(7416002)(2616005)(38100700002)(14143004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ARSHuESIg3SAbtY/c1geWpBtnQqH8AeEUOp/ZLDukOlQ918g0zdsfEZLAtBTGyuBgugUMNyzJTAxlOqnF2JidjRMsP6OWP9lsi3b5ZDx2KYJnn6/Lc9F+EU1FNd/iqI2OGhWAYfBuavv5FSflwWH52V0EFE3fa8Jl8+k3xopR+yWL4BA3PHBavMXaapy7xZNoEbuiLznSasr81q2xlu31PMv1ZO65GzCMWXNQziUvDDUWrfqLUEOoNDvKdlosyVLdufvQCEHsO6E6Tfn6VdHXkM/Y0WeoFVmMG7Ryk7w8PhQCCuCm3pRmQr3iEwT9g4DVRhNYf7KpR+Q818b1IyWq9jO0w3k67NTtVwMZEFNbVRED/JyjpR5wTIXJCyNkzqIgsu2OtP2tdIWf1YCQVmHAl11USWGuf+VF9vKWBz5CDamFuuAAedFqiau6EAuzB2P6f6eUfkxqtWOP3rlHiGO7Vo+tp49mwBIKDcF8YYRVnbQZIoIUVg5SyhIhIvxLcN06rR+6163+Z14HCEi5EAyN5FwXc28aeQxYQ+GYS9oxSnwM55ou6BDJSOtnh282hMgSp+WpGNsGhu1YV7oLJ9Eh4Kmw66qZeaZXnRzVlbsHBAG2GqSAip0H03W6ER28TkvNaSMp3Yhczaa6ALS1Q/wbZFInDZ4/J9yCSTcECZLHGncFaJB5wIDHDdH7dfH2V+HnF1Dt4reiFh/giU/x08E+5SM7TSSEx+ly4PdXo8ZIZDuitMZuf5UPw9R/E6lfJjy2bkWWJFCLBffBQFhhwxebh/3hlgl2J+ml9cPvpdMa9yUvRc3Fo/n7WDyoGTXywlwd5TtfWG7r+z/+TofN+aotLFAH1RY1cavt5vswfoTNsglwPiLer64SRqJ2PNeqp5UDXFX1j3tdSsYHhC7RIgdeRbcOgaUMunwTt/PjBhPsbvbSZudy9FUESzaHrFFxi/dMPV8483+wRP/fRuXauB3nqgNSVrkl4QTwVMw04bBLV980iz4v4JgyRGtlPi4UiFLg4t2hQvJwCU8qVyDLhkQnA3DfgyWD/OM9UKIwq8VyTnVTNv8bsuXBmsBpBPIpBvJVhOFFt/uKjscDFjV3Z/myCvcFl9KC+vK/NL733XUFX3hJKz+G9+NeHwI4z0YesC9IVS7NIRwtMMunUrqKtLG4cEM4uFmBM8QTgRJ0gSVmCUYocMFU6tgJ/N+u+Pkwe3QyLXxCGX+aqh/lgeVI0K454XZAQHmMGgd8XA0+vEKbiMidPETiqBFBFkk8EaXVG/Uve+88f0g8mwOAdJr0m2mcWXzDyhns92LmcGWvRlMZs+sqktgzGrGXiQ8gSiSBFTTaEBsmoMbzpzyFpI3J23/u+j+FODFwOcPkioLoEZfjZ4rMcgv2TkD6WaWlSojIxWkE9zNbduHRWnh5OcWMJzdCsoAxaaWnhcIGhY69JWVecPzZdQ8L7pGJN1t0ySzfv6urFuIYonbtWTN3NHSJee3RVbxhXjjiJ8al/bVzjIEJ3bMk116fwJ2H+K7iW0h5sVQ1fz7FoNRAUNNqzPouFVBdKbe3WQrIHclAICoPH2sePTVIk5wmZmwp78frQGqPygd X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5e0cf9f9-0ff2-41c8-56e3-08da7c22c810 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Aug 2022 05:23:25.4686 (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: +QM3/thaEV5+TmkPKdEnM0UHNs58xRz8d3bCJdlIsSSyeGBY0yOjk8jNRL3WMtN+TjZ09gvJdmopbuXJcRNrEg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3978 ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1660281808; a=rsa-sha256; cv=pass; b=S6nMkpyXUuVo2NGOMDHnQ0vUl0j9C0kfbrvANS4grRg/h16WrvZJzw4yAi2NnzvysjmtjH X0g9PX141cbNYtHNZHhWyX5/wanoYmncoaDlA4LJPM8vVZUwwxhGGBYgHjZzuEldS/PDtv DdEjz4IGAsDPZCnxtgP7Nr0905gSWHo= ARC-Authentication-Results: i=2; imf29.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=WvsxuMbp; spf=pass (imf29.hostedemail.com: domain of apopple@nvidia.com designates 40.107.94.74 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=1660281808; 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=OzBiq1CI9sFdXngnkKzPb3ckbdfMshqhkqGWFrXfo8lHBfQVsTW5g0FAxZHE+9AxYD0xs2 1qQpbCz23sQIbXX7baXjGUzrab04nwEWcenyu3RCHRoVxmhTQFesbLAbmYCmTZ9GPm4rg0 YS6iieY+gleuTIXSC2QMdmUDv5JaCkY= X-Stat-Signature: 9dptu9ub3s3zxofw5jqcft1thsrzth1t X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 11203120183 Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=WvsxuMbp; spf=pass (imf29.hostedemail.com: domain of apopple@nvidia.com designates 40.107.94.74 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-Rspam-User: X-HE-Tag: 1660281807-308407 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. */