Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp536140imw; Fri, 15 Jul 2022 08:07:03 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sBGVR4QxKjJnzhYfJ/k9qwcT8eWso54XmHz2/j05h1NOSYL83RwZXzMcEieRBkzRdRsYgz X-Received: by 2002:a05:6870:ea90:b0:101:fb35:883d with SMTP id s16-20020a056870ea9000b00101fb35883dmr7877873oap.23.1657897623272; Fri, 15 Jul 2022 08:07:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1657897623; cv=pass; d=google.com; s=arc-20160816; b=p4tQaZWJ+izVVbwMT1dX7xoPYoiOviQ2IiDh91CxZ0pYJpZcrrC7qTIooD8TT7ItBB v9s1hXtXezvbaaVHqD90768WVmBgEsmnm4oZLdfeHzvzSfm0ky4ZVaNnsOuUjX313ck3 PDTsQoTWWmNXkGOImHdNTEfVljfNIIyRPC9zzigoEOXqkkuq/b5ttRxvoSeTZZEL/pT9 FfbuyXSpt6a9zWBJXsD/pw3HZPBWJVzH+QHnFBWbX/wq2AqYg6y5xCNujhPWJZMrmhqU jVJHgaTUb/hWSlieHCwpEsNVgczBrOSj8UazMVVYD0DEAi2txfBPAAhlKpULoCJb0PAM KTCg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=W3l0gEneWCJT4Ri2gf2TD+v66/MIF7719Km5cR/hjO0=; b=bxPAkIjW29XMgJ4qzfJPNCGLVYaYke9i2B5CiRpWunCBoKkWBIfXK3PgDhqoy0N+P4 iUzjDCD9llvx+U+BJY9b8zNANCPcy8maWZCffDBH3OG13QjSFHG3C5/h4VnZQlFcb+Yc 2HhQRo7kVHlLz5XNKwDyTd7UE7+MdMq4xpdVP/xNuSbj5Q0xt8WUZU57mPBrZHyIh/3h 8D++EM++qb1jIdRM4e3jTnHs88QmPt3wwr7G6laDiLwpqQDZOtYv2IZvYQVU5Me52fo2 cL4HtqyvNHmIUuQnqY2dUXv9U+HXAM70l2TzoF9ZT38GQfHDer2u/7wONe16PTXatzpH ahCQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=nyJwpsWR; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r204-20020acadad5000000b0033a1755385bsi4099227oig.103.2022.07.15.08.06.27; Fri, 15 Jul 2022 08:07:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=nyJwpsWR; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233125AbiGOPGG (ORCPT + 99 others); Fri, 15 Jul 2022 11:06:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233387AbiGOPFt (ORCPT ); Fri, 15 Jul 2022 11:05:49 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2069.outbound.protection.outlook.com [40.107.223.69]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CF0D491C7; Fri, 15 Jul 2022 08:05:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RuxfDi8WJjxQr6f+IOcXOi0gzWpwqBAxG6LW+7k9C9/R7Nhg/5jQGBpZ9/52qtYEuTOeFlbMKSUL5LCIjrNigeTbWJ4tcUIv9u/IuMz0OUbo+XS4QaibfCnfXzZdrJ9E/Fl1ye/PodfWDhjoZF7CMXtysqjhGa2xrCHDp09D+lsjNICXrD6MPqujkTxKYbrp3Z+6doWFLE74f6Lnrk3fknLIa8/g739SGYyy0Ez0gw6qZxtRtKyY3dkU+uCS7yXcf5cG6l1dpzIc67x4FbeEnj2bcR8C/Ml7IRRZtRMG9XNVsC29ietKIvF11nA+EnDJZIC5XgwU+U+Cnep0BOdUyA== 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=W3l0gEneWCJT4Ri2gf2TD+v66/MIF7719Km5cR/hjO0=; b=YaENBHD/sAkJg9HXvHkPOLvpg0w9ICjzRtKFkA4nPHh7qnPy25p4pNH6/Ed4FKUN5tLp2E+eR56EU7K4PUtnFD8bpqPrQ2bk69Y7f2Tv5HGrw5KtK1II6VJOVqt84QgjnAQoTvBrsGFvK6MH0cg5Y2eQG7vPK84DqPDa0HLzNqEGx1GrZyUKCQZnJaa8sKBdAjxj7gF3kuPW9QsehI10lmPaULYvfNgyEQWMcl98HowbNljpAUbPo2Zbs9wWkleXP+coPCtzHdBz5+xbWviL+V1QExqJGxvBEFQEs4OuvbQMvg4p+wc0IvALrTvNkfYwPIFB3dp/rLqTPuyDKzpfGg== 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=W3l0gEneWCJT4Ri2gf2TD+v66/MIF7719Km5cR/hjO0=; b=nyJwpsWRmjn51UnkJaii6DfqQ2vgHxwT8gpPV1BVD9XcGNicr30szpnKzdO+QQ/JF0QlcpF4UM2nZckAscgIeiYdtQRXah4N3+06ZJrwDQhsF5eyVwAnLCTCk/VN6IEjYJMeKworEaYVOuqR8RcDj9EQKxgipjruK7Uaku/cRRo= Received: from DS7P222CA0005.NAMP222.PROD.OUTLOOK.COM (2603:10b6:8:2e::17) by MN2PR12MB3630.namprd12.prod.outlook.com (2603:10b6:208:cc::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5417.23; Fri, 15 Jul 2022 15:05:44 +0000 Received: from DM6NAM11FT045.eop-nam11.prod.protection.outlook.com (2603:10b6:8:2e:cafe::4e) by DS7P222CA0005.outlook.office365.com (2603:10b6:8:2e::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.17 via Frontend Transport; Fri, 15 Jul 2022 15:05:43 +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 DM6NAM11FT045.mail.protection.outlook.com (10.13.173.123) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5438.12 via Frontend Transport; Fri, 15 Jul 2022 15:05:43 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Fri, 15 Jul 2022 10:05:41 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v9 06/14] mm/gup: migrate device coherent pages when pinning instead of failing Date: Fri, 15 Jul 2022 10:05:13 -0500 Message-ID: <20220715150521.18165-7-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220715150521.18165-1-alex.sierra@amd.com> References: <20220715150521.18165-1-alex.sierra@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain 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: d3452268-56ce-42ce-50d0-08da66737d88 X-MS-TrafficTypeDiagnostic: MN2PR12MB3630:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MXxToRlP+XuoGQ1xwl0Wp0Hyf0j5KsWqDggjqCP/S9tQuSXJN2OIsf42dKc6+ehXN6QPZNmj7ULfYxhBBNeYiu+LB+GWusjb7morTAtkDpaPA8UH7AlfGsd6H7/SWWBOnuextJgXRSV/KwATUsXYYs984/xdbtzmf7T4wrKBfNzbZndPkd8/T877QAn/YvLTaFa80dvCUpqBT2AV7406zNSrWhalF2+yx2jWeZ8BegdtIPFNrcQccGZmxnCzLiZ4KYl6g06o7tbk0X9gR7VfDl7ZvC7h2uz2IUb1PpX+HnQFIX4OA2DettoyMBarFIZdz8iTdcBKdzwPcIcgzy3eFJxzclin8oqbUnQ+J8gjemAzoPsUJoY56avh+ts3ldhMTkcp7XmnT1dnLWj8uJBg6EKsNYNDNLeqMQoy0g8+qH+ejcmNbiuQ/b92MKSC4sRsY04a2LifXJ4qVW6P4ezliOg9XNgroK5dr6988Nap2d5T4ACRf5k6nAXgGUJurhW1ITmWL7oJZJ6Uqpu8lSsByxNLeqO6/CD8Jm3IFXj2c1suT8i819QlzwP04K+Le6bXZlyFUVvjb7z1cK2jealMOcQyTzX691mYUbl7tqzCjIn/OIZ9k7a/5yKjX19ABEg57iwrpoBProvIm0WAmTKsUpmsjwvg8I8qF3yV0JkaEh50SIGCBjmYhaCDd8uXXQYZJiIJsakAMQOlFtwJMqswHSSUBP6++CoBqSSmFrrNeK8d9Zq+gkJeG+JHx/zVPUQRDqeh6h89lkVrCm4sWfxLGWY9xrP2jtpZOG3oAFEl9WIa4v9GyaAdxT1ecExTnVUthO66EH0ejfpn2DKBWh4ecs5+DeMftZQd+qCy6AuFDkE= 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:(13230016)(4636009)(136003)(346002)(39860400002)(396003)(376002)(40470700004)(46966006)(36840700001)(44832011)(7416002)(5660300002)(2906002)(40480700001)(70586007)(316002)(8936002)(70206006)(16526019)(478600001)(186003)(54906003)(82310400005)(26005)(6916009)(7696005)(8676002)(41300700001)(2616005)(6666004)(47076005)(426003)(336012)(81166007)(82740400003)(86362001)(83380400001)(356005)(1076003)(4326008)(36756003)(40460700003)(36860700001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Jul 2022 15:05:43.8610 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d3452268-56ce-42ce-50d0-08da66737d88 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: DM6NAM11FT045.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB3630 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: Alistair Popple Currently any attempts to pin a device coherent page will fail. This is because device coherent pages need to be managed by a device driver, and pinning them would prevent a driver from migrating them off the device. However this is no reason to fail pinning of these pages. These are coherent and accessible from the CPU so can be migrated just like pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin them first try migrating them out of ZONE_DEVICE. [hch: rebased to the split device memory checks, moved migrate_device_page to migrate_device.c] Signed-off-by: Alistair Popple Acked-by: Felix Kuehling Signed-off-by: Christoph Hellwig --- mm/gup.c | 50 +++++++++++++++++++++++++++++++++++++------ mm/internal.h | 1 + mm/migrate_device.c | 52 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 7 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index b65fe8bf5af4..22b97ab61cd9 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1881,7 +1881,7 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, unsigned long isolation_error_count = 0, i; struct folio *prev_folio = NULL; LIST_HEAD(movable_page_list); - bool drain_allow = true; + bool drain_allow = true, coherent_pages = false; int ret = 0; for (i = 0; i < nr_pages; i++) { @@ -1891,9 +1891,38 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_folio = folio; - if (folio_is_longterm_pinnable(folio)) + /* + * 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)) { + /* + * We always want a new GUP lookup with device coherent + * pages. + */ + pages[i] = 0; + coherent_pages = true; + + /* + * 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); + } + + ret = migrate_device_coherent_page(&folio->page); + if (ret) + goto unpin_pages; + continue; + } + if (folio_is_longterm_pinnable(folio)) + continue; /* * Try to move out any movable page before pinning the range. */ @@ -1919,7 +1948,8 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, folio_nr_pages(folio)); } - if (!list_empty(&movable_page_list) || isolation_error_count) + if (!list_empty(&movable_page_list) || isolation_error_count + || coherent_pages) goto unpin_pages; /* @@ -1929,10 +1959,16 @@ 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++) + /* + * pages[i] might be NULL if any device coherent pages were found. + */ + for (i = 0; i < nr_pages; i++) { + if (!pages[i]) + continue; + + if (gup_flags & FOLL_PIN) + unpin_user_page(pages[i]); + else put_page(pages[i]); } diff --git a/mm/internal.h b/mm/internal.h index c0f8fbe0445b..899dab512c5a 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -853,6 +853,7 @@ int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, unsigned long addr, int page_nid, int *flags); void free_zone_device_page(struct page *page); +int migrate_device_coherent_page(struct page *page); /* * mm/gup.c diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 18bc6483f63a..7feeb447e3b9 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -686,6 +686,12 @@ void migrate_vma_pages(struct migrate_vma *migrate) } if (!page) { + /* + * The only time there is no vma is when called from + * migrate_device_coherent_page(). However this isn't + * called if the page could not be unmapped. + */ + VM_BUG_ON(!migrate->vma); if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE)) continue; if (!notified) { @@ -794,3 +800,49 @@ 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. + */ +int migrate_device_coherent_page(struct page *page) +{ + unsigned long src_pfn, dst_pfn = 0; + struct migrate_vma args; + struct page *dpage; + + WARN_ON_ONCE(PageCompound(page)); + + 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; + + /* + * We don't have a VMA and don't need to walk the page tables to find + * the source page. So call migrate_vma_unmap() directly to unmap the + * page as migrate_vma_setup() will fail if args.vma == NULL. + */ + migrate_vma_unmap(&args); + if (!(src_pfn & MIGRATE_PFN_MIGRATE)) + return -EBUSY; + + dpage = alloc_page(GFP_USER | __GFP_NOWARN); + 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 (src_pfn & MIGRATE_PFN_MIGRATE) + return 0; + return -EBUSY; +} -- 2.32.0