From patchwork Tue May 24 19:06: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: 12860441 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 7F664C4332F for ; Tue, 24 May 2022 19:07:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B58368D0005; Tue, 24 May 2022 15:07:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B06278D0001; Tue, 24 May 2022 15:07:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9558C8D0005; Tue, 24 May 2022 15:07:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 802228D0001 for ; Tue, 24 May 2022 15:07:26 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5A38F9C7 for ; Tue, 24 May 2022 19:07:26 +0000 (UTC) X-FDA: 79501570092.28.4D2C9FA Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2068.outbound.protection.outlook.com [40.107.237.68]) by imf28.hostedemail.com (Postfix) with ESMTP id 714F2C00CE for ; Tue, 24 May 2022 19:06:53 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ADrSVeBtyj4dqvQ6hwrOK2qwpYGAtnYoaLx3rNBPZLTCUtDtpBG1yjpDblIcmAq/HgczzBxxX98HtFtYKa1HX1ulxNNrcLzBrLuc1dg0ATzfgCDAoJJaOXVh8JTydMsFz2mHDZ9aew9vzV1hu/kkl00hzutpJl3bJPYB4tsR5S0lYJlQtJKqp94++CiH5RxrgJjlaxxtn4jDwP8fvPs/U+JQBlXVgMUOtO3IGBZuuVy3qJ5dkeUyPkXHfeh4HyiTrZOPGaeTWU3U175TyDv/yaHXUuJEOQGuhgepFJ5Z3BINE+sLdiXlXk0Wi74vwSUOMCI/DGxooLNlMEnMbGWxWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=uyHGPwSe3v1LETaDtyafG2r/bh8A0lXb5WCuxOn9RB8=; b=XH/QuwY1alz5vr6VoaulEGKFxbvZwogFR2nl36YCkaQPexfTI4EH3VuLtdC9QvgEIIK2DGd2ak0tMY/XPdB+2ak1wSlfKQELiwT7dKz7g/7M+OLe9ViWvzbJrX5BYFS1eY7ee9ROl4lOJA188Wh9roSepHmz2FU6/WsXKPYOA1ekQ/5LliIHRXYbOaLHvjsHbQnRSOV5kQI+bypp78pcvanFDmqbdKSefgUJ7FB0u0P34JAehTpgAzyoQX4up0LXf0YJJWDOFGkWJlYIn3+TfvuwBwr6rpOvSQSbZdhuY2TFyVMCShThFaJ1iQ06gH3yxc8blhTmcapckq1pxVvp0g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uyHGPwSe3v1LETaDtyafG2r/bh8A0lXb5WCuxOn9RB8=; b=LBbrulVfhxDvjHsdfhALLghpT1lY5d0F/7byMaOqqyDxkvVvUz9o7oSonscW7BFlXOcxavutpiQ65ApO7gMI8UEFs3pFff8WsLr8lyHut1JiWpT25rdnFkwAVKdEAmNqWwanz1YwCREgZfVrw/9eDBtKzr1pzPWSPhiyrFS6IuY= Received: from DM5PR17CA0070.namprd17.prod.outlook.com (2603:10b6:3:13f::32) by MW2PR12MB2475.namprd12.prod.outlook.com (2603:10b6:907:3::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 24 May 2022 19:07:22 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:3:13f:cafe::cd) by DM5PR17CA0070.outlook.office365.com (2603:10b6:3:13f::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13 via Frontend Transport; Tue, 24 May 2022 19:07:22 +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 DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5273.14 via Frontend Transport; Tue, 24 May 2022 19:07:22 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:16 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 01/13] mm: add zone device coherent type memory support Date: Tue, 24 May 2022 14:06:20 -0500 Message-ID: <20220524190632.3304-2-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 80919537-0c60-436d-e283-08da3db8a1be X-MS-TrafficTypeDiagnostic: MW2PR12MB2475: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: /jdFoFZ5qt/TzDoCTSAVzWtZdZlxWmXiWjhVzEZ2hJw0Fang3fIRG0f82lFMjv8JLuiZ6TtZaYMhJkFdow+sLjuTBtOckMuljb++gLB2zLDzPCAgKzr9DvOxhMuyosABzcbLmmD8eGcROXTDNeUXL/pzqrAoWRBhS5qpx6gXxmVzxi+V3oFkzPAnpaZdxT0WrAVZS/tQc4ii0c6N0ZlNlFH+rR/Kk8Z3FfYssM5PvTNVtN7Vw/qD86leP/cipv9lfyFKMljmNOyPLgQyRnPKYq+WFoYV5CJBntIAoPjsBNL7Cgwwhm76IpRYXnOILdNSXQMTmN7hWVXHolAFWYjuxn2DSpanpntmFv1Dm/3cGQxMbIsDp1K4M+zpODCta88xzvI0Mfewg1rRebeb73EUagg5+1l3eJE7XueZQZhf7IOpgLLiSB5iN+d2O0pOcefQYaHOkJnksgWdWYqcjyWEkIkAvCnm+aN1mIHobD7KYrSCGG5TD+0XQq6Ard3WUppnVQVdEO2bobloquNkxYoBNE3AlhsI7kvhGINLioq8EkRqxH4+cxtzoBxyVUUzB1W8eVJ5XvtQWnTq/FiTBiWmDzWpgFPJlOjHPiykX7r2BTTS4L8o7o3JymOvjdZWuH8hMF4BLmBWqUn3wMHoTk2piP5OmxDi03pgJGRnFrRb9o9ObynO3FhmlHtSg2ssG4MlazNwvpYy9WJrl6zFDkarZg== 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)(2616005)(54906003)(6916009)(8936002)(44832011)(36756003)(508600001)(4326008)(2906002)(40460700003)(86362001)(7416002)(70586007)(70206006)(81166007)(356005)(5660300002)(8676002)(6666004)(83380400001)(36860700001)(426003)(82310400005)(16526019)(316002)(47076005)(186003)(1076003)(26005)(336012)(7696005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:22.2014 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 80919537-0c60-436d-e283-08da3db8a1be 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: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR12MB2475 Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=LBbrulVf; spf=pass (imf28.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.237.68 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspam-User: X-Stat-Signature: g5naxq3iw1r5dqsogi1sxd9ngczqrd71 X-Rspamd-Queue-Id: 714F2C00CE X-Rspamd-Server: rspam01 X-HE-Tag: 1653419213-741231 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 598fece89e2b..3e1f97c9fdc6 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5629,8 +5629,8 @@ static int mem_cgroup_move_account(struct page *page, * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a * target for charge migration. if @target is not NULL, the entry is stored * in target->ent. - * 3(MC_TARGET_DEVICE): like MC_TARGET_PAGE but page is MEMORY_DEVICE_PRIVATE - * (so ZONE_DEVICE page and thus not on the lru). + * 3(MC_TARGET_DEVICE): like MC_TARGET_PAGE but page is device memory and + * thus not on the lru. * For now we such page is charge like a regular page would be as for all * intent and purposes it is just special memory taking the place of a * regular page. @@ -5664,7 +5664,8 @@ static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma, */ if (page_memcg(page) == mc.from) { ret = MC_TARGET_PAGE; - if (is_device_private_page(page)) + if (is_device_private_page(page) || + is_device_coherent_page(page)) ret = MC_TARGET_DEVICE; if (target) target->page = page; diff --git a/mm/memory-failure.c b/mm/memory-failure.c index d4a4adcca01f..c8a0e9fcdf42 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -1695,12 +1695,16 @@ static int memory_failure_dev_pagemap(unsigned long pfn, int flags, goto unlock; } - if (pgmap->type == MEMORY_DEVICE_PRIVATE) { + switch (pgmap->type) { + case MEMORY_DEVICE_PRIVATE: + case MEMORY_DEVICE_COHERENT: /* - * TODO: Handle HMM pages which may need coordination + * TODO: Handle device pages which may need coordination * with device-side memory. */ goto unlock; + default: + break; } /* diff --git a/mm/memremap.c b/mm/memremap.c index af0223605e69..471ec84ed82b 100644 --- a/mm/memremap.c +++ b/mm/memremap.c @@ -314,6 +314,16 @@ void *memremap_pages(struct dev_pagemap *pgmap, int nid) return ERR_PTR(-EINVAL); } break; + case MEMORY_DEVICE_COHERENT: + if (!pgmap->ops->page_free) { + WARN(1, "Missing page_free method\n"); + return ERR_PTR(-EINVAL); + } + if (!pgmap->owner) { + WARN(1, "Missing owner\n"); + return ERR_PTR(-EINVAL); + } + break; case MEMORY_DEVICE_FS_DAX: if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) { WARN(1, "File system DAX not supported\n"); diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 70c7dc05bbfc..7e267142ea34 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -499,7 +499,7 @@ EXPORT_SYMBOL(migrate_vma_setup); * handle_pte_fault() * do_anonymous_page() * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE - * private page. + * private or coherent page. */ static void migrate_vma_insert_page(struct migrate_vma *migrate, unsigned long addr, @@ -575,11 +575,8 @@ static void migrate_vma_insert_page(struct migrate_vma *migrate, page_to_pfn(page)); entry = swp_entry_to_pte(swp_entry); } else { - /* - * For now we only support migrating to un-addressable device - * memory. - */ - if (is_zone_device_page(page)) { + if (is_zone_device_page(page) && + !is_device_coherent_page(page)) { pr_warn_once("Unsupported ZONE_DEVICE page type.\n"); goto abort; } @@ -682,10 +679,11 @@ void migrate_vma_pages(struct migrate_vma *migrate) mapping = page_mapping(page); - if (is_device_private_page(newpage)) { + if (is_device_private_page(newpage) || + is_device_coherent_page(newpage)) { /* - * For now only support private anonymous when migrating - * to un-addressable device memory. + * For now only support anonymous memory migrating to + * device private or coherent memory. */ if (mapping) { migrate->src[i] &= ~MIGRATE_PFN_MIGRATE; diff --git a/mm/rmap.c b/mm/rmap.c index fedb82371efe..dcf14d225a4d 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1848,7 +1848,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, /* Update high watermark before we lower rss */ update_hiwater_rss(mm); - if (folio_is_zone_device(folio)) { + if (folio_is_device_private(folio)) { unsigned long pfn = folio_pfn(folio); swp_entry_t entry; pte_t swp_pte; @@ -1995,7 +1995,8 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags) TTU_SYNC))) return; - if (folio_is_zone_device(folio) && !folio_is_device_private(folio)) + if (folio_is_zone_device(folio) && + (!folio_is_device_private(folio) && !folio_is_device_coherent(folio))) return; /* From patchwork Tue May 24 19:06: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: 12860462 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 15EA4C433FE for ; Tue, 24 May 2022 19:07:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E4638D0007; Tue, 24 May 2022 15:07:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1CCA28D0006; Tue, 24 May 2022 15:07:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC2FE8D0007; Tue, 24 May 2022 15:07:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id CFFBE8D0006 for ; Tue, 24 May 2022 15:07:28 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id B378535156 for ; Tue, 24 May 2022 19:07:28 +0000 (UTC) X-FDA: 79501570176.20.CC5B75C Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2042.outbound.protection.outlook.com [40.107.244.42]) by imf24.hostedemail.com (Postfix) with ESMTP id 7311B180025 for ; Tue, 24 May 2022 19:07:15 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FGrhalCbeVm9LPFfs/PAXn6uIvrnvIuff7KfIOqx8g1/0xkCeQQEpQMbLV/rQEn17ZH+Mi7puHM3A910o05DuY+J+Ruy3mt5iO4g7nBH13qJ6iCBGOMiyjWvOIFnvEB/2mD4eDsXVPnekaaI5jQXe7d+NavQvRg4OEMX0du3TcS2FYjiqN6QHRWR/ougWn7DYtktPGWw1o4ULB0Nl24AwxMYLHvOZ372IjqT8Hbs5hQnndVwKr0JeFx2/T5dZhhGAKoeKNnxBoznECY9yukiOqowkFO+jZTKw3kFUaR6CI4gRRbjJj0tLYo0gzMBS93AdYkT7b48oPqA5X8KFlcvdg== 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=G9kaarrbv+hPNhMfzr2LpR0lGfMfCmxt4UTGHBBnG8E=; b=hSMYMkTl8CsN0pCHHH9DG6I1QhLVxKpOXEWHdn1wD7AEMHuQNyZz+Q0CMVEyygPem3StCPq0hN6HmcUSCY/HvSmpdmSiYglrqYK8nApeIsWFMw9obyEjodAGqpQrCL8TQqaYyIMjIaV9aXJsSPM38GDvU1caJTvZtGvGGU4/3jsm1kjaXECbiyVA9MiGSOHyMDVL+uTSnGDB2NGZ396e10qWFIRjlQSiIP7FSajn7fVKUr3Ce1ccUF+dljxoYxB0c/OV8qzCPyj+n7VfYAzRBx1wFkDwCynqJeEX+7elcirpzS8Kyjx3PrLMG7dMkI6xTND1sVyQa2hHlyTJ+q35uQ== 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=G9kaarrbv+hPNhMfzr2LpR0lGfMfCmxt4UTGHBBnG8E=; b=cJx8kFMTn2NxA7jJpufigb0Or9DT7zgzes3O0AXMVt+JAhQ9OJkhSSbwCdwFMhyPrcprXCHl1ex3+fxDB/6uQpYr1f7UerznmF8v2l6Rp3fHX69C/sxFcw/Pf7uJIH2K+95yEP4P/KIml6ugXmVCpHbn0xaDIXyCwQpfBRPxFZI= Received: from DM5PR17CA0062.namprd17.prod.outlook.com (2603:10b6:3:13f::24) by SJ0PR12MB5664.namprd12.prod.outlook.com (2603:10b6:a03:42b::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.22; Tue, 24 May 2022 19:07:23 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:3:13f:cafe::b2) by DM5PR17CA0062.outlook.office365.com (2603:10b6:3:13f::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.23 via Frontend Transport; Tue, 24 May 2022 19:07:23 +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 DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5273.14 via Frontend Transport; Tue, 24 May 2022 19:07:23 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:17 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 02/13] mm: handling Non-LRU pages returned by vm_normal_pages Date: Tue, 24 May 2022 14:06:21 -0500 Message-ID: <20220524190632.3304-3-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 93f6647f-848f-418d-3d18-08da3db8a23d X-MS-TrafficTypeDiagnostic: SJ0PR12MB5664: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: oVg5vOu44yBM5WiJeyo0RxF7rRHFw6iOvEiMh320BpaD+7bHjHcJua6G7/H0mUoYJs5cqoV8T5EawxWw3xoNofVb0S9RZ2zdTfmGr1meWJd7le3/0FzyevxyJXGCg/V52xRS49qwdmGdA7mlwREE4w/MedkqRGy4QkIwLEhTgUlaql41f5wSXWg2n/x4y//vVxEvyB7t6BO/PcIETL1qj8v56i6xKazsVZNzbqqEgVuvlb7J1sQmoM0vmsBONjDPXsqvi9q5amklUKMuUq3Zfvk8sT9kWnMhknmtnmonZxtqJk+sv7i98nTbA6t87/cGBxGWSLsl9qqSxDaacLrTRY/EwH2jiyJOcYVfiGI6lliOuOpTtylXm3/dQCqIY3hwuD5maWIkZFituEQ8yVzna7BtG4QQ1rHf23ECjeCmLJyhpYtDevtC7MkQppy8RyrCwtNyXSSZk/7UxVxIDkXUU2mD7vK1YV9cq/MJflpwdydrdCBWwNKnSu93H2Gtu5UFPpcmSKx8t20KEDAWpl7WoLoD5+XFDrCS1kX9yh8rT5u7+4I9Q6Ihyhkm6THe7LgIXUxoWmrMV2kNVzMIfd02jSA/VzMWyOO8Mwr8DgPRi3m0unXSN9ZNVNOynQkuN5rBnqA9AvgJImjwYS17PjvEulIa42ZP5IKvZtq/yOk/5JU0BjFNw55kDbkXSax+q9cfDMoIvxTXio1J4wFOi9GFNw== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(40470700004)(46966006)(36840700001)(508600001)(8676002)(8936002)(83380400001)(40460700003)(26005)(316002)(6916009)(54906003)(356005)(36756003)(4326008)(44832011)(7416002)(81166007)(70206006)(86362001)(82310400005)(36860700001)(70586007)(426003)(6666004)(7696005)(5660300002)(47076005)(336012)(186003)(2616005)(2906002)(1076003)(16526019)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:23.0451 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 93f6647f-848f-418d-3d18-08da3db8a23d 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: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB5664 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7311B180025 X-Stat-Signature: zhjfapg8szbkh1xcpfu8ozjgnz33nu39 X-Rspam-User: Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=cJx8kFMT; spf=pass (imf24.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.244.42 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-HE-Tag: 1653419235-244241 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 | 2 ++ mm/huge_memory.c | 2 +- mm/khugepaged.c | 9 ++++++--- mm/ksm.c | 6 +++--- mm/madvise.c | 4 ++-- mm/memory.c | 9 ++++++++- mm/mempolicy.c | 2 +- mm/migrate.c | 4 ++-- mm/mlock.c | 2 +- mm/mprotect.c | 2 +- 12 files changed, 30 insertions(+), 17 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index f46060eb91b5..5d620733f173 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1785,7 +1785,7 @@ static struct page *can_gather_numa_stats(pte_t pte, struct vm_area_struct *vma, return NULL; page = vm_normal_page(vma, addr, pte); - if (!page) + if (!page || is_zone_device_page(page)) return NULL; if (PageReserved(page)) diff --git a/include/linux/mm.h b/include/linux/mm.h index 9f44254af8ce..d7f253a0c41e 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -601,7 +601,7 @@ struct vm_operations_struct { #endif /* * Called by vm_normal_page() for special PTEs to find the - * page for @addr. This is useful if the default behavior + * page for @addr. This is useful if the default behavior * (using pte_page()) would not find the correct page. */ struct page *(*find_special_page)(struct vm_area_struct *vma, @@ -2929,6 +2929,7 @@ struct page *follow_page(struct vm_area_struct *vma, unsigned long address, #define FOLL_NUMA 0x200 /* force NUMA hinting page fault */ #define FOLL_MIGRATION 0x400 /* wait for page to replace migration entry */ #define FOLL_TRIED 0x800 /* a retry, previous pass started an IO */ +#define FOLL_LRU 0x1000 /* return only LRU (anon or page cache) */ #define FOLL_REMOTE 0x2000 /* we are working on non-current tsk/mm */ #define FOLL_COW 0x4000 /* internal GUP flag */ #define FOLL_ANON 0x8000 /* don't do file mappings */ diff --git a/mm/gup.c b/mm/gup.c index 501bc150792c..c9cbac06bcc5 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -479,6 +479,8 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, } page = vm_normal_page(vma, address, pte); + if ((flags & FOLL_LRU) && page && is_zone_device_page(page)) + page = NULL; if (!page && pte_devmap(pte) && (flags & (FOLL_GET | FOLL_PIN))) { /* * Only return device mapping pages in the FOLL_GET or FOLL_PIN diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 910a138e9859..eed80696c5fd 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2856,7 +2856,7 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, } /* FOLL_DUMP to ignore special (like zero) pages */ - page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP); + page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP | FOLL_LRU); if (IS_ERR(page)) continue; diff --git a/mm/khugepaged.c b/mm/khugepaged.c index a4e5eaf3eb01..8bf4126b6b9c 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -627,7 +627,7 @@ static int __collapse_huge_page_isolate(struct vm_area_struct *vma, goto out; } page = vm_normal_page(vma, address, pteval); - if (unlikely(!page)) { + if (unlikely(!page) || unlikely(is_zone_device_page(page))) { result = SCAN_PAGE_NULL; goto out; } @@ -1276,7 +1276,7 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, writable = true; page = vm_normal_page(vma, _address, pteval); - if (unlikely(!page)) { + if (unlikely(!page) || unlikely(is_zone_device_page(page))) { result = SCAN_PAGE_NULL; goto out_unmap; } @@ -1484,7 +1484,8 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr) goto abort; page = vm_normal_page(vma, addr, *pte); - + if (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. @@ -1502,6 +1503,8 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr) if (pte_none(*pte)) continue; page = vm_normal_page(vma, addr, *pte); + if (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 063a48eeb5ee..f16056efca21 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -474,7 +474,7 @@ static int break_ksm(struct vm_area_struct *vma, unsigned long addr) do { cond_resched(); page = follow_page(vma, addr, - FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE); + FOLL_GET | FOLL_MIGRATION | FOLL_REMOTE | FOLL_LRU); if (IS_ERR_OR_NULL(page)) break; if (PageKsm(page)) @@ -559,7 +559,7 @@ static struct page *get_mergeable_page(struct rmap_item *rmap_item) if (!vma) goto out; - page = follow_page(vma, addr, FOLL_GET); + page = follow_page(vma, addr, FOLL_GET | FOLL_LRU); if (IS_ERR_OR_NULL(page)) goto out; if (PageAnon(page)) { @@ -2288,7 +2288,7 @@ static struct rmap_item *scan_get_next_rmap_item(struct page **page) while (ksm_scan.address < vma->vm_end) { if (ksm_test_exit(mm)) break; - *page = follow_page(vma, ksm_scan.address, FOLL_GET); + *page = follow_page(vma, ksm_scan.address, FOLL_GET | FOLL_LRU); if (IS_ERR_OR_NULL(*page)) { ksm_scan.address += PAGE_SIZE; cond_resched(); diff --git a/mm/madvise.c b/mm/madvise.c index 1873616a37d2..e9c24c834e98 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -413,7 +413,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, continue; page = vm_normal_page(vma, addr, ptent); - if (!page) + if (!page || is_zone_device_page(page)) continue; /* @@ -628,7 +628,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, } page = vm_normal_page(vma, addr, ptent); - if (!page) + if (!page || is_zone_device_page(page)) continue; /* diff --git a/mm/memory.c b/mm/memory.c index 76e3af9639d9..571a26805ee1 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -621,6 +621,13 @@ struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, if (is_zero_pfn(pfn)) return NULL; if (pte_devmap(pte)) +/* + * NOTE: New uers of ZONE_DEVICE will not set pte_devmap() and will have + * refcounts incremented on their struct pages when they are inserted into + * PTEs, thus they are safe to return here. Legacy ZONE_DEVICE pages that set + * pte_devmap() do not have refcounts. Example of legacy ZONE_DEVICE is + * MEMORY_DEVICE_FS_DAX type in pmem or virtio_fs drivers. + */ return NULL; print_bad_pte(vma, addr, pte, NULL); @@ -4422,7 +4429,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) pte = pte_modify(old_pte, vma->vm_page_prot); page = vm_normal_page(vma, vmf->address, pte); - if (!page) + if (!page || is_zone_device_page(page)) goto out_map; /* TODO: handle PTE-mapped THP */ diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 8c74107a2b15..e32edbecb0cd 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -528,7 +528,7 @@ static int queue_pages_pte_range(pmd_t *pmd, unsigned long addr, if (!pte_present(*pte)) continue; page = vm_normal_page(vma, addr, *pte); - if (!page) + if (!page || is_zone_device_page(page)) continue; /* * vm_normal_page() filters out zero pages, but there might diff --git a/mm/migrate.c b/mm/migrate.c index 6c31ee1e1c9b..c5d50e96ecd7 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -1611,7 +1611,7 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, goto out; /* FOLL_DUMP to ignore special (like zero) pages */ - page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP); + page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP | FOLL_LRU); err = PTR_ERR(page); if (IS_ERR(page)) @@ -1802,7 +1802,7 @@ static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages, goto set_status; /* FOLL_DUMP to ignore special (like zero) pages */ - page = follow_page(vma, addr, FOLL_DUMP); + page = follow_page(vma, addr, FOLL_DUMP | FOLL_LRU); err = PTR_ERR(page); if (IS_ERR(page)) diff --git a/mm/mlock.c b/mm/mlock.c index 716caf851043..b14e929084cc 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -333,7 +333,7 @@ static int mlock_pte_range(pmd_t *pmd, unsigned long addr, if (!pte_present(*pte)) continue; page = vm_normal_page(vma, addr, *pte); - if (!page) + if (!page || is_zone_device_page(page)) continue; if (PageTransCompound(page)) continue; diff --git a/mm/mprotect.c b/mm/mprotect.c index b69ce7a7b2b7..a6f3587ea29a 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -91,7 +91,7 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, continue; page = vm_normal_page(vma, addr, oldpte); - if (!page || PageKsm(page)) + if (!page || is_zone_device_page(page) || PageKsm(page)) continue; /* Also skip shared copy-on-write pages */ From patchwork Tue May 24 19:06: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: 12860443 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 C17B6C433EF for ; Tue, 24 May 2022 19:07:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5C1228D0001; Tue, 24 May 2022 15:07:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DC4E8D0007; Tue, 24 May 2022 15:07:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 141968D0001; Tue, 24 May 2022 15:07:28 -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 E4E5C8D0006 for ; Tue, 24 May 2022 15:07:27 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C39FF209E1 for ; Tue, 24 May 2022 19:07:27 +0000 (UTC) X-FDA: 79501570134.30.6844474 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2082.outbound.protection.outlook.com [40.107.93.82]) by imf31.hostedemail.com (Postfix) with ESMTP id BE0DA20031 for ; Tue, 24 May 2022 19:06:52 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Bu6TjzO5zfxdF22hYV2QBgLVmFJgcUDwIE9eMQQyLCU939ppI8dE2BiidXIH86EaIas2QdG1zpHGv9oyzbzfwXOPzgjnBGs4jaE0TSs26AAgcSzDTy8bC2jxJVFhBS833V4HywomVlEpZMKtBVhg7d2i4vu0Iq+JkPxB0+iUsaVKiXN++MihGFfeeNKLH+vJbByf1/qC9ZHO77rP0030kPlHemaUInNZIDafWQbt50PBwSCdM8fzcSIrAnetXgXi7ztXmRpqeqUEZ72LhKlmBlHrZke+/2mzKBDG/DQQDl3xsjgfeYCGDhiR9DKH6NRvyLJnJdED2FYBlICQGTqkKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vW0Kci1BxH/G6MOqRePi9dIvH+J6rgyknG8pfmabAb0=; b=YLHdBvN+/F1vPl5f8C7F44lWIYu4gyFlbbe5oVFq2fPWcE3M4MTcMRJU9WD++iAerSBftPeOJIWkEzM42q9vVgyl281+RP49esJtnHG9l+NXvGCUcTPVO6sdokQyTFt6OrTs3f6oKbLpDZey9+Ja0g4zpwXsucmMFcFj3pIeVbDPxgmEz29vAr3HXSzmDmBAzQ6sKVLsbYUPjhmDlk1AphkoX5wte3KkZyC2CU0Zzgj3UVq0gEBkuWuX0GFUcjAxYM6BV70NKPDHpdOLd+FDdS+3dKdOYsR90UwDBTFaaflZP9r034yHz/MAo2jS5W2ksm7jBxPmsSlZN7DNB8Q3xQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vW0Kci1BxH/G6MOqRePi9dIvH+J6rgyknG8pfmabAb0=; b=0OhBwbKlHpu6Np8bWWVYghbY8JlUA9VYxT+JPXS/x1INh/v/XaOmZVI2y0NqmnaHkYKBW2MKW/vuXTaiICvcCx6HWF8IhatguYd+1iQJZBhriH9eazBhcwZvP8ot3dyU8bt2iT9lejR6rqAwotBxFXlDTJKXyFDJEyKwSNWdY1E= Received: from DM5PR17CA0058.namprd17.prod.outlook.com (2603:10b6:3:13f::20) by DM6PR12MB3417.namprd12.prod.outlook.com (2603:10b6:5:118::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.19; Tue, 24 May 2022 19:07:24 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:3:13f:cafe::8) by DM5PR17CA0058.outlook.office365.com (2603:10b6:3:13f::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13 via Frontend Transport; Tue, 24 May 2022 19:07:24 +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 DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5273.14 via Frontend Transport; Tue, 24 May 2022 19:07:24 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:18 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 03/13] mm: add device coherent vma selection for memory migration Date: Tue, 24 May 2022 14:06:22 -0500 Message-ID: <20220524190632.3304-4-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a65c8d27-6d89-4695-a847-08da3db8a2ed X-MS-TrafficTypeDiagnostic: DM6PR12MB3417: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: 17pQ4SBU8wbSGlHhFaZz+QIbTnwyfCKgYnzwsWJfT4SugeepQaBppQgfyNJ7j077U1w4a/zB7pnZil2J/Ww6PXitZK2PpmQDuJv55ZK+sVR3/3DifEy+RHs5jxAQcaCbPuIhGX2WF4KWlURR3Oq7D46+Mam5s0LvVBzUnDp+xqwd0swDmKxtd0/flRwFEkoln2zOh1KwEOa3ef8JSHkzNQI/ABe+mBkNunKZJ4X6vPHY9Smmnhjs2AwxiuOwBZOWjm1KNDQGwFoMskInsREV3hxMm2iJwro1orQSurItDELqPuqTeR3H4jyp6gfqmpT09lwkOEZIVN7EZON3K2+oUrFiKz8N+fvOAotG4AvtShJEJceOFaQxpDpsri6uqnzJsLqlnW2Eb8fUe1PqGLAAjmB5PunDwNCL59oyBMU1wMxqVdU3CTm+QR7gHkto4x4zgzPJ6mufk5NJgGJIScWPu5D4WT+0Jr2IY0ErEOcYUrSJhAQFIdAs/we02QVdkEXfa/ctYG6hMGZxiBkrbvVZNaAoAw8A67K8zw0Sq8x5MdB/OUalMc7J5D335nEEH11Ugrd2tX1LSnJY3SHCB2wJM+wWaRUxNbSyS3J7IBAxifO2lla1Ns4t7bxQitVq9SiFNEQC4UNhX01RIswtXizul2s2Mu/zod2otYK5z5NTwgbI5R1j78hVwzjkK385KHWKQO4xEcg88U4i1gJ62iqFGQ== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(40470700004)(46966006)(36840700001)(508600001)(7696005)(2616005)(6666004)(16526019)(26005)(5660300002)(356005)(8936002)(1076003)(7416002)(186003)(44832011)(81166007)(86362001)(83380400001)(36756003)(4326008)(40460700003)(316002)(82310400005)(426003)(70206006)(47076005)(8676002)(336012)(70586007)(6916009)(36860700001)(54906003)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:24.2013 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a65c8d27-6d89-4695-a847-08da3db8a2ed 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: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3417 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: BE0DA20031 X-Stat-Signature: cnnguapwoohcnfrnwohyuqqpnsx1dxhq Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=0OhBwbKl; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf31.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.93.82 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Rspam-User: X-HE-Tag: 1653419212-710238 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 90e75d5a54d6..a4a336fd81fc 100644 --- a/include/linux/migrate.h +++ b/include/linux/migrate.h @@ -138,6 +138,7 @@ static inline unsigned long migrate_pfn(unsigned long pfn) enum migrate_vma_direction { MIGRATE_VMA_SELECT_SYSTEM = 1 << 0, MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1, + MIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 << 2, }; struct migrate_vma { diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 7e267142ea34..a0b997935cf9 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -148,15 +148,21 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, if (is_writable_device_private_entry(entry)) mpfn |= MIGRATE_PFN_WRITE; } else { - if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) - goto next; pfn = pte_pfn(pte); - if (is_zero_pfn(pfn)) { + if (is_zero_pfn(pfn) && + (migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) { mpfn = MIGRATE_PFN_MIGRATE; migrate->cpages++; goto next; } page = vm_normal_page(migrate->vma, addr, pte); + if (page && !is_zone_device_page(page) && + !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) + goto next; + else if (page && is_device_coherent_page(page) && + (!(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_COHERENT) || + page->pgmap->owner != migrate->pgmap_owner)) + goto next; mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0; } From patchwork Tue May 24 19:06: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: 12860442 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 785C6C433F5 for ; Tue, 24 May 2022 19:07:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3DFC38D0008; Tue, 24 May 2022 15:07:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 29FDB8D0006; Tue, 24 May 2022 15:07:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 02BBC8D0007; Tue, 24 May 2022 15:07:27 -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 DF89E8D0001 for ; Tue, 24 May 2022 15:07:27 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id AFE623451D for ; Tue, 24 May 2022 19:07:27 +0000 (UTC) X-FDA: 79501570134.24.45ABF8B Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2073.outbound.protection.outlook.com [40.107.236.73]) by imf20.hostedemail.com (Postfix) with ESMTP id E2B4E1C003C for ; Tue, 24 May 2022 19:07:11 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=W2jrqoXww3avCUAFbAwUyx/cbbFesr0U95A0ivZ1y22o+R7Sfk+l2XSTzkO7ODOEdDc8KfMiWMLmmdNjqLRbqbFB6g4YOI4h735MB3H0Mfq/TTJHG2vfO3nqk2373XQ/uM/Qkz0YEhrJt+A7K5pxTES8AHiuLwELz0Ftgubs1lPzS4PaMxbwbPEIZ4MstZOstAK/4wBu+Hn1U88kdkRGFqfB6lRF8bzruvUkPwcveclSEqj9y0Ie8F8XUdL5Phh5/hMj5vEB61atpw8FpTBK7XIPiYvrZPrVUBzaTqe0fPZXfkF0DrR8Zqn8AMO8k8a0FAW/F09dpLefy6Wt8I09EQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=519oerrRDEbTv7GQOYa1mMQ+Fz3ORBOf1G9j+BglRrg=; b=PMvyGxLYyW0oWBIHQ+dsVLZWhpv523WHaVnvom361IJl8ZBZsuB0vXivs6cSqhEjBhvFKOmwYoNmpuVFnlnHP0SexMBTE854VlsefDnu24UFralV/gGzpn3EZz7VhuCSgJ0WcBLPJJwXQTKuhMzWFa2ptJkj2di/kR0uCD9jCw0ORVBPvVHXi/imijRzMF1fFwQOqZsY2HrWj4roZDcXnBCYmNaLbPDmkkuZ22RFcAhhfcH25uE42xR1IEZZTNrFJsOcI0jjPgXHNo0iyRFToSw3pqzTFnwbv5OhgjY9B2nlU4VNMM8fJuzWOBuG4cfdOY46nhiDIK7OjPgZygv6oQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=519oerrRDEbTv7GQOYa1mMQ+Fz3ORBOf1G9j+BglRrg=; b=UWZRWqxGVkmUabgGqh3zajL+p55pkQZSJBPJ3nFt0vdACEdfslJLs+jzbVwi1TT9yYaxVAyoZI1R153sg6Bd+UW4nuH+Tv151ghwntSsc0FnRhbwfC0dIf+Cnpj4vlNRtPEMEqhd2dGHqhL8fsG4Koy+tH15r1gzoubb3SzOOyU= Received: from DM5PR17CA0049.namprd17.prod.outlook.com (2603:10b6:3:13f::11) by CH0PR12MB5026.namprd12.prod.outlook.com (2603:10b6:610:e1::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 24 May 2022 19:07:24 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:3:13f:cafe::49) by DM5PR17CA0049.outlook.office365.com (2603:10b6:3:13f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13 via Frontend Transport; Tue, 24 May 2022 19:07:24 +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 DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5273.14 via Frontend Transport; Tue, 24 May 2022 19:07:24 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:19 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 04/13] mm: remove the vma check in migrate_vma_setup() Date: Tue, 24 May 2022 14:06:23 -0500 Message-ID: <20220524190632.3304-5-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a91ef5cc-edff-4f91-06c2-08da3db8a32e X-MS-TrafficTypeDiagnostic: CH0PR12MB5026: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: s4LmTn3ZlwfKU9Vnu6EbR78VtWkqIVTGzaX+5s92JvkJJHAieuIi0L0WizvIHMVvV9fZer/F3mktsMlfV2joOO9CjpEbT8n+xoA81fKSJFU2z74viZUdewOL3Ogi9AFam8PwGwjx/1ZiHlSOIK4gRe6fINw//kkUSZF3YyieSMCS6CHk2JmR9KpRFU4k0gG8lwXl5jE5WdQrjsa29rpz406GqkN4Goav40WxlflTx8PkUolosXiI1hBv8HtcUjOEMxwoNoqn/e8YgqJr5AmkMXBj1rBabwGhsCainDBgOelfRtD9Y+zhHBNCn+P8tOB2mhcRLRkgqJIGk2icV0bSgYzyJKfMjiuITqYfMztvWW4IOm/0SvKbJ1Y59pYVfoYdYwmTixDd5Fbx4LlkX59tvMNyzWRglmiWj6tZDeNxOQF6nV2aUN2XZTmzPaATWZCRMhNDk6JSM3OpWXxwzePMudfwqkh5dd60u0Un3RAjwCZjzww9xHWrcR5kJoXXgvrxZTMOBTJvp7CHt9S3XF8XRNy+nGQnlMW9LNR6pTVYk8VI2wRkMvSIyjIPx75Ckc+5q93lBB1vwA32X0In14r/GG/zVAPrieIWbPCIzi6X+TzIm2h/cfL5SN25V7eJl+G/Yjd+5bHbf+l00N3H06aeE4JNRzm9uZfBH8UGEVV9D1w4FwLDyRnLE5j+/8/sMEFwqIuG3gxgzdHPIBnT6RKAZA== 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)(40460700003)(2906002)(83380400001)(336012)(426003)(36860700001)(7696005)(47076005)(186003)(1076003)(5660300002)(16526019)(44832011)(36756003)(82310400005)(86362001)(4326008)(8676002)(70206006)(316002)(7416002)(356005)(6916009)(54906003)(26005)(81166007)(70586007)(6666004)(2616005)(508600001)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:24.6232 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a91ef5cc-edff-4f91-06c2-08da3db8a32e 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: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5026 Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=UWZRWqxG; spf=pass (imf20.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.236.73 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspam-User: X-Stat-Signature: csq743bo7r7ngj3r5xdrhjiqapagfkab X-Rspamd-Queue-Id: E2B4E1C003C X-Rspamd-Server: rspam01 X-HE-Tag: 1653419231-727793 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 a0b997935cf9..b399c498f0aa 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -467,24 +467,24 @@ int migrate_vma_setup(struct migrate_vma *args) args->start &= PAGE_MASK; args->end &= PAGE_MASK; - if (!args->vma || is_vm_hugetlb_page(args->vma) || - (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) - return -EINVAL; - if (nr_pages <= 0) - return -EINVAL; - if (args->start < args->vma->vm_start || - args->start >= args->vma->vm_end) - return -EINVAL; - if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end) - return -EINVAL; if (!args->src || !args->dst) return -EINVAL; - - memset(args->src, 0, sizeof(*args->src) * nr_pages); - args->cpages = 0; - args->npages = 0; - - migrate_vma_collect(args); + if (args->vma) { + if (is_vm_hugetlb_page(args->vma) || + (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma)) + return -EINVAL; + if (args->start < args->vma->vm_start || + args->start >= args->vma->vm_end) + return -EINVAL; + if (args->end <= args->vma->vm_start || + args->end > args->vma->vm_end) + return -EINVAL; + memset(args->src, 0, sizeof(*args->src) * nr_pages); + args->cpages = 0; + args->npages = 0; + + migrate_vma_collect(args); + } if (args->cpages) migrate_vma_unmap(args); @@ -666,7 +666,7 @@ void migrate_vma_pages(struct migrate_vma *migrate) continue; } - if (!page) { + if (!page && migrate->vma) { if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) continue; if (!notified) { From patchwork Tue May 24 19:06: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: 12860463 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 B2C90C4332F for ; Tue, 24 May 2022 19:07:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E48738D0009; Tue, 24 May 2022 15:07:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DADDC8D000B; Tue, 24 May 2022 15:07:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9CA558D0009; Tue, 24 May 2022 15:07:29 -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 85DC98D0006 for ; Tue, 24 May 2022 15:07:29 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 50FA380852 for ; Tue, 24 May 2022 19:07:29 +0000 (UTC) X-FDA: 79501570218.25.6343603 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2053.outbound.protection.outlook.com [40.107.92.53]) by imf19.hostedemail.com (Postfix) with ESMTP id 12F031A00DE for ; Tue, 24 May 2022 19:07:15 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h4mQo9rhe3nZay10rHSjEUUpgDGxfQIMVSsN/CBy6x9vTkwBCvfzup+eY4n9yFwjElyHh6TeAjM/c4h/bwyTtNWZ6nExmzr+1bI/jQH5SeoWjQoEZS2eSEjws1Y5Tb8yDQ/27JZDNLYJOp0foqcDC1SpA0jBRAgtdR5UaPDkEDMwAUF2vN1XnIQHyGGZvXHPNof0iaMCPdjZztf0hB6osADR6o24VxKuUBofNAvaA4unq8oyKu6+XClJnCkhnkSnUddKSnbdeNH3FPa0Q7PUd1Y3Hx3BGB7wNrDCWfqZQXUQSwfwx82Afre0RJKVpJ48ql7ii+w3ZjzasqeBVGUT/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Hya6PBw44o+VaA6dTWX8yYj49pKVc5eTiFshDTXM37Y=; b=nggwqAd6nYw4ZAb4YOUZhUiRt8YOK51kwIpzbZywap5XaI3u2xLlXQOZDH9tYK+aqQ4v+3d+0d2HC7VlEXz655OdgwAEablMRdRICEsMLXl1LXw52tdC30DZkRfMTuD76RmM3sfy9GWg7Fw+MTV5bOyOTM5IhVZ6ceGHyMVpkJYrrsis+5OYl3I0KZ4E9q/FKukNX0WFTRaM4/DVrmtVbWW5dLp4nk5tGdeslfOQvmKvDGVUjKorVHyhle5Q0mYYey3wU2FQK3U1TdyN6ue9R9u4q8cIeyq3tqsmLaB4ggG77CQhXJYSfj1nLtd1HOxvLwKxGXdGpMDe+G9QBafAIg== 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=Hya6PBw44o+VaA6dTWX8yYj49pKVc5eTiFshDTXM37Y=; b=DQ4hv0CU1tAy2X6c6vPxC8dBg2waFWuM6WyEoHw2gFVrVOow7+stE2DseGmtrTM+bBEM7hyp37CJWbNw8l2CRqhP6VNyk/zN18xGZAw6nG+9vnVuMlOAcHp68dgH+KhCqYytAehswtYOMJ3s+60poN8X7dTjJJGR7vC+1vXqJP0= Received: from DM5PR17CA0052.namprd17.prod.outlook.com (2603:10b6:3:13f::14) by DM5PR12MB1772.namprd12.prod.outlook.com (2603:10b6:3:107::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.17; Tue, 24 May 2022 19:07:25 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:3:13f:cafe::63) by DM5PR17CA0052.outlook.office365.com (2603:10b6:3:13f::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.23 via Frontend Transport; Tue, 24 May 2022 19:07:25 +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 DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5273.14 via Frontend Transport; Tue, 24 May 2022 19:07:25 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:20 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 05/13] mm/gup: migrate device coherent pages when pinning instead of failing Date: Tue, 24 May 2022 14:06:24 -0500 Message-ID: <20220524190632.3304-6-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0649b131-1f54-4e0f-29d6-08da3db8a373 X-MS-TrafficTypeDiagnostic: DM5PR12MB1772: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: HtwBWRThPZbKs4ztfnb7MqKM0MWxb4M4H8A6eB9zo7RI+R6W1yuMCGkbU6QeWFV7DdtC8Tu4zSAvilPa958DpaxOVTGQXFenH8HN5nbME5kJsOUxe/9OJJhrteOWxxRhuBWNvdi54IzJu9lpTgo+Tvld+VCFEcVaBeAij3K2eIWiYsllIKb3kzbgGsS+tc4v5nwEVFwDmiQsLYtcv3Bb0Yc6cHT+NPmOVM03B9ORHRUBUDsKt+dWCojWXaC7YBuVJbZm/yBM2NXbKBpXLuMIN80qIXBwl273vJFaDbleT8xIoJJRZNas5Fnuj+mkeVxvJDPsfBXXGV3wOoxfnjVWGS5lkMq12oOVEIizSPzgcFoyfX2FsXoEHiwDBG+rQRLQ+kyQqmIu8bspPGfCsqkiYM47YgaYp5D7SfFyH53mXB7n/D6H5W/gOVdeGjhR6hpW1dlpcs7/DSgrn1pny2xN3+ADUnAlcJGgm2mMsbpAQWuOHKpXhAhw3BhwyRXAWkWjBEDvo07Y8OCNQ6QR5LHraZZzzbiyODcH+G1zvESX7FjxKAaQ+2QlU53Yd7C5FzSCuq6Q5X2HYGfArEZhMLuttw6DHqZLxXcRWbhne8kE0c2pBZp/ULKgEeOKhvS0dnI0lQpHHXW/esfPu4jHGwZff0QvqJN9nxJnN/whNTCx1ki1zqCYY607cFZSI1eMLGMZEMP1V6WUTWRxHr4VSmJLQA== 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)(70586007)(316002)(83380400001)(356005)(81166007)(1076003)(47076005)(70206006)(4326008)(36756003)(54906003)(8676002)(6916009)(2906002)(36860700001)(82310400005)(40460700003)(508600001)(426003)(86362001)(5660300002)(7696005)(8936002)(336012)(7416002)(44832011)(26005)(186003)(2616005)(16526019)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:25.0763 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0649b131-1f54-4e0f-29d6-08da3db8a373 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: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1772 X-Rspam-User: X-Rspamd-Queue-Id: 12F031A00DE X-Stat-Signature: s3g3xm3q7hy5kp81gbb1ceez95dqszgz Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=DQ4hv0CU; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf19.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.92.53 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Rspamd-Server: rspam04 X-HE-Tag: 1653419235-145782 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 c9cbac06bcc5..0f3cac7604d3 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1817,9 +1817,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. */ @@ -1855,10 +1889,13 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, return nr_pages; unpin_pages: - if (gup_flags & FOLL_PIN) { - unpin_user_pages(pages, nr_pages); - } else { - for (i = 0; i < nr_pages; i++) + for (i = 0; i < nr_pages; i++) { + if (!pages[i]) + continue; + + if (gup_flags & FOLL_PIN) + unpin_user_page(pages[i]); + else put_page(pages[i]); } diff --git a/mm/internal.h b/mm/internal.h index cf16280ce132..32c621416966 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -750,6 +750,7 @@ int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, unsigned long addr, int page_nid, int *flags); void free_zone_device_page(struct page *page); +struct page *migrate_device_page(struct page *page, unsigned int gup_flags); /* * mm/gup.c diff --git a/mm/migrate_device.c b/mm/migrate_device.c index b399c498f0aa..1e74aa7ab580 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -775,3 +775,56 @@ void migrate_vma_finalize(struct migrate_vma *migrate) } } EXPORT_SYMBOL(migrate_vma_finalize); + +/* + * Migrate a device coherent page back to normal memory. The caller should have + * a reference on page which will be copied to the new page if migration is + * successful or dropped on failure. + */ +struct page *migrate_device_page(struct page *page, unsigned int gup_flags) +{ + unsigned long src_pfn, dst_pfn = 0; + struct migrate_vma args; + struct page *dpage; + + lock_page(page); + src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE; + args.src = &src_pfn; + args.dst = &dst_pfn; + args.cpages = 1; + args.npages = 1; + args.vma = NULL; + migrate_vma_setup(&args); + if (!(src_pfn & MIGRATE_PFN_MIGRATE)) + return NULL; + + dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0); + + /* + * get/pin the new page now so we don't have to retry gup after + * migrating. We already have a reference so this should never fail. + */ + if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) { + __free_pages(dpage, 0); + dpage = NULL; + } + + if (dpage) { + lock_page(dpage); + dst_pfn = migrate_pfn(page_to_pfn(dpage)); + } + + migrate_vma_pages(&args); + if (src_pfn & MIGRATE_PFN_MIGRATE) + copy_highpage(dpage, page); + migrate_vma_finalize(&args); + if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) { + if (gup_flags & FOLL_PIN) + unpin_user_page(dpage); + else + put_page(dpage); + dpage = NULL; + } + + return dpage; +} From patchwork Tue May 24 19:06: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: 12860464 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 79CCDC433FE for ; Tue, 24 May 2022 19:07:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 36C5A8D000B; Tue, 24 May 2022 15:07:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 320318D000A; Tue, 24 May 2022 15:07:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 125278D000C; Tue, 24 May 2022 15:07:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id C1CFC8D0006 for ; Tue, 24 May 2022 15:07:29 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 8F3C76082B for ; Tue, 24 May 2022 19:07:29 +0000 (UTC) X-FDA: 79501570218.28.3501A1A Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2043.outbound.protection.outlook.com [40.107.244.43]) by imf30.hostedemail.com (Postfix) with ESMTP id 54EB880033 for ; Tue, 24 May 2022 19:07:00 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PsWsaWvT+d9Dot0beALQmczytOlHQpiWxQPQW1JnmFPaPdjjFlXrTOVf+i+KHKkNBGc+iFE08151GSfiBrUBwxA0yLDDnycWZW9d+/0nl1mUSaNPXcfHN65Pi7L8pjeukwLazWTJ8qELPNN88oMGFrJnBwTb8wEfAKAjAQ74RUk6boM6FxbfmLhumFy71LlelikLuKaJxW53sN0+moKKCnrD+sV4j/DjGCQJTEHFS2JAISjcGDD6se6U5fzN4qec+QvGzZ7+MPGO6DqNO4rB9V/0++2bmqxwhIYngQxJij6HN+KEyASCO1V1xab51aFjjc+zQdgI9UL5sjrwOsGyhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=nl288P0aFMxHvyTdajqKdYE2bhkbzZtjR6cg4VQwpEg=; b=WCCNOf1J25IWTukc8tq0Ye/Nw4qHw5yPt7Lh1Z4WWGrH1oifwVTaCHj4GxYBCrkMvgKAx2qzJsVn2+nHqgkgrVj2vd3A2KHPNjhKKSu/rTYi2pDFjXv+E/Wibr2m2M4FIWfmvtgJfBkZUFIqFVIqf+8oqkbyfkECeNrAfOBgjTXz8mi7mraZzEGR2s5JScF4iO2Bre9hQxfJnWxxmjdzjyIXk0Mg4po/CqOg7npcuEEBrDQsbHw9pHhFhOgriG4CAkL1GINISZK2SFz0RotfssbkMj4oymZxwywsXC4BjTTlTTz16Sd4broiOUF7OjAha+1IH4nlCK8+GjFX52mFgQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=nl288P0aFMxHvyTdajqKdYE2bhkbzZtjR6cg4VQwpEg=; b=J3tEjVyyqIbquBx4SaYMCgDNE+O2B/IG0rUYW/+XGnZ6Lo2gErvBeCb7Y7nQOuTd1r65KG9KZFCQ6UBZ8FqV8Du3TXQUhilJ0RfH3QXWS2Ifi5miK4v9tW7UhB2gWhs8j+es2W7K7VrDM9OWW77/cOl4KvI1VnapwXT4gj4vHmw= Received: from DM5PR17CA0062.namprd17.prod.outlook.com (2603:10b6:3:13f::24) by DM6PR12MB3596.namprd12.prod.outlook.com (2603:10b6:5:3e::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 24 May 2022 19:07:25 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:3:13f:cafe::57) by DM5PR17CA0062.outlook.office365.com (2603:10b6:3:13f::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.23 via Frontend Transport; Tue, 24 May 2022 19:07:25 +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 DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5273.14 via Frontend Transport; Tue, 24 May 2022 19:07:25 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:21 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 06/13] drm/amdkfd: add SPM support for SVM Date: Tue, 24 May 2022 14:06:25 -0500 Message-ID: <20220524190632.3304-7-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 718ec335-643c-4a56-615b-08da3db8a3ac X-MS-TrafficTypeDiagnostic: DM6PR12MB3596: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: 6qc8n8htpyi4Yzd39kp1Nl9mZrm2fZZ55Tm1YITTwdE3Xg+gtiuxAvUZrxsHXIgGMZfYlxrgWt3t+fiIRWvUQi6635q4O3XcUC0ueQzsLJkpFuhT/owUDV8lFYiMZdI5LQydJMFqjZqgHFjTn5mN7wePRd2rWhUBqMoWXHbVazjDZ8a0/gSbMgnATIIKRZ0KySnrwZt8TLnGvgQk/Jxjrx+ugkZ6ohZgiK0ohtvOiKjNJ+r4+lrd/StJvrx+KSCOy5V/4U5WH/1fhLz/wLZYBZ/yy8hyPcfpXHMYj4b5mU22EhyJf0s/0s3T1Yqvqfx/LlP0EPBAHMVIclr36IQAptqaXKTj8SBCnwdyfAaSbW8gdNRpVefoPbFyfTfQrh47QGCbYVhtLNefG2uJSa0RuTGfnAaG5QUL/btsXflo0P2J/J3I5vncIQXbsjehPzuoqeraj84CpwKmYpF9/4GtnnrgTvlPSa/Lk3sHXk25MmqoU+KKvGcICB9Dvh+9jdBr0ASj+a+mZz9YofFK52Ui0Hs56al5uGgbx0bzIgXUokyttIKcKms4hkrOffDnKdpc50YaxdkJQfNz2n22Mlxr4ZpcWcjqGes505L9+pELRSIewgVNCMH/26puApPzpyIckcpGDGAsZLqudHgJ0l/tsO2Wd/CuOmwDsxXq18/rEhB8cenpZd17KGKY4PsCFfsDtB9CqpFM9O2MaN25SdGLNQ== 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)(2616005)(54906003)(6916009)(8936002)(44832011)(36756003)(508600001)(4326008)(2906002)(40460700003)(86362001)(7416002)(70586007)(70206006)(81166007)(356005)(5660300002)(8676002)(83380400001)(36860700001)(426003)(82310400005)(16526019)(316002)(47076005)(186003)(1076003)(26005)(336012)(7696005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:25.4512 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 718ec335-643c-4a56-615b-08da3db8a3ac 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: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3596 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 54EB880033 X-Stat-Signature: dazzsw6my7ggy45xppd71wdq4zqthys1 Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=J3tEjVyy; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf30.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.244.43 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Rspam-User: X-HE-Tag: 1653419220-54609 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 | 33 +++++++++++++++--------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c index 7e3a7fcb9fe6..25c9f7a4325d 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c @@ -673,9 +673,12 @@ svm_migrate_vma_to_ram(struct amdgpu_device *adev, struct svm_range *prange, migrate.vma = vma; migrate.start = start; migrate.end = end; - migrate.flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE; migrate.pgmap_owner = SVM_ADEV_PGMAP_OWNER(adev); + if (adev->gmc.xgmi.connected_to_cpu) + migrate.flags = MIGRATE_VMA_SELECT_DEVICE_COHERENT; + else + migrate.flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE; size = 2 * sizeof(*migrate.src) + sizeof(uint64_t) + sizeof(dma_addr_t); size *= npages; buf = kvmalloc(size, GFP_KERNEL | __GFP_ZERO); @@ -948,7 +951,7 @@ int svm_migrate_init(struct amdgpu_device *adev) { struct kfd_dev *kfddev = adev->kfd.dev; struct dev_pagemap *pgmap; - struct resource *res; + struct resource *res = NULL; unsigned long size; void *r; @@ -963,28 +966,34 @@ int svm_migrate_init(struct amdgpu_device *adev) * should remove reserved size */ size = ALIGN(adev->gmc.real_vram_size, 2ULL << 20); - res = devm_request_free_mem_region(adev->dev, &iomem_resource, size); - if (IS_ERR(res)) - return -ENOMEM; + if (adev->gmc.xgmi.connected_to_cpu) { + pgmap->range.start = adev->gmc.aper_base; + pgmap->range.end = adev->gmc.aper_base + adev->gmc.aper_size - 1; + pgmap->type = MEMORY_DEVICE_COHERENT; + } else { + res = devm_request_free_mem_region(adev->dev, &iomem_resource, size); + if (IS_ERR(res)) + return -ENOMEM; + pgmap->range.start = res->start; + pgmap->range.end = res->end; + pgmap->type = MEMORY_DEVICE_PRIVATE; + } - pgmap->type = MEMORY_DEVICE_PRIVATE; pgmap->nr_range = 1; - pgmap->range.start = res->start; - pgmap->range.end = res->end; pgmap->ops = &svm_migrate_pgmap_ops; pgmap->owner = SVM_ADEV_PGMAP_OWNER(adev); - pgmap->flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE; - + pgmap->flags = 0; /* Device manager releases device-specific resources, memory region and * pgmap when driver disconnects from device. */ r = devm_memremap_pages(adev->dev, pgmap); if (IS_ERR(r)) { pr_err("failed to register HMM device memory\n"); - /* Disable SVM support capability */ pgmap->type = 0; - devm_release_mem_region(adev->dev, res->start, resource_size(res)); + if (pgmap->type == MEMORY_DEVICE_PRIVATE) + devm_release_mem_region(adev->dev, res->start, + res->end - res->start + 1); return PTR_ERR(r); } From patchwork Tue May 24 19:06: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: 12860469 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 B7345C433EF for ; Tue, 24 May 2022 19:07:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E78138D000E; Tue, 24 May 2022 15:07:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C78CE8D000F; Tue, 24 May 2022 15:07:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FE948D000E; Tue, 24 May 2022 15:07:31 -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 626598D000F for ; Tue, 24 May 2022 15:07:31 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 19DAE35321 for ; Tue, 24 May 2022 19:07:31 +0000 (UTC) X-FDA: 79501570302.18.F4AEC24 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam08on2041.outbound.protection.outlook.com [40.107.100.41]) by imf07.hostedemail.com (Postfix) with ESMTP id 264DD400CB for ; Tue, 24 May 2022 19:07:20 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VjDER+jK5CBevpHDtXk5Q/3aJFcDLtLBCfihC4lCkNIprUR2XNORbH2HE3jZVtK0aY1G3Iw3mKfdaoggbOtlgKcZcvC74UgXsmqUSAR4GiE/9I1CkMw2uunSYxlKUcEE1RvLXfcHd3AEXeyLFfGe7hVINf1xEHOPfTfWZ3nUS7XUyGVO2urtzYLwEL2MboJe1FVOaROY9D8OIZ8sh7TdjYZFDskAxSaM8Krn2HlhonRE/SvgQAaxFOtLSm0Wa0pwE4zhcajswMzYmoF9xYVGbTSl1LT5/U01tiuvAsga2T1+QzjslgDtZ6+u+YXT81/LBz+mgE3KuRUCBtGHngvpOw== 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=JAdYs9J57gXwjmVF10dedQEInD8bXW3uKR7Beq9rtc4B/jsIa/EFfR0y0T5VsuwLMVGx11iu6oniky8SvGqRPZonGUQm++NdoNV0qqXItR4nqyo2e0FH8xVyhqGGjfu81Fo4wldMOG+lpOdhgHaZyIsHaDf3+dCA9NuTBdDeBpoxZRBTAPiSP4u5KTSqePjpjrDa2vJ3vHtb569JAVeG3Ye9CNxCgANSljx6VwyICuQCz/MWppNGS8mU5J4YhY7SvrGJzCsaMrNNMFnKVHkXMU3dcUFNsWM3sbIAxvYCQiWzNv2kVd3c4soOGAUZRku9dbI5apScDPOFvGA4hOuPhA== 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=lOHVT5E8U0Sgm1hXKo57MEYX4RfGoLKNS3yKvtZXlkE9yYYs8UxnXMdc3ARJ4o4iVeDnXHpe4BCVWi5scUVtH3vsdQHdxvtq7USCvN4XEmtfzYJxgcPtpTkJf38tJZUfHqQ0fFb82+iBbDYiBMfxksBBOnG4bg//QYNZ7JGctWw= Received: from DM5PR19CA0025.namprd19.prod.outlook.com (2603:10b6:3:9a::11) by BYAPR12MB2855.namprd12.prod.outlook.com (2603:10b6:a03:12c::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Tue, 24 May 2022 19:07:26 +0000 Received: from DM6NAM11FT034.eop-nam11.prod.protection.outlook.com (2603:10b6:3:9a:cafe::1c) by DM5PR19CA0025.outlook.office365.com (2603:10b6:3:9a::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13 via Frontend Transport; Tue, 24 May 2022 19:07:26 +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 DM6NAM11FT034.mail.protection.outlook.com (10.13.173.47) 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, 24 May 2022 19:07:25 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:21 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 07/13] lib: test_hmm add ioctl to get zone device type Date: Tue, 24 May 2022 14:06:26 -0500 Message-ID: <20220524190632.3304-8-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f0d5e536-efe4-475d-3b0b-08da3db8a3f6 X-MS-TrafficTypeDiagnostic: BYAPR12MB2855: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: KYuQLHIoQKqz09V3TRykMjBj1KOorD+mTe5v3NVYK5Lkfp2LLerhqYvN+3LH6GxKHmksXOD8bnbQSCpeD4jvV+ZEF8EliFd78wL9ceQhPCBAwwXS3zIK5RE6XsxSyzux9tj/YGVY0DKV6zzFFaEQPPE2V76zihevTkl0dpySmOcGgwHNvzoKdIRyWb+FN2J527XQPK4sF2ej4NgzF+TQjy1peVk+vccU3n+Rskgu6ZY04fy3Et8NGwBa87c4SJwhlpaKh3A3k/V12dysOTzgv/bHboQKmkkLWP8c39wMuMXU7+vEQ/+eYbzcmuOb46uWSMin8mAhckdQEgNFolahkMofPnA5m0c9jVhQXVc6u7Py8NscjoYaZ9EumR+htYRUf9Ex103l1pzcfOGRvo4NELrpX9Hfh6TT0zrrxtJHRbHw+n99LU7SABZkeBmBQ/HD8ecCOT6pVa9+wM3lvx5PN3TRWC59tJBZesns+I0F9SvRhOKEmSIHMCE3kJidZQ3Muoo8a6RyFloAG4xIz3Vdl593yuymbpr4Ah3icgty8rO0ZpO6wWjEekdNCHdOuKad1nbhOEATt4qbXQHndYw8r+IbJiUCuZH8Fd6mS6XWEX+5Hl5GFbNMZOB/sBy8u4KMh6Fr2hpLrXRlgXYctWrLtQs1tnLcMVYRVFs8Y/szWXp39o5+xoBz+D1ubap/31tTm75fVhJfc8YC1+ngmcrKlg== 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)(81166007)(6916009)(54906003)(336012)(186003)(82310400005)(36756003)(36860700001)(426003)(4326008)(70206006)(8676002)(70586007)(7416002)(356005)(316002)(16526019)(83380400001)(26005)(5660300002)(47076005)(44832011)(40460700003)(8936002)(1076003)(2616005)(2906002)(7696005)(508600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:25.9352 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f0d5e536-efe4-475d-3b0b-08da3db8a3f6 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: DM6NAM11FT034.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB2855 X-Rspam-User: X-Rspamd-Queue-Id: 264DD400CB X-Stat-Signature: g4iitygdnq7wz9kh9jo6aaa3dt3jmuac Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=lOHVT5E8; spf=pass (imf07.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.100.41 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam03 X-HE-Tag: 1653419240-704320 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 24 19:06: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: 12860465 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 E5C05C433F5 for ; Tue, 24 May 2022 19:07:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 627228D0006; Tue, 24 May 2022 15:07:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4338D8D000C; Tue, 24 May 2022 15:07:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2838A8D0006; Tue, 24 May 2022 15:07:30 -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 D18738D000A for ; Tue, 24 May 2022 15:07:29 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id AB1AF807A9 for ; Tue, 24 May 2022 19:07:29 +0000 (UTC) X-FDA: 79501570218.10.7E47A5B Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2086.outbound.protection.outlook.com [40.107.243.86]) by imf25.hostedemail.com (Postfix) with ESMTP id D7500A00C4 for ; Tue, 24 May 2022 19:07:00 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LfEffXtM68fLcX0EqAdEzXTmPJldm97Rc5WYkZh0ZO+NPOlWP5I+WV/rnZD3pKgGLU9/hfeVNS3cuQudueLA1nQUBJQc0RduFhIY2z+QsOvmJEjGtbw7NxHLwtau79ZvVQigyg7IoWNXln6fe9+g4FokcgWD8gPW+lI07JIL3QLAMNgwLoJbA1fJULyYRh9qUx4P/tQS/qSYRZm26MGED6WMsVcpXULLqHkgt9LWiCY/3BgxgPHdMmt1KTfB7lNkMUvccjvf0VkOw9tw3UXV1j0TIxljAGXbl5aBfdw83eqRJaFSiN8ckPHFtksJ9iGGRHQ1LRpfF9ZmLmohkLZXLQ== 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=eu7Zg4SoPxIM7Hdj1AwDsdIysL4EfxcsRHc5S0E07OJlzsfRzfGbCdwR3/+MAwt+clD/MBZco5BUA9eRuNw5LOPmJgRwZAwuqop0ei32XPtrkULJ5GetzyeJeF+hhAjbpb2JtQR6jpXRZ/mrJLrrG+eh3A97sgOcEeoO5Eum3DPeYrZAn2BZw6xqALLMk5ZPEvvWuKAlkm2ypTEzMHGDOaowpEZSLWinenl+9w56kTJCU2cgkmlJJrjFIEbCQnsFa1Jyp3T7vNczDA6GbhuB+/R/H0cbFAwPnb2wKMC3DnGXG++KRtWbP+r0oQEz4dmoJf/tW71pRl3OHp7IUR7fUQ== 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=FMux20/oti11pTFioDA5muuAYCIhBEGwrOt5lWOF/rA7TCgoloXWKtYV7pkmn+Jsu3zk6IC4E5W+4cQQpbD1BAnDoQFUTe+1L+Bd6IczygEk9Ve3OWugn3kAYT6DApT1XaN5DXuzT1OTMWpwnGPmyFSI33UJpNKAtAqgZtBCVCM= Received: from DM5PR17CA0058.namprd17.prod.outlook.com (2603:10b6:3:13f::20) by MN2PR12MB2879.namprd12.prod.outlook.com (2603:10b6:208:a9::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.17; Tue, 24 May 2022 19:07:26 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:3:13f:cafe::8) by DM5PR17CA0058.outlook.office365.com (2603:10b6:3:13f::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13 via Frontend Transport; Tue, 24 May 2022 19:07:26 +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 DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5273.14 via Frontend Transport; Tue, 24 May 2022 19:07:26 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:22 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 08/13] lib: test_hmm add module param for zone device type Date: Tue, 24 May 2022 14:06:27 -0500 Message-ID: <20220524190632.3304-9-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 798dd893-ee05-4c71-f567-08da3db8a444 X-MS-TrafficTypeDiagnostic: MN2PR12MB2879: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: RTWN7BjWmkVvqxHdXBRbKVJg1sNraz7E2SRDxbgjykDcVKGZZluXVn638X/+sUojYhmiu5K2SXAIb/ymFrvsqd1nLhZdMvPXgGPRNqyMmnO16y952R/xq8LFSeR5t/8fL4bSwrSuuniQkwopAcGY3R/G0cAUXd/H8IDxG1/5VZO+3e+qBmI4fdM8fqXBbVQUPnp/wiSyQEsdDOLBYH5eBO+e6bc7wIS9VaJY1S2yHq8vsg/Q821nursJ7Vb8JoLDo6bRz0twNa18hzblKnCT+SsntPRzABHw3VHUG8Wb1g0uf+vsAozqs1MhYwMwl5vDEHe6m3ialPg+ahZe/mQVjJ/YiaqPQj9DtTVlxemWcOXyl5l6dzLEknHQ2cR9ke7SXRPAtkgCGnLH8WNvI60Ty6Q0Z6n1clfNUzoestFNh8z2mJ5F8YucvcacOJKYS4mAjq4YB3bivMKqisqpZimnD/+cGAobdePK7XriLP35chXBy/m6TENi2rabhdBpxMjwRxRxZpOZjP7917thpN661s7O5MNi4DoUADouu7sUbgsG2VUnPIo/zxS2WPONr5kDd5pp1yMNaYSuJUCMqsnFXJlHaUlL/XK6dsG1xsj+Nvhre5N4LpDfsNZkWpHIQUrCtAANuyhoAemRywFA8nINtSV06sNH1TcjwYb+yl2PH31wMo3azEHqJJVbo+2WIhZf5H81TJrS/FsnJsbyJWS++w== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(36840700001)(46966006)(40470700004)(36860700001)(426003)(336012)(316002)(47076005)(26005)(8676002)(70586007)(1076003)(16526019)(7416002)(2616005)(186003)(36756003)(70206006)(6916009)(54906003)(44832011)(7696005)(81166007)(4326008)(86362001)(8936002)(508600001)(2906002)(356005)(40460700003)(5660300002)(83380400001)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:26.4512 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 798dd893-ee05-4c71-f567-08da3db8a444 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: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB2879 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: D7500A00C4 X-Stat-Signature: r4mysegzkgf9w8sr1xr9c9iim153zikq Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="FMux20/o"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf25.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.243.86 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Rspam-User: X-HE-Tag: 1653419220-10006 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 24 19:06: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: 12860468 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 14599C43217 for ; Tue, 24 May 2022 19:07:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BABED8D000D; Tue, 24 May 2022 15:07:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A45858D0011; Tue, 24 May 2022 15:07:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 55D158D000D; Tue, 24 May 2022 15:07:31 -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 288BD8D000D for ; Tue, 24 May 2022 15:07:31 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 00CEA60889 for ; Tue, 24 May 2022 19:07:30 +0000 (UTC) X-FDA: 79501570302.06.DF2B171 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2044.outbound.protection.outlook.com [40.107.236.44]) by imf19.hostedemail.com (Postfix) with ESMTP id C3A4F1A0038 for ; Tue, 24 May 2022 19:07:17 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IVcmdTA9boVxaKw+MXuSqdJs1aUX692MIb678ONfEnlOudCHgp4k75Tm8nyxqAIoSmEajxeqwXIXzOoU8amulxoY0dWaxgD8iK1Bwrw66w8Q4a+QIcKLtQbr70oEAZ82d5t8PWPDuxjAREp24csPmtZrJXtXroiVF8Mz/ANesHR6t/MKa5vzbuBz1dNGTp0iwkmvs5Z0GvL4z+JT86I1vcCd8vNroIvUxbu7Vk3JZTZWpuNXkJ8roWcirOxDi+afKiHfpUIxnf1BEW2YdpBIho5mHYipyvXywCt/p6NYiMR6iC7kuIHMlhqSnOQqWrd9nTajiKCtLCT6UhKzSPVgvw== 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=Qz2X4GBr2cNg+/sBaZtCl9OBuKq8eELpIsdI6SsmPv2VJO1cPyI675do0e2MK117JIthMo7SGcPM6iOdPipbke80wdK3uMoXwaHolPhqmHr/OcOAoNQBABMcfOWNQ8yg160x0s+Kgfue2/XI9170QQAB5zjjrCWr09ciWQECTPEok5Ut5WVpr99uHqt5otk0KPkygLkIvlYfu/FOaafyK8E++klesuAsbzqPjCrzajHBiOyAk+tZdcBsmeSzATciuv+32vTF/QCmz6nBrOnSFIS1fHgN/IspTfBpF9IWo8G3XZjvcyy6qGSUEKbELZxrjzKid9VauN4aP6ekSHWoOw== 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=r4kbTyyOzQN0PT7ckBFpVfp6+7BkRHq/BQ+wOOU+adm65upAB4vxqXCYF4GOldehb2KQzXdC+PtkOfp3Reu2Q3lkBTlFtdztXURNF0+eK+/uaIDYA3x3czILkOULXfpVoSDeiwCTKXLrgjo54zT59NtWwweHB3tTs6mmAAuIhbk= Received: from DM5PR17CA0051.namprd17.prod.outlook.com (2603:10b6:3:13f::13) by SN1PR12MB2400.namprd12.prod.outlook.com (2603:10b6:802:2f::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.15; Tue, 24 May 2022 19:07:27 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:3:13f:cafe::3d) by DM5PR17CA0051.outlook.office365.com (2603:10b6:3:13f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13 via Frontend Transport; Tue, 24 May 2022 19:07:27 +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 DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5273.14 via Frontend Transport; Tue, 24 May 2022 19:07:27 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:23 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 09/13] lib: add support for device coherent type in test_hmm Date: Tue, 24 May 2022 14:06:28 -0500 Message-ID: <20220524190632.3304-10-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 116aa58e-c29d-45ec-2721-08da3db8a4db X-MS-TrafficTypeDiagnostic: SN1PR12MB2400: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: ABJ5zuNEEq2Lj3QT0Yb8KMk7i/iSzg7nYs1O9HpPi36zkaMF3apR4wCbF5ZbG2bZv/5Kbi5YeRy8pBvxj3jc/P18Yv2EzKPMA6bhKEvTT3QIweZNL5hXtPNoqgoHLPfs6oRf9ZOudf7JzGUz60fMY3BeggCenJMNKnBGfV6US8CGW0WtBemXR4ak4eBzyBSN7/bzW/f/nlEmtJlixKcuvlcL9g0EPCVKthGCwgJXQHZtt680ahZ4XKA+g2AomEqphgngvM9XdwrBzJZCCL9bOYW6NMxQ+G1X4WzYjOMybkx8t/dHaCR8LsE9YCKET0nYHqoKhKVs99B9Za2LwSg/uOMKtkDIZh+4GrmjHFelwaWLa90nw6LD2Zm/ENj/dxMJtXOyDPERRO+X7NDohb8Z0DWj4tY+MQUYa0mSH8sk/oJzWuVc7SZxVY9bQXPZCyTHVF0qQ7OZmbRDSy3lcdgcpHMIq9vdN3Pcd3sOA0vkWFfzyplbIhs6r7iIAQxkOpde06LvqBPGAOMloGjL9DsFkaCYVbof4ISXpwvEqWAdPemZYarM9PxJ3NZ4P2tE8IPa1oV8gyu4qePP6DIBLCoNbNsFpg7nuAwCpeNSKlQ+KIe4zVDRuDmDHE/yDgys435Ti65a6Xa8E3m3/izojfnZ3lmoPFmMw+wSwDX8eHIsrfT3kZGe/pqjBhhqVUs1vaQLP/E7auH22zosBPHM2T8d3w== 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)(2906002)(4326008)(70206006)(70586007)(508600001)(316002)(36756003)(336012)(6916009)(54906003)(82310400005)(5660300002)(30864003)(44832011)(7416002)(86362001)(8936002)(2616005)(8676002)(26005)(81166007)(356005)(36860700001)(16526019)(83380400001)(426003)(186003)(1076003)(7696005)(40460700003)(47076005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:27.2793 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 116aa58e-c29d-45ec-2721-08da3db8a4db 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: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2400 X-Rspam-User: X-Rspamd-Queue-Id: C3A4F1A0038 X-Stat-Signature: rckdkkjx48knkcwrye4p1zk8rg8wkn49 Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=r4kbTyyO; spf=pass (imf19.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.236.44 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam03 X-HE-Tag: 1653419237-691642 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 24 19:06: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: 12860467 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 8A80BC433F5 for ; Tue, 24 May 2022 19:07:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 82C7E8D000A; Tue, 24 May 2022 15:07:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 75FA18D0010; Tue, 24 May 2022 15:07:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 44C6D8D000A; Tue, 24 May 2022 15:07:31 -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 0C04E8D000A for ; Tue, 24 May 2022 15:07:31 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id DB354807A9 for ; Tue, 24 May 2022 19:07:30 +0000 (UTC) X-FDA: 79501570260.21.653A829 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam08on2052.outbound.protection.outlook.com [40.107.102.52]) by imf31.hostedemail.com (Postfix) with ESMTP id 72B4F200CA for ; Tue, 24 May 2022 19:06:55 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Q2UtZ4nV0Lmc+7Z7jjKr7F74FPyaevYf6EuKwLTaYHHL6oL8yyWoLPET0X3wMWPSTtRg/gUaA2PsRN0Rslu6rzY/H73SVeTb4lfcBFYzXkhq2h07khxbyLYfumsdHumELAr6xF1rWCpk0HYecQdm+pah6tQjNMhI2cUhqEtpQSIgBcetgBE/bv9iRB/J+zc3hsSK+lShDjc6EIAXV0mXu9AsltLlnLRw1pp5eHR6lnsjiMp5EawFJR5Pc21D5mD8f0YDOoa5QA4H2tabjpeTNUbAlJFCJZz9rV5rGYWlvkFPa82KkbKvAOE1RXOZYGx8Pw7cFS8GHGhe8LLMPIUDYw== 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=cHFWyVCzlRED+zFsN0k8yr8bqtkFVZT/5TM6jG9p94OpPU5+KXO5d4yaImk1Bd2VOsW3P8GA/VBHjhUZaTddCWx27M+tcEjWY0URA9PYJLuQ9zsKkXfTaCPB3pjSw88cDxoJcPTW1uzcjFacYj5Yqh/A6ANY9edsFUHUKCAI0PwAoo9cQr08Q15/7gSrhUfZ6ksRg5DgpLY3N2e8i28pCPusl7RSZiTFrxsCtUnJRMkNrhRuYaL1weBaTyYp8VOYHcsPhYYx+f3T95unCNeocXLkDJdL7TItYlxNE+ssEKSrxOqGBovuwqGdQ8p2QPe7uy2OfgzIvjS3OnrtsRHvJA== 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=FzCcP31vXpUMAv8+GTbG1KDfwQNnUKtPLSIt18fLz6mbdxJq/X+XUDxHFeChFe4klj7jia6zN/2TYqanwQtr8aO/TjUdW7PezvNNdGVjV+a/6HwMErS4IjwYWbgY5TdIx7z9yZ4wHq2qCqG32oOSkC4a05JE9Jjg0ZXUvOfIRmU= Received: from DM5PR19CA0046.namprd19.prod.outlook.com (2603:10b6:3:9a::32) by BYAPR12MB4791.namprd12.prod.outlook.com (2603:10b6:a03:10a::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.14; Tue, 24 May 2022 19:07:27 +0000 Received: from DM6NAM11FT034.eop-nam11.prod.protection.outlook.com (2603:10b6:3:9a:cafe::3e) by DM5PR19CA0046.outlook.office365.com (2603:10b6:3:9a::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13 via Frontend Transport; Tue, 24 May 2022 19:07:27 +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 DM6NAM11FT034.mail.protection.outlook.com (10.13.173.47) 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, 24 May 2022 19:07:27 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:24 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 10/13] tools: update hmm-test to support device coherent type Date: Tue, 24 May 2022 14:06:29 -0500 Message-ID: <20220524190632.3304-11-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c320839a-267d-4dea-dc7a-08da3db8a4df X-MS-TrafficTypeDiagnostic: BYAPR12MB4791: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: qGdsCx+HCeOuZLNMtmQFuZvP2SbpXNPHq6tJzxpqCWLyFafTjEJq9DIuL/7t4db5HxTSgaJkI6VSTqQ+WW+9T06HKeA6Kei6kCLjEZYAWGx3p58GRrkSOcB1touzBg3d5YSOkFpkc7VlAY5GFMhEKZi11vQZMqULbZwzBLeeS2ArSTweWM5lDjLunuuxriczAxrCqWINFTa9Zc6buTCgJigjl6Q4fwfrmA82IMu+Z1cUK1oSfDhZxYE1rPHtEyfCo9ef+Q+c7NtbVLcii2nwiEgUHKEec/+6Js8IB6mAIX+rnj0iP82/ds+LboIfVg4a4fMgDXnO/dr7gLWV+loLVAgWg95VLPh/DKY48S3tlpX0Wa+Rgm+QVdE2B1Wu7kqFnFJn5FNmYd0gE/698zfwr7v4EcE4EO25Xfr1OQwgs0qQ3hK5y8he6dxgdRMFq53eQCEMs/la0kyF9NIbLsCz/+4nt0xzct2WiEjqKkJmo7hsd2o94BMcCRmdQwAKtKTARmVw3NtxPidCVjnXzfMET9FIa3nTU5pNcL3QK2pYa5Xt0Z7YXT5hp6tJhSf/rpFv6URDEG6B37xWlkeArAUxfIHxdszi7K+kmF12gLvN0NiSZNW//Vr9wYk6Q1VtsHLGm82o5U771h/c5RCURenMnEpMBYnML3+dRnMIbESq42ICCPiMIU7BgtgSO9Et4STVtsSoq0sMPo9xf2J4w5/n5w== 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)(81166007)(4326008)(86362001)(70206006)(44832011)(16526019)(8676002)(70586007)(8936002)(2906002)(6916009)(5660300002)(54906003)(40460700003)(7416002)(356005)(316002)(82310400005)(7696005)(426003)(26005)(508600001)(186003)(47076005)(83380400001)(2616005)(1076003)(36756003)(15650500001)(36860700001)(336012)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:27.4820 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c320839a-267d-4dea-dc7a-08da3db8a4df 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: DM6NAM11FT034.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB4791 X-Stat-Signature: i9azzips88o78i4sxpmnoz5dcmnhedpn X-Rspam-User: Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=FzCcP31v; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf31.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.102.52 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 72B4F200CA X-HE-Tag: 1653419215-117924 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 24 19:06:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12860466 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 31034C433EF for ; Tue, 24 May 2022 19:07:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 44F0F8D000C; Tue, 24 May 2022 15:07:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 35F9E8D000E; Tue, 24 May 2022 15:07:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ECDAD8D000C; Tue, 24 May 2022 15:07:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id CAC878D000A for ; Tue, 24 May 2022 15:07:30 -0400 (EDT) Received: from smtpin31.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9DB939E4 for ; Tue, 24 May 2022 19:07:30 +0000 (UTC) X-FDA: 79501570260.31.1F70798 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2076.outbound.protection.outlook.com [40.107.243.76]) by imf25.hostedemail.com (Postfix) with ESMTP id 88423A0032 for ; Tue, 24 May 2022 19:07:01 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UXUXkYQgnQrzoHmMALp/x8wde62uFkIJ/4+WD6dgvXMvB8rrZLGunWIR9cKuGnS8V3lZTpA0g+/2ACAPI5N33MeIXSyj3S0XFqFp10Zfyk0wWW07SRbxsJWCXvo+xyv+iVMncA6T//i47uByeOj9fPUcSO/OWaiJ89AxY9WR1x+ZdUuRN+dIyri/pJsVi3ZRTxgysKp98aou1GRC6WBsb26k4R4iqOMwTNpSWK12ytiuajkpYAolgx5oZkP/Peudxn7IkF3mILLWwH84tRUmWxg6u5qDE+UNZ7lP3GtFJZwVpuSvnCZI2Xz39NGkCChukbMDNSXhehr/u/oLuXqQtw== 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=KWdjNGfPL+1B2d9F7kU6A9ejzyPlXntDodT1C5OIl+hDX/hSj1GNqOZrzWx+Y5ZiXgjwDMbD6S5DczNSFwpVq+MUCgIKrFYlqjfXv6OZQkhWm0OJSgHVvST3z0L5JypKm0+tI4dP5sW5OtspFSbIYYrUCInNmiUOY62Deo9QM2xTbSvSBgJ2ZoPQYoVwUA0ReKrIODqymLbWyTA8hUN5LktXJYW4XZUwUcXIQDgGxoCGpLMpA6aCcIG9SHb+6B+cG2v0M23F8/DsG0wffNyJQW8emS6uKmDh4vVstyayAcR6hUKbYLpWTs1Ax8IdY7cypCAdknBIuIJl2PbC5BpDyA== 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=AGNc/Q2K1Hbe6s+fFNlfXRXN/Cqg90O/GZz1u4KAPoJ2EmYMtbdVjLpGT/QoQFndKusd9zA63ZwGL2J+ak7/aE8IgYfMGBrpu4RLHphjQQ+g/F5tFaSCvkBAimQqNVCxzJc2ymU8T6JfFYLTjogVe0OjT3jTpd6Us7YnJ8gnW0Y= Received: from DM5PR19CA0035.namprd19.prod.outlook.com (2603:10b6:3:9a::21) by DM6PR12MB4338.namprd12.prod.outlook.com (2603:10b6:5:2a2::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.14; Tue, 24 May 2022 19:07:28 +0000 Received: from DM6NAM11FT034.eop-nam11.prod.protection.outlook.com (2603:10b6:3:9a:cafe::97) by DM5PR19CA0035.outlook.office365.com (2603:10b6:3:9a::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.23 via Frontend Transport; Tue, 24 May 2022 19:07:28 +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 DM6NAM11FT034.mail.protection.outlook.com (10.13.173.47) 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, 24 May 2022 19:07:28 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:25 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 11/13] tools: update test_hmm script to support SP config Date: Tue, 24 May 2022 14:06:30 -0500 Message-ID: <20220524190632.3304-12-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c761fa70-9132-4c7d-5f60-08da3db8a546 X-MS-TrafficTypeDiagnostic: DM6PR12MB4338: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: JxgJ1r7W70lmTXsxM86VPYqOe504bot8uNBBl7W/Ya5Bm6C/+mUgLXZsrzB2xShKcMYKXiQlsL5LPnwC1+hdpp4+vnGWXj0emog0/T+j1hiJ2Kd7aQhetya7jZlpr+odbxp2eBBfADUJWKZZ5qpVeMQpVYbwtCkrN5nycZ4P9LwdZnUEFNxZINgsJtG0hNR7vc1B3JD47oCB7F0XHVhO3N3lp1gZNRk2SAVImHD3IW5jHBwhgLwVAm4DGvGGiZCEvPSrlwgGlKGa/FiArFjdJx4KMJUJvr0KGSfkNjPgRShpNfCJbpQ556IwpX8/++/7/3tAWIfDbWiDbumQDHhioTbQ4UAmSioR1hwWHb9mlx8sJ0HNizRjLL6hGRNelbAY01keqQtIFF4p7Ri+sO+Q+6/NCfaedf1LXKDxLBrZZK+rc9PcI7TOaPyTOFU9eprX7IvjAf3LRh711aAngmywt+R4LNHkifPU1Hjbpr/kzxY1XJkWQU7vsCx32TOHoJabGwonmp0rnXdcZWaUBcuHRKRKDTqbLNTdaNrcEKFiGHtrhQDnT13ziznv6xhHTB9w6A8XoNebTvZZQ4vwMFFpOb65/p4B+alNqsUxFyRhSmvNNSyNYg3eZy4adhWEJy/GUTQaJXLE5/zY9doa1LoeEE3b7Sab1yFxLI8mFSOVg4B6nrEGa+bp1RuJLCiDaxVI29sqWxW/AqZUbUqIT++PXA== 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)(81166007)(1076003)(36756003)(54906003)(316002)(6916009)(356005)(82310400005)(7696005)(2906002)(36860700001)(44832011)(6666004)(4326008)(47076005)(508600001)(26005)(70586007)(70206006)(83380400001)(8676002)(86362001)(16526019)(186003)(8936002)(2616005)(5660300002)(426003)(336012)(7416002)(40460700003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:28.1382 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c761fa70-9132-4c7d-5f60-08da3db8a546 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: DM6NAM11FT034.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4338 X-Rspam-User: X-Rspamd-Queue-Id: 88423A0032 X-Stat-Signature: tymh4k8q75pas3mrmjf6j7b5c7ruh7d7 Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="AGNc/Q2K"; spf=pass (imf25.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.243.76 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam03 X-HE-Tag: 1653419221-726873 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 24 19:06:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12860470 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 6BA79C43219 for ; Tue, 24 May 2022 19:07:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B4F78D0010; Tue, 24 May 2022 15:07:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C7B18D000F; Tue, 24 May 2022 15:07:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3796A8D0010; Tue, 24 May 2022 15:07:32 -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 209368D000F for ; Tue, 24 May 2022 15:07:32 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id EC406344D0 for ; Tue, 24 May 2022 19:07:31 +0000 (UTC) X-FDA: 79501570302.20.9585CE7 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2058.outbound.protection.outlook.com [40.107.92.58]) by imf04.hostedemail.com (Postfix) with ESMTP id 6D38140035 for ; Tue, 24 May 2022 19:07:15 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YrSmnYxZA4faeQ1uvIgk4Se4AWVPKamtp2jq37GoseBmvWyUF6HculERXZtVm8MYk33OSCeV355U5Qc/VeKIg+koRLlRmKC/rsMOrHhu9WgKtsYPfJqT319cnRJA551ejxrY+BO+wsAbcvk6GULU3VpbioHplUXZ3lrNItXv0sd8n/evEeLzmlygwiE9lM2dOMDlNWLSFRweQlFDWMnPNde/s95uZvuLtC9fkdbbPsSkElGynJOJx4xJB07X5BazTCMkmXeeTt1HtZNarynPU/rx+23aqTNFVyQZxt0ed/9+cKrTWAPCl5gKKRM6sCctx7R8Qi4D9PwJX373rmm4JQ== 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=Axb3vmCtZ0iJTtEDTXWB7rW1o6jcf23LVSdmkZL4vQtVZNvbusQkcRt9ussksK0f4IJv/ANRAIMf8BcGlaDYpMb96nAmU1JKRFYQJI3wh9EeG8oPRre/Bez4Hhgb9O7BjB6y7y1Qv1QDtbhNf5ghtdi6I/s4n928dBd640bzozTPk6N9MylTZhvPPhOo6HgU0QCn0T5bm/n6HPHQYR00ZJQWaj9Qjo7bXZJeYffmPVe0rteCkB+5ROu0ECeMJLnkT/rwG+doVm2mnVtMKVkVgvGTNTP6RynNfRUMbr/t9aWml4CJCBqI2/IaPx3mzwp8FHPkFT+JGm/3Rvd4rtlqwQ== 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=fDDMcnp+uvcPBJS4lJbgS1ixMMA3zjQ5WGY5ElGWtFaEcqnVHwT+R8z9Xv88/dyIM3uKbbjjtlY0WJXU/iB+rHccRJUFuwsSIKOC1rbl/ZPdHJik6ypewEp5Uw2awRUzEGYlPTHe8GagpUrhc/zGw/pUepSjv5UWXPjC5wqItTI= Received: from DM5PR17CA0064.namprd17.prod.outlook.com (2603:10b6:3:13f::26) by BYAPR12MB2806.namprd12.prod.outlook.com (2603:10b6:a03:70::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.14; Tue, 24 May 2022 19:07:28 +0000 Received: from DM6NAM11FT016.eop-nam11.prod.protection.outlook.com (2603:10b6:3:13f:cafe::b0) by DM5PR17CA0064.outlook.office365.com (2603:10b6:3:13f::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.23 via Frontend Transport; Tue, 24 May 2022 19:07:28 +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 DM6NAM11FT016.mail.protection.outlook.com (10.13.173.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5273.14 via Frontend Transport; Tue, 24 May 2022 19:07:28 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:26 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 12/13] tools: add hmm gup tests for device coherent type Date: Tue, 24 May 2022 14:06:31 -0500 Message-ID: <20220524190632.3304-13-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6fbb3b7c-ce5a-4e2a-0cf4-08da3db8a544 X-MS-TrafficTypeDiagnostic: BYAPR12MB2806: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: xLLS6WYHToXdJ0+aHNJG0XDOD/yYxcg7qs7YA22gLKXF0+hVemeseYJ+m86Vai+A3UBLZ4quzdCqRvrIhHgzdaNxgtMSG6CLIl4+6gT44Gk1gF/TaiRPCcnHNfx0xnDUtQFs2j2YHod4O/EEIfSgjswEYTMaCUzY+BrzkkbDsmBJIPpC6eYrsb5plNkqCl623w9IabjA7P2zanKwERlaX0hkQJm3nK8GNguQLHQkobXboZdmkvFhUdPV+dxYe5etQ4GfvQw3+CFixd0hwiLQpIN7lBRdj/ocytmnBC1kOVkOCEuVLiar9fBqqy4MWld6B9UJX0oKidthK39GTPFGIfQjsQBiHbb7SsKqUnp9gCVCjJwVt70Sgl5Pqlh6LWpjxHzUaytYxrTsXqCs3PdFS7/cvnLb9503XpfiK59POzt+11zxXlWcYe8Rh687XogXSrHO8nkbefkC/qWAOFqKF1Kww/TodVzJWAvH8gu0BY5vKmV/9E6hmA8OvDX9SCcFyWA1GmKXrAUj53DKR3LZCQsldobhRIzPPiAW/aS+W9Buy9ILHAyO04gK4PaYr1gwhGyJGK8+tWBupHvlzr3PR6iMqEpRJiVi1QMRrMXZRjPvkvs6T9hMwDF3mzfUNlAy7bigERrAwBBm1qKLwoFD+zSpfOXkAQn2vXUVnVN/mXXJFJnZsCBmFNzIiCfp3/jpGQ+9WGUOrWQMlnqmaSYUZg== 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)(36756003)(356005)(8676002)(4326008)(1076003)(7696005)(316002)(16526019)(6916009)(186003)(82310400005)(70206006)(70586007)(36860700001)(44832011)(40460700003)(8936002)(54906003)(83380400001)(81166007)(6666004)(2906002)(336012)(426003)(47076005)(86362001)(508600001)(26005)(2616005)(7416002)(5660300002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:28.1230 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6fbb3b7c-ce5a-4e2a-0cf4-08da3db8a544 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: DM6NAM11FT016.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB2806 Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=fDDMcnp+; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf04.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.92.58 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 6D38140035 X-Stat-Signature: mgkzm7aosxys9pg8jxtp7fqs6emd934g X-HE-Tag: 1653419235-294230 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 24 19:06:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sierra Guiza, Alejandro (Alex)" X-Patchwork-Id: 12860471 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 125B3C433F5 for ; Tue, 24 May 2022 19:07:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D5D118D0011; Tue, 24 May 2022 15:07:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D31FD8D000F; Tue, 24 May 2022 15:07:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA9818D0011; Tue, 24 May 2022 15:07:34 -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 A5E248D000F for ; Tue, 24 May 2022 15:07:34 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay12.hostedemail.com (Postfix) with ESMTP id 85AAB12080D for ; Tue, 24 May 2022 19:07:34 +0000 (UTC) X-FDA: 79501570428.10.F886A2D Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2078.outbound.protection.outlook.com [40.107.93.78]) by imf20.hostedemail.com (Postfix) with ESMTP id 607611C003E for ; Tue, 24 May 2022 19:07:18 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V/qPVSUT+kqbKGzWHZAQoaYVMOm/C6qAmu6/r0xY8osMGuWNIwI1sG5xMz+4TG4DzdAai9UgFlFqoJG37xbFs+oCB0eC57SSt1CWQnF6MSV9qt8LgKIgCEAqy/lGAw+hmbYWjHwajxJAondTzX+QZq4mNN68Qq1Qs3yqWma/arE1VJj8kXVJFb7MRbH391wJ2/yrR6D3QgR6pYd2ikOadlxtQhiYqVxbU7QFRutFKb+8g/RTDvWCVpVnQESUXVmQeQRKb6L5KX4hnt3GyQv99407B2Cq4hIaPyP1bRrZdksjMageZ618BEV6SJjdQOeSNJ+smleS9w5ETa22hMnYWQ== 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=G1V6WcyWm88/o8kFRRXAgQ/rEkGDNaC2VxPK09mVOrKn4AGBZ/74Q1HPygJ2hGFeUIM2EzHGJKVePeRiaw3NLwJ08+KmEjEIcn0bVhKXIUOuokKwJKhqRGrtaz0O/3125bv73phoS1ITo/m0VAsoLC6svPf5/tW8FzqvsKPkV5XS4AnQVIUVscdIfwSsA+O0h0Pn2hKa0JPyTphnb35IHKwBUiyxVUw00tk2wOUZhACjJS9HMvSSRKP0Q1EvqGMjrNLtvmXaQekhyQGuN3DSwRF3redoUDHM6YN+FgJAYNmaaX8AuUUk/dF2wfxDcI2bm++zQHnnb+uMa9SZoA2bpg== 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=KhD9nQMyj3Kwf3alX7fHWoFE7k8wszIRp7vTsBGI8WxDS7CRHz0SWKiVS0tJendO+XLmYk9DaTG55zDTLcw9RTipZdn/P6i198nBe5Fq3KQTWuCFEvSbq0QRLOinlcVDecxg1Ov+kVjziuoJ5m5q0vn6sXY+saMndZ4r/L9woLw= Received: from DM5PR19CA0027.namprd19.prod.outlook.com (2603:10b6:3:9a::13) by BY5PR12MB3842.namprd12.prod.outlook.com (2603:10b6:a03:1ab::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.18; Tue, 24 May 2022 19:07:29 +0000 Received: from DM6NAM11FT034.eop-nam11.prod.protection.outlook.com (2603:10b6:3:9a:cafe::52) by DM5PR19CA0027.outlook.office365.com (2603:10b6:3:9a::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13 via Frontend Transport; Tue, 24 May 2022 19:07:28 +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 DM6NAM11FT034.mail.protection.outlook.com (10.13.173.47) 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, 24 May 2022 19:07:28 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Tue, 24 May 2022 14:07:27 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v3 13/13] tools: add selftests to hmm for COW in device memory Date: Tue, 24 May 2022 14:06:32 -0500 Message-ID: <20220524190632.3304-14-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220524190632.3304-1-alex.sierra@amd.com> References: <20220524190632.3304-1-alex.sierra@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 34c3ce29-7eb6-4505-968b-08da3db8a57f X-MS-TrafficTypeDiagnostic: BY5PR12MB3842: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: DxS0EFk4k+7YLZyPStILHEuZChIfaI2TV90mRRtz9HGKkCdc8yK2JIxLgvV/czqhykp0xQrp8VtJRyGKZ1rFK3VxTiTzH2rigFTFJLn37fExMJjUUNapppekVAoEmH44wDI4KH6HQP7kYG/+2JbeQj2k1Hgyl4rPGH9yUZDYOOoEoq3Nzmqv6DSJh1fsBwc/pv/rju8ySFeYJNvn2hXi2Q8jyHqYgFuM+pW2FjTZuFaI1UJJSYchdEa6fwEJes4XuMeN1v8n5bicBFjEz6aP/Rc875xvaLecQ4AJOidsnaym55QmtGW6J7XHclR1/MnvOWDiV/1c5u+ziNmFETT35HIFBSGE2pk3okAt2I4oxgGQGAdCv5JVAs9FFYxoNYF+rODlHw5f4tkrUgsykpQ0X9OXlemDV2+3hMtJGkgce8qyEPRrgFVt9eJEdeT2agKw5hRHe/5EvwbYsbX4nUtBbjk58mWN2KVpHcMjo2CIQs3P+9ztTiE2W3VzVx7Rn67y3bZL0vzFcB52v4n2WmbxAR4IkDtE/kuOoYgVrsbj3iGzIPkQ3IrvUyh68yINnUghc3XEhr7FX81zxd675kDIpO5dBIottYAYZdVWlZO17dnplka6kLeJM1xbJg1rJmptJJuzPQnubtzhlUDJKKwa+chzlX4E7UAm5/rNRlhu7T/MC84ihHyNLY1G19v+2JcD0Wol2TfaCOdK6DNUlWeHhA== 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)(70206006)(186003)(7416002)(44832011)(16526019)(5660300002)(508600001)(70586007)(4326008)(316002)(6666004)(82310400005)(8676002)(54906003)(36756003)(6916009)(426003)(47076005)(336012)(36860700001)(1076003)(2906002)(40460700003)(86362001)(81166007)(8936002)(2616005)(7696005)(26005)(356005)(83380400001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 May 2022 19:07:28.5288 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 34c3ce29-7eb6-4505-968b-08da3db8a57f 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: DM6NAM11FT034.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3842 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 607611C003E X-Stat-Signature: wnrayb7i65ykrcmbr8noh5fjqb6x4fxa X-Rspam-User: Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=KhD9nQMy; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf20.hostedemail.com: domain of Alex.Sierra@amd.com designates 40.107.93.78 as permitted sender) smtp.mailfrom=Alex.Sierra@amd.com X-HE-Tag: 1653419238-875416 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