Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp701464pxb; Mon, 7 Feb 2022 23:58:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJxJD3ZnA35RRVp+bt3sdGQOaXZZx6q/ShZlUk+W0/01YeLt9Cr1khh76jTVABTS8tvBJ40X X-Received: by 2002:a17:903:244e:: with SMTP id l14mr3249065pls.148.1644307098186; Mon, 07 Feb 2022 23:58:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1644307098; cv=pass; d=google.com; s=arc-20160816; b=j6wCxoF0LI6Frclgnl31msUS6xvxoOTNk7tp1Ki7CqoGvmkdZQynpTLZcDJuGgH6kn 2+yQkbGWPpwYL6sN/Pw2P8IMtkgXMkzKtR9u4+ZrTu5Aq7GHsEyRyvBoCjkD7fAWQQlK FwVvsUejYu1r4dguluFWD8eQa5kYslYFOfAUExPVDN/ssg5Za3eGyrjkqyNLyYxjPb/r KKhyfvAm4aSpk1uK9qxTHf99ldQkDVgbRwICy0vC3qeQ1GSDn3u6K/l3oKSp/GJqjb36 E34C5FSxKhhbP90qQBMhXz741rBlvgwLO6D8auCfz4ZdgL/d1b6L7oex3dZYdZyja+4C HO5Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PVWwywowYnYpDCBBurDM6uN+lzL3XuJ6mzPfSe2PfX4=; b=wQILO1EMqHJrNn/Cp1UYh9l/LEse7O+MUA25Rz9FX1EbMRQOzxYrOm073Xx9jJ89oi RUk1GW8eZtDcJg/jPNf1VhNnPWSbBsjswQl1/ZW4PdOWmm0+VntHI+ZvbWTCetrWZ61B ykaSrdnRJ4bgGq8fs6KaOTkocEaJak7ERetsknaaSvkfOMwayMfa3WuLTqob4u5GLQsz x1y/bpfjQIAUngDwUUQWIaGoOc3DWhdB3LPGOQILy+xGSjLzXO0qHppCM54ioBWKgGwe KKRQR5LIFzbA8CtnpXlow9+hYtHM/09+GH5xiKp7PR0WYXfOLH9BcZqPP6F0msYayyx6 gUVg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=GU9JzCPD; arc=pass (i=1); 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=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bc11si11814933plb.199.2022.02.07.23.57.51; Mon, 07 Feb 2022 23:58:18 -0800 (PST) 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=@Nvidia.com header.s=selector2 header.b=GU9JzCPD; arc=pass (i=1); 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=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348792AbiBGGRj (ORCPT + 99 others); Mon, 7 Feb 2022 01:17:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351637AbiBGE1X (ORCPT ); Sun, 6 Feb 2022 23:27:23 -0500 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2076.outbound.protection.outlook.com [40.107.96.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBD83C043181; Sun, 6 Feb 2022 20:27:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QaT1Knlm5cChfrSE/P8R8yIyWi0QW5Ej+PFgg7asBjfNMtpDzJOWMEW3yl7PImf5eHYC7OcF5e3f8wAOH8ICwZEG3LGZOEHwQ/Go3xNkNvzFD3e7JG7S0PdcWEuW9PCiQZDwZMbzTtAoAZnVmPRkQ24UtVD5YSmxSOW7qVi1gSIC4P6lj6PuFsVLFBdAAqYOdwWJnQQtQaiJOMhpzpPl4X8vR2LkIspHpr3KapafcN1wlyazEIe93zExbN3Utf6VrOeEqxWz9viE4fQVb1b7jjjylGzQaKXgYseGrSEKbfIDLU/hpZIRmo/T0I1VP0LSFqrYU1dUoHwyvqiICXufaA== 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=PVWwywowYnYpDCBBurDM6uN+lzL3XuJ6mzPfSe2PfX4=; b=cpYHe/yItr+TygUmG3reynNytkkscMOnnlB2crNNYUHEaQRnp+BJxy/bQKoi1RkeP3Qj74eD2UfTEh/Hq3558z2gSB5pDl41UcBeZ+OmeVkbTG++IiqoTVc6PfE2dcjNMJkXD7GuM+uoO0ZNanXRe2z/bne3csmukcv8V90rT2q1KHqTmBuFqwT5OnBGVbL/n+AEWE0DQtAFRji3p04jcikuV7aPqgl7+m+boY5ayvEgX8R5Ot/pOfDQvksjao7a5EJGSPgvS+lyBkEIMpSIBkONjt7V23LHIhLIWk+0u3w7/aoOtAxMj7k6iekP0m3BbwhAp6RlEAh5rlIjlvDXvQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PVWwywowYnYpDCBBurDM6uN+lzL3XuJ6mzPfSe2PfX4=; b=GU9JzCPD2Xj0rm3FzrVXq4zdGz2hmg3magGfAIOdOTO1c7OU3E+RUxv3Abp2WNEdgxf3e+mKdF/Zfi9jxQ4z+7T1EFIgw3gHJSE6vrvN7aoC0NhGOzOPLVPPBZt14g1wCq4i/9V7KJEnfheYWwcYKm7p4aFkZ9LIT1Lcet/xEidlzrJABj00hcWhGpdLmhUHxmrg1NzlIwjxdVX61l6My13pxuOPrk2xTtDNwtcbQ1TSORgbF29MRHDkcTQs1PrnSkt/ax9wmEaScH9rEAfh0rkaSreL6y8GRaxste96DGYMEJX3M0Pi5tWBuk3P5d+x+bvzkDU8XshE7l7k5Db2hw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by BL0PR12MB4705.namprd12.prod.outlook.com (2603:10b6:208:88::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.14; Mon, 7 Feb 2022 04:27:21 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::1528:82a2:aa0:7aa6]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::1528:82a2:aa0:7aa6%5]) with mapi id 15.20.4951.018; Mon, 7 Feb 2022 04:27:20 +0000 From: Alistair Popple To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: Felix.Kuehling@amd.com, rcampbell@nvidia.com, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, hch@lst.de, jgg@nvidia.com, jglisse@redhat.com, willy@infradead.org, alex.sierra@amd.com, jhubbard@nvidia.com, Alistair Popple Subject: [PATCH v2 2/3] mm/gup.c: Migrate device coherent pages when pinning instead of failing Date: Mon, 7 Feb 2022 15:26:47 +1100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SJ0P220CA0030.NAMP220.PROD.OUTLOOK.COM (2603:10b6:a03:41b::6) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c4d3e71b-ed7c-4a72-af22-08d9e9f221ab X-MS-TrafficTypeDiagnostic: BL0PR12MB4705:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 59UOEl+p+Mu8YBtp58ed4Y/LbG2vJ/hJeud8REHr8t6SHY+cPfWqO3El9C/aP01vckJPKh6uCvf4A+SZcI+de/kVdwVgvk0ivLCLLaofPiWi/Qbmsl4KEbAmtpByrHJOc1TsV3ChE79rE9Jx/vkxAjs7kbzRlJ8SwaDVG+CI6IzVyw3Ly4cEjHtCfH0gJ6VE8MRd2G+b80c1cylds5Em7u4fvgUxfenZEtZDi68j8mICr9mAjd+65QspRApYOzhRZR18zepRCSlRdE1Jf5R9RzHY8ZBBYNQNMMWohCUmIIkSdB9+PO4H8JYT8VVcjOwiXCy6TDOS/LISpH02aq3HCIu+iaZB7WFolpzmx0dotEDOqD1kHL8Zcgu6n3C6zjAIXBPCEBM26bX7FHWxHCLuaAPHrnYZw3B24p4vjQKzHZfLWxRusl6sdDO7Gt3DSpHKy8xn/9WkYbRZKZheh+sutXwYbfrSvF+W9al730o9mpmvxlMiG69DLTqkP2laBR+8BDGK29oKoPDOYQ6/qxhgGeRP6paP+dKk5yzhbV5zaXKuMCTlczqXoEG6hMsMoLooV6Szn84HHAY80Y+Q9/khc5nQ4Ik2Xr1hxfxNnfpVCDxpF5QohoaKMNs5UjcGWvnIYWySnCuSS2HCyelDk1XTEQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3176.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(4636009)(366004)(86362001)(107886003)(6486002)(2906002)(508600001)(26005)(186003)(6666004)(6506007)(2616005)(6512007)(316002)(66946007)(8936002)(36756003)(83380400001)(7416002)(38100700002)(5660300002)(4326008)(66476007)(66556008)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?KWgbDTWqTXKMQ4UT35aTAWeJnmUvo7ADGd9oCgg25q+Z/HjX2yYQtgTsN4rN?= =?us-ascii?Q?bpiE16pSAuBtxF8ZP5K4fuklp1eOwyoxbLFyOHcH8blo/k1rKk0G/qIKxhFI?= =?us-ascii?Q?LexhpWFEOA9sgM7X2cJRdlYLHTUMPtEPlFacZbhVfVjb4KILTEAzgyOEvoIk?= =?us-ascii?Q?nR+qF5FoGoYTBmtvw75VIXAp7OIbD21pZLD42wZqXqD/dEVFzHAB6Uzj+fZN?= =?us-ascii?Q?gTWYVSq/sUVKYDuobbyTNibOjoQB1rxKNNcIr/fV6hDxjBsNlJ6Ave4G7ak4?= =?us-ascii?Q?zlqdaLwwtWXddFBfkyKlsBphn5IhT1qO6tISI5k4IenrStbCPa9NzkXk3J6O?= =?us-ascii?Q?5yovGUEbx3E/SCkKHCqiY+3bn8wx46+veP39aikW2iHwSRQdpMyJDvYd6b2X?= =?us-ascii?Q?Xt1+N633cafmjMci+cB0gM7jmfK+ghJVAfVXeKN0m1CBPXF6USpIZrI51AnA?= =?us-ascii?Q?BgQUHGlOQnrDzlHs6weVLXydRhqSXOjyZw7qQWC2NoAHsP9GdPqni6dAQQNC?= =?us-ascii?Q?c8tfh3rb66JCPon/FjxwrL7ZkmpriUI3NA58JvQ24ai/a0oThUkZ4Ix1tHpn?= =?us-ascii?Q?SMtIr4HE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ByR69NcGAN7hNoe7O?= =?us-ascii?Q?p/sETFTa7MSI4PkuqASVi4YMpEeEheJEW8LX5kN1g/TUlk6X/FTq/G/WBf5X?= =?us-ascii?Q?43A7rbNngD3GU94s1INUsLzX68VJkKkG+goePuMITfXN76orGJW4QDtLx4gV?= =?us-ascii?Q?pZwFekFLZ2+HfIAGXf/PlmU=3D?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c4d3e71b-ed7c-4a72-af22-08d9e9f221ab X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Feb 2022 04:27:20.6674 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ib1EhYmyzaWQRdiMVW1EVl954Dq78iUOuqeA5XTs9eaIjv7gdFyX/RnDB60J2tjVwGbV+ka0kOHU6x6qyNIxdw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL0PR12MB4705 X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_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 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 --- Changes for v2: - Added Felix's Acked-by - Fixed missing check for dpage == NULL mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 10 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 56d9577..5e826db 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1861,6 +1861,60 @@ struct page *get_dump_page(unsigned long addr) #ifdef CONFIG_MIGRATION /* + * Migrates a device coherent page back to normal memory. Caller should have a + * reference on page which will be copied to the new page if migration is + * successful or dropped on failure. + */ +static struct page *migrate_device_page(struct page *page, + unsigned int gup_flags) +{ + struct page *dpage; + struct migrate_vma args; + unsigned long src_pfn, dst_pfn = 0; + + 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; +} + +/* * Check whether all pages are pinnable, if so return number of pages. If some * pages are not pinnable, migrate them, and unpin all pages. Return zero if * pages were migrated, or if some pages were not successfully isolated. @@ -1888,15 +1942,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_head = head; /* - * If we get a movable page, since we are going to be pinning - * these entries, try to move them out if possible. + * 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 + * migrating page out of device memory. */ if (is_dev_private_or_coherent_page(head)) { + /* + * device private pages will get faulted in during gup + * so it shouldn't be possible to see one here. + */ WARN_ON_ONCE(is_device_private_page(head)); - ret = -EFAULT; - goto unpin_pages; + WARN_ON_ONCE(PageCompound(head)); + + /* + * 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(head); + unpin_user_page(head); + } + + pages[i] = migrate_device_page(head, gup_flags); + if (!pages[i]) { + ret = -EBUSY; + break; + } + continue; } + /* + * If we get a movable page, since we are going to be pinning + * these entries, try to move them out if possible. + */ if (!is_pinnable_page(head)) { if (PageHuge(head)) { if (!isolate_huge_page(head, &movable_page_list)) @@ -1924,16 +2003,22 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, * If list is empty, and no isolation errors, means that all pages are * in the correct zone. */ - if (list_empty(&movable_page_list) && !isolation_error_count) + if (!ret && list_empty(&movable_page_list) && !isolation_error_count) 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; + else if (gup_flags & FOLL_PIN) + unpin_user_page(pages[i]); + else put_page(pages[i]); + + if (ret && !list_empty(&movable_page_list)) { + putback_movable_pages(&movable_page_list); + return ret; } + if (!list_empty(&movable_page_list)) { ret = migrate_pages(&movable_page_list, alloc_migration_target, NULL, (unsigned long)&mtc, MIGRATE_SYNC, -- git-series 0.9.1