From patchwork Tue May 31 15:56:17 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: 12865881 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F5ADC433FE for ; Tue, 31 May 2022 15:56:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9CB466B0073; Tue, 31 May 2022 11:56:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A4F66B0074; Tue, 31 May 2022 11:56:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 867E66B0075; Tue, 31 May 2022 11:56:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7989B6B0073 for ; Tue, 31 May 2022 11:56:45 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2BED434B8E for ; Tue, 31 May 2022 15:56:45 +0000 (UTC) X-FDA: 79526491170.12.A44B73F Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam07on2067.outbound.protection.outlook.com [40.107.212.67]) by imf26.hostedemail.com (Postfix) with ESMTP id 07BE2140054 for ; Tue, 31 May 2022 15:56:39 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lV3NFX8vWvVzoj4b2f4wXaxD66XGtyoEshJvqbgMnoN91bwoxONLLD4gPlSCRFjpm4XeuF8RyK2VHad0fPApGdxbVCAqwGdCuZ8v1k3KtPJ7PdcfmOyP5OkIE9jbRgb4ve/o0xey5z9Fq86sM9OJq6XbLXavLuzi8W+aLXelm4SPpj3uY8q+11uhS48D9tnTbxab/xbDw56+nrXc8buqT/3EbKGpIXBMtjN/lYu65PYPt7Zhxg1cUwgcypJvoH+GG1F26qnV+5xNU81C6A5gRmzLuqsDGmSFNPYn2syTqO7VPE1WXWFyUp8GpVjICj2d0StN10WnzSMn5P14cTrasw== 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=yM1u4yCkqEAz9mq9J47hjozDMbEz1XEieINgg7ldrfs=; b=NTIDymA6KRVpfvKUa1ZsmcJCVgOBjXzx9W4/AdJDZFbkbmPGODdbP+5bzNxeDP+rIj77IlWqyxOqq/e2EL28XEcvPgV/iNdDGcX5gKMzLAbOBv4OvnSd8V1MYSHNyTW+xJjUTA9qkJMymK63fT/zFAZNxWaXVzUD5LsmV9kwTBrWd31kt+KDw5HI6mR+Bj+2S03P+6vSjzkz8K9WJVO2LFCCCGCY/1xMYvqZ4dilXceIeM9i3RsdI7bNIPkFxfQL8RBt/wdaXE4OzQPdSvzfQcEsN5RnUrmBtazgpYG8FRpObZpAzQs42CTnpeQY0h0tiazaumTUJvpl6aCaKqy7YA== 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=yM1u4yCkqEAz9mq9J47hjozDMbEz1XEieINgg7ldrfs=; b=tYndGtoAG/6aevOTiwohYgQVdq9AY4z6CLJImNu0ztwLVkazhawsoYNKRIPm2DqDMT7/g1wxBojIgSkl2MFO5ztFQ6cn9oktRnHNWcfUlr6/vB0qRLBniiU+IlKP/3hmSUAlkDdaIs3fvWHkxZFQE2eUnsCCBbgxj8f6CuCW+mg= Received: from BN6PR13CA0050.namprd13.prod.outlook.com (2603:10b6:404:11::12) by MWHPR12MB1214.namprd12.prod.outlook.com (2603:10b6:300:e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.15; Tue, 31 May 2022 15:56:41 +0000 Received: from BN8NAM11FT053.eop-nam11.prod.protection.outlook.com (2603:10b6:404:11:cafe::5f) by BN6PR13CA0050.outlook.office365.com (2603:10b6:404:11::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5314.12 via Frontend Transport; Tue, 31 May 2022 15:56:40 +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; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT053.mail.protection.outlook.com (10.13.177.209) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:40 +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.28; Tue, 31 May 2022 10:56:39 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 01/13] mm: add zone device coherent type memory support Date: Tue, 31 May 2022 10:56:17 -0500 Message-ID: <20220531155629.20057-2-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a80e9a18-f1ca-4b70-bab7-08da431e26f8 X-MS-TrafficTypeDiagnostic: MWHPR12MB1214: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: 1XtWpGgGB3ur3sx+ublBcvdCwpKj+ELF3V1wChAXl7sfUFqf5PKhZMHJ8RQSsXaw9/LE6wVJIW6qT6WIl/1tr4+vdp7/q9ttYoqfWvShjBNpqDMExwUrJQKO33o1EhVeMkdhF3lAuYhFpnRTEAxyeNld5KIUgrQc+tJ1S8tZMFK8CBPjAtSreZjWSrfFaWEa3xsH7LB5jlzi3MO8C61lFRD+bQU7Yno1tkG9Xfj3U+Fp6nS9lqDvBsuN3fCf6bG08ryS4QCIOKwlaHmFgK5uPFM9WKoZdC/xqJajsggapqbLSLr9U2yfntCITANy2mVpR61+Rq1kIQIv6Q57WfmpdbM9Y4q2z1bwE4N5KHKhE8P8vCti9bGJ6X4NyecAnVJa/LOz7KKz/MLnAE94QfNgaQmFaZ2E4l0AnM7wzn1BquQOAMUp9AbD2Ueerg4k+sHzXVKjnGDjFTRJdj3Xe+uyif2+LWt1g9Szh4TjlJPIPXmUKx16t/mwUQZxgMwucmnM9MFovDopocDb/vxk6V4JWW0WQdsQFCddXv7Ci8NshTjLz4rgD+nnZQjknprpWXQqWc3tNXk+blNubNURJbrTJe8lCL7g6+gMf8Xa2QLQLoxMMDEH6rYsVJHmVgSG6GrsjaEy4EOD1HukUA4bAxY3VbXpD7oDedTiel6Egj1X/3H0SV+y3zR/yjE8t46/DQYZRwEiw2Q1iqvWV+mrNGwU0g== 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)(81166007)(356005)(44832011)(36860700001)(82310400005)(316002)(40460700003)(26005)(54906003)(6916009)(70586007)(47076005)(70206006)(4326008)(508600001)(36756003)(86362001)(7696005)(6666004)(1076003)(2906002)(83380400001)(2616005)(426003)(16526019)(336012)(186003)(5660300002)(8936002)(7416002)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:40.7636 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a80e9a18-f1ca-4b70-bab7-08da431e26f8 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: BN8NAM11FT053.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR12MB1214 X-Rspam-User: Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=tYndGtoA; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.212.67 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 07BE2140054 X-Stat-Signature: fwhs83p1hgeehnpy48xuwjf6yjyz4eae X-HE-Tag: 1654012599-138327 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 abec50f31fe6..93f80d7ca148 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5665,8 +5665,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. @@ -5704,7 +5704,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 b85661cbdc4a..0b6a0a01ee09 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1683,12 +1683,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 2b92e97cb25b..dbd2631b3520 100644 --- a/mm/memremap.c +++ b/mm/memremap.c @@ -315,6 +315,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 5052093d0262..a4847ad65da3 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -518,7 +518,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, @@ -594,11 +594,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; } @@ -701,10 +698,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 5bcb334cd6f2..04fac1af870b 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1957,7 +1957,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; @@ -2131,7 +2131,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 Tue May 31 15:56:18 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: 12865882 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41748C4332F for ; Tue, 31 May 2022 15:56:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E95C16B0074; Tue, 31 May 2022 11:56:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E658E6B0075; Tue, 31 May 2022 11:56:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CDF2A6B0078; Tue, 31 May 2022 11:56:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id BD1256B0074 for ; Tue, 31 May 2022 11:56:45 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5CF0621141 for ; Tue, 31 May 2022 15:56:45 +0000 (UTC) X-FDA: 79526491170.21.34679CB Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2062.outbound.protection.outlook.com [40.107.220.62]) by imf16.hostedemail.com (Postfix) with ESMTP id 24BFF180078 for ; Tue, 31 May 2022 15:56:27 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AYjR/0M2IZPSGNruWJorK29/PXun7bFuzFoplNfX7GtGyIptahpfGbMgPjKXMTEwshBYqm33HW1Z1sbClq4ta13tuwWLHFE4zFP2zD3h7JtTlqOlltgdUQRQLRVg1oMfCur8HYRJIJMMGFRNcCbVgw5B89FiDgMDMdcS7JNZIk4T1eGDf0GiZk56+eppgyDt+FkL2rAGk7YnlANmbu0J6BPLbYk5GLN0zjPyM/TdfCh8nIAm/vFd77cLCX7BewH+ER1IvLUfSo9+ChPObJRwY82kSB2gP6lvpKFfB3GHViguf7pg+M6GoIDIvdLCaeoMif05v3WF54uWds+k2M2vxQ== 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=UB1A6mVp2FjGDcQ5stu74yF2RVvpM6RjTh5m5iBLXAk=; b=nQRBUK/g3aTGruJ5gMq8DzQ2ZCb/H7LBSai5jq/9DrJScIp9yEJZryGE2v+DkvDTT8OwHAJk+q5cUArsn1ENgyxfRjt0CGebNUNCpyC/xxuRTzg4CoqnoyGZPxRcX/F++iVeBa4xSCTuSBQdUTizyyMHVgf0l820D6OUKdq4dq4hTJNm4JfZNFzbuIW8hxymolYl48VwKD98ShB63shtr8ZXza3feS89lvU2Kdv/A851n7HyPbWOWAZgLTmKjCaMh2/HyZykPComZ6BxBHyBqhADD679GyJGYN0UUPvaTEct5HnFVops37HaHdHneE1+vC4Ax5S08FkBeXR8GZZ7Pw== 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=UB1A6mVp2FjGDcQ5stu74yF2RVvpM6RjTh5m5iBLXAk=; b=LntWL5h8l2203PH3wW61zCnB3YZz8J35fx8RGly9vSazHndSSr8lFYwVJiKl6L/RcPIWeaH08fVRlyv06oDJU803FZgK4K6qfNJ0hDJ0JEGPYjyUR4ZmImUwa1FEIOjBGpkNLIZBaBj7dW16QvNuqJvuyt26gxiPHHiGgr+f5j8= Received: from BN6PR13CA0049.namprd13.prod.outlook.com (2603:10b6:404:11::11) by MWHPR12MB1630.namprd12.prod.outlook.com (2603:10b6:301:c::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 31 May 2022 15:56:42 +0000 Received: from BN8NAM11FT053.eop-nam11.prod.protection.outlook.com (2603:10b6:404:11:cafe::aa) by BN6PR13CA0049.outlook.office365.com (2603:10b6:404:11::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5314.12 via Frontend Transport; Tue, 31 May 2022 15:56:41 +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; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT053.mail.protection.outlook.com (10.13.177.209) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:41 +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.28; Tue, 31 May 2022 10:56:40 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 02/13] mm: handling Non-LRU pages returned by vm_normal_pages Date: Tue, 31 May 2022 10:56:18 -0500 Message-ID: <20220531155629.20057-3-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b5473dfc-cb76-43e3-4f58-08da431e2791 X-MS-TrafficTypeDiagnostic: MWHPR12MB1630: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: zd5/sb5SmkEdaD5jcLbhxCdgkNqFInRaotgKlIyDW3CmKQZP3H8GSeoNLgnSCSJpHGBtTR+Ckc+0FzxcwqC5PCG8gI6bPBsYSDRxZ1f33vpXMBQYyMJidgzFU5GejUgMc8yMcGRl1Qq65VoBASHCO5h5Ga90OMshqAdRWQjn2AdZBYys+kcpVJ0cQ9AmNA+O5q4jhG5XtXOjze5Q/BDsn8yplZ0S0dX7zQeHqRLq+2PWRZiFsm7uF+P7cE5VgdBMT+aN9Uh6JNDXN3mLs2Qvo70ewZZcUnmoEWgqWvxm05ZjYfleHGOwHFmj8pRXEuYCH51CgVEI7JcDh4m1Vdq5S7vCQTJd0M7VlBrUkJ2Do+Z/Wjr2LYbndcTP21d+ObbrHLvNfypXMtacw2ZPiqt4z5Fp2F1+ldsQMiOx/CYz+wJ06gUwBCZW36TnbPmLggYutaSYXbAuJvCGM/j7dx1DPw/Y64fCOwuT/B04PwCgFGYQ40AB9QyOgmX6XUFg7lh2OMOOI8QJkATzCQnU3i7V+TAeyL3ik0CiNSbyi95Fl4rjC642VEZmDkN9jdLDaqUVRZ1TPREf2BrfLBugQl0pu3HL2zgGXvlM+v1cpIvFCOZ5E7ylBl+K1dRS1NMNyKDFy4AmVCEPGHvDsXUbM7XE7Az2eiPHbP5Cv4r0yJ6LS0KWxqEuZbaUNu/KP4huYQ44VKwfdbH3E7Yl8e+3JfJd6GpgZ4VPd9OT36JvsEWOAKctAUufVMG/tW0qIgA8Uaic 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)(16526019)(86362001)(2616005)(508600001)(1076003)(186003)(81166007)(44832011)(6666004)(2906002)(7696005)(356005)(8936002)(5660300002)(26005)(82310400005)(6916009)(54906003)(316002)(36756003)(7416002)(36860700001)(4326008)(83380400001)(40460700003)(426003)(47076005)(336012)(70586007)(70206006)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:41.7635 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b5473dfc-cb76-43e3-4f58-08da431e2791 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: BN8NAM11FT053.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR12MB1630 X-Stat-Signature: jrunpizf96pjqyd4a9sim9rfwrzd7is6 X-Rspam-User: Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=LntWL5h8; spf=pass (imf16.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.220.62 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 24BFF180078 X-HE-Tag: 1654012587-730270 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 | 6 +++++- 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, 33 insertions(+), 18 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 2d04e3470d4c..2dd8c8a66924 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1792,7 +1792,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 bc8f326be0ce..d3f43908ff8d 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, @@ -2934,6 +2934,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 551264407624..48b45bcc8501 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -532,7 +532,11 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, } page = vm_normal_page(vma, address, pte); - if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) { + if ((flags & FOLL_LRU) && ((page && is_zone_device_page(page)) || + (!page && pte_devmap(pte)))) { + page = ERR_PTR(-EEXIST); + goto out; + } else if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) { /* * Only return device mapping pages in the FOLL_GET or FOLL_PIN * case since they are only valid while holding the pgmap diff --git a/mm/huge_memory.c b/mm/huge_memory.c index a77c78a2b6b5..48182c8fe151 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2906,7 +2906,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 16be62d493cd..671ac7800e53 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -618,7 +618,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; } @@ -1267,7 +1267,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; } @@ -1479,7 +1479,8 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr) goto abort; page = vm_normal_page(vma, addr, *pte); - + if (WARN_ON_ONCE(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. @@ -1497,6 +1498,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 (WARN_ON_ONCE(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 54f78c9eecae..400790128102 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)) { @@ -2307,7 +2307,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 d7b4f2602949..e5637181de1b 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -421,7 +421,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; /* @@ -639,7 +639,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 21dadf03f089..30ecbc715e60 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -624,6 +624,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); @@ -4685,7 +4692,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 d39b01fd52fe..abc26890fc95 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -523,7 +523,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 e51588e95f57..f7d1b8312631 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1612,7 +1612,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)) @@ -1803,7 +1803,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_GET | FOLL_DUMP); + page = follow_page(vma, addr, FOLL_GET | 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 ba5592655ee3..e034aae2a98b 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -95,7 +95,7 @@ static unsigned long change_pte_range(struct mmu_gather *tlb, 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 Tue May 31 15:56:19 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: 12865883 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0232DC433F5 for ; Tue, 31 May 2022 15:56:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B7566B0075; Tue, 31 May 2022 11:56:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 828A76B0078; Tue, 31 May 2022 11:56:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 677D56B007B; Tue, 31 May 2022 11:56:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 57CAB6B0075 for ; Tue, 31 May 2022 11:56:47 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 049732064E for ; Tue, 31 May 2022 15:56:46 +0000 (UTC) X-FDA: 79526491254.18.05E78E6 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2058.outbound.protection.outlook.com [40.107.244.58]) by imf30.hostedemail.com (Postfix) with ESMTP id 492EE8005D for ; Tue, 31 May 2022 15:56:12 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lnuAVYykDZjOAnnfUCz4sF57Cmx1U7NTL07BTlQAgNe6h+3R77Z4vPCEMe1f48PoW3BHil/jjja8TQ3KbOENEL2QQJlJ75Gau+UgwXRxWxEBar7O8VHybKkVk7MHkbe2GidWQZqm3n1oi4aJgMc/LrHKnXPElqtMWPddcqf4+6tVKmKAqJgZGXzzlYg8+sJEsMFLG0p21en7+LRla0ZAdsRCe/4AsiZ5YpyR7Cq7WwUFtowd8a872L9jCUn49wsruFw3zVmLvdWbeg13gyp/VsZJB3IV4dcTevIDyRn/gcnTs2oRsLHgeT8hlqHaTAuQueihdbMhYoxOeL+c1/nrVg== 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=Tsm1npeQJz5adlkprwdiF30iBrFOX7mFKo901b9QP1o=; b=kblmqDAZG2IqiEyNpAI14A4Y4Qp7lFOFnxC+9cdEKO6mEbyn87vpUaIvnqrgePzKAd7lZRHIXQUufZf1ir7s3kMYL1WMBPm4wQBK8XVTmyWU1ATbKb45v2i9+2oYnYke0B/JivU4NbFjKGRfdM4J6I7EFuQb7WarAkZbUe56GVhWoM/4JCXbTjxR+GytHNu5+Xg5cy6yyW7nBRUvT5ZZHkXuC/d0Szj8z2ooDkMW9UQLnLhYusvAUpEK9953N2JQTTrjybO0r+TaJ48/86AExLWryWK7yliO6pQnLCDLMDR31lIi6uQhz6kZdUAkHEn4HYTMQinIN2UaVrpt64OOjA== 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=Tsm1npeQJz5adlkprwdiF30iBrFOX7mFKo901b9QP1o=; b=oYgQs81+qO3AS69IpjZAH4Qs8jhU9+Qt7Zfy3zd9cTQKYhsiOFUTBnLQjWVajZrg2tEj3z5Tbe5pgUiEVIDrN06+XM87DGsOq1pYBjuz+6qFQhxVbJd924mhyHS5uMhqytyXdNJyKrD87p0RPTZJlmbDKrCu0hh7cJ975c8QOnI= Received: from BN6PR13CA0050.namprd13.prod.outlook.com (2603:10b6:404:11::12) by SN1PR12MB2445.namprd12.prod.outlook.com (2603:10b6:802:31::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.17; Tue, 31 May 2022 15:56:42 +0000 Received: from BN8NAM11FT053.eop-nam11.prod.protection.outlook.com (2603:10b6:404:11:cafe::37) by BN6PR13CA0050.outlook.office365.com (2603:10b6:404:11::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5314.12 via Frontend Transport; Tue, 31 May 2022 15:56:42 +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; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT053.mail.protection.outlook.com (10.13.177.209) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:42 +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.28; Tue, 31 May 2022 10:56:41 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 03/13] mm: add device coherent vma selection for memory migration Date: Tue, 31 May 2022 10:56:19 -0500 Message-ID: <20220531155629.20057-4-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 52e155d4-b24e-4722-e79b-08da431e27eb X-MS-TrafficTypeDiagnostic: SN1PR12MB2445: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: tHpksrVOEX9knmB5+Efsh4SXPHhrknUbFYo+Db2CJSAtrncfHIB3ze5YWZHn4B50oSEKjpBtNF+IOPreYXLOkPMNv4vhDSUReXFroKdublybOT5tVcR6A5hVSjGacfpLXWBtFj2xNtoriogu2ZN4eqxWjRtEwqFSqKEey0mw2s2SaKAP78J013dvfJGfQ0o86RZ7k8xtrZ8Dk8DAdGgQ2YdEXIIQEOiWWX3VwSvfAb9+jRqFLX/7oSP2D9EjCr33bZ5w1w7HjTgAdiy7wyxl6fof1diGU5THsGCq6YCk5Rprlhjoof1IzjZ7pqz7fjVtulgoac3e/lVHCqKTVKmJK6iFAGxIvnvZXw8Z6vmY8w9+sMnyvpM85z/m8B57gWRNKS+x97qH8cAjw+Xi69i8MK6u8cOf252huey33tXpLCIlnhkU1ULOQ6Ds/If/KUGOzGlxQJcVHQOJft8j1P7BIVt5mZlEYLBi4ZifZt4OYXPt29Ru4g1+eXJg+1CjS2JGhIMxXDie+6YpAsoC2q4EgCOGeUYOdTBabhgd+EXKyH5zjZ/Enti/THkR4Op0F8BR4g+rHZzSIINzarpO6iVNh/N1xoYuP2wyJdSqZ10+9QIds4Ie+qDYzP2k93eNH7FHIF1NMxycfX3+C18XwSOrXTc5xrQLppJdpCds9jiFmj3UL1fiSutwYnEmB1wTNBzS33xs7fsoEw0+egI5k0+TQg== 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)(86362001)(70586007)(356005)(4326008)(8676002)(186003)(70206006)(16526019)(5660300002)(7696005)(7416002)(508600001)(44832011)(336012)(6666004)(47076005)(83380400001)(426003)(8936002)(6916009)(2906002)(54906003)(36756003)(316002)(1076003)(2616005)(36860700001)(81166007)(40460700003)(82310400005)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:42.3573 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 52e155d4-b24e-4722-e79b-08da431e27eb 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: BN8NAM11FT053.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2445 X-Stat-Signature: m5hz6kqe19y9rb7iqg9z4ef9sjbmddo9 X-Rspam-User: Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=oYgQs81+; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf30.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.244.58 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 492EE8005D X-HE-Tag: 1654012572-583909 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 069a89e847f3..b84908debe5c 100644 --- a/include/linux/migrate.h +++ b/include/linux/migrate.h @@ -148,6 +148,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 a4847ad65da3..18bc6483f63a 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 Tue May 31 15:56: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: 12865884 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 822FFC4332F for ; Tue, 31 May 2022 15:56:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BF3356B0078; Tue, 31 May 2022 11:56:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BA2EA6B007B; Tue, 31 May 2022 11:56:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A67C86B007D; Tue, 31 May 2022 11:56:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 946056B0078 for ; Tue, 31 May 2022 11:56:48 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 5E2338038A for ; Tue, 31 May 2022 15:56:48 +0000 (UTC) X-FDA: 79526491296.13.4A58B23 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2059.outbound.protection.outlook.com [40.107.95.59]) by imf08.hostedemail.com (Postfix) with ESMTP id AA67F160059 for ; Tue, 31 May 2022 15:56:22 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lQdhuIcOoAM6qNRcPXbrcsvQzQCoicAoJA7xmBp+rGiaNRANT+50PXJ2IQ8NFH3KZpjla2M6ix1vLPt4m1v+3r3+LPXGQl86p1RWFr/fOf2a9dxtShtYIQrgrmmlyz4M5bJjYITnIJWDSpRSAMYNc/JbQxO1cI3gO3kOlOumkVrC7zzMH5jcrNlOrWIFyqELZOrjOyJ6I0viMTjvh4DoGEJuw4jUOikPpCAO66OLU10dLw9MVrdHygfe4eNotulFe19MiUNFRUSl3BIVindzpLSV6MclcVgzQzqnc6RIq8ri7gJzk1hbECllQ3Uk/+t5NtUNtXDeOghtGdh3n+uNzA== 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=4EJHkZDbAdLVpEXXE+RGiJTJBXEdpU2pe/Diqmm86p8=; b=MPLMfL1Xd9J12XbOiLWBoqbjsWIyl+7PVKgxTelAlDQ05fKqLoIfPJBgMM3nJZn6Dyfe0mBzHkTViCDYtOwhSwSszglJiz8mP6fqvAsMgiPtCZh0JFkBDfu0z/iPl+jkRCZwCkkW/ibS8T7pt2Vx1qI5Ulodd197MvUi0pJhAcwgUB5rlbIcW6bk5PYlOi1t0z8crfMU2POkCkb30wfV2Gd981cqs8WHh9MaH+Rd0othWeaLpGZWDCw31KiYZaOFQLa48j8N2XBoglNuM0K/CiwoMQa1y7RN1d3NI1PEWnA8tE3JJ94YFLdO2r+e4vgxT8cpbMA9Dx+pL/HjueYAJQ== 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=4EJHkZDbAdLVpEXXE+RGiJTJBXEdpU2pe/Diqmm86p8=; b=RzFrllmuEbUpr8KTtFL9+8l9grUYSMB08evt4Z4wa3e+S3x6Y3ULQTNLUX5SLCHhoB4GUCTnvuzPkqzao3FKcw4SDmXf8KPCewV1hEmq6LSVfvJv+motP9RuWIdjIjpTamZeJQXk8u5yTHtgFZpgxtOaVW7PWm5shg2N40Sfpd0= Received: from BN6PR16CA0030.namprd16.prod.outlook.com (2603:10b6:405:14::16) by BYAPR12MB2872.namprd12.prod.outlook.com (2603:10b6:a03:12e::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 31 May 2022 15:56:45 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:405:14:cafe::c) by BN6PR16CA0030.outlook.office365.com (2603:10b6:405:14::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.18 via Frontend Transport; Tue, 31 May 2022 15:56:44 +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; pr=C 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.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:44 +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.28; Tue, 31 May 2022 10:56:42 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 04/13] mm: remove the vma check in migrate_vma_setup() Date: Tue, 31 May 2022 10:56:20 -0500 Message-ID: <20220531155629.20057-5-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ee4dc4e2-bd44-4192-3c80-08da431e2941 X-MS-TrafficTypeDiagnostic: BYAPR12MB2872: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: ixbuOcX4ZXPUzFEeGNs/p21jEykGmffe/YKe32budcTch4qCb00pOE1g1Q5+666uD3wL7Vv+rcJAtiCoDrdzRxXpiLOYvRwgW5Bs/VmmXd9n4xyFVqRWkpbKb/AeE/raI4kOY4ohhpZ2kX17WwjyjRTZxXAryqZDP2Y6+7dtVs9W2LiOb9yyViQDzk5svcZJsA6Ite+ILV/XsfaLbSV4n4j9BMUyrhvsCo0uFIesXstpHl4jbvFeg1kyQ0sr0CDMBeyrzpMKVlfO9tt4FmNtkE/EzJutNJLKMuItNWFh3oZKRXt2yjQU8X+bjEBT7gDKWbdYr6jM+y2NaKuS1cMpYM0Ywu1+wBULUDtlZYT38nrX150PdedpCl/lVD5cXVJCvvhbgw/wUQNdTz7xJI9xCvuG6YA/stLzUMWzRWYqsatLLOpI/bgNVJ1BBHjPpMy7F4/f/dPIxShDuc0iGswZEv1nBQ/GMUfuBb+GqOuVLIC8cGBxHPVZEEv89LissTxOOWsoOfkXewo/PNk1PrkZXypLFJf0E94iFKbQ8IDxm9q1Bf/GIGw+TwGe/zdCBxDKdQXFrJEJRYYYTU5mCl8UG8r5Z7JQ5fEHE2Eg3760vKOa6mmrfh/PqETzhDpN5VTXQxLi7YHu0/IUSgBkJNore+iZGr1oIj+yopg7+QbQjzY/HJaoZd1fpdte3E7sFQtt9h/9ryj/bC+xc28xATL9cw== 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)(7416002)(5660300002)(44832011)(8936002)(508600001)(7696005)(86362001)(40460700003)(26005)(6666004)(2906002)(81166007)(36860700001)(356005)(2616005)(82310400005)(47076005)(426003)(336012)(16526019)(186003)(1076003)(83380400001)(4326008)(8676002)(6916009)(54906003)(36756003)(316002)(70586007)(70206006)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:44.5942 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ee4dc4e2-bd44-4192-3c80-08da431e2941 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: BYAPR12MB2872 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: AA67F160059 X-Stat-Signature: yk86s73da315at6g1y41hit6aw5buq97 Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=RzFrllmu; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf08.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.95.59 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-HE-Tag: 1654012582-293754 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 18bc6483f63a..cf9668376c5a 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -486,24 +486,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); @@ -685,7 +685,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 Tue May 31 15:56: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: 12865886 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64ADCC433FE for ; Tue, 31 May 2022 15:56:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 93B1F6B007B; Tue, 31 May 2022 11:56:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8C2246B007D; Tue, 31 May 2022 11:56:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C2636B0080; Tue, 31 May 2022 11:56:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 558006B007B for ; Tue, 31 May 2022 11:56:50 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 31B2C34EAF for ; Tue, 31 May 2022 15:56:50 +0000 (UTC) X-FDA: 79526491380.25.9003AE5 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2084.outbound.protection.outlook.com [40.107.94.84]) by imf09.hostedemail.com (Postfix) with ESMTP id BDDDC140063 for ; Tue, 31 May 2022 15:56:34 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NUR9/xF9ne0cpKhQrrj18sPG+Vw1tqEb6zEY/CB6wz2Ea+0jFjWq+EWaPatphTdRV7zJTvQG5xbhvjiPxCC+COEcTLoPj4HVxicdfzOA6Hql+BbqlbbSty4sqzbrJZ7zqTa4Acssm6uifJE6SUtv29x5TanIokZLhZdrtY2UfATEHYcM4qLekEtoX6F1r/GBxHeGuB5rwnBYcPwGSrdc+DF3mWhDgvK5JwzJaS7494YR6SMEUEBBfkGA9JhTymlnBuI1qqXehaNNiDCK8x0YnwDm2jpaLJy2+SW1Svxy21Lai0GTFfBeKzXcKdTVVMbsz9LqvzgblQu4D+HnbCmk6w== 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=X/au3voUTSWe7TnDakyMc0huLOQFLzS/24zd0f/lP6w=; b=nQf51ywWIgP9xY4bWWU8CBnkoWTAOITy6nMFn5wzP8bijoTNu3FUB+N0AhKLqm3HDEn0p0LkfgfJlgYdV0TODKxH/1Hp1SKGC167IhlWGGygg4k1swSw2U/0otKB7tvuI9/QXQNZN5tpq1lNSsX8/bjHN7QEwETY8xhk2OkCKAkB0LX/b6dBqMMBNXiIHiEG5qZi+7gNwcnV3rJddFtWQeFkSPo6TZMA8YWGeeY/VuXQZjelBvyLx+RlDrmsB4iiaUhy2IK++R/Y3N3Eebdnje8u19gh2B3lGZXkxhsdqX81S8rhPID/gURjbow4JO9FyMMecX0qFGjqsAf1+NF7DQ== 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=X/au3voUTSWe7TnDakyMc0huLOQFLzS/24zd0f/lP6w=; b=SgTbOEL/RBGQJ2BIDmNhsE1ISOSPzFTO2L03IkwKF7yKIaNbq78E6rKjLd21Js/oPRCfmtJ7yAtXtAfJ6yogTRFN6gX32fYHRku2jZXOX4T1CUbJ78B2ss2kciRewh8dUK5KLskuO0WbSVWzLlKBb1LvtjvIDE2YpZ6l2Rq+mJM= Received: from BN6PR16CA0048.namprd16.prod.outlook.com (2603:10b6:405:14::34) by IA1PR12MB6163.namprd12.prod.outlook.com (2603:10b6:208:3e9::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.19; Tue, 31 May 2022 15:56:46 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:405:14:cafe::70) by BN6PR16CA0048.outlook.office365.com (2603:10b6:405:14::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.18 via Frontend Transport; Tue, 31 May 2022 15:56:46 +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; pr=C 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.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:46 +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.28; Tue, 31 May 2022 10:56:44 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 05/13] mm/gup: migrate device coherent pages when pinning instead of failing Date: Tue, 31 May 2022 10:56:21 -0500 Message-ID: <20220531155629.20057-6-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: dcd7553a-5d7b-44ef-8ce3-08da431e2a5a X-MS-TrafficTypeDiagnostic: IA1PR12MB6163: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: OT85iq5/0PCVBLUEEJoFBTwgun9tKd8IQoeQsBrrUXGxeYo3YQuJTQsfUScjYiEIG+gS/M/G83SFp2/dXGQj8rTHmY/2LHbephIBH7xlzjskLBQb6Uk/Q5/Q2atBO0OEyhhlhI7Vl2IFQeJ7bx7cbCfVCidS/7Q/DIzqwYFH5ia5Tf7/I4fCNgpjhhenp3rOArsONy3XJziPUfZ7OuV9XJcjXf7davfCmG9JZOhfFVDvR/ZbLgwh1WrzyqILIeUdXj74Fs5gJ1nqQhAxr8T2fXF6cFwBOEBX9io5Nr8qUoGEv1YX5fLvkTIYLEnsf+cP/zhOSQYJyLKjyQqNz8xGSobHxuMl+eyt5eS69SLq8Qqe6TLYVPngKa/AYinTyuiQN+dAwkHxkkCwyXT5r8cGiOZAIg+fCHuY5LJ1wo6wAEOJaicRZ1qE9AwiMF1q6pJsUz36eSqOy0k9u9Xu8Y2s2AFwlTNNm6prZ3HfbNXJ1bMD0EWIF6xj1mNkaKqMoM/NVoBksQ/gmiI0JDK92fAopmVkLX4/SE/S/BXQhep9VRjMrNcT7eB1hiMat7NKXHDeaAD7wMBwNOZYPnLWvt4G7oitiUrJnAB2Pf5cmwXBuO2y1qQNboQy96O9nG48T2KMRGc5Ri6g8ALDwGx2rapXOvxJ0AFbgyDPuZ4rAZMQjhwMotj3g938NIAhGv7CUaV0l1fAJ5Cb6whEzoZSbjESeg== 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)(186003)(16526019)(8676002)(40460700003)(2616005)(36756003)(70586007)(70206006)(7416002)(86362001)(26005)(356005)(316002)(1076003)(4326008)(2906002)(81166007)(82310400005)(54906003)(83380400001)(508600001)(36860700001)(336012)(426003)(44832011)(6666004)(47076005)(6916009)(8936002)(5660300002)(7696005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:46.4378 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dcd7553a-5d7b-44ef-8ce3-08da431e2a5a 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: IA1PR12MB6163 X-Rspam-User: Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="SgTbOEL/"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf09.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.94.84 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: BDDDC140063 X-Stat-Signature: 41jksnotur6f8utspnderq1os7a9ch9h X-HE-Tag: 1654012594-360473 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 48b45bcc8501..e6093c31f932 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1895,9 +1895,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. */ @@ -1933,10 +1967,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 c0f8fbe0445b..eeab4ee7a4a3 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -853,6 +853,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 cf9668376c5a..5decd26dd551 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -794,3 +794,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 Tue May 31 15:56: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: 12865885 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DE80BC43217 for ; Tue, 31 May 2022 15:56:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D50946B007E; Tue, 31 May 2022 11:56:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CCEB86B007D; Tue, 31 May 2022 11:56:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B45F16B007E; Tue, 31 May 2022 11:56:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A82F26B007B for ; Tue, 31 May 2022 11:56:49 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7C51A4A8 for ; Tue, 31 May 2022 15:56:49 +0000 (UTC) X-FDA: 79526491338.11.B93C178 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2058.outbound.protection.outlook.com [40.107.237.58]) by imf14.hostedemail.com (Postfix) with ESMTP id 22FE1100042 for ; Tue, 31 May 2022 15:56:44 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OSs/Ld04MMg0TretMXIG3YIE8eUoJQ96g2N6P200mwBcHYDpSuhwduoOaRSg7Kdz2BSar8KU/2mcZybdRdq74Hb/zRqFph/v71AuA1yaSqanNS2UmB9hOIKKK/EMLkVksPO9kst6rf6rTXuwTV8PNB/wNP/Cbc7L+JDwW/aWDDr5mkI2ErXKH8I+HqffQ50Ijl59E7FaAutDLLN+9tYaO6tdjEeSDgjwjwOJkHC0pLlPKeEltloWrpBxtmQU3s86Zyd1GjHoE00YG7ZA7JpeI3nOquSZM+JZ1Z29Qzp+z5cosnNnfM3vPvRkWzyUmEYheOHyFQCOq5H+WSo9qgIg2Q== 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=Xa846fnxeU9bk+TyhpVkrT9/4QOUTaeNbDS2MCZJQec=; b=nkxxKHFWQTHGHLPKoPYPIsEPkxmsyDrDv9eyzfM+fBuO0qRif02xgqUv3I5bTpg0KRla1YFEX0QwsIGShF27WmWCrOhCZMUXCVeTmwCHfAo2FOf4G/DsxFaPem+bna3V9F4B+1O7hRMjGfYxDJrA1pj0wbYyW0LADhtUyDjLy1YQ6cd4NUeYNxj2APB0bf9aTtPTP0nJUQZK3pJUOeMd1bIuwsaTnm67xd8fUrkQYju7AEGNcRCnk3Fx/1snAPXr3ycOuP9vqycFIcUB/HNI0aD6ozazsjcI8EOHKqpu1vQhg5qvI4HAHSXaSCKYo+Vss/yOljAkctO06iqkaZ4Lsg== 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=Xa846fnxeU9bk+TyhpVkrT9/4QOUTaeNbDS2MCZJQec=; b=bPgc18dyHpBb5l35YOSItZmEaleflNznXY4JsPxLvJQ0mfGXQlZltYrBtfz5AFjYI1sjsH8kj8o+/Mlag6BZAgd9g05du5z0k5yyl6id/usZaThUvdGfNrsEkvg5lf3QcsUHrIbXP51p1jhHzUgj54df8rH0eorhJygsiBA9fbw= Received: from BN6PR16CA0025.namprd16.prod.outlook.com (2603:10b6:405:14::11) by PH7PR12MB6396.namprd12.prod.outlook.com (2603:10b6:510:1fc::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.19; Tue, 31 May 2022 15:56:47 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:405:14:cafe::6d) by BN6PR16CA0025.outlook.office365.com (2603:10b6:405:14::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5314.12 via Frontend Transport; Tue, 31 May 2022 15:56:46 +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; pr=C 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.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:46 +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.28; Tue, 31 May 2022 10:56:44 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 06/13] drm/amdkfd: add SPM support for SVM Date: Tue, 31 May 2022 10:56:22 -0500 Message-ID: <20220531155629.20057-7-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e0d9fafc-d521-4f3e-ffab-08da431e2a8e X-MS-TrafficTypeDiagnostic: PH7PR12MB6396: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: wcuAcTHnZrjXr+f3PgetLqaQsYF846Hapf9Edg5j7q97iKwuhZV/GwCVLAS6b5XqtoKOpdUNlCr12i0PFi4+uVtY9pmI8o8AQLMJIoTh/vwLNbu7tW026mwIpPod2Wqa37l6kXWzblp03L+Z/g/iWjZiCM56TzBDv9NH80x7W2WhzP11Wh8AOXwY17PUeuI1MqDAs9p7VhpS0ioud4WO/n79FWqCgu8+xJURk8Lc93rNO3ly8g5OAuH4UyV4V32YKizwxLl+ZafVQZAdRpHgO0ygz0ogJifLUYFQPtJj/9yayBnnF8mxLw4SA4S0AOArZH/rDTa2Po6eNNULtFt6NdmM6NP847LIiaZA9fUctCYYLLVldeVuPyKwo7yoaE/xuJv8Gx6wInqVvtUjQTnRCSf27Y9RZfbjU+Vl+LkhF1+tsxEJlJN2Lx/fN9xzEEtMGNd5xNNaXJTmUe3zkVwJtJLQUwTCfzswYltXvAFDXFLjzkgPymSQqLpCvEPKClaS71uKnO+RZIXdPXVCdyH3V+LhRql52Ql7AsEU1NfywyVF4kqKzVJ2UvWe53QWCz5ZoJ3eJPaQSmXtafz9+tips2Ui9LIUPW52BooFLcZb8iyrK9b/gYAUFfNwDOkdJVW2Ry7BEEiXA1agaU2vanfiCYm6g9p2nSsdECa6zsjOyD7GAIe+97vBdTbEAvLb2KKuaCZ6Ww60nP8346wzcIIavw== 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)(336012)(426003)(4326008)(186003)(16526019)(1076003)(70586007)(70206006)(2616005)(83380400001)(40460700003)(8676002)(47076005)(86362001)(2906002)(81166007)(5660300002)(356005)(7416002)(44832011)(36860700001)(8936002)(508600001)(36756003)(7696005)(6666004)(6916009)(26005)(54906003)(316002)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:46.7816 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e0d9fafc-d521-4f3e-ffab-08da431e2a8e 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: PH7PR12MB6396 X-Stat-Signature: hx9repcz5ug6tbaj7d8ira7rhtaxbsrg Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=bPgc18dy; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf14.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.237.58 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 22FE1100042 X-HE-Tag: 1654012604-77743 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: When 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 | 34 +++++++++++++++--------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c index 997650d597ec..39b8c4710caf 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c @@ -671,13 +671,15 @@ 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; buf = kvcalloc(npages, 2 * sizeof(*migrate.src) + sizeof(uint64_t) + sizeof(dma_addr_t), GFP_KERNEL); - if (!buf) goto out; @@ -947,7 +949,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; @@ -962,28 +964,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 Tue May 31 15:56: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: 12865888 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D77AC433FE for ; Tue, 31 May 2022 15:56:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 747916B0080; Tue, 31 May 2022 11:56:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 60A6D6B0081; Tue, 31 May 2022 11:56:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 42F316B0082; Tue, 31 May 2022 11:56:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 3154C6B0080 for ; Tue, 31 May 2022 11:56:52 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 00744510 for ; Tue, 31 May 2022 15:56:51 +0000 (UTC) X-FDA: 79526491464.23.557BC48 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2054.outbound.protection.outlook.com [40.107.94.54]) by imf10.hostedemail.com (Postfix) with ESMTP id 4BE74C0063 for ; Tue, 31 May 2022 15:56:08 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YazggbAUpZO7V2n9VUBwqymcxCWBBVAtj797+qmOVqrW9qQnVG0Viw7qW07YrT+xpJKxV3cYL/zNId5AljomwgMX0jON7+m8+mQ6wioc/XzPqRQnD6ilAABkqiYetoEs7p1GdaBYtOvT+y91YzmKZSDAGvCGWudoSKAaVMDkqaOI31pbmrBGLdKczpT8UFZeBkYnS6FXfo7xIKzAUybYXHWcmpoxRiCuUbAFuVQlTdRKmtDGX63n0vumH06SujuYYCLlvS++pzkI3lZIm8qaAvtRQwWFhxPlPT9muRDvSGyBWxTWSVv4KpujtHITD/GzBHzP011//yP7yQWwhzEzbg== 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=RnyGRNfwHmHQ9ksEDrCxMo9gVB4E7euoQ1miU/KUZAoVBtNKai983tcXfZRV64fztteHDnqs779bldVpHShBJ1zmoJJMHyqemKfj/l9zMnVCZTinAi2A4iZ0CdjATFjqOFHAm7DSl9KDnOF81HJU1cs/iln+HLXXMpi6pNGk4AiDeGELGS7C8PCJcW0gPz2JoFxceVTPLDmq89+Jso7brpcxz6oKcVkPo2Ze1hlYhXZNToL3X9lir1ulB3HDH7s1WgJ2jsZL5rPWZ188ZGo/FbRbGHF6FnDfvp6uuuQcbR0Ji15ybVNPptc1kFzoj3g25ptuIe5xb6nUoLS0J7vqHg== 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=TRHDKlMp8Ik7n+RSv+45ZizypaNGUYqCJHoyeRGlRrcPYWvIKWtyyh4ZRR0MNEJ0VWLCOxSxFPFTdOR/vH5YLzVvXDA0MRb0idOqcQLjGRO43T0ICYmckwiVf+rNIw7LUTn+gg56pK6x7NjWSD3HY9p3wJdF60rg0sNpdJDnGqw= Received: from BN6PR16CA0040.namprd16.prod.outlook.com (2603:10b6:405:14::26) by DM6PR12MB3996.namprd12.prod.outlook.com (2603:10b6:5:1c1::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 31 May 2022 15:56:47 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:405:14:cafe::e9) by BN6PR16CA0040.outlook.office365.com (2603:10b6:405:14::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.19 via Frontend Transport; Tue, 31 May 2022 15:56:47 +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; pr=C 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.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:47 +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.28; Tue, 31 May 2022 10:56:45 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 07/13] lib: test_hmm add ioctl to get zone device type Date: Tue, 31 May 2022 10:56:23 -0500 Message-ID: <20220531155629.20057-8-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c0ff19af-07ea-4dd9-d2c5-08da431e2ae4 X-MS-TrafficTypeDiagnostic: DM6PR12MB3996: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: V+4wN5PvA5HX4SqadOhXLZahps4qeoiyH7oyPljLL2zj/8E0FAQUGapWSE1s4dUIyWBuaknhB6x9QUD84ZRaws0ye/fYlbQeK1iI6xojQ4H9KVfDoXwbkDPTIGGoGnLDQaSvYslNp8BO4VDcnBwazr3FRu1w3EjeiyP0mEl0NyCZZxCualNuGzOUM/AeibsaeINqzsvH0ivFIGLO3gVLLw3cvczWb/ZcgTB0PVLk/phN5hdX4QuAyJdya0cL5qk9/uacJwNRqi7HxQhBI5Rk4p8x6cw7gJ55IigesCA4TQbDlAJAbAv7PA2OEYpkKg8aBlqKdUHqPhDflSp3htKVBRd5U9KKGKBRGw8n3zaDjKWWQiv4PaAcF4sV4cRY9+sBP+IT2pU4pwiP3XriT+cVQhSd4QcsJcs0PqQlWPhUYQZHLrO1tUVXbTBT10RP1hpRDaXo0GYOoymJ61ynYCoLmbGHYFv1Nzipp+sjM02VYNtZWLWVbDjpC48piGyHFhXBOLxdqAQLENHcsE9Pf/25urGq4x/d/Hr4AyvLMqtA9aTjBYUaGsMDuidXvWVIB2LeF2Te5ioFIFYFrWMftnt/T+pcaRUjlCZ0ExclRz8KeWyJJG1DbYV5GtbU2iV2rfO2a9QJfTDCuEYBuV3wSUdIiro9BrD5ctof7jKMxS0G5DeaKQN/eUiNum6MgMEdWcKTDc/BNYmwy+W+LaQjLKHkTQ== 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)(426003)(1076003)(336012)(2616005)(5660300002)(508600001)(186003)(7416002)(82310400005)(44832011)(81166007)(47076005)(86362001)(36860700001)(16526019)(83380400001)(70586007)(36756003)(4326008)(54906003)(316002)(70206006)(7696005)(6916009)(8676002)(6666004)(8936002)(2906002)(356005)(26005)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:47.3440 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c0ff19af-07ea-4dd9-d2c5-08da431e2ae4 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: DM6PR12MB3996 X-Rspam-User: Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=TRHDKlMp; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf10.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.94.54 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 4BE74C0063 X-Stat-Signature: e1gm6edg7fyumu9kmcdnfbbqztidx3er X-HE-Tag: 1654012568-771603 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Tue May 31 15:56: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: 12865887 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D42FFC433EF for ; Tue, 31 May 2022 15:56:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DA1A26B007D; Tue, 31 May 2022 11:56:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C388E6B0080; Tue, 31 May 2022 11:56:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ADAF76B0081; Tue, 31 May 2022 11:56:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 966C16B007D for ; Tue, 31 May 2022 11:56:51 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4AEA535303 for ; Tue, 31 May 2022 15:56:51 +0000 (UTC) X-FDA: 79526491422.17.0A13380 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2086.outbound.protection.outlook.com [40.107.223.86]) by imf28.hostedemail.com (Postfix) with ESMTP id 97420C0078 for ; Tue, 31 May 2022 15:56:12 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eWDJ46v3MgxjJU3kPLmRp1hcfNRhXeX9w7nYy8rYeX1kFwRrS4JR6CLMgvLDKcthychUdxqe4w0FKRY4L2azqVCl/BUWJz8wwvgyXpjP1QuWQC0hPKKvWNfFl4KJTPDVvew6C99wpvyfqZmkyOJ4fNGp14VAeAqddShvxSb/sYvun6ehthth75dYAu6RIBPVOeigZ/aHg1MdzELj5GkBXj6abGbup/0tOW7nBam7eBEIsxjBFfcgmLkrTszzzPpgCHmrNzJviiTkEOcYr6eypAE6JSkRU2VZd11dCkY8KLrp7uIX91792jdxfdDmonc56vP6YhNKmYe2kLRb6aNdyA== 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=cm/IAUWVMaV8PprkzsDyB+AYhkq7dJ9gA4kgbSUjB+F+C8Msg3paAsX8i04M+TIpcda1ks72HztU9DUP2ZysrfhLniwQi1phNUTj0g+fXdtiFC1Y12BqSoMRxIJgHakRXh4SjS9G8nFXGKniNow0kGCCyGczVsrbd2JyO7mf72in0lzHS8vnE5Rk1WIaWPbbUlvI3X2zD6x+d4Ns+dn0JN/ZfKbUZaXVqR3psMP0JhUapaK7XFnTsctvQIbI6tKAbdjC25Xlwfl4eEUUCfOJUH62ZI5RxwfJNhBxB+RTGvn/9MWooMd5su8ih7bP/DYy5xQuthmNVWlt1uCZc5+lrQ== 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=qxn+6dccPaD0DStEsIEnkSilu1cCEbMDaV0a7dAqu0XohUTRA2a3AIBe8+2lt0wqGU2PVPJ6QEzBcZ8lUWmPe3fUVNx3qiqeS+hDkDqOoR3q4UH55pPpRma7K3rwQoeQKEWklmz4aUAOJezCnhsmQZB2/3E3/o1O9dsKkc+GtcY= Received: from BN6PR16CA0033.namprd16.prod.outlook.com (2603:10b6:405:14::19) by DM6PR12MB3196.namprd12.prod.outlook.com (2603:10b6:5:187::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.18; Tue, 31 May 2022 15:56:48 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:405:14:cafe::bc) by BN6PR16CA0033.outlook.office365.com (2603:10b6:405:14::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.19 via Frontend Transport; Tue, 31 May 2022 15:56:48 +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; pr=C 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.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:48 +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.28; Tue, 31 May 2022 10:56:46 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 08/13] lib: test_hmm add module param for zone device type Date: Tue, 31 May 2022 10:56:24 -0500 Message-ID: <20220531155629.20057-9-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ab8d8d64-cdd1-4259-f3bb-08da431e2b48 X-MS-TrafficTypeDiagnostic: DM6PR12MB3196: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: B5bnRfvuhd3H0eC96F9f0DOnDZMfe6vsTZlJtlnfl5+8FUTTEkbSwVSsSfuBtkJtCErc9H0DbwtKrSLjSl6HUiwPUCEpnljzQATH7g8pJzftBFrv01/UnBhGW2a/UGDe/qRNFgFjOPkfwmCYhzlCeqDViom6kIVHZaposg+XDoHXw84hYtJ4/Fjc3IZaTugs+xpnfTv5zm3Lk1z9WkwggdVqfZ2uMhUXUUptrv0xZL57svHqmFK/avqdUIb0+zHdQvnCm8Bx2TK1YGfAnc+zlOsoXqoj/J/nAbBN2RaXcCebRgdAMombdU+Q7IXlRVt6/ZxSFu7UPC4pGww4qfJd2KN8MrD80mZkaFNwGR9m/n2kJZwhlcw2wsS566bfmcbPbMsQ84hqrrThqm91UDhTdeoTVqYb1uJUEU4/gXRUYdh6nAZ0fpiry5/rDi4yH8Gi3Q4+SDbWOq47fX6+GNPXoW/BAN2Nj1RdAK6MMcN5u+7MVgONtKx+K8c7bB64cbCgLLfUIZKAYM1U1xvbUtUT8XNc4Q2acZJzzRsbhFwOGp+l2BRSm87YQkg1UhIlaRLD9iFWXIPHb+1VE6+IZ4yll+ydOHBwf4CzFQw7KBiHn4HZih3BNFFj7f+AqH8ROMqJ5v/qlH6vv3yCe/cfx7ciBjD6pPjJFy3GDW7fcuIdpW9pvQSaBWK79+atETO/RWVfrxIomuAy20oSb/FSL2O/klI3ph0yCThH8nwXvgqYpLYmZG75dUOZ90qmxTpUrmQT 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)(6916009)(356005)(316002)(36756003)(16526019)(508600001)(186003)(81166007)(2616005)(1076003)(426003)(336012)(86362001)(40460700003)(54906003)(36860700001)(26005)(47076005)(8676002)(82310400005)(7416002)(8936002)(4326008)(83380400001)(5660300002)(44832011)(6666004)(7696005)(70586007)(70206006)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:48.0002 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ab8d8d64-cdd1-4259-f3bb-08da431e2b48 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: DM6PR12MB3196 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 97420C0078 X-Stat-Signature: bsqapa4ytt8zro3hpjxab4dt5d8nio3d X-Rspam-User: Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=qxn+6dcc; spf=pass (imf28.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.223.86 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-HE-Tag: 1654012572-859115 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Tue May 31 15:56: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: 12865889 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E9F4CC433F5 for ; Tue, 31 May 2022 15:56:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 49D216B0081; Tue, 31 May 2022 11:56:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 420E66B0082; Tue, 31 May 2022 11:56:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2241F6B0083; Tue, 31 May 2022 11:56:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0E45A6B0081 for ; Tue, 31 May 2022 11:56:53 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BEC4451E for ; Tue, 31 May 2022 15:56:52 +0000 (UTC) X-FDA: 79526491464.23.735C08B Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2056.outbound.protection.outlook.com [40.107.94.56]) by imf30.hostedemail.com (Postfix) with ESMTP id 0B03180056 for ; Tue, 31 May 2022 15:56:17 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HA2b8MIrvmgPuMdiEV0/woDjjXobTu5FGcuV2HzuBEG27H78pPAaimdJ6OmEyIEYCotPZ1wCrMAP3iw6Tl+uN1rG3/QsCz1Z/LMjqb4hpmuN0BtFen22oeHbeO5KkFAnT2patLuT8L8DQ4j43MGteN/2e9sXeggUTSWcGavtig569DykflsS1ZdLVN+z2t8TRuNGyhycNykcY+xwik3n3DajdLwz4Fn3MlZ/dDVCrxF8KSy3xScEcoKxL2BCzfFBzOCowxqMxHW1Cm5N8ho9NIta9u5Qbdfu/dU/zx3Fop3J44DLIvKUV4TJGO+gE9CRF8vzTlS1tHMIEovFeFbk6w== 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=jAHQiGo2gpEUx05OBUC7Zbw+yzEWDRPH4Dylw3P0oP2ZlAG+puCdFc51nds7dY3MlSB1KG7QmTJF8DH59UD8HokqDGtHnufwr6UWYGtEWIFwAP0dt6keqp9v3wrY8qRdZvj9hPb8aUewWJ31u/5zVtKQ4dep4gc4rEcL8xXBQdotrpdQELZsJ59wZ9utRVDmM6mY1y0W3ta9R4ZFoiG7irZFTEdXUjxWO8VW/21MDQ/DZZ0j78zabWDT2DAXvlBlh6pvNaWLMl9kopc491vdwguHeTFmy1F7k7mfTH81+1aIfF381wN3oC+FMS7BVry9QuWELpSI0NXJC96d+1TWfw== 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=DLZjdreynAwbHrzgtq4G7kjWga0qYBpGGgdBCtbVhSlhzb/Gc0Ythoda6EChSTcTnCA6OuVrMAo1mY0W1a5OC+KZF1qn4R+jE0YtPF06o2lakRwlFuFRj/N4xjkxwwMomfMXn4VqlLwc7zBG5omiUFJzAmN25djRFe/YMJfwb+k= Received: from BN6PR16CA0031.namprd16.prod.outlook.com (2603:10b6:405:14::17) by DM6PR12MB2841.namprd12.prod.outlook.com (2603:10b6:5:49::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 31 May 2022 15:56:49 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:405:14:cafe::de) by BN6PR16CA0031.outlook.office365.com (2603:10b6:405:14::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.18 via Frontend Transport; Tue, 31 May 2022 15:56:48 +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; pr=C 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.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:48 +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.28; Tue, 31 May 2022 10:56:47 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 09/13] lib: add support for device coherent type in test_hmm Date: Tue, 31 May 2022 10:56:25 -0500 Message-ID: <20220531155629.20057-10-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d1a4e07e-f101-4dd4-d951-08da431e2bb1 X-MS-TrafficTypeDiagnostic: DM6PR12MB2841: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: HI5lmNw291O+lyGiFonGV/fFn0YFViF7NeTZF/M3oi8yk6zqmVXMT72bvIGXoDZFFTN78XzZWKEYqXV81GCEbiKW2OPn8zb2+f2REAVK8aFxQWOX2gc6KTRmrlZ9QIZGBmUeG1Oxs1UZuiKBR0ShVJxFAbrt2PrwZGNd7Yc482X+vlp/FIX8NZYPZe0Or/5Pjt5a9+BfyjLyU55AtjB9lcvXUTF84Ai/AJHOIQCsqMDpeXiXafrZNY689Cnf5MPIs3py5kvsDH005qWFNNHG1V2fDVO6ZMfFqJmVw527J/JC/jnM4x9YMTo0RY8RK81zZrQMwPA3YIV80VtOfZ2q9zNsDT99rE1Rz6+gxcg9wJm4Lm8tsFKT/gizSl9HHtqsMY3xx1Nfy+80l6BgDln+L2myp8j0/OLp0d2zC78FFssEfBL8JvZk/fFqyUgZIqYdCjO9fWGltLOLyF/ywBK25txrmRj6fI0vxlaUbvgiDsr6JiSRyI61XZ5GHfo5q9WGqvxj/ielqIBcTvczblcDzgMa9DTVPVTIZM4mBX0jfLGkx0Tf6AuTLA5w/+wuMCSv5zaJIDAvWDaa3GX5IeOfbfIe4097KXaYI0iGfbVi4QUr4I3xnVQGtTDtuO2NqF9aB9GDk+Rz/eLF9tq+20G9GkGF+NpwyPZWq1JjTTFk6GFINV8LAwG8stEuJVdx2uNI+64qd6LwpfggMD4jY82hxw== 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)(5660300002)(82310400005)(86362001)(70206006)(8676002)(44832011)(70586007)(4326008)(7416002)(2616005)(16526019)(186003)(30864003)(36860700001)(1076003)(6666004)(508600001)(40460700003)(26005)(6916009)(7696005)(54906003)(316002)(2906002)(81166007)(47076005)(426003)(336012)(83380400001)(36756003)(356005)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:48.6877 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d1a4e07e-f101-4dd4-d951-08da431e2bb1 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: DM6PR12MB2841 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 0B03180056 X-Stat-Signature: 8tohxig9d4q9fj8961xzyzifohsc9hn7 X-Rspam-User: Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=DLZjdrey; spf=pass (imf30.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.94.56 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-HE-Tag: 1654012577-797340 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Tue May 31 15:56: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: 12865893 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07DC3C433FE for ; Tue, 31 May 2022 15:57:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4AC8F6B0085; Tue, 31 May 2022 11:56:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 437F46B0088; Tue, 31 May 2022 11:56:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 23C786B0089; Tue, 31 May 2022 11:56:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 06C686B0085 for ; Tue, 31 May 2022 11:56:57 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id E1F5E60AD1 for ; Tue, 31 May 2022 15:56:55 +0000 (UTC) X-FDA: 79526491590.24.EC24153 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2073.outbound.protection.outlook.com [40.107.244.73]) by imf22.hostedemail.com (Postfix) with ESMTP id BCDFCC0065 for ; Tue, 31 May 2022 15:56:51 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FcoXbFc4E/PhiKYygvL+4gFESxKuuv7mkqDA3aCoctgAYo5POsx9F1PKceZ2BE4QWj3Ap1Sa7jgLNuVLsYlYxDPqwfQRThB/v8mr2AccdF1Uegu36/6u47BLzTfqUx7J/4Dz7NhaaKji0v67BRBVXB8NgCHvNoPxvuB3hbY/ExMfp8FwaU+VHZ6oju2uSM46d2QHV5dD2agFftffAkNgtTcjDp4S/moWdEDCuBgY0NbLMWl6CgXwSnkRZuA8t/TsUzbp9yQrBDM8LB+WBba9T3ridWiRXZQXIX7NCAD9w8AynB8/yiR9OYXc073AXRmM/o99e94ArW1zSce2mfpvYw== 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=CU5ans6CE9CERooA8JqIa4f6GqCLugGlvyiEMC5wjQYeHTcfuORair6VXNCg4WYlyr9/rcURGiHAPFrfivxRYenB5WtdfCcgbCmtCZmbjvOe8T6YwF+MIS82V67JUn3431kLhwxj6hONxMY9ftbFMv2+OeIthKdkQE9iLhTooUHggVGluDDq107/xHpya5rYHTSV6QYLs1R1TqnFwlqMVUiPT/Xx8LI1VKwS+xnSij8u+Hu/I7u7GCidMpHG8lk5t4/Tgr0Gfd34ZjgTlEiGoC9BOPsWkN9knVHvy/S163Hi2wlkEz8nCiooOYVIp7Hfuy0w0IcUnbfY/Q7xxcMV5Q== 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=GluFu2nMbibMhXiU0SULL+hgJSnezChztaCMpHGvp8pJsyuZ5QSAlFdTO9jxUgoqS24TetuD357amCrLPJgOgJgiwnkkR7C8na2AyJ/wRvB4flnWIi/tB707X1eh52RhjgM61iv3RzxqfXSHw3g+NaGSjUT/aVgplrvYXdDrLxU= Received: from BN6PR16CA0031.namprd16.prod.outlook.com (2603:10b6:405:14::17) by BN8PR12MB2883.namprd12.prod.outlook.com (2603:10b6:408:98::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 31 May 2022 15:56:49 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:405:14:cafe::de) by BN6PR16CA0031.outlook.office365.com (2603:10b6:405:14::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.18 via Frontend Transport; Tue, 31 May 2022 15:56:49 +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; pr=C 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.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:49 +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.28; Tue, 31 May 2022 10:56:48 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 10/13] tools: update hmm-test to support device coherent type Date: Tue, 31 May 2022 10:56:26 -0500 Message-ID: <20220531155629.20057-11-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a5c091a8-b205-4d23-2132-08da431e2c1d X-MS-TrafficTypeDiagnostic: BN8PR12MB2883: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: SgPa3b9obnF6IhMTsLI4ez7tCDYtCRBCv5s1S42nCIztebm8c9INKlBCzd7rBrjpC67spJZy2XjIzoxlHdfZuUrlrYBbv2wtVxFYeZftxyFc+hyPXOO2RHZgH5qAcZ553D+lDvr7OQCPZI7v2bqhioyn4g/iG1ioWKpqssTguIkeHmofV+fYzVaPrfKXpwfIUIloA1zfZ1J0nrm9PCdwNMQDaoi5fiu7VmpF+kEbWRwaUINg1PRkgEYVL7+1xBPfYHsAcvLRmy0RgNSXzHmruusWTnFas1OxItPxCx8ZWIZ66gw29tFbtFFYLvWI1mhQ7jTaJ0Win6vHPC46KochblrkE2cOmk/WZKHPiuRmF74vG2fcg51+zgxZ7qIrceJAJrqjZLo8A2C47gtNDi2thZYTIzKNIAuWAlVUsKnXCB5DsniF4xX7Lm6fayP/IM7Hhpa5/JL4lSHW3iKpJ3URN1inGjMNAiFkEPOeJcQm4TXMTjNQVriYuZIoA1HrCP5dnWW+VUA7d+Us1dMg2uiV9mumWRnMwo0/kdn1FkxoR08TsAW+9u7YNQLMLkDJcNLyf4K5Fvui/oFK4GWPE0KrwvjDCzcBiuoQZOp53vznmJidTtK1ljb3DgHAAP3DeaXdjeUJ7gC/BtJP+ZAgNZklvx+LAwGmBh88D8FclvKi3YAxobML4xIA941sRWIscyEiwsqnTeQiqohYniuM14Z6Dw== 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)(54906003)(36756003)(7416002)(70586007)(86362001)(70206006)(316002)(40460700003)(8936002)(1076003)(4326008)(8676002)(336012)(426003)(186003)(5660300002)(6916009)(16526019)(7696005)(2616005)(2906002)(6666004)(81166007)(83380400001)(508600001)(36860700001)(26005)(356005)(47076005)(82310400005)(44832011)(15650500001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:49.3907 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a5c091a8-b205-4d23-2132-08da431e2c1d 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: BN8PR12MB2883 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: BCDFCC0065 X-Stat-Signature: efo4yh5xngfiyw5h5i1kgksc9izuyugb Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GluFu2nM; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf22.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.244.73 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-HE-Tag: 1654012611-578743 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Tue May 31 15:56: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: 12865891 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD207C433EF for ; Tue, 31 May 2022 15:57:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9B786B0087; Tue, 31 May 2022 11:56:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D26C16B0085; Tue, 31 May 2022 11:56:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A38CA6B0087; Tue, 31 May 2022 11:56:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 940B26B0083 for ; Tue, 31 May 2022 11:56:54 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 73F406061D for ; Tue, 31 May 2022 15:56:54 +0000 (UTC) X-FDA: 79526491548.16.51E9468 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2068.outbound.protection.outlook.com [40.107.92.68]) by imf28.hostedemail.com (Postfix) with ESMTP id B9E93C005C for ; Tue, 31 May 2022 15:56:15 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KcMahLub3cTHr+W0xDtkaHAjnDzxNF08M3PxhZBHHtlBV1ffJyGloZ8VDtItFuQhBsKEeil5sDH1x2BTZxd974OtntVQ1/mqUg782XMYZzdr8nRaSopiEB2IwcWAiWxtjMScv9pL79Ku8KQj0UyYNHgz/YXr9ViWnafEv7Y7oMj9JrYySyognFSwMjBsvpll6vpmkUah5ZzMKv4Gk2CzOFgpIQu+EBzk170a0zFgDEFbVKeb64ztaC92wfRLMUkjG4EtBZ+4Ej8LZ4lRrwONCJWSL+r6UrySnd0gyAJmSz3AZWWhvNzFtR8JTRNW5f7L3ZodumRaRqBsan4vAA0zAA== 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=hBOBOGLc7FXO7a70ljNHtNd62KezrSPqmdl5a/pYRwexVeVrTSh/rOoqV4WJFijdNMlUDu99bmZIirNCiPAZHi8ANWb5Ean8zUxBac0Th+qhCTbmQ5hLkwR3gIxsjv9bYa57M8s3McUnz9DwhHyPxy8Z3/FOpRBd+qQX1GkmGVd6dUmKeSW5JC2LJ0g1cc8LBnIDhn5uOsO97zzlDjD41uIWkdb64dD3yw3kgHou5ak70+DSvKtqDOG/z1QIkdjY3QV4bS+JHkTX3k8FNBTcgmEFz3iGPP1JcUzvLCsX/ECaF/LP79yt9glgF7Y/7OlPUbq07u6MrhGY/N2HnO3sSQ== 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=UueEJ3++IYF0CYoQmWmv41n7xnT8GEa7eiIp1rrYVCQ6Y1VNEsMybwWIOYbYsXNFYPtINLeOsO553PfBXf3WMSB8XHYEHzUZGa8PCmV6BvtcebtiKgTDuA70ScEA6Rvxomfmp0/FeIv5koiNl2djwzzMG7xU+qGqGIj2k29L7Z0= Received: from BN6PR16CA0037.namprd16.prod.outlook.com (2603:10b6:405:14::23) by BYAPR12MB2680.namprd12.prod.outlook.com (2603:10b6:a03:63::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 31 May 2022 15:56:50 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:405:14:cafe::2f) by BN6PR16CA0037.outlook.office365.com (2603:10b6:405:14::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.17 via Frontend Transport; Tue, 31 May 2022 15:56:50 +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; pr=C 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.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:50 +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.28; Tue, 31 May 2022 10:56:48 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 11/13] tools: update test_hmm script to support SP config Date: Tue, 31 May 2022 10:56:27 -0500 Message-ID: <20220531155629.20057-12-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cbeb1d61-d8a7-4ef3-cb70-08da431e2c8a X-MS-TrafficTypeDiagnostic: BYAPR12MB2680: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: LOVtzuzA7FJMUR5sxibnCJa+h0N0bvyriMitz9gnsemgHDAdFfGsy/RD3clPRB7FLFgTg4L39CEOpqq7DU9IzStdcivzPTEdHhJ/FkodCfbC6uyXU/bMyWQyt0iX2TIBCJuIK4dGse7B0nnT2zYETAY7+Aycd3Mige2ECU90g5xzNAA7jyStzaW/kIycwgKIRluc+dUEOJFBQ3h+dJvnzIEKYeMgnyjjR6hi4n3MEinQyHLv/+ycV4hbWLOQVrP1r5X3mdo00xon5JSIVPEKIQwoCfG5KGY9COh3lgngymSkaqPvzLZKuEAzdfZs5wgkTrxwkXAwhPEGuqWpvZ0zum4S8YaLfjuWbrn7c9ImzxxZVthWXz8U4M1OOcfzRDeGPuk6TYR28m7/gy//asKv7laWbz1tQw/Y8fSU6XGBhllfsJ3QjwEGbegRqd7cj5hhFuBVNsJaNGyS1Myc1MpPctrmBS10UuA5gOqDNKh0s1YoPQqepkiZ8K6YZQ/GfdVd8S2r202z3N8L6QjmneNmaZbPNq6eU1mOV58zxzt9k6+9zBMxUoCyP11PiEvmuZ4Xsc4PBkGIrCVcmiIAOY512gerwZzaWAOrDBGSkNU4SZe1MCvhy/cpFquR9m4nu6IYWWDK/vzQ4pW9t/Nwyb7XPn3tB2KB5bweNsZYPA1ZhQRDd7F780anQypQzcOxxA7Wp/SRx/b3OEeV6vwGz/44/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)(36840700001)(46966006)(40470700004)(82310400005)(83380400001)(4326008)(81166007)(8676002)(36756003)(6916009)(36860700001)(316002)(336012)(16526019)(47076005)(426003)(186003)(1076003)(2616005)(26005)(7416002)(70586007)(54906003)(70206006)(6666004)(7696005)(40460700003)(356005)(2906002)(5660300002)(86362001)(508600001)(8936002)(44832011)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:50.1094 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cbeb1d61-d8a7-4ef3-cb70-08da431e2c8a 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: BYAPR12MB2680 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B9E93C005C X-Stat-Signature: 1gcftato896p6ab13by9fga5d47o69hc X-Rspam-User: Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UueEJ3++; spf=pass (imf28.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.92.68 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-HE-Tag: 1654012575-727162 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Tue May 31 15:56: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: 12865892 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7038DC43217 for ; Tue, 31 May 2022 15:57:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 98CD16B0083; Tue, 31 May 2022 11:56:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 916446B0085; Tue, 31 May 2022 11:56:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7418C6B0088; Tue, 31 May 2022 11:56:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 571D86B0083 for ; Tue, 31 May 2022 11:56:55 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 379B8803E4 for ; Tue, 31 May 2022 15:56:55 +0000 (UTC) X-FDA: 79526491590.14.CA1EFA9 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2053.outbound.protection.outlook.com [40.107.96.53]) by imf25.hostedemail.com (Postfix) with ESMTP id 9EF65A004E for ; Tue, 31 May 2022 15:56:20 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FEdhJh+YbQkJNbauKeInVRmvehrSvJnqSPwLKAfiuMJCyeWLjbh7RKUkQZl3Od8ES9GP/bZFHtqozsaxc7qLBlaAumPcUaLig3I7A35+p4bbhzt6M42IFWR7ZIvtabNneH1dKbNZOzkL+4MnbesiV4QogvRNqFR9LXLFPpSaMCjc5JE3euT8s925xs9Pniv06PmlH2N6pwIE11RzsUJz84UTsiAtF16x310WJg+3VD5zGeSPfzKEiRboMPV3UCJynEvb21rTNb/PjNuMyG1w0lwoP+BMQgwVj9eOFgWcF3unt1K78xYvAw4wOIcPrGGvY9880gJ1djz8U0I40s3gjw== 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=NhE45xSnkb5THISN6QaW6n4gbcJIelRf+hW2hYHTANQHcR0f/UG1KGHgfZkKAS1AC3dGdf8AIfLQXfUARQcUbSPzkvroHXRs6cybzsRTXSX8pzh8sDyOoqL2wYe4ywHxAkjYSwxleSg3xBJwlGNTHRp4pLH6vF0h0QahATllgxzFW76ofly5slFw8CrSyBT4zy0gWew9vPLAJ7ARuphMBayGl9u3yMJDf4Vt7f+jA5N1VNCqKIVnQNZOobvCDW4CO8ACNpAhto930S0q0jxu0S3ca3C8PCB+wKvIdwZOz8NUdr9HYlJHEv3Kk6jS88Yp6KPY7QAQWhJuehkY0L0ooQ== 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=SSax7oDA62xW/e9NQFJRleMw/sU87CoI6TtTeUE6NjOH3i14CnJqhF4X0iieoPcizRWNeNQmoV1+Jona9V8eudZ0Ct+G9rd8gz08hNdXq0xc+YVYJV3JJLcbiO0ix9Fl32JLCPCyp0rtVbgYGhVlOgHL2Gid0PwfNaDaZeO7y84= Received: from BN6PR16CA0037.namprd16.prod.outlook.com (2603:10b6:405:14::23) by BY5PR12MB4918.namprd12.prod.outlook.com (2603:10b6:a03:1df::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 31 May 2022 15:56:51 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:405:14:cafe::5e) by BN6PR16CA0037.outlook.office365.com (2603:10b6:405:14::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.17 via Frontend Transport; Tue, 31 May 2022 15:56:50 +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; pr=C 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.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:50 +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.28; Tue, 31 May 2022 10:56:49 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 12/13] tools: add hmm gup tests for device coherent type Date: Tue, 31 May 2022 10:56:28 -0500 Message-ID: <20220531155629.20057-13-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 34ad03e8-c247-4160-4eae-08da431e2cf1 X-MS-TrafficTypeDiagnostic: BY5PR12MB4918: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: B0eWRaL/wwwCabOBauV8xiSw+psx69pzgR3Nyd8iTiW4ZluIv0cVZHp93p/87kHIGW+VTbRoZFvTG/NuHBEI84Y8/rT2qHFRM5PH9zLbcE3fcl9L6de9JpcDqqvcrIs+RgTgCJv9medTL3Lbv1XNLebnavYzCyS5C9aD70BCOYb3BgZiSoSefwJfJABwGmB5DBvbF7R6mg3iDTMkkLzm5h4HagvK8CY+ZRxzS/wXJwUW4TxrhGJbDXmrlwulZobfvni/mltAAlXfHf/QHU6c/ck+kC9pbg+8i/+YL+jHAP7Mx99pCNIubYbXwEc3c77dvI91Rm7BZ5gopPIbxzKvDaG21+NYR34+VlGGlY1eiowdfM56+nlp5xIEJwkHr6Q0BNFt/DR5s8buBR9kZ276MAFJfgtyjCqsTmnVoHVi6GTsAQ1yud4dqGNQzOj3U3aGtlAww/6X/jiGI7s6Dojuqh0yHByL7e2ScA3CX7Y0pDB8/k6Om0xTNnutwqUXhJS7ByYxfWc17IpNZFqYOOEvfKJsjcVKiiiTD6CiLahp/LivhwNNAuWsMpB4/rB/FdrOuUQPvtnyXpuQVw599J81fRaVAarEQ1xQPSQiWfcxcNEZSoqvonwCAjA3DMym/Fyb6HSN10TjM9bMxKsZ5PwnthZxuTX4BVODcu1VulobBoa/idrELTy1vbq8Mdv94bQO6uwHrsHmLhSQfTSQZeyFcg== 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)(54906003)(316002)(81166007)(2906002)(26005)(7696005)(6916009)(36756003)(356005)(47076005)(336012)(426003)(83380400001)(16526019)(4326008)(8936002)(2616005)(7416002)(70206006)(82310400005)(70586007)(86362001)(5660300002)(8676002)(44832011)(40460700003)(508600001)(1076003)(186003)(36860700001)(6666004)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:50.7813 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 34ad03e8-c247-4160-4eae-08da431e2cf1 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: BY5PR12MB4918 Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=SSax7oDA; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf25.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.96.53 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Stat-Signature: re6g1g8hh13tja548g73zegmrr8r4tqs X-Rspam-User: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 9EF65A004E X-HE-Tag: 1654012580-719970 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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 Tue May 31 15:56: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: 12865890 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA14CC4167D for ; Tue, 31 May 2022 15:56:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 579FE6B0082; Tue, 31 May 2022 11:56:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 528776B0083; Tue, 31 May 2022 11:56:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 39ED26B0085; Tue, 31 May 2022 11:56:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 222C86B0082 for ; Tue, 31 May 2022 11:56:54 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0028C33E0C for ; Tue, 31 May 2022 15:56:53 +0000 (UTC) X-FDA: 79526491548.19.7541EFE Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2079.outbound.protection.outlook.com [40.107.93.79]) by imf28.hostedemail.com (Postfix) with ESMTP id 4B1ACC0078 for ; Tue, 31 May 2022 15:56:15 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OIEAXMtvk05EoL6YbBkfBmAVF9pnyujpHojCEwx9kKFmhGMzDOj6Hg+OKahhVbrHBp/XgrsNrq1W/JjaNiptdq31blr+EqRN1gKiG6RygTmeFN6Q/B0jlY8zb7Dmw5NljUInDAxBx6LJfQtZjxHTVh/mT6OpNJPmKufUrc7p8hy5UXTmVapQTEsBxON2vUdwtmqTLwafuVkcbEtj/UOSwBK6FNAr85fyR90a3lTlqh/hZPx2N3KVEy7wx7wESPhJlJdgzpvh1vdVBQcVEL2lMOefiIaf7tmcQQxafc9CJVUi71v71NzXLfNiNfFq7orum+tIoLuTZaWLN5BdMeqsnw== 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=iP02pSPjmRWwHGBrQIikU8ehdNd4qZyNE4vr2/7bQ493Q6Vr5kfIAVG4BWuy7ynHRcrCJ9FQ8X8PahpMZS/VBAXZ46ICQtLyEW53EOULCitxhlo1FCNGTuQtcLZhpcDzrNY8AS678Clte+RkVdGGBExXwMotQMG4gfMAUllO6k3hfQer9Zcjqpj5YJMtKL15WkBm5DlFuqhVKZGaVcIK4TYvTN+jyU3WTGTFVoow+Yz69oUkkMHmWdHv/NjBlnNT6naIDADGNQF28n/4yDVqem1foWNz+g67O4SWGtDQC+Dkxy45+PKFFxhqqwt+QqR8O4JDQKNc1A8H1eZxFjdeLg== 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=1YVjM79yuZOhjo+B0bl70ccMG4dV/XzGkl7kDZLGW7Xvic/JVKYcF1AHSPivexRoTY1IeUy1G9QxBpN/lKG2CHGWntbZiE6LNI9ww3qvQERuhOeKH0W7xKEJM4v7iAYIPj/qE871CAflh43/le32w+vF4KHyqnfE/gYGXtpmGRc= Received: from BN6PR16CA0027.namprd16.prod.outlook.com (2603:10b6:405:14::13) by CH0PR12MB5137.namprd12.prod.outlook.com (2603:10b6:610:bc::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 31 May 2022 15:56:51 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:405:14:cafe::35) by BN6PR16CA0027.outlook.office365.com (2603:10b6:405:14::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.18 via Frontend Transport; Tue, 31 May 2022 15:56:51 +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; pr=C 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.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:51 +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.28; Tue, 31 May 2022 10:56:50 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 13/13] tools: add selftests to hmm for COW in device memory Date: Tue, 31 May 2022 10:56:29 -0500 Message-ID: <20220531155629.20057-14-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 73f4f803-82ec-4d52-e6eb-08da431e2d4b X-MS-TrafficTypeDiagnostic: CH0PR12MB5137: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: riTnT8lZvDS7axhvwnn3ZAWrBf2t8UUU1jkz8AWZrurZDU9l04POFpE8E9kcleRyut7h0lXKiu+aKShqos3u0FlTdv/eOgWK5roA5zWCLFdLziqlR+M/HcXLk20ukE73a/fPQcVBjsSi6wXVeGHw/xxUX62gsXrYtyKKVSzWoLBoiLhZ2WLjy8gtLsmTo5gPfZgT3P6OFbhutmAskBOaOyEGXrimdb4vSEbI5T8L0iUj+5GlqKSxbSu1TjVgZn02Yh8gls055H5NyD9qgV5FAHqh3u38z3Qzevzn+3lpAplgYd9zMrCVbvNK5xhx2fA9EEo+A89EwgQa1F8McojAAqmzZK4hdVM6e2gbCrIhJJTgFAUXaaJe+yy79LQly3tvoQqZksr7S4ajJwuh3x1ZblcqOFPNHYwWgIxUqKuT65RY1j9THJac7xNrbnPcaolyEVAjFaL7qtYbDNnWkzwSBZMUAXkMoYiFB049wbc6OLGmLUvTkf+a+9SMniA2no8TMO0iSa/806t2JwKNRm7Q8w93EXtaysrLgoe04z00yFw/AbB399Q60zM1vHFlcWsVKqj/srhsDzUVzRlYBh/1vKf4ZYB2C2jdEtpGr+Cxl97qV9xsan0Vvh8/YbUkBpCHAtXOxqLFtNM4DbCvWqfJvCcWf0X1arFDxvW9mgMtVoKtETCl5Z6erW3HInLF3UfYIpZbxHG+d4B07Qc5oO2JFw== 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)(44832011)(2616005)(82310400005)(36860700001)(356005)(81166007)(36756003)(47076005)(336012)(316002)(83380400001)(426003)(7696005)(6916009)(54906003)(6666004)(2906002)(16526019)(186003)(40460700003)(7416002)(8936002)(86362001)(4326008)(8676002)(5660300002)(508600001)(70586007)(70206006)(1076003)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:51.3750 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 73f4f803-82ec-4d52-e6eb-08da431e2d4b 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: CH0PR12MB5137 X-Stat-Signature: yp8wbqb9xpa7p7fp3tr4kxxzyj4zoh1a X-Rspam-User: Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=1YVjM79y; spf=pass (imf28.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.93.79 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 4B1ACC0078 X-HE-Tag: 1654012575-986911 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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