Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp5819080ioo; Wed, 1 Jun 2022 13:15:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxicE7SIcj0BlGPm0yjnO81ob/lFxJI8ZAX6amntsk8stgrLsgm+6T8UY7pR+CWvJHbRiGr X-Received: by 2002:a05:6a00:124a:b0:518:e5c8:ead4 with SMTP id u10-20020a056a00124a00b00518e5c8ead4mr39302393pfi.72.1654114558997; Wed, 01 Jun 2022 13:15:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1654114558; cv=pass; d=google.com; s=arc-20160816; b=iSWw0rPxr0p6053dZn2mfht8frgaxmcdKd90kSYuUxGFYbPMMShjMpO5vb3m0vH1lE YlNMuelY0NMI/FW11bEhOX9ybpG7McCcv+wQYsGU3HQXmxAZJASuuHFW4Lx5p8MxtrES +QyITE0+O0HMZLPL2uuZHvm4pBEnZJbWUrtJKMk3MMSj9flOUpY8nVcejrsv/588C7Yv hN60O7QG0XPhj5P4XHUWalstFnDCSHqBRW2EixLonpaqJ8eU8IaG6a6frQ6TUxztD0GT 4cT+kYSpm64BJ3IBZ8r3kC5jAQ+gMkSd3lqAdSqL9wkdo+bJM4YMsHhO458YxqWra9po EboQ== 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=X/au3voUTSWe7TnDakyMc0huLOQFLzS/24zd0f/lP6w=; b=p3bcJYkynAOqi2n+zBuwZC7tlw6YKs5IwtDFjqsLCg/EwHQyoWdH2m8xQZbbKG+0K9 Pdmm8rwfIZWAbhvQl+xU8kDCTewFhAfTMv/3FyihrvG7l8dYR4zEHqH0w6+oWOyDg3TA e7bkNXCHkgyS44ztLf6EqLvzXp5t8ML/bjqS+zIKWsx5bd/c/mrvl7J5hIYxzv3Nx8CT OjapRaeyT8ylCa3UFLuTtbPKV6I3r2ZyLgJbBtf5ZImYaU1NK2l2cfXWN/z05y64Yp48 fnDdQdpc3aNeRNfzjJziGQCo9CA2Hf1tkyitPCHdGCenMZ3TJYowGk2gaHJt/Ldpi53G /aJg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b="SgTbOEL/"; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=softfail (google.com: domain of transitioning linux-ext4-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id f129-20020a636a87000000b003fc0e8ae0c2si3120452pgc.719.2022.06.01.13.15.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 13:15:58 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-ext4-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b="SgTbOEL/"; arc=pass (i=1 spf=pass spfdomain=amd.com dmarc=pass fromdomain=amd.com); spf=softfail (google.com: domain of transitioning linux-ext4-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6400C23E801; Wed, 1 Jun 2022 12:27:21 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345943AbiEaP5D (ORCPT + 99 others); Tue, 31 May 2022 11:57:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345947AbiEaP4u (ORCPT ); Tue, 31 May 2022 11:56:50 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2073.outbound.protection.outlook.com [40.107.94.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F93DBC18; Tue, 31 May 2022 08:56:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NUR9/xF9ne0cpKhQrrj18sPG+Vw1tqEb6zEY/CB6wz2Ea+0jFjWq+EWaPatphTdRV7zJTvQG5xbhvjiPxCC+COEcTLoPj4HVxicdfzOA6Hql+BbqlbbSty4sqzbrJZ7zqTa4Acssm6uifJE6SUtv29x5TanIokZLhZdrtY2UfATEHYcM4qLekEtoX6F1r/GBxHeGuB5rwnBYcPwGSrdc+DF3mWhDgvK5JwzJaS7494YR6SMEUEBBfkGA9JhTymlnBuI1qqXehaNNiDCK8x0YnwDm2jpaLJy2+SW1Svxy21Lai0GTFfBeKzXcKdTVVMbsz9LqvzgblQu4D+HnbCmk6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=X/au3voUTSWe7TnDakyMc0huLOQFLzS/24zd0f/lP6w=; b=nQf51ywWIgP9xY4bWWU8CBnkoWTAOITy6nMFn5wzP8bijoTNu3FUB+N0AhKLqm3HDEn0p0LkfgfJlgYdV0TODKxH/1Hp1SKGC167IhlWGGygg4k1swSw2U/0otKB7tvuI9/QXQNZN5tpq1lNSsX8/bjHN7QEwETY8xhk2OkCKAkB0LX/b6dBqMMBNXiIHiEG5qZi+7gNwcnV3rJddFtWQeFkSPo6TZMA8YWGeeY/VuXQZjelBvyLx+RlDrmsB4iiaUhy2IK++R/Y3N3Eebdnje8u19gh2B3lGZXkxhsdqX81S8rhPID/gURjbow4JO9FyMMecX0qFGjqsAf1+NF7DQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=nvidia.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=X/au3voUTSWe7TnDakyMc0huLOQFLzS/24zd0f/lP6w=; b=SgTbOEL/RBGQJ2BIDmNhsE1ISOSPzFTO2L03IkwKF7yKIaNbq78E6rKjLd21Js/oPRCfmtJ7yAtXtAfJ6yogTRFN6gX32fYHRku2jZXOX4T1CUbJ78B2ss2kciRewh8dUK5KLskuO0WbSVWzLlKBb1LvtjvIDE2YpZ6l2Rq+mJM= Received: from BN6PR16CA0048.namprd16.prod.outlook.com (2603:10b6:405:14::34) by IA1PR12MB6163.namprd12.prod.outlook.com (2603:10b6:208:3e9::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.19; Tue, 31 May 2022 15:56:46 +0000 Received: from BN8NAM11FT043.eop-nam11.prod.protection.outlook.com (2603:10b6:405:14:cafe::70) by BN6PR16CA0048.outlook.office365.com (2603:10b6:405:14::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.18 via Frontend Transport; Tue, 31 May 2022 15:56:46 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT043.mail.protection.outlook.com (10.13.177.218) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5293.13 via Frontend Transport; Tue, 31 May 2022 15:56:46 +0000 Received: from alex-MS-7B09.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.28; Tue, 31 May 2022 10:56:44 -0500 From: Alex Sierra To: CC: , , , , , , , , , , , , Subject: [PATCH v4 05/13] mm/gup: migrate device coherent pages when pinning instead of failing Date: Tue, 31 May 2022 10:56:21 -0500 Message-ID: <20220531155629.20057-6-alex.sierra@amd.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220531155629.20057-1-alex.sierra@amd.com> References: <20220531155629.20057-1-alex.sierra@amd.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: dcd7553a-5d7b-44ef-8ce3-08da431e2a5a X-MS-TrafficTypeDiagnostic: IA1PR12MB6163:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OT85iq5/0PCVBLUEEJoFBTwgun9tKd8IQoeQsBrrUXGxeYo3YQuJTQsfUScjYiEIG+gS/M/G83SFp2/dXGQj8rTHmY/2LHbephIBH7xlzjskLBQb6Uk/Q5/Q2atBO0OEyhhlhI7Vl2IFQeJ7bx7cbCfVCidS/7Q/DIzqwYFH5ia5Tf7/I4fCNgpjhhenp3rOArsONy3XJziPUfZ7OuV9XJcjXf7davfCmG9JZOhfFVDvR/ZbLgwh1WrzyqILIeUdXj74Fs5gJ1nqQhAxr8T2fXF6cFwBOEBX9io5Nr8qUoGEv1YX5fLvkTIYLEnsf+cP/zhOSQYJyLKjyQqNz8xGSobHxuMl+eyt5eS69SLq8Qqe6TLYVPngKa/AYinTyuiQN+dAwkHxkkCwyXT5r8cGiOZAIg+fCHuY5LJ1wo6wAEOJaicRZ1qE9AwiMF1q6pJsUz36eSqOy0k9u9Xu8Y2s2AFwlTNNm6prZ3HfbNXJ1bMD0EWIF6xj1mNkaKqMoM/NVoBksQ/gmiI0JDK92fAopmVkLX4/SE/S/BXQhep9VRjMrNcT7eB1hiMat7NKXHDeaAD7wMBwNOZYPnLWvt4G7oitiUrJnAB2Pf5cmwXBuO2y1qQNboQy96O9nG48T2KMRGc5Ri6g8ALDwGx2rapXOvxJ0AFbgyDPuZ4rAZMQjhwMotj3g938NIAhGv7CUaV0l1fAJ5Cb6whEzoZSbjESeg== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230001)(4636009)(36840700001)(40470700004)(46966006)(186003)(16526019)(8676002)(40460700003)(2616005)(36756003)(70586007)(70206006)(7416002)(86362001)(26005)(356005)(316002)(1076003)(4326008)(2906002)(81166007)(82310400005)(54906003)(83380400001)(508600001)(36860700001)(336012)(426003)(44832011)(6666004)(47076005)(6916009)(8936002)(5660300002)(7696005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2022 15:56:46.4378 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dcd7553a-5d7b-44ef-8ce3-08da431e2a5a X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT043.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6163 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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. Signed-off-by: Alistair Popple Acked-by: Felix Kuehling [hch: rebased to the split device memory checks, moved migrate_device_page to migrate_device.c] Signed-off-by: Christoph Hellwig --- mm/gup.c | 47 +++++++++++++++++++++++++++++++++++----- mm/internal.h | 1 + mm/migrate_device.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 5 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 48b45bcc8501..e6093c31f932 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1895,9 +1895,43 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_folio = folio; - if (folio_is_pinnable(folio)) + /* + * Device private pages will get faulted in during gup so it + * shouldn't be possible to see one here. + */ + if (WARN_ON_ONCE(folio_is_device_private(folio))) { + ret = -EFAULT; + goto unpin_pages; + } + + /* + * Device coherent pages are managed by a driver and should not + * be pinned indefinitely as it prevents the driver moving the + * page. So when trying to pin with FOLL_LONGTERM instead try + * to migrate the page out of device memory. + */ + if (folio_is_device_coherent(folio)) { + WARN_ON_ONCE(PageCompound(&folio->page)); + + /* + * Migration will fail if the page is pinned, so convert + * the pin on the source page to a normal reference. + */ + if (gup_flags & FOLL_PIN) { + get_page(&folio->page); + unpin_user_page(&folio->page); + } + + pages[i] = migrate_device_page(&folio->page, gup_flags); + if (!pages[i]) { + ret = -EBUSY; + goto unpin_pages; + } continue; + } + if (folio_is_pinnable(folio)) + continue; /* * Try to move out any movable page before pinning the range. */ @@ -1933,10 +1967,13 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, return nr_pages; unpin_pages: - if (gup_flags & FOLL_PIN) { - unpin_user_pages(pages, nr_pages); - } else { - for (i = 0; i < nr_pages; i++) + for (i = 0; i < nr_pages; i++) { + if (!pages[i]) + continue; + + if (gup_flags & FOLL_PIN) + unpin_user_page(pages[i]); + else put_page(pages[i]); } diff --git a/mm/internal.h b/mm/internal.h index c0f8fbe0445b..eeab4ee7a4a3 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -853,6 +853,7 @@ int numa_migrate_prep(struct page *page, struct vm_area_struct *vma, unsigned long addr, int page_nid, int *flags); void free_zone_device_page(struct page *page); +struct page *migrate_device_page(struct page *page, unsigned int gup_flags); /* * mm/gup.c diff --git a/mm/migrate_device.c b/mm/migrate_device.c index cf9668376c5a..5decd26dd551 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -794,3 +794,56 @@ void migrate_vma_finalize(struct migrate_vma *migrate) } } EXPORT_SYMBOL(migrate_vma_finalize); + +/* + * Migrate a device coherent page back to normal memory. The caller should have + * a reference on page which will be copied to the new page if migration is + * successful or dropped on failure. + */ +struct page *migrate_device_page(struct page *page, unsigned int gup_flags) +{ + unsigned long src_pfn, dst_pfn = 0; + struct migrate_vma args; + struct page *dpage; + + lock_page(page); + src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE; + args.src = &src_pfn; + args.dst = &dst_pfn; + args.cpages = 1; + args.npages = 1; + args.vma = NULL; + migrate_vma_setup(&args); + if (!(src_pfn & MIGRATE_PFN_MIGRATE)) + return NULL; + + dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0); + + /* + * get/pin the new page now so we don't have to retry gup after + * migrating. We already have a reference so this should never fail. + */ + if (dpage && WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) { + __free_pages(dpage, 0); + dpage = NULL; + } + + if (dpage) { + lock_page(dpage); + dst_pfn = migrate_pfn(page_to_pfn(dpage)); + } + + migrate_vma_pages(&args); + if (src_pfn & MIGRATE_PFN_MIGRATE) + copy_highpage(dpage, page); + migrate_vma_finalize(&args); + if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) { + if (gup_flags & FOLL_PIN) + unpin_user_page(dpage); + else + put_page(dpage); + dpage = NULL; + } + + return dpage; +} -- 2.32.0