From patchwork Mon May 16 23:14:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851697 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA5C3C433EF for ; Mon, 16 May 2022 23:15:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350265AbiEPXPJ (ORCPT ); Mon, 16 May 2022 19:15:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44496 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240651AbiEPXPA (ORCPT ); Mon, 16 May 2022 19:15:00 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2064.outbound.protection.outlook.com [40.107.220.64]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C017546B21; Mon, 16 May 2022 16:14:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=j/n7BGqhpkQxYNKueqcYBCWOabK4EHtXKUHCdIFpf/A1uYUy7RngRqZNlKzUz47q0p3/iBmE1E0IgQDvM9CrI1gOZyjZVI4jssEEIIsdSC9+hkJuxi9aoz4hxR9Bfj0psVyueZnuMXPy8IxJVvJ8V2Zkx3A6EJmcVxkZvn20/azCiI+M+xKN6J3eVR9f0ZsGqNsuXo6YKtLe9BL6YGcm9yGXWfdgqJO0ih/+nlRFfOuWYdY3WU4Wjd81HtXCg2KoCtGxlTlNOHTcGSt3ci2prRGd/DDSUJ26rN2zClT918bBUtskO1PYSzimWCsxae24/idgrwpFYrvy5zBTlIm/6w== 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=uyHGPwSe3v1LETaDtyafG2r/bh8A0lXb5WCuxOn9RB8=; b=bxO+TOxp6vYrvZa9YHe1wSG4yWefcGpzrgnJEa9Gh1aE65O0OMxy8U5wCK2oKbuZHsUaBDKoek0vmQWc+Aa4D6avahhuZYFQOFYetL4Q5EKCRj7DWEiL1Cy//t6UB+dtkKDKlIspASS1AKmrW6CCEkOjI4WNZvFx5HDQhAw3RlsL8+LDGZ87tJ+899lzjvfYBvQVnkQGVfbTky8EO2vTTb8xrPMx89IDcVASxNONEOQianVdN/qQMxsPuExUfy3UykSuWsGE5zL7BOlXhOv2oc+VG0+gwZNDbS2ka6gc7v8wiWVf0Cmp+spmUCg/1TopJ19e62AHOcr40HwH3A65CA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uyHGPwSe3v1LETaDtyafG2r/bh8A0lXb5WCuxOn9RB8=; b=un3grWSI9YtmfnzsePL/SEGM8uz5RtB4Pf0HEIKKimBmw1vzUILCdtfehIwhnSB5KQH7TV5BSrG1tJSRulU23vtemIVWE8fhGhgwBxQokbaZiDF27qc0luL33ZJuyq22/qJl4JeBHOdJFWR16sP2c6qqQvinpekD3W2sc+Lto08= Received: from BN9PR03CA0361.namprd03.prod.outlook.com (2603:10b6:408:f7::6) by MW2PR12MB2585.namprd12.prod.outlook.com (2603:10b6:907:3::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14; Mon, 16 May 2022 23:14:57 +0000 Received: from BN8NAM11FT051.eop-nam11.prod.protection.outlook.com (2603:10b6:408:f7:cafe::fa) by BN9PR03CA0361.outlook.office365.com (2603:10b6:408:f7::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.15 via Frontend Transport; Mon, 16 May 2022 23:14:57 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT051.mail.protection.outlook.com (10.13.177.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:14:56 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:14:55 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 01/13] mm: add zone device coherent type memory support Date: Mon, 16 May 2022 18:14:20 -0500 Message-ID: <20220516231432.20200-2-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 78748352-393e-4f63-48d7-08da3791e487 X-MS-TrafficTypeDiagnostic: MW2PR12MB2585: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: /6EOcH41ZHzX0EbzmEDulFXPGn1C6B6Qw+FHgbOAc10jToM4rjyi3Nyiv0km9yftWAHxnw0pKgsNJWqwNSUrK635XGNvPOL1UznzmoW+nUlFJVTeBwhBs3IVtojoPWlSixaRaEHAidVzxNDMT3KilTKsRkGx8fo5WgkfgqG5JB4Ycj9dgxyxb3wI96RKvW5J/A7vVhkedR0MEjX6UrMFiy2zyQ+W04977omkAH57NhTCVI/qNTiSSgUmSzCQcvk06wmgRhqmHduqm2O/pUaEmT0FhK+Aub2lpvb/1VPx5FekyZW3hk7n+26Pna06Wfcm6RloBnMoxPgd3wlBGJ6IcVbNs8nOpuUcuREnH8sQy9A9FgRS5H0SxuDo08rpZkodhI60eReVKzVTtId2zFV6wWFgwHC5/8yLhUbHvT9xi714bJyARXS2bjfjtQIFpvuHD4y7tHdd3ZBJtxLfjFCpQxsHFNO6nM6YJTuVnpHDMXqjKQLRq9n+uxgVJXB6pW59vAOEI3yj4fVO+miia7Ukm+swsK1151T+5PPY8qTYsuJq1Pretzshpn7gxW3wrf4hmxb2fQlCz/EBlBjUGTO6HtUvf7wD7u3uOTrzMz3kCdgUMo7kpX4GBbON4C3gYC6slnFWVSV6DipMN8v2cBIfxz9wcaMvOmgBUf9y5vR/NQDqtzl36EoYdyta9knX3HC4+zAqTja7CPiMhAfySjGm3w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(36840700001)(40470700004)(46966006)(508600001)(44832011)(7416002)(8936002)(16526019)(83380400001)(1076003)(426003)(36860700001)(81166007)(336012)(5660300002)(356005)(86362001)(26005)(40460700003)(2616005)(47076005)(7696005)(6916009)(54906003)(70586007)(2906002)(6666004)(8676002)(4326008)(70206006)(36756003)(316002)(82310400005)(186003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:14:56.9578 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 78748352-393e-4f63-48d7-08da3791e487 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT051.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR12MB2585 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org Device memory that is cache coherent from device and CPU point of view. This is used on platforms that have an advanced system bus (like CAPI or CXL). Any page of a process can be migrated to such memory. However, no one should be allowed to pin such memory so that it can always be evicted. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling Reviewed-by: Alistair Popple [hch: rebased ontop of the refcount changes, removed is_dev_private_or_coherent_page] Signed-off-by: Christoph Hellwig --- include/linux/memremap.h | 19 +++++++++++++++++++ mm/memcontrol.c | 7 ++++--- mm/memory-failure.c | 8 ++++++-- mm/memremap.c | 10 ++++++++++ mm/migrate_device.c | 16 +++++++--------- mm/rmap.c | 5 +++-- 6 files changed, 49 insertions(+), 16 deletions(-) diff --git a/include/linux/memremap.h b/include/linux/memremap.h index 8af304f6b504..9f752ebed613 100644 --- a/include/linux/memremap.h +++ b/include/linux/memremap.h @@ -41,6 +41,13 @@ struct vmem_altmap { * A more complete discussion of unaddressable memory may be found in * include/linux/hmm.h and Documentation/vm/hmm.rst. * + * MEMORY_DEVICE_COHERENT: + * Device memory that is cache coherent from device and CPU point of view. This + * is used on platforms that have an advanced system bus (like CAPI or CXL). A + * driver can hotplug the device memory using ZONE_DEVICE and with that memory + * type. Any page of a process can be migrated to such memory. However no one + * should be allowed to pin such memory so that it can always be evicted. + * * MEMORY_DEVICE_FS_DAX: * Host memory that has similar access semantics as System RAM i.e. DMA * coherent and supports page pinning. In support of coordinating page @@ -61,6 +68,7 @@ struct vmem_altmap { enum memory_type { /* 0 is reserved to catch uninitialized type fields */ MEMORY_DEVICE_PRIVATE = 1, + MEMORY_DEVICE_COHERENT, MEMORY_DEVICE_FS_DAX, MEMORY_DEVICE_GENERIC, MEMORY_DEVICE_PCI_P2PDMA, @@ -143,6 +151,17 @@ static inline bool folio_is_device_private(const struct folio *folio) return is_device_private_page(&folio->page); } +static inline bool is_device_coherent_page(const struct page *page) +{ + return is_zone_device_page(page) && + page->pgmap->type == MEMORY_DEVICE_COHERENT; +} + +static inline bool folio_is_device_coherent(const struct folio *folio) +{ + return is_device_coherent_page(&folio->page); +} + static inline bool is_pci_p2pdma_page(const struct page *page) { return IS_ENABLED(CONFIG_PCI_P2PDMA) && diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 598fece89e2b..3e1f97c9fdc6 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5629,8 +5629,8 @@ static int mem_cgroup_move_account(struct page *page, * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a * target for charge migration. if @target is not NULL, the entry is stored * in target->ent. - * 3(MC_TARGET_DEVICE): like MC_TARGET_PAGE but page is MEMORY_DEVICE_PRIVATE - * (so ZONE_DEVICE page and thus not on the lru). + * 3(MC_TARGET_DEVICE): like MC_TARGET_PAGE but page is device memory and + * thus not on the lru. * For now we such page is charge like a regular page would be as for all * intent and purposes it is just special memory taking the place of a * regular page. @@ -5664,7 +5664,8 @@ static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma, */ if (page_memcg(page) == mc.from) { ret = MC_TARGET_PAGE; - if (is_device_private_page(page)) + if (is_device_private_page(page) || + is_device_coherent_page(page)) ret = MC_TARGET_DEVICE; if (target) target->page = page; diff --git a/mm/memory-failure.c b/mm/memory-failure.c index d4a4adcca01f..c8a0e9fcdf42 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1695,12 +1695,16 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags, goto unlock; } - if (pgmap->type == MEMORY_DEVICE_PRIVATE) { + switch (pgmap->type) { + case MEMORY_DEVICE_PRIVATE: + case MEMORY_DEVICE_COHERENT: /* - * TODO: Handle HMM pages which may need coordination + * TODO: Handle device pages which may need coordination * with device-side memory. */ goto unlock; + default: + break; } /* diff --git a/mm/memremap.c b/mm/memremap.c index af0223605e69..471ec84ed82b 100644 --- a/mm/memremap.c +++ b/mm/memremap.c @@ -314,6 +314,16 @@ void *memremap_pages(struct dev_pagemap *pgmap, int nid) return ERR_PTR(-EINVAL); } break; + case MEMORY_DEVICE_COHERENT: + if (!pgmap->ops->page_free) { + WARN(1, "Missing page_free method\n"); + return ERR_PTR(-EINVAL); + } + if (!pgmap->owner) { + WARN(1, "Missing owner\n"); + return ERR_PTR(-EINVAL); + } + break; case MEMORY_DEVICE_FS_DAX: if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) { WARN(1, "File system DAX not supported\n"); diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 70c7dc05bbfc..7e267142ea34 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -499,7 +499,7 @@ EXPORT_SYMBOL(migrate_vma_setup); * handle_pte_fault() * do_anonymous_page() * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE - * private page. + * private or coherent page. */ static void migrate_vma_insert_page(struct migrate_vma *migrate, unsigned long addr, @@ -575,11 +575,8 @@ static void migrate_vma_insert_page(struct migrate_vma *migrate, page_to_pfn(page)); entry = swp_entry_to_pte(swp_entry); } else { - /* - * For now we only support migrating to un-addressable device - * memory. - */ - if (is_zone_device_page(page)) { + if (is_zone_device_page(page) && + !is_device_coherent_page(page)) { pr_warn_once("Unsupported ZONE_DEVICE page type.\n"); goto abort; } @@ -682,10 +679,11 @@ void migrate_vma_pages(struct migrate_vma *migrate) mapping = page_mapping(page); - if (is_device_private_page(newpage)) { + if (is_device_private_page(newpage) || + is_device_coherent_page(newpage)) { /* - * For now only support private anonymous when migrating - * to un-addressable device memory. + * For now only support anonymous memory migrating to + * device private or coherent memory. */ if (mapping) { migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; diff --git a/mm/rmap.c b/mm/rmap.c index fedb82371efe..dcf14d225a4d 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1848,7 +1848,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, /* Update high watermark before we lower rss */ update_hiwater_rss(mm); - if (folio_is_zone_device(folio)) { + if (folio_is_device_private(folio)) { unsigned long pfn = folio_pfn(folio); swp_entry_t entry; pte_t swp_pte; @@ -1995,7 +1995,8 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags) TTU_SYNC))) return; - if (folio_is_zone_device(folio) && !folio_is_device_private(folio)) + if (folio_is_zone_device(folio) && + (!folio_is_device_private(folio) && !folio_is_device_coherent(folio))) return; /* From patchwork Mon May 16 23:14:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851698 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5743BC43219 for ; Mon, 16 May 2022 23:15:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240651AbiEPXPK (ORCPT ); Mon, 16 May 2022 19:15:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350243AbiEPXPB (ORCPT ); Mon, 16 May 2022 19:15:01 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2072.outbound.protection.outlook.com [40.107.223.72]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EA2746657; Mon, 16 May 2022 16:15:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mGycQLZWpk1jFm9bT5KIJ8eBDNm1I54th9zc2VUj8CbrMrHN2KTat/tOHfSIcxAaYT3oQEIoZH51lcDv8MK4V2mAm6N981LokwXqcx/AQ7ahkV9sqeupOkwEu8GrzTCELI7Utklqgu6HVT4jCiZRAvOW0UPhyGSuwuw8gNb3UtjRVGjTdJ07RVw7qjH3RCwelHVOwvq2kQpRmx44JH9NhG9OX3IYWDJFD8fkd7QXo/uu8XiwPAr1B25a9iMU2UZPT4uj/Io0ue/wpf/rjCNby4Kdq5670ucVkPSR6k+Y406mIQtratiEthCn3NYtQAz8UTLXsncozi7ufp67cIbmNg== 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=vW0Kci1BxH/G6MOqRePi9dIvH+J6rgyknG8pfmabAb0=; b=Cdcww4ZrclRAMxIfv/JH73PI9iJmm7wPOnwS7b0zHrdCXC3UH9Abuab4trPxdlMsoqr8TbqR2HWidD1/qynptaZm2n42IDBV1wpnBs5EmEOC172uo9CZlYOveTIoYTj2c5rpIdQp8Li9p1dDewsHzDDTyJmWw6V2ZwKdtYvHcn7qkU/77tja7Zcxbr8tJbaumUGb7FkHxzMzzuDGB87vWKVmefm/A0MaZ33sYaqamFUGd6phbnDILkizsnQTWFXu0NN9bgD7hC8RwEIBantjkF754RCazVvOLx65h5Dx/n1akF93SqdsVTefK7L+y0cKma8VD/2d/0NCTvD862dxNg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vW0Kci1BxH/G6MOqRePi9dIvH+J6rgyknG8pfmabAb0=; b=u/s9X83HfiLN03WjvO8F6VDWORylPzL7gY1O9gBWMPM0nqfWa41Yid5KcatytaXea6YK+FKo8WJaAay7m35KHsSy+1ndHTMEfVgwNPMo9i8XxVAbL4giV4lrMrxMBEUT9H3Rp2nYBMJFOmnpvYlK+kfmBuLUL9VpTi+ItCQ29JI= Received: from BN9PR03CA0383.namprd03.prod.outlook.com (2603:10b6:408:f7::28) by SN1PR12MB2574.namprd12.prod.outlook.com (2603:10b6:802:26::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14; Mon, 16 May 2022 23:14:58 +0000 Received: from BN8NAM11FT051.eop-nam11.prod.protection.outlook.com (2603:10b6:408:f7:cafe::ea) by BN9PR03CA0383.outlook.office365.com (2603:10b6:408:f7::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18 via Frontend Transport; Mon, 16 May 2022 23:14:58 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT051.mail.protection.outlook.com (10.13.177.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:14:58 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:14:56 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 02/13] mm: add device coherent vma selection for memory migration Date: Mon, 16 May 2022 18:14:21 -0500 Message-ID: <20220516231432.20200-3-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0f0267e8-b2a9-4983-c82e-08da3791e52b X-MS-TrafficTypeDiagnostic: SN1PR12MB2574: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: xemjRM8QmqgDayYUVnNcmNuSf3D+ZNZEM81/soXIrjNgmdMwpvDA4AyVDGLDcPfIYtfzZN7GzFXH+6V1XY/8ThTDXO1nViQZ55tIPy0CFDqSFiZ2WbvK4nc+hnCkEqYNI1WoCSqI+rvWVIKqiJ8bs+8125QznvjY9PlFsDZ+IIX+/pn+awv/uxOex+8BGe2JUbzgzERPVJznKcKMist5bh4CQS5+ykuGQw/U7DYJ6Y1cruU0Vxv3mHGsK1EvpwWGn6dvmEWQTNrb6a51uwy0aAvytC3lWNIZ8RPT7MBfqzKm25RVypbH5HvteT8DuTdZzk6dx7dEc1rHL/5tpsRUH/IpNjHrB60SnexD+VQJ9KDsCgLpFlUKjekmHObHX/FtawyCWVNVyG8q3YewiGAj98M8JdMmFMS7DAQ1tASJamGWNvsQzuJa3OOllM4hAV7wwWzCcETuXZTf8kG13O8Op6/tRw3cJcY5LdREc1e1RkxPC1D9Eajw0QiHsyBKzVKLrs0LjbhZLBWTNoMViprLlcSw6iZzySqaduiko0+z0l92bYmVasjWdinwyGrQS9GcI9g1L/W7X3KSiuO1IzUcwGFfTW0XWM97MO2PFx+7SYVnJBbNV4EYo34CShhp8peiWMCe2Ks34TL0RZoICunc0KkRHa3AZIRZewLnfOMbYWsCgGu45Xq+hluF4/4KsZ23HD7ms9VpfuyL52Eo38/Vgw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(46966006)(36840700001)(40470700004)(47076005)(426003)(336012)(86362001)(16526019)(186003)(316002)(83380400001)(1076003)(7416002)(5660300002)(44832011)(26005)(8936002)(4326008)(508600001)(54906003)(70586007)(70206006)(6916009)(36756003)(356005)(8676002)(2616005)(2906002)(7696005)(6666004)(82310400005)(40460700003)(36860700001)(81166007)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:14:58.0358 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0f0267e8-b2a9-4983-c82e-08da3791e52b X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT051.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2574 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org This case is used to migrate pages from device memory, back to system memory. Device coherent type memory is cache coherent from device and CPU point of view. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling Reviewed-by: Alistair Poppple Signed-off-by: Christoph Hellwig --- include/linux/migrate.h | 1 + mm/migrate_device.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/linux/migrate.h b/include/linux/migrate.h index 90e75d5a54d6..a4a336fd81fc 100644 --- a/include/linux/migrate.h +++ b/include/linux/migrate.h @@ -138,6 +138,7 @@ static inline unsigned long migrate_pfn(unsigned long pfn) enum migrate_vma_direction { MIGRATE_VMA_SELECT_SYSTEM = 1 << 0, MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1, + MIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 << 2, }; struct migrate_vma { diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 7e267142ea34..a0b997935cf9 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -148,15 +148,21 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, if (is_writable_device_private_entry(entry)) mpfn |= MIGRATE_PFN_WRITE; } else { - if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) - goto next; pfn = pte_pfn(pte); - if (is_zero_pfn(pfn)) { + if (is_zero_pfn(pfn) && + (migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) { mpfn = MIGRATE_PFN_MIGRATE; migrate->cpages++; goto next; } page = vm_normal_page(migrate->vma, addr, pte); + if (page && !is_zone_device_page(page) && + !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) + goto next; + else if (page && is_device_coherent_page(page) && + (!(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_COHERENT) || + page->pgmap->owner != migrate->pgmap_owner)) + goto next; mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0; } From patchwork Mon May 16 23:14:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851701 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE0A2C43219 for ; Mon, 16 May 2022 23:15:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350282AbiEPXPO (ORCPT ); Mon, 16 May 2022 19:15:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350244AbiEPXPC (ORCPT ); Mon, 16 May 2022 19:15:02 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2085.outbound.protection.outlook.com [40.107.237.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0791446B2B; Mon, 16 May 2022 16:15:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bjZE2e9bcegl7298mZGuI+zJ8Aq7us6Td7xR3EBKsihJt4EOT8gD4Zn7NJPMLAKYsCSWtXUs/t7KWeEA+3BQ4/tKNR780P+vBI84hfqjptQKN+X0+aI4IEOHGy+5vSY51XYXIwNA3ATeKzq15dwblkQNaFvDa1IcuuL4PKsuNGNVXJwAxQQlOuzoQDmmqiXn0ckXoUEsh5/pusnE/jY+lN7mnbSrjxQxQXgNWPqMrTxu/zHgwjttKyA5g14+ale9+Tk459g8Bpf5c/26k2W2wQ24sOVwepOXUuFrK9GRNBWb2Xv4BqoCZK9Fb5Ps1wqVnuvJUqhLVJhCrGm3zTBWcw== 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=519oerrRDEbTv7GQOYa1mMQ+Fz3ORBOf1G9j+BglRrg=; b=mGXoYneU/D5QPDNhf4VL+8cCX0vghYIbGZ0wqj4p+vY/JzRa5TWUpVjl0imxtgq2sWhh+J+TlT7LNwBSP1ljG5X6oOkFXLpemcVkE6mPLfh63l9M2nERLg43ISHGwjBepDD5U64wdI0TEkXt6IapFAX4aX27ZBAsQLwgAsxeBZp08SVVfUDD/JOmgJULwipcgo01biBsE0/ONEgKGByoE9JEB/4E48YeTn2+yCo3Cp4j+eMIrnxX+hXiGOcZUE2xR+lUqSmrwK37SNJJkbqh1E7mOXYqexEyrg+sJodW3GAlS0yySthSXYf+T8M3DS8mrfc8IDRvQ1hvKLjVn30/uw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=519oerrRDEbTv7GQOYa1mMQ+Fz3ORBOf1G9j+BglRrg=; b=QGof7MP+VX0skbHJTRpZFILF7FjXa7rte6Lil4eBMoIBrwursKSq5potuvOvdBPdJjBrYUbDNnqyK21XzSwZKR2VaQq6j579coVaSfu3JA8h6WNm1FQ4QqrSPNW+pE01/4M/1Q8RCDD08QeN49CaOkDxhUbs1S8143pSX+8Q13M= Received: from BN9PR03CA0387.namprd03.prod.outlook.com (2603:10b6:408:f7::32) by DM6PR12MB4204.namprd12.prod.outlook.com (2603:10b6:5:212::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14; Mon, 16 May 2022 23:14:59 +0000 Received: from BN8NAM11FT051.eop-nam11.prod.protection.outlook.com (2603:10b6:408:f7:cafe::8c) by BN9PR03CA0387.outlook.office365.com (2603:10b6:408:f7::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14 via Frontend Transport; Mon, 16 May 2022 23:14:58 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT051.mail.protection.outlook.com (10.13.177.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:14:58 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:14:57 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 03/13] mm: remove the vma check in migrate_vma_setup() Date: Mon, 16 May 2022 18:14:22 -0500 Message-ID: <20220516231432.20200-4-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 561bb88d-291a-47f8-86e0-08da3791e58d X-MS-TrafficTypeDiagnostic: DM6PR12MB4204: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: +khPt/n1FmZX8XqIAkfYFWqJQkQ0Yedk+XVR7mKqVJRuVgaq/Jl5kq/9drzq7KQxAOiwjavyAugYIjGdnOt5tFJKASN0JdvZIHTgACWqCnxVwC6eFwYbsobPF6skuba17Gh/6IeDjYydgbac9t+R+tv4AbJpL1+x207BwIg5+4VSPgbWSmW20FOSCKU2l/afo4oM5np/i6mT8WgYB8e0LTQnmEoPh43M3k9r5z+QlkTIvnu1c8ggASplgxSB1wpOurA0C18YVJl+nnBO9B3eyeDks4wXZlPcts9FPS6HRjz/HvFuAhJFOuyz3GhyIfTWGUbfRC4E+GaomTKvuuY5UflkRbiTrUR2/FASmmZ3eh5GcT3UZ8MCV1geq+xSkhrCClnUpmEX3nB39YNHU8YobpW8gsqTIWUlI7jQj9TRaP5TGxmDqVGsha+YuQcsOxYgnvR6WP60JJRs5mm6Dp29ETqGek7yhog2AlITMoLyQ953Wsfob+pErVI77o8q0tWsd8Pj8sEsEe1qMKVTcs4MHA632cCQ/m0gDXQOZEJTm6TS5pQqYHbfURTabR337YIwJ8u3DWU6UVeIHlrDig4EYGwV6jrHYidYWHmc6bJwofWIFTAVAPGcu1HjsKRw7mKjr/WZotC6a9Jsh6mHO97kowizyfOx7SrBwblIyzHmDmraV/0KCMl76QEdr/8IQBCoIRlQyzuE8RpspN9odVHP+A== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(40470700004)(36840700001)(46966006)(70586007)(8676002)(4326008)(70206006)(7696005)(6666004)(2616005)(36860700001)(86362001)(47076005)(1076003)(426003)(83380400001)(5660300002)(7416002)(8936002)(40460700003)(16526019)(186003)(36756003)(82310400005)(81166007)(26005)(336012)(44832011)(2906002)(508600001)(6916009)(54906003)(356005)(316002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:14:58.6764 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 561bb88d-291a-47f8-86e0-08da3791e58d X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT051.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4204 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Alistair Popple migrate_vma_setup() checks that a valid vma is passed so that the page tables can be walked to find the pfns associated with a given address range. However in some cases the pfns are already known, such as when migrating device coherent pages during pin_user_pages() meaning a valid vma isn't required. Signed-off-by: Alistair Popple Acked-by: Felix Kuehling Signed-off-by: Christoph Hellwig --- mm/migrate_device.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/mm/migrate_device.c b/mm/migrate_device.c index a0b997935cf9..b399c498f0aa 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -467,24 +467,24 @@ int migrate_vma_setup(struct migrate_vma *args) args->start &= PAGE_MASK; args->end &= PAGE_MASK; - if (!args->vma || is_vm_hugetlb_page(args->vma) || - (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) - return -EINVAL; - if (nr_pages <= 0) - return -EINVAL; - if (args->start < args->vma->vm_start || - args->start >= args->vma->vm_end) - return -EINVAL; - if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end) - return -EINVAL; if (!args->src || !args->dst) return -EINVAL; - - memset(args->src, 0, sizeof(*args->src) * nr_pages); - args->cpages = 0; - args->npages = 0; - - migrate_vma_collect(args); + if (args->vma) { + if (is_vm_hugetlb_page(args->vma) || + (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) + return -EINVAL; + if (args->start < args->vma->vm_start || + args->start >= args->vma->vm_end) + return -EINVAL; + if (args->end <= args->vma->vm_start || + args->end > args->vma->vm_end) + return -EINVAL; + memset(args->src, 0, sizeof(*args->src) * nr_pages); + args->cpages = 0; + args->npages = 0; + + migrate_vma_collect(args); + } if (args->cpages) migrate_vma_unmap(args); @@ -666,7 +666,7 @@ void migrate_vma_pages(struct migrate_vma *migrate) continue; } - if (!page) { + if (!page && migrate->vma) { if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) continue; if (!notified) { From patchwork Mon May 16 23:14:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851699 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 46926C433F5 for ; Mon, 16 May 2022 23:15:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350243AbiEPXPL (ORCPT ); Mon, 16 May 2022 19:15:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350245AbiEPXPD (ORCPT ); Mon, 16 May 2022 19:15:03 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2044.outbound.protection.outlook.com [40.107.223.44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4951C46B31; Mon, 16 May 2022 16:15:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kXNX9ABoUKXJP+ufKtBqXLtVengdFOtnRTOI4c7XwNJB4gYkWnl5+Uhj/UktW3N0rK7VdBVGYlPwCRGyGq5j55YlAC9tpn3P6lQHl+wn5o+GWP9MZbXfZtcdz8bHiMR3hLMZ9R5Fea0AgnqVD3/cUcaE9Dy2qerGqXalE0SgaoO/i3Ia88zsCVdK1NgPimMCTbuEjZtw7NGh8pYccK4Rwcbgql4q5C7Z5aQZ9bc/FHzpFy3lgMx8DRczyaUvCa/FndqmKum0q11b6EFt4LHTEhBIeXHOhv6pGcSw7SwyP+wlRUTcmOz5dpZ+Y+3H31fYHKw2GJXwJdqpDseSq7Qe2g== 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=cLboctgqtU8q+2w0PX43DNmgl62uyxYt9SRLY3lWywg=; b=Gmd9kTkJWDHwE2tKOYwZCsb34Yc4vw1YfQNMoYCOKB/7RZXA3wRFMB1Gz1SEi3QzgpWbhzrRB8ChvQ7w7l2YU3C67StgXJxUQaNKv23xmkzZp6hmwu/0vIrk/7e5XphwLRiI4X46Zz4xEPzZXDRrD5sbFbYSzVzZYibQKvJmsMzH6XEF/THpKd7IzHrHlPj4wc/Gpj7Cx5zmcr0f+axDZTa0f6hng9l5RVPp/0EFgQTXWI/t+GIBtkrCAoWuWk/0DQUr1D7IkvwVVdEwCjU4HAdiG248IEzn6W7DH4BAZUb3Xcf7uNrzamhSu2Nf8DyuWLlQNvI5Wei61oafd8eg8A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=cLboctgqtU8q+2w0PX43DNmgl62uyxYt9SRLY3lWywg=; b=SzzOTTFGPx8dCcszRd8MgEO+fLdb6ZFOaRWV4GPiW/u7yKI0dywcGY0sdD6tLC2VlX2uaQIgdc+rX3muCU59uEGImmzTO4mverDNxqEdFMVYJXcYKz7v8ffVO9OH5+5dTWgZE9amvqC8D+q5z+spcBS0p1QHba0JN+dZVzkQd5E= Received: from BN9PR03CA0367.namprd03.prod.outlook.com (2603:10b6:408:f7::12) by BY5PR12MB4321.namprd12.prod.outlook.com (2603:10b6:a03:204::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14; Mon, 16 May 2022 23:15:00 +0000 Received: from BN8NAM11FT051.eop-nam11.prod.protection.outlook.com (2603:10b6:408:f7:cafe::62) by BN9PR03CA0367.outlook.office365.com (2603:10b6:408:f7::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.15 via Frontend Transport; Mon, 16 May 2022 23:14:59 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT051.mail.protection.outlook.com (10.13.177.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:14:59 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:14:58 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 04/13] mm/gup: migrate device coherent pages when pinning instead of failing Date: Mon, 16 May 2022 18:14:23 -0500 Message-ID: <20220516231432.20200-5-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 35c167ff-ce42-45cc-9562-08da3791e5de X-MS-TrafficTypeDiagnostic: BY5PR12MB4321: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: QAGUl2dtF1RPfEgOUhor2NALYcSdtfabqwg2ey5pmJ79Ow61tfFu7teWjJBa1nbMXJazkk1PzD/JTxYJxShMfk29YIq/q+lgjXm0UOnqUFtONlnPgY+ePoKW5gRJWA63LQpFBPAR4hVVmBDlefukSWacnWhisp3wv+ESDVlxIhpAoEQ3+DSoV6zx4YOH3EcwyS+NjH44DWTpqB1Fdx7wh5QV4hyGogOGAeDn7nNxxm6fhD2ecZUX6Paf84YfIpCnWKPuxAlbxLsCiiIAVdQv6YlaZNoFSeItivLwk9fUmuB4d6qcVxBOau1UmQlTTaBrzohfl/gNPUCMlJJki1Kg6DwPXeIdBmNyNQYH47ewjfnL7qdPOPmipD8mI6F2OzTEx4UZVzSOAKj8V3apWw9rhYRPExd3PcBi7VTG99zN/kbxwaWTuG472hN9qDtc6E+DNulVB53FOAip+FKvbESgWNMTvDgdwl42aHJNe0L6JhEW5NQJtMhYIg+8FRxAJglRd7vmQVxk+eSNioLhRztDzDT701oN1HPxQC5nirdAShEzdVbwpeE+0VMzTQ9LWrWLNIQmLPufp/6YmZm0DRdnztwE4vz9tC+HZujWbdfhLU7X2IdHPiJlGbj6J/JBphVIFtVVe42wuE8qcHKdLxSfibuKVVWvv0TE2Q8Xn5csopqmrnzqEaqyhEncAOvKu1fAlO8RiQlUEMvP/Rtp6vWK2Q== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(46966006)(36840700001)(40470700004)(86362001)(81166007)(26005)(4326008)(8676002)(70206006)(70586007)(16526019)(8936002)(508600001)(7696005)(356005)(186003)(336012)(1076003)(44832011)(6916009)(5660300002)(7416002)(36860700001)(2906002)(54906003)(2616005)(6666004)(316002)(36756003)(83380400001)(47076005)(82310400005)(426003)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:14:59.2076 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 35c167ff-ce42-45cc-9562-08da3791e5de X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT051.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4321 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Alistair Popple Currently any attempts to pin a device coherent page will fail. This is because device coherent pages need to be managed by a device driver, and pinning them would prevent a driver from migrating them off the device. However this is no reason to fail pinning of these pages. These are coherent and accessible from the CPU so can be migrated just like pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin them first try migrating them out of ZONE_DEVICE. Signed-off-by: Alistair Popple Acked-by: Felix Kuehling [hch: rebased to the split device memory checks, moved migrate_device_page to migrate_device.c] Signed-off-by: Christoph Hellwig --- mm/gup.c | 47 +++++++++++++++++++++++++++++++++++----- mm/internal.h | 1 + mm/migrate_device.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 5 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index f598a037eb04..a214c8df7140 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1786,9 +1786,43 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_folio = folio; - if (folio_is_pinnable(folio)) + /* + * Device private pages will get faulted in during gup so it + * shouldn't be possible to see one here. + */ + if (WARN_ON_ONCE(folio_is_device_private(folio))) { + ret = -EFAULT; + goto unpin_pages; + } + + /* + * Device coherent pages are managed by a driver and should not + * be pinned indefinitely as it prevents the driver moving the + * page. So when trying to pin with FOLL_LONGTERM instead try + * to migrate the page out of device memory. + */ + if (folio_is_device_coherent(folio)) { + WARN_ON_ONCE(PageCompound(&folio->page)); + + /* + * Migration will fail if the page is pinned, so convert + * the pin on the source page to a normal reference. + */ + if (gup_flags & FOLL_PIN) { + get_page(&folio->page); + unpin_user_page(&folio->page); + } + + pages[i] = migrate_device_page(&folio->page, gup_flags); + if (!pages[i]) { + ret = -EBUSY; + goto unpin_pages; + } continue; + } + if (folio_is_pinnable(folio)) + continue; /* * Try to move out any movable page before pinning the range. */ @@ -1824,10 +1858,13 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, return nr_pages; unpin_pages: - if (gup_flags & FOLL_PIN) { - unpin_user_pages(pages, nr_pages); - } else { - for (i = 0; i < nr_pages; i++) + for (i = 0; i < nr_pages; i++) { + if (!pages[i]) + continue; + + if (gup_flags & FOLL_PIN) + unpin_user_page(pages[i]); + else put_page(pages[i]); } diff --git a/mm/internal.h b/mm/internal.h index cf16280ce132..32c621416966 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -750,6 +750,7 @@ int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, unsigned long addr, int page_nid, int *flags); void free_zone_device_page(struct page *page); +struct page *migrate_device_page(struct page *page, unsigned int gup_flags); /* * mm/gup.c diff --git a/mm/migrate_device.c b/mm/migrate_device.c index b399c498f0aa..1e74aa7ab580 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -775,3 +775,56 @@ void migrate_vma_finalize(struct migrate_vma *migrate) } } EXPORT_SYMBOL(migrate_vma_finalize); + +/* + * Migrate a device coherent page back to normal memory. The caller should have + * a reference on page which will be copied to the new page if migration is + * successful or dropped on failure. + */ +struct page *migrate_device_page(struct page *page, unsigned int gup_flags) +{ + unsigned long src_pfn, dst_pfn = 0; + struct migrate_vma args; + struct page *dpage; + + lock_page(page); + src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE; + args.src = &src_pfn; + args.dst = &dst_pfn; + args.cpages = 1; + args.npages = 1; + args.vma = NULL; + migrate_vma_setup(&args); + if (!(src_pfn & MIGRATE_PFN_MIGRATE)) + return NULL; + + dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0); + + /* + * get/pin the new page now so we don't have to retry gup after + * migrating. We already have a reference so this should never fail. + */ + if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) { + __free_pages(dpage, 0); + dpage = NULL; + } + + if (dpage) { + lock_page(dpage); + dst_pfn = migrate_pfn(page_to_pfn(dpage)); + } + + migrate_vma_pages(&args); + if (src_pfn & MIGRATE_PFN_MIGRATE) + copy_highpage(dpage, page); + migrate_vma_finalize(&args); + if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) { + if (gup_flags & FOLL_PIN) + unpin_user_page(dpage); + else + put_page(dpage); + dpage = NULL; + } + + return dpage; +} From patchwork Mon May 16 23:14:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851704 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E054EC433EF for ; Mon, 16 May 2022 23:15:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350244AbiEPXPR (ORCPT ); Mon, 16 May 2022 19:15:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350253AbiEPXPJ (ORCPT ); Mon, 16 May 2022 19:15:09 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2046.outbound.protection.outlook.com [40.107.92.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0DD346B3E; Mon, 16 May 2022 16:15:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Qxpn3Ng79XJ6NCZ1PuIQGxKccc0v12wvp2nJXDrAvE5W01p7vdVCjzIt/iV/hXHabuMwOUF1MhCkItbbl9XNVPZ+WQSObSl0/UPYOwB4jI5fsNhjCGEP2+L/jMa/QBzmoI7/Voa/as1FhPBPwqMrg1UNXZJB8YsJLGrfEAE0FeP0fFhTVN+toKQ62n7KXjyagHvyZyinxeNEuMaSqm3r6k7mKJMJIranQt23fF8+RqtgFF2/WM2bd0H7A6QsTLXUuqMpIoCsiFkSdPk0sQsq4fdRS5aePmRlx8OYgDscIMAb5/2/lBt56SSrW0Ed62t8fENODj5G/T3D3U93SpT/AQ== 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=nl288P0aFMxHvyTdajqKdYE2bhkbzZtjR6cg4VQwpEg=; b=kRnIxbrKUHTvmTqumiYUL+Eb7ORNwOWDKpBYzK2MYvzpSwQxei83mY78NctGwsnJQApFjysr8s7WDpM+Dp60MxvAm0JyY6golbRdU9EEjfyIlDakkEgnMPoTOB1PUNEGT5ER/RGXPf6F8mYWZRXrDvGv4oyBlNY4axmmJjodOGpTTldbJ59DoO9rXXZyjxkcFJVhzE3UJmZv1RLOpPJxuFsoDlvb1oNKODtsQ/6MdHvyN9mes4En3Mh/oW28ioHKyloW8x8rvdSCy8znglCCyqFNi+fz5VdYdLlFqLh5xr2tsVfKKO4q+uC+oueSlAGfFZerq5G6ZZxwucnNFMAOdQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=nl288P0aFMxHvyTdajqKdYE2bhkbzZtjR6cg4VQwpEg=; b=ocvCMLTec9+0iVnrea0FBdWwHKS4DXrA7nQyFaLpohD+ZJvpit8n5Z2jvIHkK+RzQl4nqNDhY3AGI4cc7oxZubBVxlEXll0MR+HEniaxinhKLMl3XGZ0Y6jiBl5puf8/rhrBa08EMI2/gPF9icrmTx4g+jYKYTF5CX12XZ3JVMc= Received: from BN9PR03CA0369.namprd03.prod.outlook.com (2603:10b6:408:f7::14) by MN2PR12MB3182.namprd12.prod.outlook.com (2603:10b6:208:107::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Mon, 16 May 2022 23:15:00 +0000 Received: from BN8NAM11FT051.eop-nam11.prod.protection.outlook.com (2603:10b6:408:f7:cafe::e) by BN9PR03CA0369.outlook.office365.com (2603:10b6:408:f7::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14 via Frontend Transport; Mon, 16 May 2022 23:15:00 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT051.mail.protection.outlook.com (10.13.177.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:15:00 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:14:59 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 05/13] drm/amdkfd: add SPM support for SVM Date: Mon, 16 May 2022 18:14:24 -0500 Message-ID: <20220516231432.20200-6-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a86ff996-e302-4bcc-8987-08da3791e698 X-MS-TrafficTypeDiagnostic: MN2PR12MB3182: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: CMhDJrvW7UMUslFmym0jG1ZSwj40AJceDoZXsLDRX1xQ+4UiEV6X4UrNFULG7sNHhEkytyOphp0leebXJ9nk7jERDr4kWNdsRs0R+z6arnIWmr//Y6tnKPXZZzq/wb6hYsaVdYMsEGwQBEAIzH3DNa0naf3YJWC3hCi6YPGI3PylZYBooZ05jiMb5UZALc/6T4U5icE2RaHlZX5PnN3+kiR+JCVnWGdzw3htV4ny7xS7C9CFKo7PkTJkv/Jc3ZUzxJEpyM7tK8T5Oi1pJoHeNIdcc7kQpkeCxg6KYCIBPPff4uA27lQWCbRjEa5Y2axeocDg4stF6pVhxraZHSSl5TxVBSSuWy796Pnm/xVIYttKOADUWGC35+bKtCntkkcsrLiPhyzAAhLOmyTyCTZV2e0lr35Pt1brjG9bUQ8gnzvoRKp1xkYtRtAGIfbLuZQYQlUxohRAadouWIPTeViylNKQlMn6IncVVuYTahcm/dFGR6K3q6UR6256ihF9s2jKNGDz2wk0oNlZYzo+2xq1vQKO4XWBXUfESjZZEs5tfbYqDRQkZxQk6SCbnAUkF8Ca6yS9QUuyAM/WHCVzz6/EpD1laHa2isj03BhvJF11Hs7T9jHMmz/2qW8rROQKfg+b3KBlDlxbzCAqcyFKIV9stq5EJrDNDRC0KMce73oTRMzW6bHpRyg5/qD7C09F3Yds+nN1lxdM2TqDCPQUeiWmBQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(46966006)(40470700004)(36840700001)(44832011)(508600001)(5660300002)(70206006)(7416002)(70586007)(8936002)(4326008)(8676002)(86362001)(40460700003)(186003)(1076003)(16526019)(26005)(36756003)(2616005)(7696005)(2906002)(54906003)(47076005)(426003)(356005)(336012)(6666004)(81166007)(36860700001)(83380400001)(82310400005)(6916009)(316002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:15:00.4107 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a86ff996-e302-4bcc-8987-08da3791e698 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT051.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB3182 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org When CPU is connected throug XGMI, it has coherent access to VRAM resource. In this case that resource is taken from a table in the device gmc aperture base. This resource is used along with the device type, which could be DEVICE_PRIVATE or DEVICE_COHERENT to create the device page map region. Also, MIGRATE_VMA_SELECT_DEVICE_COHERENT flag is selected for coherent type case during migration to device. Signed-off-by: Alex Sierra Reviewed-by: Felix Kuehling Signed-off-by: Christoph Hellwig --- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 33 +++++++++++++++--------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c index 7e3a7fcb9fe6..25c9f7a4325d 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c @@ -673,9 +673,12 @@ svm_migrate_vma_to_ram(struct amdgpu_device *adev, struct svm_range *prange, migrate.vma = vma; migrate.start = start; migrate.end = end; - migrate.flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE; migrate.pgmap_owner = SVM_ADEV_PGMAP_OWNER(adev); + if (adev->gmc.xgmi.connected_to_cpu) + migrate.flags = MIGRATE_VMA_SELECT_DEVICE_COHERENT; + else + migrate.flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE; size = 2 * sizeof(*migrate.src) + sizeof(uint64_t) + sizeof(dma_addr_t); size *= npages; buf = kvmalloc(size, GFP_KERNEL | __GFP_ZERO); @@ -948,7 +951,7 @@ int svm_migrate_init(struct amdgpu_device *adev) { struct kfd_dev *kfddev = adev->kfd.dev; struct dev_pagemap *pgmap; - struct resource *res; + struct resource *res = NULL; unsigned long size; void *r; @@ -963,28 +966,34 @@ int svm_migrate_init(struct amdgpu_device *adev) * should remove reserved size */ size = ALIGN(adev->gmc.real_vram_size, 2ULL << 20); - res = devm_request_free_mem_region(adev->dev, &iomem_resource, size); - if (IS_ERR(res)) - return -ENOMEM; + if (adev->gmc.xgmi.connected_to_cpu) { + pgmap->range.start = adev->gmc.aper_base; + pgmap->range.end = adev->gmc.aper_base + adev->gmc.aper_size - 1; + pgmap->type = MEMORY_DEVICE_COHERENT; + } else { + res = devm_request_free_mem_region(adev->dev, &iomem_resource, size); + if (IS_ERR(res)) + return -ENOMEM; + pgmap->range.start = res->start; + pgmap->range.end = res->end; + pgmap->type = MEMORY_DEVICE_PRIVATE; + } - pgmap->type = MEMORY_DEVICE_PRIVATE; pgmap->nr_range = 1; - pgmap->range.start = res->start; - pgmap->range.end = res->end; pgmap->ops = &svm_migrate_pgmap_ops; pgmap->owner = SVM_ADEV_PGMAP_OWNER(adev); - pgmap->flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE; - + pgmap->flags = 0; /* Device manager releases device-specific resources, memory region and * pgmap when driver disconnects from device. */ r = devm_memremap_pages(adev->dev, pgmap); if (IS_ERR(r)) { pr_err("failed to register HMM device memory\n"); - /* Disable SVM support capability */ pgmap->type = 0; - devm_release_mem_region(adev->dev, res->start, resource_size(res)); + if (pgmap->type == MEMORY_DEVICE_PRIVATE) + devm_release_mem_region(adev->dev, res->start, + res->end - res->start + 1); return PTR_ERR(r); } From patchwork Mon May 16 23:14:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851700 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D994C4332F for ; Mon, 16 May 2022 23:15:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350280AbiEPXPL (ORCPT ); Mon, 16 May 2022 19:15:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350248AbiEPXPE (ORCPT ); Mon, 16 May 2022 19:15:04 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2060.outbound.protection.outlook.com [40.107.96.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0084C46B33; Mon, 16 May 2022 16:15:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OrC5bI8PC08ZbO+rkM4cBR0Be01kxRXwr+QXvkGlsFoIVdvwL3YK5f9jFlZNbyVOphfkW1TDkCHpKc19Jbkf0TuhXFhWogzQ9JVMD0VH6GAFo952CsJVMEU5nIlsm4rnNZq6FxrJpeNn7HK6XuN24pvwmyHz+EI+m82EwG99XjevGO43Tq1/94dz6K884B4Q7Rze4bPtogGMifbWuum6SLNyN9H7hdL6EiMKaviGQddmeQ42TIKx/2Y8MYmr0W7nbJb3id+/rJEd4z+smfPAvzZ/5gPNtFFXuerfAX2913FG47WXQAY2wjultxXBJeXAed1ER3HnCN0zEICVScZMEQ== 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=BO1XgdLChGhcmXQYgQRlktGItGh1h3zZzS9U0EV0V6A=; b=BJjf1ginelt/sZwoIk+7Zsqia6FGiD5LZevghgnw250YI2Fm5vrk10Bpl6O5xbNEdvASgUayEZESfibJgLakVjPvBOJH5PE3WfXyd1h3Neq9S4rtCzd3kIKXuBBORUODy/KNRfAsRnm9qqRUGJr+HnIbJKdedEpFCBHuJzfC0Vz2Q2ZlSPHad8s587KEvuk+k0Ff6kOBeixu89LNsin9tCqEJO9H6LJIpr85uFX9tCBrvnsLiJqWHr6LQn+Bywktm9oFhIAwIKCimycxr+cUdrFyb/pAS9bnPtq/dbA0+xgtUSisBpPWUtIPPrqSlvF4bj2Hp+9U/tfzqilBl3QxRA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BO1XgdLChGhcmXQYgQRlktGItGh1h3zZzS9U0EV0V6A=; b=CCgUYQG3x1TLvpRK4Qd6fpfEhmjTtkt0x67TdG72bLORK4P4OEojCCUjPWt85mwJF6yWGJnzMoHtQlahueF5cFy7CRzz71pH2qUTGLvh4slRXe6Tsp0ZZPqtyNODEFr0hk51rgJTPGIvGJqhlI9qeIDZuk02r19D7AqvvXVtRlI= Received: from BN9PR03CA0386.namprd03.prod.outlook.com (2603:10b6:408:f7::31) by DS7PR12MB5863.namprd12.prod.outlook.com (2603:10b6:8:7a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Mon, 16 May 2022 23:15:01 +0000 Received: from BN8NAM11FT051.eop-nam11.prod.protection.outlook.com (2603:10b6:408:f7:cafe::85) by BN9PR03CA0386.outlook.office365.com (2603:10b6:408:f7::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18 via Frontend Transport; Mon, 16 May 2022 23:15:01 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT051.mail.protection.outlook.com (10.13.177.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:15:01 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:14:59 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 06/13] lib: test_hmm add ioctl to get zone device type Date: Mon, 16 May 2022 18:14:25 -0500 Message-ID: <20220516231432.20200-7-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: fb9f560a-7a9a-4cb4-beff-08da3791e6f0 X-MS-TrafficTypeDiagnostic: DS7PR12MB5863: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: 4Hf0ginmzvh8sgNXZpekH1EM82s/mYPXEIyasvq4iMnXeH5Tmadkxm0uwFNUDkuIjawjrhZXyDCOik+pHoXB8N+kZLzzB/7i63/2h8WnNLTiuNEgmce7dX5eC1LSJBdggg5vytjzT47VGunPddeAUbP337uk16L80zFyeOOySW0N4Qm/c9qDJWZd3cSCr6dZ5Tm4TLHv6zvT6Q3x3e7DXNQ4pKTGq6Y+xVrP1SOgpUC0s5RGzZOOnEYB3jOIlF1u9IyMqKsLdG1DnZD/9MjkiPDebOX6fXpLd9hyXj2xORjTeprCiY32tGBGty6fsWlvvuejibof2xI0e9wadcYxQx7QFN59HUTquKwZcPwM3dsSSvf4TJ3Tpzg4KgFngVn/k1UnSwlW0hd8cByEj/qzgXa3vB1TgNXuG3mOKLYN5CW2Pbwykn9XDpWQgJLopKO9HYjSJNzttCiCTW14kwPa1Q+ODrZm0rjIjIM5ub685T66bQdF1FHVtqNdqYWu/LrN8+1/KVtYuMPOELtQmcX+URBzoefPYRKqrVLco/pBCtA0J6qBmd0AChZrxjcHmQMO2lpX8nVy8ftBQX4CTLMAIzXQvo3fLHYoixo3WwCVbc878yt0IXSEs6745usycGxt2wG8uEPf2eP1E97wdAvtgsKuprBWU7I3QmrlezQItSkMbCAPgpgtoLmGzyNtP/PoqSmj9dhe7VWKzBPRUxz0/w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(46966006)(36840700001)(40470700004)(4326008)(70586007)(70206006)(81166007)(8676002)(36860700001)(82310400005)(26005)(54906003)(6916009)(40460700003)(316002)(2906002)(1076003)(36756003)(2616005)(16526019)(186003)(8936002)(356005)(7696005)(83380400001)(86362001)(6666004)(5660300002)(336012)(426003)(44832011)(7416002)(508600001)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:15:01.0044 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fb9f560a-7a9a-4cb4-beff-08da3791e6f0 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT051.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB5863 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org new ioctl cmd added to query zone device type. This will be used once the test_hmm adds zone device coherent type. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling Reviewed-by: Alistair Poppple Signed-off-by: Christoph Hellwig --- lib/test_hmm.c | 23 +++++++++++++++++++++-- lib/test_hmm_uapi.h | 8 ++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index cfe632047839..7a27584484ce 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -87,6 +87,7 @@ struct dmirror_chunk { struct dmirror_device { struct cdev cdevice; struct hmm_devmem *devmem; + unsigned int zone_device_type; unsigned int devmem_capacity; unsigned int devmem_count; @@ -1026,6 +1027,15 @@ static int dmirror_snapshot(struct dmirror *dmirror, return ret; } +static int dmirror_get_device_type(struct dmirror *dmirror, + struct hmm_dmirror_cmd *cmd) +{ + mutex_lock(&dmirror->mutex); + cmd->zone_device_type = dmirror->mdevice->zone_device_type; + mutex_unlock(&dmirror->mutex); + + return 0; +} static long dmirror_fops_unlocked_ioctl(struct file *filp, unsigned int command, unsigned long arg) @@ -1076,6 +1086,9 @@ static long dmirror_fops_unlocked_ioctl(struct file *filp, ret = dmirror_snapshot(dmirror, &cmd); break; + case HMM_DMIRROR_GET_MEM_DEV_TYPE: + ret = dmirror_get_device_type(dmirror, &cmd); + break; default: return -EINVAL; } @@ -1260,14 +1273,20 @@ static void dmirror_device_remove(struct dmirror_device *mdevice) static int __init hmm_dmirror_init(void) { int ret; - int id; + int id = 0; + int ndevices = 0; ret = alloc_chrdev_region(&dmirror_dev, 0, DMIRROR_NDEVICES, "HMM_DMIRROR"); if (ret) goto err_unreg; - for (id = 0; id < DMIRROR_NDEVICES; id++) { + memset(dmirror_devices, 0, DMIRROR_NDEVICES * sizeof(dmirror_devices[0])); + dmirror_devices[ndevices++].zone_device_type = + HMM_DMIRROR_MEMORY_DEVICE_PRIVATE; + dmirror_devices[ndevices++].zone_device_type = + HMM_DMIRROR_MEMORY_DEVICE_PRIVATE; + for (id = 0; id < ndevices; id++) { ret = dmirror_device_init(dmirror_devices + id, id); if (ret) goto err_chrdev; diff --git a/lib/test_hmm_uapi.h b/lib/test_hmm_uapi.h index f14dea5dcd06..17f842f1aa02 100644 --- a/lib/test_hmm_uapi.h +++ b/lib/test_hmm_uapi.h @@ -19,6 +19,7 @@ * @npages: (in) number of pages to read/write * @cpages: (out) number of pages copied * @faults: (out) number of device page faults seen + * @zone_device_type: (out) zone device memory type */ struct hmm_dmirror_cmd { __u64 addr; @@ -26,6 +27,7 @@ struct hmm_dmirror_cmd { __u64 npages; __u64 cpages; __u64 faults; + __u64 zone_device_type; }; /* Expose the address space of the calling process through hmm device file */ @@ -35,6 +37,7 @@ struct hmm_dmirror_cmd { #define HMM_DMIRROR_SNAPSHOT _IOWR('H', 0x03, struct hmm_dmirror_cmd) #define HMM_DMIRROR_EXCLUSIVE _IOWR('H', 0x04, struct hmm_dmirror_cmd) #define HMM_DMIRROR_CHECK_EXCLUSIVE _IOWR('H', 0x05, struct hmm_dmirror_cmd) +#define HMM_DMIRROR_GET_MEM_DEV_TYPE _IOWR('H', 0x06, struct hmm_dmirror_cmd) /* * Values returned in hmm_dmirror_cmd.ptr for HMM_DMIRROR_SNAPSHOT. @@ -62,4 +65,9 @@ enum { HMM_DMIRROR_PROT_DEV_PRIVATE_REMOTE = 0x30, }; +enum { + /* 0 is reserved to catch uninitialized type fields */ + HMM_DMIRROR_MEMORY_DEVICE_PRIVATE = 1, +}; + #endif /* _LIB_TEST_HMM_UAPI_H */ From patchwork Mon May 16 23:14:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851702 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0C643C433F5 for ; Mon, 16 May 2022 23:15:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350272AbiEPXPP (ORCPT ); Mon, 16 May 2022 19:15:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350254AbiEPXPI (ORCPT ); Mon, 16 May 2022 19:15:08 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2085.outbound.protection.outlook.com [40.107.93.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E814746B34; Mon, 16 May 2022 16:15:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aaQ4Q7TFNVID8ZGPhbpRuLLNhHi2+IwYzoWmfvF+Mdd/AiBTq27gk1PiA4k7VcIlGWBPR6QGY07TzpqreW9zAizXujSFmIkiEaD+qX8sJECJQqEHDD3nwyHB08crqN2BtFsxU3QY28lSs7uT8FgO3uQvg3f973fAiYyEO2V2pwQPfc0jRNVBOqHXn0zc5BsndqvFY4fLLI0jmFfgylh6fkWnVWdG4PloLqUki8RdjTr2g7rFjzZUv7EFV1fximbFvz8Dt09y/kXpkP5G74kq9Q9mDbtrein9KT1DHXpYyF4EQsCuK/BxggHDrIYFtljxuuHWzWuCM4GyHWvPxTKN7Q== 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=EBC8zbdByiow3Ar7kghS8CNIwlQlMj13Ni6WUh9t/L0=; b=jHGtTc7ROhSQEAFY3tFJbEXpy8lZrslBV/hmIOxx2qq+W9olbkRZ0PAhOvjAYIO6fO7iqpYkvN93XCSB20QoycyZHLzsPZqKpBNAkZFwx9EehJxP1OPF4zR/fwsQvXqCpA3tNgEpFvoRd4JsVc5Xt7DuQalXdoBrMQ0y1vQtuDUQTtSV/GfBfrJfDD1bCg/RH6BZj2a0QSrmeyHNiqYcYhZQWKAMGwV22a4E2CoZRazZc50tdeqNqIbU4FF4S3Warsk8Uj1X+e2grccNLHm3j6CE/+x2JJa8QXj7nS9P6K9aUN3ICH6WnG8/A/o7mKJ/mo8fTSHGDjB8XQ+Axa9poA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=EBC8zbdByiow3Ar7kghS8CNIwlQlMj13Ni6WUh9t/L0=; b=g3gn/zLl1q8RJ1ORHEPxYU50MHX1cko+qeUIh7dkII+l3/D0WJbKHVCWosoqCPuWzWXWMzl/pVwvaiob2uoqLlPxijbrWF6NH7L20oW5B5JCcHG03N076mBODo8PpmsLZNe749LTMbmjR1zqC1dMW6LLLkMJFtfPOGSUhLnk8Rc= Received: from BN9PR03CA0542.namprd03.prod.outlook.com (2603:10b6:408:138::7) by MN2PR12MB4390.namprd12.prod.outlook.com (2603:10b6:208:26e::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14; Mon, 16 May 2022 23:15:02 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:408:138:cafe::4) by BN9PR03CA0542.outlook.office365.com (2603:10b6:408:138::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.15 via Frontend Transport; Mon, 16 May 2022 23:15:02 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT043.mail.protection.outlook.com (10.13.177.218) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:15:01 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:15:00 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 07/13] lib: test_hmm add module param for zone device type Date: Mon, 16 May 2022 18:14:26 -0500 Message-ID: <20220516231432.20200-8-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 49d64dc3-1658-468c-c6b3-08da3791e77e X-MS-TrafficTypeDiagnostic: MN2PR12MB4390: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: NIHmp7iRtdlAFEqCYoo489Ru3rX0QUyP/Q0WYP7YffndXST+spC+FHC1f+bo/tgaydJu5daoP56UN7bb8sgw2M97KCcIrMVt5iJeNIKgiBw4n11use0Zg+4SzcDeZl2VfzLPEv+IAIJZDKS/LXBRYzQq16dUPJGI6QfzrttlkHHVQCk45DBkF6RbLG1KZBBZfDfHEEhwA7WyiFqGkY86hAXsMs0dG0tsdjqU2aWx8HJT+lbzubIc6RPpWQqFVBjLGIzlk8gCnpM7b32akRtT83XRzHfoXDdcPotCdhUVbaH9Ci8ZqgumdiS/8AlA0CJzDOhOnMWCo5n0PcY2/ibGNWANsD1KwvsjiZbs21wL5L90J6oecmSbR0+7wXkvUI/IQcj6G/REMO3kWG7Wu0GmU2BZQKlm34uDYjBnkDIjeIXQHVG5LkGuS1lnI4M4bNm2BMYmONnNWlRbL1IlalkGHEE70MTwCIbq7Y3NcaPxguDaVwpISokscsxlBffWF5eFwzDow8VgHAUgcMy0CPIs9SNJNkKkn8IgPHssbFDTSyOc2IPAUromDuwbNtv6sL73UBZp1WUAsZCT7jSFkfbHQB/Wf6bCaN79dBmrVGOMAtDupUL92fIrT1sG5ReL/ZxQvhF/ow9EoasXYBOmVc/J9E3sUzipW7SXfEO6b6cP47YCVtX2SQIPsK0amPry6yr76+RzRMhx5sLt0LRmHQNwPQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(36840700001)(40470700004)(46966006)(36756003)(83380400001)(6666004)(2616005)(316002)(54906003)(40460700003)(82310400005)(47076005)(426003)(2906002)(508600001)(8936002)(81166007)(70586007)(70206006)(86362001)(8676002)(26005)(4326008)(36860700001)(16526019)(1076003)(7696005)(356005)(186003)(336012)(6916009)(44832011)(7416002)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:15:01.9352 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 49d64dc3-1658-468c-c6b3-08da3791e77e X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT043.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4390 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org In order to configure device coherent in test_hmm, two module parameters should be passed, which correspond to the SP start address of each device (2) spm_addr_dev0 & spm_addr_dev1. If no parameters are passed, private device type is configured. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling Reviewed-by: Alistair Poppple Signed-off-by: Christoph Hellwig --- lib/test_hmm.c | 73 ++++++++++++++++++++++++++++++++------------- lib/test_hmm_uapi.h | 1 + 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index 7a27584484ce..15747f70c5bc 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -37,6 +37,16 @@ #define DEVMEM_CHUNK_SIZE (256 * 1024 * 1024U) #define DEVMEM_CHUNKS_RESERVE 16 +static unsigned long spm_addr_dev0; +module_param(spm_addr_dev0, long, 0644); +MODULE_PARM_DESC(spm_addr_dev0, + "Specify start address for SPM (special purpose memory) used for device 0. By setting this Coherent device type will be used. Make sure spm_addr_dev1 is set too. Minimum SPM size should be DEVMEM_CHUNK_SIZE."); + +static unsigned long spm_addr_dev1; +module_param(spm_addr_dev1, long, 0644); +MODULE_PARM_DESC(spm_addr_dev1, + "Specify start address for SPM (special purpose memory) used for device 1. By setting this Coherent device type will be used. Make sure spm_addr_dev0 is set too. Minimum SPM size should be DEVMEM_CHUNK_SIZE."); + static const struct dev_pagemap_ops dmirror_devmem_ops; static const struct mmu_interval_notifier_ops dmirror_min_ops; static dev_t dmirror_dev; @@ -455,28 +465,44 @@ static int dmirror_write(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd) return ret; } -static bool dmirror_allocate_chunk(struct dmirror_device *mdevice, +static int dmirror_allocate_chunk(struct dmirror_device *mdevice, struct page **ppage) { struct dmirror_chunk *devmem; - struct resource *res; + struct resource *res = NULL; unsigned long pfn; unsigned long pfn_first; unsigned long pfn_last; void *ptr; + int ret = -ENOMEM; devmem = kzalloc(sizeof(*devmem), GFP_KERNEL); if (!devmem) - return false; + return ret; - res = request_free_mem_region(&iomem_resource, DEVMEM_CHUNK_SIZE, - "hmm_dmirror"); - if (IS_ERR(res)) + switch (mdevice->zone_device_type) { + case HMM_DMIRROR_MEMORY_DEVICE_PRIVATE: + res = request_free_mem_region(&iomem_resource, DEVMEM_CHUNK_SIZE, + "hmm_dmirror"); + if (IS_ERR_OR_NULL(res)) + goto err_devmem; + devmem->pagemap.range.start = res->start; + devmem->pagemap.range.end = res->end; + devmem->pagemap.type = MEMORY_DEVICE_PRIVATE; + break; + case HMM_DMIRROR_MEMORY_DEVICE_COHERENT: + devmem->pagemap.range.start = (MINOR(mdevice->cdevice.dev) - 2) ? + spm_addr_dev0 : + spm_addr_dev1; + devmem->pagemap.range.end = devmem->pagemap.range.start + + DEVMEM_CHUNK_SIZE - 1; + devmem->pagemap.type = MEMORY_DEVICE_COHERENT; + break; + default: + ret = -EINVAL; goto err_devmem; + } - devmem->pagemap.type = MEMORY_DEVICE_PRIVATE; - devmem->pagemap.range.start = res->start; - devmem->pagemap.range.end = res->end; devmem->pagemap.nr_range = 1; devmem->pagemap.ops = &dmirror_devmem_ops; devmem->pagemap.owner = mdevice; @@ -497,10 +523,14 @@ static bool dmirror_allocate_chunk(struct dmirror_device *mdevice, mdevice->devmem_capacity = new_capacity; mdevice->devmem_chunks = new_chunks; } - ptr = memremap_pages(&devmem->pagemap, numa_node_id()); - if (IS_ERR(ptr)) + if (IS_ERR_OR_NULL(ptr)) { + if (ptr) + ret = PTR_ERR(ptr); + else + ret = -EFAULT; goto err_release; + } devmem->mdevice = mdevice; pfn_first = devmem->pagemap.range.start >> PAGE_SHIFT; @@ -529,15 +559,17 @@ static bool dmirror_allocate_chunk(struct dmirror_device *mdevice, } spin_unlock(&mdevice->lock); - return true; + return 0; err_release: mutex_unlock(&mdevice->devmem_lock); - release_mem_region(devmem->pagemap.range.start, range_len(&devmem->pagemap.range)); + if (res && devmem->pagemap.type == MEMORY_DEVICE_PRIVATE) + release_mem_region(devmem->pagemap.range.start, + range_len(&devmem->pagemap.range)); err_devmem: kfree(devmem); - return false; + return ret; } static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice) @@ -562,7 +594,7 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice) spin_unlock(&mdevice->lock); } else { spin_unlock(&mdevice->lock); - if (!dmirror_allocate_chunk(mdevice, &dpage)) + if (dmirror_allocate_chunk(mdevice, &dpage)) goto error; } @@ -1244,10 +1276,8 @@ static int dmirror_device_init(struct dmirror_device *mdevice, int id) if (ret) return ret; - /* Build a list of free ZONE_DEVICE private struct pages */ - dmirror_allocate_chunk(mdevice, NULL); - - return 0; + /* Build a list of free ZONE_DEVICE struct pages */ + return dmirror_allocate_chunk(mdevice, NULL); } static void dmirror_device_remove(struct dmirror_device *mdevice) @@ -1260,8 +1290,9 @@ static void dmirror_device_remove(struct dmirror_device *mdevice) mdevice->devmem_chunks[i]; memunmap_pages(&devmem->pagemap); - release_mem_region(devmem->pagemap.range.start, - range_len(&devmem->pagemap.range)); + if (devmem->pagemap.type == MEMORY_DEVICE_PRIVATE) + release_mem_region(devmem->pagemap.range.start, + range_len(&devmem->pagemap.range)); kfree(devmem); } kfree(mdevice->devmem_chunks); diff --git a/lib/test_hmm_uapi.h b/lib/test_hmm_uapi.h index 17f842f1aa02..625f3690d086 100644 --- a/lib/test_hmm_uapi.h +++ b/lib/test_hmm_uapi.h @@ -68,6 +68,7 @@ enum { enum { /* 0 is reserved to catch uninitialized type fields */ HMM_DMIRROR_MEMORY_DEVICE_PRIVATE = 1, + HMM_DMIRROR_MEMORY_DEVICE_COHERENT, }; #endif /* _LIB_TEST_HMM_UAPI_H */ From patchwork Mon May 16 23:14:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851706 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B221FC41535 for ; Mon, 16 May 2022 23:15:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350258AbiEPXPT (ORCPT ); Mon, 16 May 2022 19:15:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350259AbiEPXPK (ORCPT ); Mon, 16 May 2022 19:15:10 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2086.outbound.protection.outlook.com [40.107.92.86]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F16546B38; Mon, 16 May 2022 16:15:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bzicAVUwtuEu8NK7sum2B08bt424pKklxUDxZ38MgkuKk50vyo18z3YhRx09Ard4qY40JpOEUf5rPZJQUAHxA/uKXQzZf2cQm+qgbqJLt7qoeR+oW737Pqrjwc2GBzX3Z/d8zX/qeh/jS2PP5OHmURQVEycNWhqXOlD/KZkldhgWeACfdxvjSdWqBGnFyPeMIp8MWBK/tdZfg3JbDbIMfoUwHRK0iU8gHVB4nUqutkSQlynMn4l56zWUFnJ+LmlagxsuCtIWNeBUdzrSikZ7oPC1joIiU7Exc1lLHlU4oOu/sG25rrfPRkbOonZaC6fydOMLMrTOjx+HCAMvKucMiA== 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=7NnrGS6YZU8gQt+7tgfyVxU1Jq6fVp8xiHVCSF24jGU=; b=SnAIDta48whzyQ7tY898f96rdmCXvt244/20kNU3mgeGjw7iXz/7kOTSjU6K8EIHUpdv8dlzor/lHQ40AvtWz/7zPNhtW9GVXePOIzu7xVfkQoWfXQnmffU9n1j5+a/uuqaE+ZZHrnUaFLbGdRXcHW2q6Gz0PTwB7+fBlzR9XahhftXp2gv8fIm04gX8adVBcKE9+64HUg1oVteRfXQNCbsRjdcZX3SdSVuUFMj6J15siBIYav7WiRIcsgjoCUAGNzSb3u+PWljSyj7sLOPZ2bFH0NE7fjCNaAJY5x2pbd/tMCW2UESBAc2ZDDIH6J58F2GK+gQvHA7AjpmSe3J3wg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7NnrGS6YZU8gQt+7tgfyVxU1Jq6fVp8xiHVCSF24jGU=; b=iUBTTDIkItYMLtb97XiB9SzC3KsaahIUkSllHAY0irqOD9VeFK/X+UM2ajrGu/rU4OAYNYFY+kefQer80AuHh2z2mofJU8XwkulUiRB2Od/K2WnBuH48QJOXlKh7AollR2GxMIZiVUUpsqHbEYfjhW73WeVtB4oixQ9Pk0LZoeU= Received: from BN9PR03CA0696.namprd03.prod.outlook.com (2603:10b6:408:ef::11) by BY5PR12MB3794.namprd12.prod.outlook.com (2603:10b6:a03:1aa::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Mon, 16 May 2022 23:15:04 +0000 Received: from BN8NAM11FT041.eop-nam11.prod.protection.outlook.com (2603:10b6:408:ef:cafe::99) by BN9PR03CA0696.outlook.office365.com (2603:10b6:408:ef::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:15:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT041.mail.protection.outlook.com (10.13.177.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:15:03 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:15:01 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 08/13] lib: add support for device coherent type in test_hmm Date: Mon, 16 May 2022 18:14:27 -0500 Message-ID: <20220516231432.20200-9-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cc73e4a3-26b0-407c-f1c3-08da3791e8b0 X-MS-TrafficTypeDiagnostic: BY5PR12MB3794: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: 6dYplB4Y+yu5U2HSBoTsu/y+0V7ZaSxPY1gkmY79DqzIpYWKkpDc4Nqr4wpsD5Od++kJFJ+XBkBsS+Ht74mYiXuSo2b/XnDc5YjpuSntc008zuXnwhLylK/S/dCT1V9i1nsUtrtNhDYo+tpTafakQqBGpVFKfNAQqYHrfkrvlGG4vMjNMELBpHJ4MefCjb7qvaKXcFC/F2Iq6lVF24E+yZA/8XQdW+vDOxHvSQ/uPF3w5m3YgPCyoyP0Hn0W6KNNXW/QYdvICrZ33+wBvr1giSPFrgXr8Ngr9PNvGwymqkMtqSBJdAWnSiXqqscWwnrzdgJB/xSJtkTP/ppUVNpTRAnBCl1AtI0v5Ec7/5ELmI21ZZOFFlrMRj2e/j63dr5lETWalpmL+E3rQ1f//FrruhaaHLdMThhZY+7mrXENOzINlioYaM+4f7suuw/Lj8YeZApGRQs9Yhjf3x06GfR7lReLDKDiPdTjUEp2W5U5PiW9bgwQTPWrbfnDZGD08tdngjuGckY1momkRhC89sdoc52GgCT/K5GqQlozRh3KxazlnmPGNhRmzirFBPCpABsNGl0aG1r3Cs8ZbLygt/QjmTim1XqBUabDWgedEbfQUpfRJfLqu7HK5Wkgl1cFL4ZurGKk9AZIba4SIM044ilgvPC6HH2HE6UbOZ3JKUt8dpxEB4fCjjLjJvKmMNySqsJckAxAn/r8BTuempMO0Ta/eQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(40470700004)(36840700001)(46966006)(8676002)(36756003)(6666004)(7696005)(6916009)(86362001)(54906003)(316002)(508600001)(81166007)(70206006)(70586007)(36860700001)(426003)(1076003)(2616005)(47076005)(336012)(16526019)(186003)(356005)(83380400001)(44832011)(2906002)(8936002)(82310400005)(30864003)(4326008)(5660300002)(40460700003)(26005)(7416002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:15:03.9261 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cc73e4a3-26b0-407c-f1c3-08da3791e8b0 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT041.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3794 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org Device Coherent type uses device memory that is coherently accesible by the CPU. This could be shown as SP (special purpose) memory range at the BIOS-e820 memory enumeration. If no SP memory is supported in system, this could be faked by setting CONFIG_EFI_FAKE_MEMMAP. Currently, test_hmm only supports two different SP ranges of at least 256MB size. This could be specified in the kernel parameter variable efi_fake_mem. Ex. Two SP ranges of 1GB starting at 0x100000000 & 0x140000000 physical address. Ex. efi_fake_mem=1G@0x100000000:0x40000,1G@0x140000000:0x40000 Private and coherent device mirror instances can be created in the same probed. This is done by passing the module parameters spm_addr_dev0 & spm_addr_dev1. In this case, it will create four instances of device_mirror. The first two correspond to private device type, the last two to coherent type. Then, they can be easily accessed from user space through /dev/hmm_mirror. Usually num_device 0 and 1 are for private, and 2 and 3 for coherent types. If no module parameters are passed, two instances of private type device_mirror will be created only. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling Reviewed-by: Alistair Poppple --- lib/test_hmm.c | 253 +++++++++++++++++++++++++++++++++----------- lib/test_hmm_uapi.h | 15 ++- 2 files changed, 202 insertions(+), 66 deletions(-) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index 15747f70c5bc..361a026c5d21 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -32,11 +32,22 @@ #include "test_hmm_uapi.h" -#define DMIRROR_NDEVICES 2 +#define DMIRROR_NDEVICES 4 #define DMIRROR_RANGE_FAULT_TIMEOUT 1000 #define DEVMEM_CHUNK_SIZE (256 * 1024 * 1024U) #define DEVMEM_CHUNKS_RESERVE 16 +/* + * For device_private pages, dpage is just a dummy struct page + * representing a piece of device memory. dmirror_devmem_alloc_page + * allocates a real system memory page as backing storage to fake a + * real device. zone_device_data points to that backing page. But + * for device_coherent memory, the struct page represents real + * physical CPU-accessible memory that we can use directly. + */ +#define BACKING_PAGE(page) (is_device_private_page((page)) ? \ + (page)->zone_device_data : (page)) + static unsigned long spm_addr_dev0; module_param(spm_addr_dev0, long, 0644); MODULE_PARM_DESC(spm_addr_dev0, @@ -125,6 +136,21 @@ static int dmirror_bounce_init(struct dmirror_bounce *bounce, return 0; } +static bool dmirror_is_private_zone(struct dmirror_device *mdevice) +{ + return (mdevice->zone_device_type == + HMM_DMIRROR_MEMORY_DEVICE_PRIVATE) ? true : false; +} + +static enum migrate_vma_direction +dmirror_select_device(struct dmirror *dmirror) +{ + return (dmirror->mdevice->zone_device_type == + HMM_DMIRROR_MEMORY_DEVICE_PRIVATE) ? + MIGRATE_VMA_SELECT_DEVICE_PRIVATE : + MIGRATE_VMA_SELECT_DEVICE_COHERENT; +} + static void dmirror_bounce_fini(struct dmirror_bounce *bounce) { vfree(bounce->ptr); @@ -575,16 +601,19 @@ static int dmirror_allocate_chunk(struct dmirror_device *mdevice, static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice) { struct page *dpage = NULL; - struct page *rpage; + struct page *rpage = NULL; /* - * This is a fake device so we alloc real system memory to store - * our device memory. + * For ZONE_DEVICE private type, this is a fake device so we allocate + * real system memory to store our device memory. + * For ZONE_DEVICE coherent type we use the actual dpage to store the + * data and ignore rpage. */ - rpage = alloc_page(GFP_HIGHUSER); - if (!rpage) - return NULL; - + if (dmirror_is_private_zone(mdevice)) { + rpage = alloc_page(GFP_HIGHUSER); + if (!rpage) + return NULL; + } spin_lock(&mdevice->lock); if (mdevice->free_pages) { @@ -603,7 +632,8 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice) return dpage; error: - __free_page(rpage); + if (rpage) + __free_page(rpage); return NULL; } @@ -629,12 +659,16 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args, * unallocated pte_none() or read-only zero page. */ spage = migrate_pfn_to_page(*src); + if (WARN(spage && is_zone_device_page(spage), + "page already in device spage pfn: 0x%lx\n", + page_to_pfn(spage))) + continue; dpage = dmirror_devmem_alloc_page(mdevice); if (!dpage) continue; - rpage = dpage->zone_device_data; + rpage = BACKING_PAGE(dpage); if (spage) copy_highpage(rpage, spage); else @@ -648,6 +682,8 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args, */ rpage->zone_device_data = dmirror; + pr_debug("migrating from sys to dev pfn src: 0x%lx pfn dst: 0x%lx\n", + page_to_pfn(spage), page_to_pfn(dpage)); *dst = migrate_pfn(page_to_pfn(dpage)); if ((*src & MIGRATE_PFN_WRITE) || (!spage && args->vma->vm_flags & VM_WRITE)) @@ -725,11 +761,7 @@ static int dmirror_migrate_finalize_and_map(struct migrate_vma *args, if (!dpage) continue; - /* - * Store the page that holds the data so the page table - * doesn't have to deal with ZONE_DEVICE private pages. - */ - entry = dpage->zone_device_data; + entry = BACKING_PAGE(dpage); if (*dst & MIGRATE_PFN_WRITE) entry = xa_tag_pointer(entry, DPT_XA_TAG_WRITE); entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC); @@ -809,15 +841,126 @@ static int dmirror_exclusive(struct dmirror *dmirror, return ret; } -static int dmirror_migrate(struct dmirror *dmirror, - struct hmm_dmirror_cmd *cmd) +static vm_fault_t dmirror_devmem_fault_alloc_and_copy(struct migrate_vma *args, + struct dmirror *dmirror) +{ + const unsigned long *src = args->src; + unsigned long *dst = args->dst; + unsigned long start = args->start; + unsigned long end = args->end; + unsigned long addr; + + for (addr = start; addr < end; addr += PAGE_SIZE, + src++, dst++) { + struct page *dpage, *spage; + + spage = migrate_pfn_to_page(*src); + if (!spage || !(*src & MIGRATE_PFN_MIGRATE)) + continue; + + if (WARN_ON(!is_device_private_page(spage) && + !is_device_coherent_page(spage))) + continue; + spage = BACKING_PAGE(spage); + dpage = alloc_page_vma(GFP_HIGHUSER_MOVABLE, args->vma, addr); + if (!dpage) + continue; + pr_debug("migrating from dev to sys pfn src: 0x%lx pfn dst: 0x%lx\n", + page_to_pfn(spage), page_to_pfn(dpage)); + + lock_page(dpage); + xa_erase(&dmirror->pt, addr >> PAGE_SHIFT); + copy_highpage(dpage, spage); + *dst = migrate_pfn(page_to_pfn(dpage)); + if (*src & MIGRATE_PFN_WRITE) + *dst |= MIGRATE_PFN_WRITE; + } + return 0; +} + +static unsigned long +dmirror_successful_migrated_pages(struct migrate_vma *migrate) +{ + unsigned long cpages = 0; + unsigned long i; + + for (i = 0; i < migrate->npages; i++) { + if (migrate->src[i] & MIGRATE_PFN_VALID && + migrate->src[i] & MIGRATE_PFN_MIGRATE) + cpages++; + } + return cpages; +} + +static int dmirror_migrate_to_system(struct dmirror *dmirror, + struct hmm_dmirror_cmd *cmd) { unsigned long start, end, addr; unsigned long size = cmd->npages << PAGE_SHIFT; struct mm_struct *mm = dmirror->notifier.mm; struct vm_area_struct *vma; - unsigned long src_pfns[64]; - unsigned long dst_pfns[64]; + unsigned long src_pfns[64] = { 0 }; + unsigned long dst_pfns[64] = { 0 }; + struct migrate_vma args; + unsigned long next; + int ret; + + start = cmd->addr; + end = start + size; + if (end < start) + return -EINVAL; + + /* Since the mm is for the mirrored process, get a reference first. */ + if (!mmget_not_zero(mm)) + return -EINVAL; + + cmd->cpages = 0; + mmap_read_lock(mm); + for (addr = start; addr < end; addr = next) { + vma = vma_lookup(mm, addr); + if (!vma || !(vma->vm_flags & VM_READ)) { + ret = -EINVAL; + goto out; + } + next = min(end, addr + (ARRAY_SIZE(src_pfns) << PAGE_SHIFT)); + if (next > vma->vm_end) + next = vma->vm_end; + + args.vma = vma; + args.src = src_pfns; + args.dst = dst_pfns; + args.start = addr; + args.end = next; + args.pgmap_owner = dmirror->mdevice; + args.flags = dmirror_select_device(dmirror); + + ret = migrate_vma_setup(&args); + if (ret) + goto out; + + pr_debug("Migrating from device mem to sys mem\n"); + dmirror_devmem_fault_alloc_and_copy(&args, dmirror); + + migrate_vma_pages(&args); + cmd->cpages += dmirror_successful_migrated_pages(&args); + migrate_vma_finalize(&args); + } +out: + mmap_read_unlock(mm); + mmput(mm); + + return ret; +} + +static int dmirror_migrate_to_device(struct dmirror *dmirror, + struct hmm_dmirror_cmd *cmd) +{ + unsigned long start, end, addr; + unsigned long size = cmd->npages << PAGE_SHIFT; + struct mm_struct *mm = dmirror->notifier.mm; + struct vm_area_struct *vma; + unsigned long src_pfns[64] = { 0 }; + unsigned long dst_pfns[64] = { 0 }; struct dmirror_bounce bounce; struct migrate_vma args; unsigned long next; @@ -854,6 +997,7 @@ static int dmirror_migrate(struct dmirror *dmirror, if (ret) goto out; + pr_debug("Migrating from sys mem to device mem\n"); dmirror_migrate_alloc_and_copy(&args, dmirror); migrate_vma_pages(&args); dmirror_migrate_finalize_and_map(&args, dmirror); @@ -862,7 +1006,10 @@ static int dmirror_migrate(struct dmirror *dmirror, mmap_read_unlock(mm); mmput(mm); - /* Return the migrated data for verification. */ + /* + * Return the migrated data for verification. + * Only for pages in device zone + */ ret = dmirror_bounce_init(&bounce, start, size); if (ret) return ret; @@ -905,6 +1052,12 @@ static void dmirror_mkentry(struct dmirror *dmirror, struct hmm_range *range, *perm = HMM_DMIRROR_PROT_DEV_PRIVATE_LOCAL; else *perm = HMM_DMIRROR_PROT_DEV_PRIVATE_REMOTE; + } else if (is_device_coherent_page(page)) { + /* Is the page migrated to this device or some other? */ + if (dmirror->mdevice == dmirror_page_to_device(page)) + *perm = HMM_DMIRROR_PROT_DEV_COHERENT_LOCAL; + else + *perm = HMM_DMIRROR_PROT_DEV_COHERENT_REMOTE; } else if (is_zero_pfn(page_to_pfn(page))) *perm = HMM_DMIRROR_PROT_ZERO; else @@ -1101,8 +1254,12 @@ static long dmirror_fops_unlocked_ioctl(struct file *filp, ret = dmirror_write(dmirror, &cmd); break; - case HMM_DMIRROR_MIGRATE: - ret = dmirror_migrate(dmirror, &cmd); + case HMM_DMIRROR_MIGRATE_TO_DEV: + ret = dmirror_migrate_to_device(dmirror, &cmd); + break; + + case HMM_DMIRROR_MIGRATE_TO_SYS: + ret = dmirror_migrate_to_system(dmirror, &cmd); break; case HMM_DMIRROR_EXCLUSIVE: @@ -1167,14 +1324,13 @@ static const struct file_operations dmirror_fops = { static void dmirror_devmem_free(struct page *page) { - struct page *rpage = page->zone_device_data; + struct page *rpage = BACKING_PAGE(page); struct dmirror_device *mdevice; - if (rpage) + if (rpage != page) __free_page(rpage); mdevice = dmirror_page_to_device(page); - spin_lock(&mdevice->lock); mdevice->cfree++; page->zone_device_data = mdevice->free_pages; @@ -1182,43 +1338,11 @@ static void dmirror_devmem_free(struct page *page) spin_unlock(&mdevice->lock); } -static vm_fault_t dmirror_devmem_fault_alloc_and_copy(struct migrate_vma *args, - struct dmirror *dmirror) -{ - const unsigned long *src = args->src; - unsigned long *dst = args->dst; - unsigned long start = args->start; - unsigned long end = args->end; - unsigned long addr; - - for (addr = start; addr < end; addr += PAGE_SIZE, - src++, dst++) { - struct page *dpage, *spage; - - spage = migrate_pfn_to_page(*src); - if (!spage || !(*src & MIGRATE_PFN_MIGRATE)) - continue; - spage = spage->zone_device_data; - - dpage = alloc_page_vma(GFP_HIGHUSER_MOVABLE, args->vma, addr); - if (!dpage) - continue; - - lock_page(dpage); - xa_erase(&dmirror->pt, addr >> PAGE_SHIFT); - copy_highpage(dpage, spage); - *dst = migrate_pfn(page_to_pfn(dpage)); - if (*src & MIGRATE_PFN_WRITE) - *dst |= MIGRATE_PFN_WRITE; - } - return 0; -} - static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf) { struct migrate_vma args; - unsigned long src_pfns; - unsigned long dst_pfns; + unsigned long src_pfns = 0; + unsigned long dst_pfns = 0; struct page *rpage; struct dmirror *dmirror; vm_fault_t ret; @@ -1238,7 +1362,7 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf) args.src = &src_pfns; args.dst = &dst_pfns; args.pgmap_owner = dmirror->mdevice; - args.flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE; + args.flags = dmirror_select_device(dmirror); if (migrate_vma_setup(&args)) return VM_FAULT_SIGBUS; @@ -1317,6 +1441,12 @@ static int __init hmm_dmirror_init(void) HMM_DMIRROR_MEMORY_DEVICE_PRIVATE; dmirror_devices[ndevices++].zone_device_type = HMM_DMIRROR_MEMORY_DEVICE_PRIVATE; + if (spm_addr_dev0 && spm_addr_dev1) { + dmirror_devices[ndevices++].zone_device_type = + HMM_DMIRROR_MEMORY_DEVICE_COHERENT; + dmirror_devices[ndevices++].zone_device_type = + HMM_DMIRROR_MEMORY_DEVICE_COHERENT; + } for (id = 0; id < ndevices; id++) { ret = dmirror_device_init(dmirror_devices + id, id); if (ret) @@ -1339,7 +1469,8 @@ static void __exit hmm_dmirror_exit(void) int id; for (id = 0; id < DMIRROR_NDEVICES; id++) - dmirror_device_remove(dmirror_devices + id); + if (dmirror_devices[id].zone_device_type) + dmirror_device_remove(dmirror_devices + id); unregister_chrdev_region(dmirror_dev, DMIRROR_NDEVICES); } diff --git a/lib/test_hmm_uapi.h b/lib/test_hmm_uapi.h index 625f3690d086..e190b2ab6f19 100644 --- a/lib/test_hmm_uapi.h +++ b/lib/test_hmm_uapi.h @@ -33,11 +33,12 @@ struct hmm_dmirror_cmd { /* Expose the address space of the calling process through hmm device file */ #define HMM_DMIRROR_READ _IOWR('H', 0x00, struct hmm_dmirror_cmd) #define HMM_DMIRROR_WRITE _IOWR('H', 0x01, struct hmm_dmirror_cmd) -#define HMM_DMIRROR_MIGRATE _IOWR('H', 0x02, struct hmm_dmirror_cmd) -#define HMM_DMIRROR_SNAPSHOT _IOWR('H', 0x03, struct hmm_dmirror_cmd) -#define HMM_DMIRROR_EXCLUSIVE _IOWR('H', 0x04, struct hmm_dmirror_cmd) -#define HMM_DMIRROR_CHECK_EXCLUSIVE _IOWR('H', 0x05, struct hmm_dmirror_cmd) -#define HMM_DMIRROR_GET_MEM_DEV_TYPE _IOWR('H', 0x06, struct hmm_dmirror_cmd) +#define HMM_DMIRROR_MIGRATE_TO_DEV _IOWR('H', 0x02, struct hmm_dmirror_cmd) +#define HMM_DMIRROR_MIGRATE_TO_SYS _IOWR('H', 0x03, struct hmm_dmirror_cmd) +#define HMM_DMIRROR_SNAPSHOT _IOWR('H', 0x04, struct hmm_dmirror_cmd) +#define HMM_DMIRROR_EXCLUSIVE _IOWR('H', 0x05, struct hmm_dmirror_cmd) +#define HMM_DMIRROR_CHECK_EXCLUSIVE _IOWR('H', 0x06, struct hmm_dmirror_cmd) +#define HMM_DMIRROR_GET_MEM_DEV_TYPE _IOWR('H', 0x07, struct hmm_dmirror_cmd) /* * Values returned in hmm_dmirror_cmd.ptr for HMM_DMIRROR_SNAPSHOT. @@ -52,6 +53,8 @@ struct hmm_dmirror_cmd { * device the ioctl() is made * HMM_DMIRROR_PROT_DEV_PRIVATE_REMOTE: Migrated device private page on some * other device + * HMM_DMIRROR_PROT_DEV_COHERENT: Migrate device coherent page on the device + * the ioctl() is made */ enum { HMM_DMIRROR_PROT_ERROR = 0xFF, @@ -63,6 +66,8 @@ enum { HMM_DMIRROR_PROT_ZERO = 0x10, HMM_DMIRROR_PROT_DEV_PRIVATE_LOCAL = 0x20, HMM_DMIRROR_PROT_DEV_PRIVATE_REMOTE = 0x30, + HMM_DMIRROR_PROT_DEV_COHERENT_LOCAL = 0x40, + HMM_DMIRROR_PROT_DEV_COHERENT_REMOTE = 0x50, }; enum { From patchwork Mon May 16 23:14:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851703 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E0F90C4167E for ; Mon, 16 May 2022 23:15:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350285AbiEPXPQ (ORCPT ); Mon, 16 May 2022 19:15:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350258AbiEPXPJ (ORCPT ); Mon, 16 May 2022 19:15:09 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2043.outbound.protection.outlook.com [40.107.236.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74F4846B37; Mon, 16 May 2022 16:15:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ibb5F7HfDP82DwX6qh0BViG2IOCfolyc92MWi2mSMY/THOdVwJrGAkNUaELUUORZuCkYjbhsQTwpUbU6I1bxCboEcGuYpLL5OXARSGOOCHx04yViY10dvAdselfC7N2NwNXFFi7fVJ8rQE8GeHAg5yyki7fXRRUcD6S3fcC6XkcaHUhK4A91RCUlymWnWI2rqIawKKuAjekESWs22uWDdJG8KrWjpZD3S03dGjSZIgl/YUXTOMDqYyE/wdo2DTF9+5GRhuqEHlLnZ0Ee1YJ95IPESEGNJmQ5sA1HfDZG46ARSUenxtCA+/cHUHlzAk/v62vAdtCEF+k703VFu9rBUA== 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=wiZRZSmKoAHg2kQsT+4ka7fOS9kQUakT1QrbMGC56FU=; b=YlIdyvbIyKCIoVTMjnxbWz7CXky5rKvZdAJeN7ARyvo/dJMSOuIb0oLUJDzwsDdan+J6DABF7Xvi0Z450PEQtzVEDC6oQdbSCHULyvKgxK/gGmQlYH2QeptQAmEIIQ+Hj5FBJMTRIseFn2rz5aW9YH2UMKciIo+2YlXDv7nzU1uQ/+4wFZCGwAtUpmKmgakrFgwc1vAvAfAOwl8D8kxjUs+mgqNwbjIL8AaVXp+lgnZJmqo1tF1qGgZyYePMiZmuD88IoQrph8so37+P0t7VgsFIcd8eRbvJt2r932cpyEqe9/8dCV3Edcskf4jL7RQ1eE5BlyVNhvN1mi7F9HSnJg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wiZRZSmKoAHg2kQsT+4ka7fOS9kQUakT1QrbMGC56FU=; b=LWX5F3c6IFSSb1vOCPOkGgOewY0klK9zeRTHJQstQZje8QaFow/3Cu73adWX0zkqM8rEWLVgIm9vT6QWu4TfMiPTVaGRM+RdAor489Y6/h1L3kLJfBd+uRgGvkeOJ2p6Z4OZEeTGE/rbxiMruIhpq8AKi4uHIgS15sO/xNEfKNg= Received: from BN9PR03CA0716.namprd03.prod.outlook.com (2603:10b6:408:ef::31) by SJ1PR12MB6362.namprd12.prod.outlook.com (2603:10b6:a03:454::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5227.21; Mon, 16 May 2022 23:15:05 +0000 Received: from BN8NAM11FT041.eop-nam11.prod.protection.outlook.com (2603:10b6:408:ef:cafe::4a) by BN9PR03CA0716.outlook.office365.com (2603:10b6:408:ef::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14 via Frontend Transport; Mon, 16 May 2022 23:15:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT041.mail.protection.outlook.com (10.13.177.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:15:04 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:15:02 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 09/13] tools: update hmm-test to support device coherent type Date: Mon, 16 May 2022 18:14:28 -0500 Message-ID: <20220516231432.20200-10-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a4b13977-6012-4b50-c0e1-08da3791e944 X-MS-TrafficTypeDiagnostic: SJ1PR12MB6362: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: X/oMZkKCO2I3W+Qrm9Gdw7LMdnZAhRNWuxt/wmB4tFBfSdqe3xbnWJrVobnY6Dif1oBUqxhnhlNwYJPwmw0OFJGTTTuqaprDFJD5KQHFIIHIo2OfBKpjYGr4FwIEiY4OG8nr9VfpDtCqnK5gT2ayR1M6/5cDY/E/rpsj/n2A47rTE+D7HamTmujU8RWyyrC7ZRksAiE2ZOKzB5eNRk2XzOLvREGAOr+LseGB15ZuXRxqBeCW+NESaWRbxQmuBhwXt8BFGuLV5Nf0ofdQRvbm8yKntU0/bL0f+z9tueSJ/Gc0n/VRNfk4eIH1oOTTV/WrsEEDm29b8qhEIdXHbgYEfYHe1O7jB158IYZYtXhbhn22t9ja7Xz1vmGKufUeuXqJL7fSQ5MNESPUp8eVqvLVEsp0X9tk+xV2A313TYyg2VAxss6/YQ9dujSmQhx21qm4uZRJNBzcvuTlWvoutePSUk6RIW2Hqg31v3/y7T/Qh5Jtu4w+vE8PVYRD3JnLFIBpYhkItBMx/UdOYKl2N67KMU6EMwpr7+gxaHVcsSFA7hJLxzN9cABuxCN0fJo/GYztuPvjNU/N6YMW1yy0KCuisMX7xPHL400zxWba+ptTu3a3+97pYTrp5IwKlQqttoyaqn15FRnlO2X1W+EOPDREcXD9DVFa/FUUHLZBUDmwy0bUrDwrFxqpQqluILHC8qKDRBULB5UpqC2/u9eHZc7eaQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(36840700001)(46966006)(40470700004)(82310400005)(54906003)(81166007)(36860700001)(26005)(70206006)(15650500001)(508600001)(70586007)(1076003)(7696005)(6666004)(4326008)(8676002)(44832011)(40460700003)(7416002)(316002)(2906002)(8936002)(86362001)(6916009)(2616005)(356005)(47076005)(336012)(426003)(16526019)(186003)(5660300002)(36756003)(83380400001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:15:04.8948 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a4b13977-6012-4b50-c0e1-08da3791e944 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT041.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6362 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org Test cases such as migrate_fault and migrate_multiple, were modified to explicit migrate from device to sys memory without the need of page faults, when using device coherent type. Snapshot test case updated to read memory device type first and based on that, get the proper returned results migrate_ping_pong test case added to test explicit migration from device to sys memory for both private and coherent zone types. Helpers to migrate from device to sys memory and vicerversa were also added. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling Reviewed-by: Alistair Popple Signed-off-by: Christoph Hellwig --- tools/testing/selftests/vm/hmm-tests.c | 123 ++++++++++++++++++++----- 1 file changed, 102 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c index 203323967b50..84ec8c4a1dc7 100644 --- a/tools/testing/selftests/vm/hmm-tests.c +++ b/tools/testing/selftests/vm/hmm-tests.c @@ -44,6 +44,14 @@ struct hmm_buffer { int fd; uint64_t cpages; uint64_t faults; + int zone_device_type; +}; + +enum { + HMM_PRIVATE_DEVICE_ONE, + HMM_PRIVATE_DEVICE_TWO, + HMM_COHERENCE_DEVICE_ONE, + HMM_COHERENCE_DEVICE_TWO, }; #define TWOMEG (1 << 21) @@ -60,6 +68,21 @@ FIXTURE(hmm) unsigned int page_shift; }; +FIXTURE_VARIANT(hmm) +{ + int device_number; +}; + +FIXTURE_VARIANT_ADD(hmm, hmm_device_private) +{ + .device_number = HMM_PRIVATE_DEVICE_ONE, +}; + +FIXTURE_VARIANT_ADD(hmm, hmm_device_coherent) +{ + .device_number = HMM_COHERENCE_DEVICE_ONE, +}; + FIXTURE(hmm2) { int fd0; @@ -68,6 +91,24 @@ FIXTURE(hmm2) unsigned int page_shift; }; +FIXTURE_VARIANT(hmm2) +{ + int device_number0; + int device_number1; +}; + +FIXTURE_VARIANT_ADD(hmm2, hmm2_device_private) +{ + .device_number0 = HMM_PRIVATE_DEVICE_ONE, + .device_number1 = HMM_PRIVATE_DEVICE_TWO, +}; + +FIXTURE_VARIANT_ADD(hmm2, hmm2_device_coherent) +{ + .device_number0 = HMM_COHERENCE_DEVICE_ONE, + .device_number1 = HMM_COHERENCE_DEVICE_TWO, +}; + static int hmm_open(int unit) { char pathname[HMM_PATH_MAX]; @@ -81,12 +122,19 @@ static int hmm_open(int unit) return fd; } +static bool hmm_is_coherent_type(int dev_num) +{ + return (dev_num >= HMM_COHERENCE_DEVICE_ONE); +} + FIXTURE_SETUP(hmm) { self->page_size = sysconf(_SC_PAGE_SIZE); self->page_shift = ffs(self->page_size) - 1; - self->fd = hmm_open(0); + self->fd = hmm_open(variant->device_number); + if (self->fd < 0 && hmm_is_coherent_type(variant->device_number)) + SKIP(exit(0), "DEVICE_COHERENT not available"); ASSERT_GE(self->fd, 0); } @@ -95,9 +143,11 @@ FIXTURE_SETUP(hmm2) self->page_size = sysconf(_SC_PAGE_SIZE); self->page_shift = ffs(self->page_size) - 1; - self->fd0 = hmm_open(0); + self->fd0 = hmm_open(variant->device_number0); + if (self->fd0 < 0 && hmm_is_coherent_type(variant->device_number0)) + SKIP(exit(0), "DEVICE_COHERENT not available"); ASSERT_GE(self->fd0, 0); - self->fd1 = hmm_open(1); + self->fd1 = hmm_open(variant->device_number1); ASSERT_GE(self->fd1, 0); } @@ -144,6 +194,7 @@ static int hmm_dmirror_cmd(int fd, } buffer->cpages = cmd.cpages; buffer->faults = cmd.faults; + buffer->zone_device_type = cmd.zone_device_type; return 0; } @@ -211,6 +262,20 @@ static void hmm_nanosleep(unsigned int n) nanosleep(&t, NULL); } +static int hmm_migrate_sys_to_dev(int fd, + struct hmm_buffer *buffer, + unsigned long npages) +{ + return hmm_dmirror_cmd(fd, HMM_DMIRROR_MIGRATE_TO_DEV, buffer, npages); +} + +static int hmm_migrate_dev_to_sys(int fd, + struct hmm_buffer *buffer, + unsigned long npages) +{ + return hmm_dmirror_cmd(fd, HMM_DMIRROR_MIGRATE_TO_SYS, buffer, npages); +} + /* * Simple NULL test of device open/close. */ @@ -875,7 +940,7 @@ TEST_F(hmm, migrate) ptr[i] = i; /* Migrate memory to device. */ - ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_MIGRATE, buffer, npages); + ret = hmm_migrate_sys_to_dev(self->fd, buffer, npages); ASSERT_EQ(ret, 0); ASSERT_EQ(buffer->cpages, npages); @@ -923,7 +988,7 @@ TEST_F(hmm, migrate_fault) ptr[i] = i; /* Migrate memory to device. */ - ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_MIGRATE, buffer, npages); + ret = hmm_migrate_sys_to_dev(self->fd, buffer, npages); ASSERT_EQ(ret, 0); ASSERT_EQ(buffer->cpages, npages); @@ -936,7 +1001,7 @@ TEST_F(hmm, migrate_fault) ASSERT_EQ(ptr[i], i); /* Migrate memory to the device again. */ - ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_MIGRATE, buffer, npages); + ret = hmm_migrate_sys_to_dev(self->fd, buffer, npages); ASSERT_EQ(ret, 0); ASSERT_EQ(buffer->cpages, npages); @@ -976,7 +1041,7 @@ TEST_F(hmm, migrate_shared) ASSERT_NE(buffer->ptr, MAP_FAILED); /* Migrate memory to device. */ - ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_MIGRATE, buffer, npages); + ret = hmm_migrate_sys_to_dev(self->fd, buffer, npages); ASSERT_EQ(ret, -ENOENT); hmm_buffer_free(buffer); @@ -1015,7 +1080,7 @@ TEST_F(hmm2, migrate_mixed) p = buffer->ptr; /* Migrating a protected area should be an error. */ - ret = hmm_dmirror_cmd(self->fd1, HMM_DMIRROR_MIGRATE, buffer, npages); + ret = hmm_migrate_sys_to_dev(self->fd1, buffer, npages); ASSERT_EQ(ret, -EINVAL); /* Punch a hole after the first page address. */ @@ -1023,7 +1088,7 @@ TEST_F(hmm2, migrate_mixed) ASSERT_EQ(ret, 0); /* We expect an error if the vma doesn't cover the range. */ - ret = hmm_dmirror_cmd(self->fd1, HMM_DMIRROR_MIGRATE, buffer, 3); + ret = hmm_migrate_sys_to_dev(self->fd1, buffer, 3); ASSERT_EQ(ret, -EINVAL); /* Page 2 will be a read-only zero page. */ @@ -1055,13 +1120,13 @@ TEST_F(hmm2, migrate_mixed) /* Now try to migrate pages 2-5 to device 1. */ buffer->ptr = p + 2 * self->page_size; - ret = hmm_dmirror_cmd(self->fd1, HMM_DMIRROR_MIGRATE, buffer, 4); + ret = hmm_migrate_sys_to_dev(self->fd1, buffer, 4); ASSERT_EQ(ret, 0); ASSERT_EQ(buffer->cpages, 4); /* Page 5 won't be migrated to device 0 because it's on device 1. */ buffer->ptr = p + 5 * self->page_size; - ret = hmm_dmirror_cmd(self->fd0, HMM_DMIRROR_MIGRATE, buffer, 1); + ret = hmm_migrate_sys_to_dev(self->fd0, buffer, 1); ASSERT_EQ(ret, -ENOENT); buffer->ptr = p; @@ -1070,8 +1135,12 @@ TEST_F(hmm2, migrate_mixed) } /* - * Migrate anonymous memory to device private memory and fault it back to system - * memory multiple times. + * Migrate anonymous memory to device memory and back to system memory + * multiple times. In case of private zone configuration, this is done + * through fault pages accessed by CPU. In case of coherent zone configuration, + * the pages from the device should be explicitly migrated back to system memory. + * The reason is Coherent device zone has coherent access by CPU, therefore + * it will not generate any page fault. */ TEST_F(hmm, migrate_multiple) { @@ -1107,8 +1176,7 @@ TEST_F(hmm, migrate_multiple) ptr[i] = i; /* Migrate memory to device. */ - ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_MIGRATE, buffer, - npages); + ret = hmm_migrate_sys_to_dev(self->fd, buffer, npages); ASSERT_EQ(ret, 0); ASSERT_EQ(buffer->cpages, npages); @@ -1116,7 +1184,13 @@ TEST_F(hmm, migrate_multiple) for (i = 0, ptr = buffer->mirror; i < size / sizeof(*ptr); ++i) ASSERT_EQ(ptr[i], i); - /* Fault pages back to system memory and check them. */ + /* Migrate back to system memory and check them. */ + if (hmm_is_coherent_type(variant->device_number)) { + ret = hmm_migrate_dev_to_sys(self->fd, buffer, npages); + ASSERT_EQ(ret, 0); + ASSERT_EQ(buffer->cpages, npages); + } + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) ASSERT_EQ(ptr[i], i); @@ -1354,13 +1428,13 @@ TEST_F(hmm2, snapshot) /* Page 5 will be migrated to device 0. */ buffer->ptr = p + 5 * self->page_size; - ret = hmm_dmirror_cmd(self->fd0, HMM_DMIRROR_MIGRATE, buffer, 1); + ret = hmm_migrate_sys_to_dev(self->fd0, buffer, 1); ASSERT_EQ(ret, 0); ASSERT_EQ(buffer->cpages, 1); /* Page 6 will be migrated to device 1. */ buffer->ptr = p + 6 * self->page_size; - ret = hmm_dmirror_cmd(self->fd1, HMM_DMIRROR_MIGRATE, buffer, 1); + ret = hmm_migrate_sys_to_dev(self->fd1, buffer, 1); ASSERT_EQ(ret, 0); ASSERT_EQ(buffer->cpages, 1); @@ -1377,9 +1451,16 @@ TEST_F(hmm2, snapshot) ASSERT_EQ(m[2], HMM_DMIRROR_PROT_ZERO | HMM_DMIRROR_PROT_READ); ASSERT_EQ(m[3], HMM_DMIRROR_PROT_READ); ASSERT_EQ(m[4], HMM_DMIRROR_PROT_WRITE); - ASSERT_EQ(m[5], HMM_DMIRROR_PROT_DEV_PRIVATE_LOCAL | - HMM_DMIRROR_PROT_WRITE); - ASSERT_EQ(m[6], HMM_DMIRROR_PROT_NONE); + if (!hmm_is_coherent_type(variant->device_number0)) { + ASSERT_EQ(m[5], HMM_DMIRROR_PROT_DEV_PRIVATE_LOCAL | + HMM_DMIRROR_PROT_WRITE); + ASSERT_EQ(m[6], HMM_DMIRROR_PROT_NONE); + } else { + ASSERT_EQ(m[5], HMM_DMIRROR_PROT_DEV_COHERENT_LOCAL | + HMM_DMIRROR_PROT_WRITE); + ASSERT_EQ(m[6], HMM_DMIRROR_PROT_DEV_COHERENT_REMOTE | + HMM_DMIRROR_PROT_WRITE); + } hmm_buffer_free(buffer); } From patchwork Mon May 16 23:14:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851709 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AEB93C433FE for ; Mon, 16 May 2022 23:15:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350315AbiEPXP1 (ORCPT ); Mon, 16 May 2022 19:15:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350284AbiEPXPP (ORCPT ); Mon, 16 May 2022 19:15:15 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2047.outbound.protection.outlook.com [40.107.223.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC0B846B2B; Mon, 16 May 2022 16:15:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=P1pnwNVd/vd6yD+RlpvDmUMVDteM3Vvio0h4v7cQGe7RWhqOEkY8kwDdw2BAdBGct+rtRxWBQ+TOV12x8etNqWaDd/5QiCvmdsoryVlBhqMq6uDujw/k+PVn7yJdfpnCCC1kkggWfv1k324KKtX3chWAjtFVXoakbV/C/TY/RJt+SzqChQ6yzmSqPjkBvX+iZXcXCVh1myQnuVu5vUeh4XGDPIxeN8BvjvlgbKVyg/jvNvKwf/2JUgojiNGaPZtkRXeTVS5QIspZ+t+7VH42m2mdrBfr24mQQLjWH8wCpdwh7dF5XayEykRxGObmjCHwc/oCFOcGbEKMSK21dTFSRw== 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=Ds2C6hKBCPm3LMFGvWFrTQmYNyEijOJ3/DIUhvifx8M=; b=GDQLkC6St/MwUk9t4qd3gcej/gJjSxB+seeDQmzvOPNxaDAY9vah8pRTdi7bYTeWLk0GCLt13ikB7UuFzrwVx4yQlW0rtldbhhAIsKmpVq/xZr7JNTMKmUktdi6xhSBm7TClGi9ZNvsMmzeQsnc4Q/BsODDzdQklmuo/EbDr23w9Pkk9NfjpBzC0MBa5zlVFFRZfTrY+vSoB1tlSD5ZqHwBu7uAk61aZDQMWD7aeKYRs6GRJynGIzyLDRtMFdgUaVCribk4+MX7jLn9o2C6budItRA7lAUHs6jElHjVCJuZk0KIRDnjrG7A8nw1EQzE3LwZiIcCr4VGIa1Fp1HaNbA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Ds2C6hKBCPm3LMFGvWFrTQmYNyEijOJ3/DIUhvifx8M=; b=MvY5hrCIREeMeUOihUkvQqp4JKXVxGRAJUdzLgUgHkBlzI2TAOGWLjMStmJlmbtEq1R+xJUjZ//w16G1WWaMGZO5K7imOXddfoXXzJy6NNTlCmB9wFG1ZnizQjBgFpZ90g7n5A8CoDpsa1xrMwczOj8xnfxpi+znTCZZtKRm4Vg= Received: from BN9PR03CA0717.namprd03.prod.outlook.com (2603:10b6:408:ef::32) by IA1PR12MB6305.namprd12.prod.outlook.com (2603:10b6:208:3e7::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Mon, 16 May 2022 23:15:05 +0000 Received: from BN8NAM11FT041.eop-nam11.prod.protection.outlook.com (2603:10b6:408:ef:cafe::9a) by BN9PR03CA0717.outlook.office365.com (2603:10b6:408:ef::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14 via Frontend Transport; Mon, 16 May 2022 23:15:05 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT041.mail.protection.outlook.com (10.13.177.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:15:05 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:15:03 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 10/13] tools: update test_hmm script to support SP config Date: Mon, 16 May 2022 18:14:29 -0500 Message-ID: <20220516231432.20200-11-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1f54dd1b-7e43-4712-cf66-08da3791e995 X-MS-TrafficTypeDiagnostic: IA1PR12MB6305: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: 8c4pZ1gq6MhFIE8irMsc3thW1QW2qz2d/eYN4IpjVMrlWpByJ455oIryNvDeqNmlVpioB7Ni9noo8VISHxtfKABH0Er/VvWQREGM3Jz0/5ymJiPmCEsW5XtmqWzgVa8BPagff1VXEpDXbOI7S/EliQUg0S3N7HXtNkOJpTrWyJKKSPKxC9tay/r5LZzC9prr1wEEB4gNL/UBsGxU4zk4oVzBtmgpko51AoNoQBtjkwECF/7lea0+lNEK7GqhYurdwpLET7p1MLiZpkCsG0JAg3jE46tO8rpg1Jr29xAmAm438ZpVsBxI45jSFlnDWsP7sH8TB7P4eVOYoGTLf8z94rWnSC/P70avMaOOZqDb1SXAeLaNpjA6Bxfi1MXTw7v8rZS6+jzFYdd9nGHb9QNrYTYbxp9NhFslW8aEbLznZ5a5FXZeLaDeyBnz2m8hYEgkYOgp5AX/5gFdro/d1AOJuFyvz060ISGDwDW9LaXpxLePkDNCRknyWjEptlpgm7ObS1+6RsHzLFoLFbLDvKpIvYS1wbW0bUPCPrRr/zRJcQ4o3gbbiholK1hj13B/3okKwfmEXYaLfLfe7ymVWEsKEsEY0fGqMBrIrz0V1z/uJgpXskWUC/O/I8PkH/VMg+ywPXRi4RSCrd1VMEDILwVpwEcW/MAlxgS9AgliIwIT37O1e4ReeOTU5WUos8OMS8DiVX+0y9TLiCxXj63hzRK42Q== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(40470700004)(46966006)(36840700001)(508600001)(83380400001)(81166007)(316002)(7696005)(8936002)(36756003)(6666004)(2906002)(8676002)(1076003)(26005)(86362001)(2616005)(336012)(47076005)(426003)(16526019)(40460700003)(5660300002)(186003)(4326008)(6916009)(70206006)(82310400005)(7416002)(54906003)(44832011)(36860700001)(70586007)(356005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:15:05.4416 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1f54dd1b-7e43-4712-cf66-08da3791e995 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT041.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6305 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org Add two more parameters to set spm_addr_dev0 & spm_addr_dev1 addresses. These two parameters configure the start SP addresses for each device in test_hmm driver. Consequently, this configures zone device type as coherent. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling Reviewed-by: Alistair Popple Signed-off-by: Christoph Hellwig --- tools/testing/selftests/vm/test_hmm.sh | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/vm/test_hmm.sh b/tools/testing/selftests/vm/test_hmm.sh index 0647b525a625..539c9371e592 100755 --- a/tools/testing/selftests/vm/test_hmm.sh +++ b/tools/testing/selftests/vm/test_hmm.sh @@ -40,11 +40,26 @@ check_test_requirements() load_driver() { - modprobe $DRIVER > /dev/null 2>&1 + if [ $# -eq 0 ]; then + modprobe $DRIVER > /dev/null 2>&1 + else + if [ $# -eq 2 ]; then + modprobe $DRIVER spm_addr_dev0=$1 spm_addr_dev1=$2 + > /dev/null 2>&1 + else + echo "Missing module parameters. Make sure pass"\ + "spm_addr_dev0 and spm_addr_dev1" + usage + fi + fi if [ $? == 0 ]; then major=$(awk "\$2==\"HMM_DMIRROR\" {print \$1}" /proc/devices) mknod /dev/hmm_dmirror0 c $major 0 mknod /dev/hmm_dmirror1 c $major 1 + if [ $# -eq 2 ]; then + mknod /dev/hmm_dmirror2 c $major 2 + mknod /dev/hmm_dmirror3 c $major 3 + fi fi } @@ -58,7 +73,7 @@ run_smoke() { echo "Running smoke test. Note, this test provides basic coverage." - load_driver + load_driver $1 $2 $(dirname "${BASH_SOURCE[0]}")/hmm-tests unload_driver } @@ -75,6 +90,9 @@ usage() echo "# Smoke testing" echo "./${TEST_NAME}.sh smoke" echo + echo "# Smoke testing with SPM enabled" + echo "./${TEST_NAME}.sh smoke " + echo exit 0 } @@ -84,7 +102,7 @@ function run_test() usage else if [ "$1" = "smoke" ]; then - run_smoke + run_smoke $2 $3 else usage fi From patchwork Mon May 16 23:14:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851705 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 031CFC43217 for ; Mon, 16 May 2022 23:15:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350254AbiEPXPS (ORCPT ); Mon, 16 May 2022 19:15:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350261AbiEPXPJ (ORCPT ); Mon, 16 May 2022 19:15:09 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2070.outbound.protection.outlook.com [40.107.93.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0F3946B3B; Mon, 16 May 2022 16:15:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cXO306FxIXg1LVDoYXSICOfjaIsHYZLHRt5YJQ30t6pvkO/xpNgwwIXEDLzwNHAz2BxgMysdoSgnZcwnnv9LwOE6DJBQCxefPDMmX2yJtgBdRFLG/wTK16lV41egkThzbsNOXbS3C0tNpXAbSuWWS9G5NkRYGNKpiV8zcq0TPAkrTfCWG5Q3xVpd9cLV75g8yeBZKbrvhB195rbkzCX25jIR4/ohYbmoN9DnwN5wwmiWVI1aX10PiuI8/OqghvG5FbYMt/B0mOT3Lkl6F+Q46MQK4n9zsIZ+H695ypMAKN1cHkeXfn0HXsr4UwpHwrXmyNaoNhIhYuLv7w2DnJkLGQ== 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=+YYePLtbTgIOF8ew+l9yPAyCRqb6w1OAoVj7JzT49p4=; b=keNZML1hFDaUWNnX7Xsi/uxFf4RS9EVS3OjlgxjaanK4/sVH2zMlRPzF4qoQ4w1ci4VDjmx0Zu5d58qlMCIKEn0KtYrrF/phbhm0tkhaNLxmW9MmHEAICnNcpHxYjTzzDMB1fCZgcxX3DgK4jUHqJIDE5Kw0MiZnqRnA2JYLZjeWNiu0YXuacbkaGqbmrYnb33iLl4/ewUgJwJwveHVipCCuk9th1mGFzOEuaOZ7w2zZGnRCjqszoYmnSsrzDo77ISuudAwi951RCWpy8imQTSmqwh1Kc0LNzY2o4jeCfC94/h2RxVXN3qC//BUIdKzk8dxSILVyfKcRgeHr1F3TeQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=+YYePLtbTgIOF8ew+l9yPAyCRqb6w1OAoVj7JzT49p4=; b=DSXbAQHh9RxEjCgsIf5ex4EgghAvSt7eG8Cq3JCQNbg0h9xwhIR5mFVksESh4wp4qypEP/sDzC1POmO6lm6INbHzo9WmYsarzp6P+l69LpIGuNjrDrXM/vA45aFO/5xMAy4j6v3scH+2VLdTtjkeIZ/YrP3D1C2GwGlBgnrLJtA= Received: from BN9PR03CA0695.namprd03.prod.outlook.com (2603:10b6:408:ef::10) by BN8PR12MB2866.namprd12.prod.outlook.com (2603:10b6:408:68::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Mon, 16 May 2022 23:15:05 +0000 Received: from BN8NAM11FT041.eop-nam11.prod.protection.outlook.com (2603:10b6:408:ef:cafe::6) by BN9PR03CA0695.outlook.office365.com (2603:10b6:408:ef::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14 via Frontend Transport; Mon, 16 May 2022 23:15:05 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT041.mail.protection.outlook.com (10.13.177.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:15:05 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:15:04 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 11/13] mm: handling Non-LRU pages returned by vm_normal_pages Date: Mon, 16 May 2022 18:14:30 -0500 Message-ID: <20220516231432.20200-12-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 370e0492-adfa-4eed-422a-08da3791e9ca X-MS-TrafficTypeDiagnostic: BN8PR12MB2866: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: kWFTRVyra4vXZFsVwArUywf3jzGIPWZMRnxhFYrMtqQPRri/EDkUIRrEVLus0g9imLXR8qhtUmSFUcUF/XLZD2597T1BKiDlgJwXYrB3fMhOGJXRm7nYhCdYBr2R9aZeBZ0SdoloBu5GQsBlg6iAUnDHycLeMsgpPiygagTTXMFI7w7G76jl/cNwnwoV3BlLxVXjr1o4ZZAgu65LSBTbKqhnYL2wFoQ3F5pfygfQ6eRBa8pSwJSO1hyM7Sqom1LQsd7J5N6j/XZq5B/7IxYfKwdCaK8ByULhFB4ItdgTSX8LYjDzfYEt7bCPmHlbRmRv0WBgkwc2OGhl+Dro8q3SR3wm+D/nU1+ic2DcTE+o9UknQNc1T78qWY2bTG41vNICTEtLDTiChhYZnef4E2Hk2B8eZSIEYYmP2N4aNQUnfCR3zp33wYp7lxHjLeO8DTRjjhFtSA9PgrvJpIj6rftSHdNLNeu8bItgG3pQQT649JZyKNC6/DJV0BTrUl+fEXC6doPqPVW340EqzXeFV9xBCqGL0v4BKvuTliS3dJOJJXMcU7Mk/wTQADhqomnkJ9XD99RFhoQothRod0U87dmOfC4UdsBdxA6fakb0V1ch0q2MatynUKWvPHm3/mz2dOdEsHrNwBr3Uy7+KVirryjl8sWlieyQ5Cw94lG5m4kbykzRY4dGi06v/wKzNpWzY4wIaT2UMdbw028WNEXk3JM9hw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(46966006)(36840700001)(40470700004)(40460700003)(316002)(83380400001)(186003)(336012)(5660300002)(426003)(8676002)(6916009)(7416002)(86362001)(70206006)(47076005)(4326008)(54906003)(356005)(81166007)(7696005)(1076003)(6666004)(36756003)(16526019)(44832011)(8936002)(508600001)(2616005)(82310400005)(70586007)(2906002)(26005)(36860700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:15:05.7853 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 370e0492-adfa-4eed-422a-08da3791e9ca X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT041.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN8PR12MB2866 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org With DEVICE_COHERENT, we'll soon have vm_normal_pages() return device-managed anonymous pages that are not LRU pages. Although they behave like normal pages for purposes of mapping in CPU page, and for COW. They do not support LRU lists, NUMA migration or THP. We also introduced a FOLL_LRU flag that adds the same behaviour to follow_page and related APIs, to allow callers to specify that they expect to put pages on an LRU list. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling --- fs/proc/task_mmu.c | 2 +- include/linux/mm.h | 3 ++- mm/gup.c | 2 ++ mm/huge_memory.c | 2 +- mm/khugepaged.c | 9 ++++++--- mm/ksm.c | 6 +++--- mm/madvise.c | 4 ++-- mm/memory.c | 9 ++++++++- mm/mempolicy.c | 2 +- mm/migrate.c | 4 ++-- mm/mlock.c | 2 +- mm/mprotect.c | 2 +- 12 files changed, 30 insertions(+), 17 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index f46060eb91b5..5d620733f173 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1785,7 +1785,7 @@ static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma, return NULL; page = vm_normal_page(vma, addr, pte); - if (!page) + if (!page || is_zone_device_page(page)) return NULL; if (PageReserved(page)) diff --git a/include/linux/mm.h b/include/linux/mm.h index 9f44254af8ce..d7f253a0c41e 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -601,7 +601,7 @@ struct vm_operations_struct { #endif /* * Called by vm_normal_page() for special PTEs to find the - * page for @addr. This is useful if the default behavior + * page for @addr. This is useful if the default behavior * (using pte_page()) would not find the correct page. */ struct page *(*find_special_page)(struct vm_area_struct *vma, @@ -2929,6 +2929,7 @@ struct page *follow_page(struct vm_area_struct *vma, unsigned long address, #define FOLL_NUMA 0x200 /* force NUMA hinting page fault */ #define FOLL_MIGRATION 0x400 /* wait for page to replace migration entry */ #define FOLL_TRIED 0x800 /* a retry, previous pass started an IO */ +#define FOLL_LRU 0x1000 /* return only LRU (anon or page cache) */ #define FOLL_REMOTE 0x2000 /* we are working on non-current tsk/mm */ #define FOLL_COW 0x4000 /* internal GUP flag */ #define FOLL_ANON 0x8000 /* don't do file mappings */ diff --git a/mm/gup.c b/mm/gup.c index a214c8df7140..13bf04bce132 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -479,6 +479,8 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, } page = vm_normal_page(vma, address, pte); + if ((flags & FOLL_LRU) && page && is_zone_device_page(page)) + page = NULL; if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) { /* * Only return device mapping pages in the FOLL_GET or FOLL_PIN diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 910a138e9859..eed80696c5fd 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2856,7 +2856,7 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, } /* FOLL_DUMP to ignore special (like zero) pages */ - page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP); + page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP | FOLL_LRU); if (IS_ERR(page)) continue; diff --git a/mm/khugepaged.c b/mm/khugepaged.c index a4e5eaf3eb01..eb3cfd679800 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -627,7 +627,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, goto out; } page = vm_normal_page(vma, address, pteval); - if (unlikely(!page)) { + if (unlikely(!page) || unlikely(is_zone_device_page(page))) { result = SCAN_PAGE_NULL; goto out; } @@ -1276,7 +1276,7 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, writable = true; page = vm_normal_page(vma, _address, pteval); - if (unlikely(!page)) { + if (unlikely(!page) || unlikely(is_zone_device_page(page))) { result = SCAN_PAGE_NULL; goto out_unmap; } @@ -1484,7 +1484,8 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr) goto abort; page = vm_normal_page(vma, addr, *pte); - + if (page && is_zone_device_page(page)) + page = NULL; /* * Note that uprobe, debugger, or MAP_PRIVATE may change the * page table, but the new page will not be a subpage of hpage. @@ -1502,6 +1503,8 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr) if (pte_none(*pte)) continue; page = vm_normal_page(vma, addr, *pte); + if (page && is_zone_device_page(page)) + goto abort; page_remove_rmap(page, vma, false); } diff --git a/mm/ksm.c b/mm/ksm.c index 063a48eeb5ee..f16056efca21 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -474,7 +474,7 @@ static int break_ksm(struct vm_area_struct *vma, unsigned long addr) do { cond_resched(); page = follow_page(vma, addr, - FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE); + FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE | FOLL_LRU); if (IS_ERR_OR_NULL(page)) break; if (PageKsm(page)) @@ -559,7 +559,7 @@ static struct page *get_mergeable_page(struct rmap_item *rmap_item) if (!vma) goto out; - page = follow_page(vma, addr, FOLL_GET); + page = follow_page(vma, addr, FOLL_GET | FOLL_LRU); if (IS_ERR_OR_NULL(page)) goto out; if (PageAnon(page)) { @@ -2288,7 +2288,7 @@ static struct rmap_item *scan_get_next_rmap_item(struct page **page) while (ksm_scan.address < vma->vm_end) { if (ksm_test_exit(mm)) break; - *page = follow_page(vma, ksm_scan.address, FOLL_GET); + *page = follow_page(vma, ksm_scan.address, FOLL_GET | FOLL_LRU); if (IS_ERR_OR_NULL(*page)) { ksm_scan.address += PAGE_SIZE; cond_resched(); diff --git a/mm/madvise.c b/mm/madvise.c index 1873616a37d2..e9c24c834e98 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -413,7 +413,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, continue; page = vm_normal_page(vma, addr, ptent); - if (!page) + if (!page || is_zone_device_page(page)) continue; /* @@ -628,7 +628,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, } page = vm_normal_page(vma, addr, ptent); - if (!page) + if (!page || is_zone_device_page(page)) continue; /* diff --git a/mm/memory.c b/mm/memory.c index 76e3af9639d9..571a26805ee1 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -621,6 +621,13 @@ struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, if (is_zero_pfn(pfn)) return NULL; if (pte_devmap(pte)) +/* + * NOTE: New uers of ZONE_DEVICE will not set pte_devmap() and will have + * refcounts incremented on their struct pages when they are inserted into + * PTEs, thus they are safe to return here. Legacy ZONE_DEVICE pages that set + * pte_devmap() do not have refcounts. Example of legacy ZONE_DEVICE is + * MEMORY_DEVICE_FS_DAX type in pmem or virtio_fs drivers. + */ return NULL; print_bad_pte(vma, addr, pte, NULL); @@ -4422,7 +4429,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) pte = pte_modify(old_pte, vma->vm_page_prot); page = vm_normal_page(vma, vmf->address, pte); - if (!page) + if (!page || is_zone_device_page(page)) goto out_map; /* TODO: handle PTE-mapped THP */ diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 8c74107a2b15..e32edbecb0cd 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -528,7 +528,7 @@ static int queue_pages_pte_range(pmd_t *pmd, unsigned long addr, if (!pte_present(*pte)) continue; page = vm_normal_page(vma, addr, *pte); - if (!page) + if (!page || is_zone_device_page(page)) continue; /* * vm_normal_page() filters out zero pages, but there might diff --git a/mm/migrate.c b/mm/migrate.c index 6c31ee1e1c9b..c5d50e96ecd7 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1611,7 +1611,7 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, goto out; /* FOLL_DUMP to ignore special (like zero) pages */ - page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP); + page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP | FOLL_LRU); err = PTR_ERR(page); if (IS_ERR(page)) @@ -1802,7 +1802,7 @@ static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, goto set_status; /* FOLL_DUMP to ignore special (like zero) pages */ - page = follow_page(vma, addr, FOLL_DUMP); + page = follow_page(vma, addr, FOLL_DUMP | FOLL_LRU); err = PTR_ERR(page); if (IS_ERR(page)) diff --git a/mm/mlock.c b/mm/mlock.c index 716caf851043..b14e929084cc 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -333,7 +333,7 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long addr, if (!pte_present(*pte)) continue; page = vm_normal_page(vma, addr, *pte); - if (!page) + if (!page || is_zone_device_page(page)) continue; if (PageTransCompound(page)) continue; diff --git a/mm/mprotect.c b/mm/mprotect.c index b69ce7a7b2b7..a6f3587ea29a 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -91,7 +91,7 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, continue; page = vm_normal_page(vma, addr, oldpte); - if (!page || PageKsm(page)) + if (!page || is_zone_device_page(page) || PageKsm(page)) continue; /* Also skip shared copy-on-write pages */ From patchwork Mon May 16 23:14:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851707 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F1CCC4332F for ; Mon, 16 May 2022 23:15:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350302AbiEPXPU (ORCPT ); Mon, 16 May 2022 19:15:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350267AbiEPXPK (ORCPT ); Mon, 16 May 2022 19:15:10 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2071.outbound.protection.outlook.com [40.107.96.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63E5D46657; Mon, 16 May 2022 16:15:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hWSgUfLnhE3Xe+qWBcIROmTeq6SJHum/YFTFRXiCwEPbIuQZE5bqZA6dITACXdzhQrj8Qp7wr6PXpoxMjbVfMo8Tr8CKVw8pkD9XzGVsvmlG7MKX2VDZBk1OaEPNNEiGI6TDIpXfkjQNLNfWkAY/ORyGI15cO8lGXY5w3ZTVUhBR0xXTiiWTC6ZJjxRUP2I29cxSo3ntqdK8r3JHkXi8m/zdOm3XbKJ3lrhr/j6elYuKIP2B11Rm+WDaLEqPbPmmTcGm8H++XAOZq51UXKrxkSBvOUbztXNMeLKFkls7W2XlKt4ZuEYwHR5d21z0udzRbMgLmY83Jf7010oauGEhQA== 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=FDtE0UiWruDuhx3PGoXdolkeW/K0qLVDBaC3nvm9Xp8=; b=aFfyXoIPrS/+RtcVUHVwsqstl7RWZv0DBu/UAOlMPmEI/bvU9ORUl8DPhZfzRoR5roGYo4IZUVUBmHo52ojmNpVdjg9b0RgCj5X9BU8lMUng6FEek+I16ntcxOWR62AsLXnf3BSMQ/VtWWSIiSnyVWFTsr4dWIPVjEAkpwQygtkP/wTFEE5dM/2A8g6ztIXPQxAIh3kt6hLI/n/Pu9ukWFV8oe5tzH1JEwF+unsMKbXwqA/Pab9QvclyaNmhi63/kUAwwyXgMffN6Hf1jZym20VvjN6Dfts4nl1WgNbosu1QQwl/GDE7a8u+qVqm9FFLrxahVwo1T+6S88vJrIZ4OA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=FDtE0UiWruDuhx3PGoXdolkeW/K0qLVDBaC3nvm9Xp8=; b=UVp4ehEefTRoPSl4ohQ+0oDyjwcSailyVaCaBVx4TpdcW6MeAXpBpTVOgIt9hDbgVQ7XT3sF+xhl4AwJ6RsZEat12KeWxHj3YjbA07Oh+KLFTr8rcxSvFPl6755pS4kSaga8ocb9EqEtkPwLoBqSCtNz8gJEDx5wO62GOJbl/b4= Received: from BN9PR03CA0713.namprd03.prod.outlook.com (2603:10b6:408:ef::28) by SN1PR12MB2591.namprd12.prod.outlook.com (2603:10b6:802:30::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.15; Mon, 16 May 2022 23:15:06 +0000 Received: from BN8NAM11FT041.eop-nam11.prod.protection.outlook.com (2603:10b6:408:ef:cafe::f1) by BN9PR03CA0713.outlook.office365.com (2603:10b6:408:ef::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:15:06 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT041.mail.protection.outlook.com (10.13.177.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:15:06 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:15:04 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 12/13] tools: add hmm gup tests for device coherent type Date: Mon, 16 May 2022 18:14:31 -0500 Message-ID: <20220516231432.20200-13-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1a60e5f1-fb84-45ba-2bb6-08da3791e9f5 X-MS-TrafficTypeDiagnostic: SN1PR12MB2591: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: wJthxU2+zshA6r/5EE74K0pBPoYw54kherCC0lTKiY81MFsQv8kz0rbLNwPSg+XnJsq5bTGca0PnmJW18YOVifCtVQB8FFE/2jRADhAIN4JsCfzM5FnaJ1RwiOBM6iAH89abl6b7jatxSdrGouLWwq8PnZBmS2B2UDMsnfE3jeMP6cVCUCFsuhAqHJWigiwlzl6a6Vf8gbXhRsrH0pJEAw59Y137cbcMpVolU0h0y87x/toM5SZlbP5cc+nNbAc9+PkqXH2zIpU6Cv1YS5UNIwkpLNGY0uYz39Ga5NrnnRlFUQD+oLWu8Tuty+ZqjFRmWL770ndLXmDYadRuwfl0vecRZcK0m/ra9JBpcYLxHLEpbxcCErEyHv6ZOekwi3HO/vbJXdTq/XRlxOEluEih3Nu9nv/+brWH3mDnMNhi/inf7cDFrf/+m3HJH9UVcOAMhyy7YNNPssnOK0ZsFz9AlkCey0l8PgN98+UC0AqamqzMmafMoKVFAVODZmUPJ/syOVRTpq9KGOdcISkOqS8BZMfIPkwmPvBy3Wg+rsXq2bf5GZsai6CTtdhTfRFnYsviRJcN7HOAmmEWWZRvow9Cjkt2sjh8ZkDxpD4QF+aRAGepqZ+13AqM4T09VtkxGpOZPCsqOc+sbhnVf2A3Dmc3RxXEg5Zzx1QaPkJCt6BUbff7q7+MJK5Sm1sMbeQeNPqXpl9MKOvl8I4aPAXMq//nMA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(46966006)(40470700004)(36840700001)(44832011)(7416002)(5660300002)(508600001)(8936002)(4326008)(8676002)(70206006)(70586007)(86362001)(40460700003)(26005)(36756003)(2616005)(7696005)(54906003)(186003)(1076003)(16526019)(426003)(356005)(336012)(47076005)(6666004)(81166007)(2906002)(36860700001)(83380400001)(82310400005)(6916009)(316002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:15:06.0665 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1a60e5f1-fb84-45ba-2bb6-08da3791e9f5 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT041.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2591 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org The intention is to test hmm device coherent type under different get user pages paths. Also, test gup with FOLL_LONGTERM flag set in device coherent pages. These pages should get migrated back to system memory. Signed-off-by: Alex Sierra Reviewed-by: Alistair Popple Reviewed-by: Felix Kuehling --- tools/testing/selftests/vm/hmm-tests.c | 105 +++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c index 84ec8c4a1dc7..61a02a6a3dea 100644 --- a/tools/testing/selftests/vm/hmm-tests.c +++ b/tools/testing/selftests/vm/hmm-tests.c @@ -36,6 +36,7 @@ * in the usual include/uapi/... directory. */ #include "../../../../lib/test_hmm_uapi.h" +#include "../../../../mm/gup_test.h" struct hmm_buffer { void *ptr; @@ -60,6 +61,8 @@ enum { #define NTIMES 10 #define ALIGN(x, a) (((x) + (a - 1)) & (~((a) - 1))) +/* Just the flags we need, copied from mm.h: */ +#define FOLL_WRITE 0x01 /* check pte is writable */ FIXTURE(hmm) { @@ -1766,4 +1769,106 @@ TEST_F(hmm, exclusive_cow) hmm_buffer_free(buffer); } +static int gup_test_exec(int gup_fd, unsigned long addr, + int cmd, int npages, int size) +{ + struct gup_test gup = { + .nr_pages_per_call = npages, + .addr = addr, + .gup_flags = FOLL_WRITE, + .size = size, + }; + + if (ioctl(gup_fd, cmd, &gup)) { + perror("ioctl on error\n"); + return errno; + } + + return 0; +} + +/* + * Test get user device pages through gup_test. Setting PIN_LONGTERM flag. + * This should trigger a migration back to system memory for both, private + * and coherent type pages. + * This test makes use of gup_test module. Make sure GUP_TEST_CONFIG is added + * to your configuration before you run it. + */ +TEST_F(hmm, hmm_gup_test) +{ + struct hmm_buffer *buffer; + int gup_fd; + unsigned long npages; + unsigned long size; + unsigned long i; + int *ptr; + int ret; + unsigned char *m; + + gup_fd = open("/sys/kernel/debug/gup_test", O_RDWR); + if (gup_fd == -1) + SKIP(return, "Skipping test, could not find gup_test driver"); + + npages = 3; + 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); + + 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] = i; + + /* Migrate memory to device. */ + ret = hmm_migrate_sys_to_dev(self->fd, buffer, npages); + ASSERT_EQ(ret, 0); + ASSERT_EQ(buffer->cpages, npages); + /* Check what the device read. */ + for (i = 0, ptr = buffer->mirror; i < size / sizeof(*ptr); ++i) + ASSERT_EQ(ptr[i], i); + + ASSERT_EQ(gup_test_exec(gup_fd, + (unsigned long)buffer->ptr, + GUP_BASIC_TEST, 1, self->page_size), 0); + ASSERT_EQ(gup_test_exec(gup_fd, + (unsigned long)buffer->ptr + 1 * self->page_size, + GUP_FAST_BENCHMARK, 1, self->page_size), 0); + ASSERT_EQ(gup_test_exec(gup_fd, + (unsigned long)buffer->ptr + 2 * self->page_size, + PIN_LONGTERM_BENCHMARK, 1, self->page_size), 0); + + /* Take snapshot to CPU pagetables */ + ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_SNAPSHOT, buffer, npages); + ASSERT_EQ(ret, 0); + ASSERT_EQ(buffer->cpages, npages); + m = buffer->mirror; + if (hmm_is_coherent_type(variant->device_number)) { + ASSERT_EQ(HMM_DMIRROR_PROT_DEV_COHERENT_LOCAL | HMM_DMIRROR_PROT_WRITE, m[0]); + ASSERT_EQ(HMM_DMIRROR_PROT_DEV_COHERENT_LOCAL | HMM_DMIRROR_PROT_WRITE, m[1]); + } else { + ASSERT_EQ(HMM_DMIRROR_PROT_WRITE, m[0]); + ASSERT_EQ(HMM_DMIRROR_PROT_WRITE, m[1]); + } + ASSERT_EQ(HMM_DMIRROR_PROT_WRITE, m[2]); + /* + * Check again the content on the pages. Make sure there's no + * corrupted data. + */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + ASSERT_EQ(ptr[i], i); + + close(gup_fd); + hmm_buffer_free(buffer); +} TEST_HARNESS_MAIN From patchwork Mon May 16 23:14:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12851708 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9257C43219 for ; Mon, 16 May 2022 23:15:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350253AbiEPXPV (ORCPT ); Mon, 16 May 2022 19:15:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350281AbiEPXPM (ORCPT ); Mon, 16 May 2022 19:15:12 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2042.outbound.protection.outlook.com [40.107.244.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29BB146657; Mon, 16 May 2022 16:15:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XF+tGikl06V0I0QaK/oSKgeRtXkXyVS9uA8VxevGsxOdfEx6Sh4FXxjMzqvcaYMRtBpQ2z/J2LxApWWM8oK+cmp/g9wNwXY3pnzhqOxnhjOZSwHnaqccFUx3dlWFNTHaoowpp4KznCtfLz6R4gct6R+nnfKUC9xJfOuVnraMpD5gS/sOCsG0X8++QW3ZQDwUmI6MamHFQOetNWl7pCnhay6d+WvlraUkcWhQqExIS7tW2U8tAIu4Qu08WD2uoZRebLTo5DvID7uXcPN3wzwl5ScQwb5YZmkuVEIOH7Hhqgbp6i+gvkTqtNPUnq+XJnklqsPrzCt0h/4KmoYKQkYL+w== 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=OnJVDDXrT3V3lhnLAI+w6Qq16TsYyO7G/KHCNUvHeHE=; b=USNY6Wn1LljVQ6ya6IZdqtaAH7OWsvYYwEQju1icnaQlAHyHawn2Hi6geNCPI5w0im1ircCmP66ho8nnYb6mmI6OlEHL/cSTWJl35wcmCoWla5hxSEsZjbIlsoRUdVP+dsZ4KLUvs1EbCv6odCnay8IWnZFBWNpJ+qAq3DupUxHaNUHGQyz6n/V5P1qvTU7rtrfhYSXWeFhxOEiNs0dH8Qh4mZB2jm54h7oFB0JUtkd3bn+zQfZkMkIqhxhPufcwX/I9T9AKrNlZFhvXp7hHp9+ch/kqAmm4I0IKAmyo+VeCfkGs8zFhR6gr2ME8ubGSy6tS3TCEsUiIjQJRONuZQA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OnJVDDXrT3V3lhnLAI+w6Qq16TsYyO7G/KHCNUvHeHE=; b=mGLOIWz7F5zgeuyMqxvt1NGQwdUuOFXEEY15lARMDhvc617C4h5wcrFIj1wOOFbYgwml5ycXMLaOmSVDb8X1fWv7+oToPO6T4B05o8/KWNtQmJSN9xItz4ShlAHV/syg63IlAxSiudYzWScyUsCMOVIW08xGMfNgBulE43EhvE8= Received: from BN9PR03CA0716.namprd03.prod.outlook.com (2603:10b6:408:ef::31) by CY4PR1201MB2468.namprd12.prod.outlook.com (2603:10b6:903:d7::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.17; Mon, 16 May 2022 23:15:06 +0000 Received: from BN8NAM11FT041.eop-nam11.prod.protection.outlook.com (2603:10b6:408:ef:cafe::b1) by BN9PR03CA0716.outlook.office365.com (2603:10b6:408:ef::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14 via Frontend Transport; Mon, 16 May 2022 23:15:06 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT041.mail.protection.outlook.com (10.13.177.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5250.13 via Frontend Transport; Mon, 16 May 2022 23:15:06 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 16 May 2022 18:15:05 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v2 13/13] tools: add selftests to hmm for COW in device memory Date: Mon, 16 May 2022 18:14:32 -0500 Message-ID: <20220516231432.20200-14-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220516231432.20200-1-alex.sierra@amd.com> References: <20220516231432.20200-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4e8b0fbc-c602-4b4a-c374-08da3791ea4f X-MS-TrafficTypeDiagnostic: CY4PR1201MB2468: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: n+qqDEfRCL2742eNa1TyJMrDw6OwsY+zG+rYU4q3zfFa8qtgaXQlgEpFM9MC/JU/IIhu6ejBfgI8PefDMHewPCNG8S9uQw+b7yOtMTtm+vsf0Qebb3Bw1/4F+gizKkj0CW3o1g4qHS4zVwmsjCf4O21hLQW6z9cPvSHDp6WgaEhiXmp1Nmg8/poE4b/yB6RvewQ2iVsdTFEzYYXtOiXenL7T7U0R80+rQOUx1PFl/k+17OpMA0wBsCu0N0nfWMimgy3SlBxwobv3a9WKsHS26MeeeFTgwPtirfkEF+oMxZ8r5cTT+zVGQfIuTYlwEqMXBg5mWWhcMdh4fqk22iQ3kfFgMhKWILpa4bLIF93dP6DvlKzaz0yjwBj4Lzy2Gfi10q/BQ5nhD5urGqXGDILnkAdsbK4vb5esUbdMb8WkZwEKpHMe+8VZl9stmrG35+lWLKNC5Eg+vter89RoNkf0OWxvGdSL2KOpdlx9c/jA+d6poMhYRLLY6PgZTDF7w01Vz3bEcSLTKxGNHnd7wCU2ML5o2EO2GEZLwf2/iukmHuV42CXCc2qXbmVivVmpIiH9AAeQ5Als4DABHNnzO/RpoOcogCrmugQldywpA3E/DxmbDRLJPhIRGQcSK9CtcoQ0rzsj315CYs+1hNXWfZz5tRZhDzr2fTRnDNS7JyrL+uUlBwhmrzA64VLhqN5qqAKfqWcOwCGyWBCnF3T4R/u7Rg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(46966006)(36840700001)(40470700004)(316002)(36860700001)(36756003)(44832011)(70586007)(82310400005)(81166007)(7416002)(4326008)(8676002)(7696005)(6666004)(86362001)(70206006)(2616005)(356005)(5660300002)(2906002)(1076003)(54906003)(6916009)(186003)(16526019)(40460700003)(508600001)(8936002)(83380400001)(26005)(426003)(47076005)(336012)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2022 23:15:06.6446 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4e8b0fbc-c602-4b4a-c374-08da3791ea4f X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT041.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR1201MB2468 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org The objective is to test device migration mechanism in pages marked as COW, for private and coherent device type. In case of writing to COW private page(s), a page fault will migrate pages back to system memory first. Then, these pages will be duplicated. In case of COW device coherent type, pages are duplicated directly from device memory. Signed-off-by: Alex Sierra Acked-by: Felix Kuehling --- tools/testing/selftests/vm/hmm-tests.c | 80 ++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tools/testing/selftests/vm/hmm-tests.c b/tools/testing/selftests/vm/hmm-tests.c index 61a02a6a3dea..76570030ba5b 100644 --- a/tools/testing/selftests/vm/hmm-tests.c +++ b/tools/testing/selftests/vm/hmm-tests.c @@ -1871,4 +1871,84 @@ TEST_F(hmm, hmm_gup_test) close(gup_fd); hmm_buffer_free(buffer); } + +/* + * Test copy-on-write in device pages. + * In case of writing to COW private page(s), a page fault will migrate pages + * back to system memory first. Then, these pages will be duplicated. In case + * of COW device coherent type, pages are duplicated directly from device + * memory. + */ +TEST_F(hmm, hmm_cow_in_device) +{ + struct hmm_buffer *buffer; + unsigned long npages; + unsigned long size; + unsigned long i; + int *ptr; + int ret; + unsigned char *m; + pid_t pid; + int status; + + npages = 4; + 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); + + 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] = i; + + /* Migrate memory to device. */ + + ret = hmm_migrate_sys_to_dev(self->fd, buffer, npages); + ASSERT_EQ(ret, 0); + ASSERT_EQ(buffer->cpages, npages); + + pid = fork(); + if (pid == -1) + ASSERT_EQ(pid, 0); + if (!pid) { + /* Child process waitd for SIGTERM from the parent. */ + while (1) { + } + perror("Should not reach this\n"); + exit(0); + } + /* Parent process writes to COW pages(s) and gets a + * new copy in system. In case of device private pages, + * this write causes a migration to system mem first. + */ + for (i = 0, ptr = buffer->ptr; i < size / sizeof(*ptr); ++i) + ptr[i] = i; + + /* Terminate child and wait */ + EXPECT_EQ(0, kill(pid, SIGTERM)); + EXPECT_EQ(pid, waitpid(pid, &status, 0)); + EXPECT_NE(0, WIFSIGNALED(status)); + EXPECT_EQ(SIGTERM, WTERMSIG(status)); + + /* Take snapshot to CPU pagetables */ + ret = hmm_dmirror_cmd(self->fd, HMM_DMIRROR_SNAPSHOT, buffer, npages); + ASSERT_EQ(ret, 0); + ASSERT_EQ(buffer->cpages, npages); + m = buffer->mirror; + for (i = 0; i < npages; i++) + ASSERT_EQ(HMM_DMIRROR_PROT_WRITE, m[i]); + + hmm_buffer_free(buffer); +} TEST_HARNESS_MAIN