From patchwork Thu Aug 18 01:56:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12946608 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 13397C25B08 for ; Thu, 18 Aug 2022 01:56:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 792358D0002; Wed, 17 Aug 2022 21:56:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 741F26B0074; Wed, 17 Aug 2022 21:56:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5BCC38D0002; Wed, 17 Aug 2022 21:56:53 -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 4D06F6B0073 for ; Wed, 17 Aug 2022 21:56:53 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1C02B1C5E33 for ; Thu, 18 Aug 2022 01:56:53 +0000 (UTC) X-FDA: 79811049864.03.379DC52 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2048.outbound.protection.outlook.com [40.107.223.48]) by imf10.hostedemail.com (Postfix) with ESMTP id 64A65C01C7 for ; Thu, 18 Aug 2022 01:56:51 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AhMNc1p9e4l238oJEqg4jcLPp3us2/437PmnUVmRA3Ui5AkrwlM987I+WYv4feb6PPTZMCyfo0sugYZoBKrRTGwFDALFpBquGUa+DjfBkZmEI33QpHYbDj51tWRwfuY1r6sQtpN4XFrmA2UBTNBBt4ehy1mBc4gB8y11p6TW3iwoD4z0F7HKUkIHrtaBibIm0uUFiSG72GNno4OwA/o1tblGT+l/kOUdMSnGTKuNtpapJZPFy2Ht41qlNnopVyfooohozooqgp06jGes22HxpInGHj50sCVElrTgo7TpZ115kfMVeVfyx2xCOTRWL0bzug+/tI/Uf63Syu74935rBQ== 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=76IQdPubhMWITmpzgq+05WjE3ITgDSyIf3LKiC1ibmU=; b=UkgnkZvNyF0TMj8YtG4vVg/pp/8h00DVR7tntXyL0GyLttGMfB7mamoYQfccLIklDsmRNK6gA5vOyiDs8VMQrzgkRJT3ZJXShld7+iUxC/nzMcH6P5y25ys3MGVUZ83xIbgazviepfWDmTX6fq8FFZ265vaswEjRv5NcUo8shD13J4PqrkwhpbS70ZBFMrqbz54giRx8tmvNjO1O8W7plr3BBda72aHm4Mb1qZ8cPJ5mnXwBfzb5qABMM6ftqlSDoJO6JOuqmPZ9su4xRIU9+1Sb8Rr80XPZmcgVGpVLv+nbLO9ShBv5qTBdf9Vlj5cD5cn2qY942YXj/81Nlal/9g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=76IQdPubhMWITmpzgq+05WjE3ITgDSyIf3LKiC1ibmU=; b=UZ2zgG+fHoxWIyGOsPkZXeO/9pFi6IF/+VygqaSEpHRJCbxnlcD+cMtqmqxIzVpLCnrEzYUaMBRnwU2ztPH/lvO8oRYByqTA9gM3mfCcW/vlGRu0HdfP6i/OxKO1gWP7R6WbLAgqo8F/N0HD677IJWkg7cnTYlPoZDZuYjB815XqDrnv5z+fgHyWxNv7t0tRllvPz6mBdnEiNxuGqA6w7x+7xB7apucAmdffPiWBB5F6Gnme8u2sf3dkFiqvgqA0hOa3zkzyme5RgcjxulQXCp3MT3kPi4Jde4YZx//GluAAWCnaS0jDcWImYJacG5Asas7LapiGx7wid5X4XXpj3w== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by BL1PR12MB5286.namprd12.prod.outlook.com (2603:10b6:208:31d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5525.19; Thu, 18 Aug 2022 01:56:49 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::eca6:a4a7:e2b2:27e7]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::eca6:a4a7:e2b2:27e7%5]) with mapi id 15.20.5525.019; Thu, 18 Aug 2022 01:56:48 +0000 From: Alistair Popple To: linux-mm@kvack.org, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, "Sierra Guiza, Alejandro (Alex)" , Dan Williams , Felix Kuehling , Jason Gunthorpe , John Hubbard , Logan Gunthorpe , Miaohe Lin , Muchun Song , Ralph Campbell , David Hildenbrand , Matthew Wilcox , Alistair Popple Subject: [PATCH v5 1/2] mm/gup.c: Don't pass gup_flags to check_and_migrate_movable_pages() Date: Thu, 18 Aug 2022 11:56:22 +1000 Message-Id: <487960bf67c7273ff5606c76f73bb51271bc7b90.1660787700.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.35.1 X-ClientProxiedBy: BYAPR04CA0036.namprd04.prod.outlook.com (2603:10b6:a03:40::49) 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: e53ff4d6-ba70-457c-b645-08da80bce955 X-MS-TrafficTypeDiagnostic: BL1PR12MB5286:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UX55uI7KKIsvWzb4FHZ2lZDP4N2dV9cjrNH5q9xrjWpXJlqFIhHlBWyH2GViw59OVfqmJ4mmjjVKPf9FjqJHqkDwyDZTms4X512IHXdYkWz/OwrIUSlow8zqaUVyXPJ0ft/s4mAJrJo3x8YanD1OPFGul72r9kHa9lFSnlOglP0ieoWYNd3Hx0t1UpwFh0Gr3ixnx1a528fCGNw0bT6Bc8Nf7w9Pjf4cRAwRWct0Ue9+Bv73ZC7cgWEFgv64/bYjagMjqrDL5n/MY+amwxAcYviEY7XtlxzHTYvDaX25RDWVAvkfA6cP2Qn9PnfZz9vWtawKPuOMS23mqpYJ/OsM1YMxvROiHG+LB24b20y8NP5TaBxWA7zhY25BEBc8bnBh0jmMDBV4l5352GhqycPI52kjn0sOxjdiAsDYA7iVd3jpX+e0sRPYXVfiZ4K714M3jjy0KY5TWo5RurIFJAlxJckoJHjyTv9tf2I2gy/NGwdPKAlx0F69LN5ufF98ECJcUazhrxJfUjuiHT0EkOVFAzasBbl8WwMH2U0heyF5fJ440v9z9ltGfEB+c84XVDcXlXSU3yHG/K3yfCcOaknvSzBMN/gvh9HTdIaorUore7LUmUjytJVJHE7/vEyZ2rVmmGBMZY6ct5+qykkWuVDUTuMLPaWMt2MFOuUimf737oOoex/FafxWvZ40R3sLMt0zZ7ZEr9Xu7GqZdpFWJ0L4MQ== 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:(13230016)(4636009)(396003)(366004)(376002)(346002)(39860400002)(136003)(5660300002)(36756003)(478600001)(7416002)(6486002)(107886003)(316002)(6666004)(54906003)(41300700001)(66946007)(2906002)(66556008)(8676002)(8936002)(66476007)(4326008)(186003)(6512007)(2616005)(6506007)(86362001)(26005)(83380400001)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HXk0dVkvz7xIAbHRkSjIgVXneuvzTfWUdr4NbyWqqIay/t3UkNZNxbC96nMBDVqq9l+SjKCduG44C6aXWjMipe4LNkRHkAhEBWl8v2Qj2kxkX76GVwiEIsy7bKAGybLEJEwMYfAQUVA9Q/HrGrUXdI8pEgjuJeQyCuUxu8LD940WohpjpIoCQ1TNv8KngoUHIxeAmK2yOoLbEDKCzvmzqCs87SOjhhHWW+oNxDI17S5/Qyk7o+AWg8IzjW2Dn3ZIY/ZeHpt4jk6e57I5/bQ0Jo27OMT+3TEktVl22Il6SMzv97DQVMY2CMsWDVVJ4HsEriOz4bf8OloOFDDd7zpUO5cJ/QAU3AqqXvGGBj6LC9c/dujL2E5PGkwEiX8/kbLtJwf3cCq4YqKolOZqTxwP2Y4C2KI/RMahfuE8+4n974TALH0EDnqe41oOttT3vfruXviGKkd85oUEcSg/nXnBZxNVxK9HdSVJfXhk61dC+NryAecE59B4h8Rex0i2EsOvoeoVqPO04xF5XHeyvqWwF+JHdiPif0PiBz1XKXREyFNSpgSkPbFb+RJ+gYNxzYaw+YNv47eTGOqcoQny8XQMUBlWPVRYpNAgh3F2ObEOgV9LLjK3AX0pjQyx51BHq0vHFXsnpgubEVQEkUhP8CXj9yp82OgOFCJPJv8eQJ4z0P/MTR/72pCHYPQI6Q0+qIFNjTiWX3vTEKSmfcp1EWMeW1PlHywsRhdCWs0tiDgz1eUlFI6ibdP445aEoaC9CRxoXX53mmwiNwsPkeRB1gYZc38vIBp0rPtTrDCiFHS0T0E6LikSwN9UncVNI2cNkDGZ3JQaiZQfFgjzSQsccmXj2fFitluEsYP3G/o7TxdOUtn24q+r89lHVc/nwDammrjG8W7Ml8WwxN/ui2vj19w8UzUrVj9kzFBUdrnPAbsDQjdpoFLXKjYxMLnJZHyP26kDJrOYVJ9wT+dTKa3CHxIcSqIL10xyCHAC+OGJkF+nHE6jlom/K+kNp4EIDrLQW073cEYkKIPKQzKYSiL46fWJcdFr3+k4ahtBnLMRUVsVRuVa++Cg0+FYaF+RY3IJzSWqp+U0fGXpH1gkavaFWiNbntj5f+gRFKsuUOJyVOpKWcQlGYtnAj99eKRfIODky1rkc9RFp8cBog2PfBd9dFbUR1FMjMp77AWwToHev+LMh/HANhYI3ySSVQZMd2yT9/d127bchHdy+546hb26eiYWJJy60k56DiLYlqkMxW8OPC3o7I7eFGhRk3rB+UzENRJnuAZANT2UGZDcCd8+9n3U8dnZSgyS8ZgKkoms+QND7ZulygPGk48WXfxXnKXySaYC2XXV5ScfciVJpHUeGNL4QepJQbPeRWIMkSBIzqje4I/h+7wPN0UIKpLQ9uTD2sWab2JuVYVId/S3TvexfGutFrCvfkOVhAZXi5/SVORv7DXHUEcAjfz0qI9LDgMCSbo/Mm8nwWode04pVo1M68ewI6U5OEHW4MyqMkkC9R8G6zLEG5NPnkAzjxzeRiIbm2/6BPIY3J8+fTPSCEHHAwMTGpCUQF8PgpFqQmBi9ZrbfNY/s+BzkKFwSQiPJiiFzGRa X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e53ff4d6-ba70-457c-b645-08da80bce955 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Aug 2022 01:56:48.4980 (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: NrzoU1GmGql4u7cEBfCOzz88WICsGnlruuzSgTppYOIwAj9HpvaDl2prUBgg4p/EcltM4kHQ9pb5wfDvA/xIHA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5286 ARC-Authentication-Results: i=2; imf10.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=UZ2zgG+f; spf=pass (imf10.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.48 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1660787811; a=rsa-sha256; cv=pass; b=da7IyH+Fij2fmBvMpdsv4jX3b9N2lxx54DTnU3AvMGxD5zGmE3S6PAqfkn0UdfxwA+y8EN lmPHqdh2wLA18oi0T0OFDuirDOd1GQXZwQUlwPbO9Ynp6936gAwjM305s7Q39qSStESwc1 V+UUqszRXgpYdb7tdj/flLKWcQOejhU= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660787811; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=76IQdPubhMWITmpzgq+05WjE3ITgDSyIf3LKiC1ibmU=; b=f4IsQiF53NW110NX4NpypUezgevy0HsRGV+149uP4IuL0jdw7UuN9q62yIBpdeOkkAomAT 0G2sBWqUrx5GUFH431w0K5jR9pAGsk28yvcVknTMIpCmwFjgme5sGk+08g1I8o7WpZ7pmH AjEVJ4FIDefm3LssRSAsF0z4nrAdL+w= X-Rspam-User: X-Rspamd-Queue-Id: 64A65C01C7 X-Stat-Signature: h9ydirhc87ednh1jbjmjq7kc4x1e6s8g Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=UZ2zgG+f; spf=pass (imf10.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.48 as permitted sender) smtp.mailfrom=apopple@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com X-Rspamd-Server: rspam11 X-HE-Tag: 1660787811-466084 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: gup_flags is passed to check_and_migrate_movable_pages() so that it can call either put_page() or unpin_user_page() to drop the page reference. However check_and_migrate_movable_pages() is only called for FOLL_LONGTERM, which implies FOLL_PIN so there is no need to pass gup_flags. Signed-off-by: Alistair Popple Reviewed-by: David Hildenbrand Reviewed-by: John Hubbard --- Changes for v3: - Move WARN_ON() out of loop --- mm/gup.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) base-commit: 360614c01f81f48a89d8b13f8fa69c3ae0a1f5c7 diff --git a/mm/gup.c b/mm/gup.c index c6d060d..a2baa8b 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1907,8 +1907,7 @@ struct page *get_dump_page(unsigned long addr) * Return negative error if migration fails. */ static long check_and_migrate_movable_pages(unsigned long nr_pages, - struct page **pages, - unsigned int gup_flags) + struct page **pages) { unsigned long isolation_error_count = 0, i; struct folio *prev_folio = NULL; @@ -1941,10 +1940,8 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, * 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); - } + get_page(&folio->page); + unpin_user_page(&folio->page); ret = migrate_device_coherent_page(&folio->page); if (ret) @@ -1998,10 +1995,7 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, if (!pages[i]) continue; - if (gup_flags & FOLL_PIN) - unpin_user_page(pages[i]); - else - put_page(pages[i]); + unpin_user_page(pages[i]); } if (!list_empty(&movable_page_list)) { @@ -2023,8 +2017,7 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, } #else static long check_and_migrate_movable_pages(unsigned long nr_pages, - struct page **pages, - unsigned int gup_flags) + struct page **pages) { return nr_pages; } @@ -2047,13 +2040,17 @@ static long __gup_longterm_locked(struct mm_struct *mm, if (!(gup_flags & FOLL_LONGTERM)) return __get_user_pages_locked(mm, start, nr_pages, pages, vmas, NULL, gup_flags); + /* check_and_migrate_movable_pages() assumes pages have been pinned. */ + if (WARN_ON(!(gup_flags & FOLL_PIN))) + return -EINVAL; flags = memalloc_pin_save(); do { rc = __get_user_pages_locked(mm, start, nr_pages, pages, vmas, NULL, gup_flags); if (rc <= 0) break; - rc = check_and_migrate_movable_pages(rc, pages, gup_flags); + + rc = check_and_migrate_movable_pages(rc, pages); } while (!rc); memalloc_pin_restore(flags); From patchwork Thu Aug 18 01:56:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12946609 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 BB0CAC25B08 for ; Thu, 18 Aug 2022 01:56:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4FAD28D0003; Wed, 17 Aug 2022 21:56:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4AA6D6B0074; Wed, 17 Aug 2022 21:56:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D5AD8D0003; Wed, 17 Aug 2022 21:56:56 -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 1F3D96B0073 for ; Wed, 17 Aug 2022 21:56:56 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B6AB5A0533 for ; Thu, 18 Aug 2022 01:56:55 +0000 (UTC) X-FDA: 79811049990.01.C364D24 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2059.outbound.protection.outlook.com [40.107.223.59]) by imf25.hostedemail.com (Postfix) with ESMTP id 3DB3FA01D3 for ; Thu, 18 Aug 2022 01:56:55 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WVNG4okNsePZmOX54J8f7RUotDuVRi8wx8PIV+GW8vB7wAPEcZuD9D0FUKs8wHHHkomG6GL/RsgJ7qqOOonmVjql26XDCPnxs+28KzyrecapKSdySbxMedT4zkWJfw7YUM3yhthz2hvL6QxNMjlM+pp8LIHML+5GYQ2250t6kWYx2aBRIDV/yYE2lJwITtNVbBdxNfyC4zqpfTxJd38VhqTlz2DtLIU+XnhNkVMgOq2FRZbgWJJh7L8d+ovXlJsSy0f7Kh/llvJclm46RQ7OB2CA2Ja633xxHHxtDg0lEJHQO4y94xuEiuvIRMv1IW4cBSdbJwLc7AtkPPf3+9x1bA== 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=MCKeJXLsIngQdXbGadMhinw8sGoEtHj2BAFX2EvUDKA=; b=NzTXyb380DNNDseQ8qaT4uhuPUeo6pn1+GpklCYYcDJmkqj2er43xOzWvjRXE0PRnyZknmhkLvDCy5R4GNLVaiBcNz1fKgW7niVnodFbgxi6nHenU8JnF3zthUjMDVdzJc3+02ZZqRTOTb4HPr3t47jW9aDruMWVdoZGfpoin2RZkhUVizNhlKy9r92nWeqDzIDI4B9TzE0zS8PU1mqUH+e1pit7GiSQCRsLsuTV/NYr6ReRHptT54EWlgaSUo8AzbnuvZpo7X/KSHT4sS5991cI/PiTHKrkhyKnsXlyWQ9Q+4zKjeqQyj+qcta60hWfymXOjX/sNJGos5yvl3t0bw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=MCKeJXLsIngQdXbGadMhinw8sGoEtHj2BAFX2EvUDKA=; b=FrfsnxdY4qsJQtFYCImMCpg5O0TkXtLZUeDQTCEpGmGdHka4jYAoAF3+KNkJqnFZZV0XCi1lCWOUqaua19bOJOzsZieivDK68tKXDblsNHVlGv63V9M+zQpL5RHROHQsuyLMkSGlkdf35y/wYWl9kDTt7PmCrJBtuZ+xRqCaBh2WkIcE9tpwRQpM6UfZRqPwfY8T4MtVKYug0s/r7g5Xo20kAWA7jnZHXZjZ1lkttSIsvl+pLmvx4QNYwe6ELz0Ch508cDjoWgKjI1eqOG5iNcEUMDnb9YiHR3DGeAZeMEVjsYO9d6ce8dTxxcXRM1rPI5zo/W0vGDPHsljHPY1rpQ== Received: from BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) by BL1PR12MB5286.namprd12.prod.outlook.com (2603:10b6:208:31d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5525.19; Thu, 18 Aug 2022 01:56:52 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::eca6:a4a7:e2b2:27e7]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::eca6:a4a7:e2b2:27e7%5]) with mapi id 15.20.5525.019; Thu, 18 Aug 2022 01:56:52 +0000 From: Alistair Popple To: linux-mm@kvack.org, akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, "Sierra Guiza, Alejandro (Alex)" , Dan Williams , Felix Kuehling , Jason Gunthorpe , John Hubbard , Logan Gunthorpe , Miaohe Lin , Muchun Song , Ralph Campbell , David Hildenbrand , Matthew Wilcox , Alistair Popple Subject: [PATCH v5 2/2] mm/gup.c: Refactor check_and_migrate_movable_pages() Date: Thu, 18 Aug 2022 11:56:23 +1000 Message-Id: X-Mailer: git-send-email 2.35.1 In-Reply-To: <487960bf67c7273ff5606c76f73bb51271bc7b90.1660787700.git-series.apopple@nvidia.com> References: <487960bf67c7273ff5606c76f73bb51271bc7b90.1660787700.git-series.apopple@nvidia.com> X-ClientProxiedBy: BY3PR03CA0030.namprd03.prod.outlook.com (2603:10b6:a03:39a::35) 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: 883484d4-0758-4124-01e2-08da80bcebd7 X-MS-TrafficTypeDiagnostic: BL1PR12MB5286:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RXQSEss5xgqGUzrf8uthJfuPt5RLlX0vlGQH4Ji/yfAzeitIz3F8gKU4GTDebsegpKF3UFxS+/7N/rOQ85btv69yBa5hnZ9mYmbouKguRESo0t4VJe4O1pTts2h/85Rvy/ZeycAlWYWL34ztaEVOu0AtsoaKOUh29DIButeONFbngS49ON8ogXuN6Vo2aDacWCuQhxR+yL6QFXAV2By4ItHJlNf0YOpjTTSc2DkRP+9lRRT+RKAkT20q4pDq5UkVaID8IIDet5spz6aY8v968MUpaygGG/hZLLrMMv5q7DhhA+1/un5bu65MXMLhocH4vwvLRHXARi2RSSYg0hV3UmMS++ZibatCbBaFWKOMHw8OyhRILr7GMXFT5ps9OhAHGIFr9ztdAzFnIOssUZ1ac1LgPAzX3yO1KNrsk+XQqrvh0N8aslLYX7itLd5n5DOkqMNAhIHUTgZjItSn4/J7ot/J9VrGSxw1uUKTgRtK3i/KuHbiSpBML+HJ0p2dPbMw0hOl+3axbARIO1MhDLWezIr58/Qd57kUE30eydlD3K5V779jFETsyb446CYWjuPrfGaPzt+ZcJUGxCUHkt3bhDeLAGofsZCUlOfq7iyekRruktl/Vt+Hw0hZyvn96hiCsPKjitrrdKlTC/dHl7aVFc9nuZMIvbdTV23ib8GCfbtqLuKYNB/NhaWBIMLXGVxo5t4aCDg5Ah147o8LWF+jgcHjR89Ld0x80L8udW5iZlNd0cr8aUPwh3iPG6EdPnIRd8Ay6RkiUK11vHKCF9mYBH8OhYfGtvVSwICJOumseow= 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:(13230016)(4636009)(396003)(366004)(376002)(346002)(39860400002)(136003)(5660300002)(36756003)(478600001)(7416002)(6486002)(107886003)(316002)(6666004)(54906003)(41300700001)(66946007)(966005)(2906002)(66556008)(8676002)(8936002)(66476007)(4326008)(186003)(6512007)(2616005)(6506007)(86362001)(26005)(83380400001)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: K/NUkaP2eQes2gC8qU30DcOxpD0qFDQo0mbOeT918a+zllrOv2oVPeNZ7kqG97rTY0Q/Ta5xSjPahyFU6V4EWlm2i4BZFhjDoVHZsFll9o6md01VRUm+bj/oDj/bOpWVCCYnrB0IQSVCkNTHKVsMz4K0GWk2YlED3ZUuT5vTNjVmFbveH032uw3lLYV6WctV48QToHCuPoKS/JHltUIeuWrLGzLtHz033aynRpHrc4Q3KGCEVUROxsNoXbowvyD5tpPq5pwE8hTh9IVcHLpEZSQI5lb8zJq1REcOiJY+19+BwKjIeNd17xgn3DImOSaJmcIx6G7rO9WSJninQbdOBF0kHiQXL8MRsIrpBKv7qjecJBbCTS/moS0CDa4YMlFXTKd4PWzyQdLcWHY5PROzrLM/GTPNgbhXJJ9uSLwm7N0CDwhsfNvPn6MoHojx4ciKKAwtpmvcpAvhF1pXKNL0AYllS68AvXB/HJ05GaUKDPCIrVWvPcHqcVrOug8ZQVEV2MA6lBxEMW00fBq793Tc9sT5dinmq2wV0uNBQu9FiFJaTi+FZPlmPlQ3Zxlr7imb7TUnJ27ikS46TZ/kr/FnUj/9aEDnw/45myIFjwlukkWTmoq7ep/OOyADJKL4Ye9rid4fcKrt2dHAvBtDRJ7m2BykaFlgIUILOE+ZWFjcxjKs4Hrys8RZwfTHrHmY/cU64ByTwizq1DTJYRJPLdcdOMNPRHjo4C2GYuQXRO5wC++un2V5Sd9tDaAPC8Y2S0K3XsaUqLpicUL2X+Ax50V6TyiYHJ4p0dhiZ7dVTfZHUHAYXlo/QfPrYNkkE8GvhviUmai29i6IaP7D6lgnxl2iiAbaSws7MjIpS58zu/VLH30SJCqlH4kU2ssiPGA4ojm6IcI3DannKd9wqsSQ7arflVhGXrrRCRp/rvkuzNRS/aSOyBqL03xOGgr7qkTQXfAvHFpWzM5bcKeXYGDgG2L4GBkCSO5O8ZT9i0QMMN8HjnlePX7MsYIPoH24UyJnoOXvsfj+DPTOry760i13C+ahHXIsfxda/GtqUCIYZwG/aKO+nFGg88Y3D+gMQB9wJBdwxYXswY35X0ck3FyKiY4NQLRrnGDwPFyHA7S3nxioOflqO9WjkNdSgFn92MDVp16KFeSFFVHWCabx7sPIOTNcwa1xIlfxZMgUuNRJmzkGSkTVlhaJP9oXwLT9dFZGy/zWAc0ChpM7LjzsZ9/5zDDTYzINHpQdwhkHRbYNe/8rZuIjm8Uzc+Vcga0EGbM3r32VZJGrj9ZCQyGLDzlA2oeWX4rW1a67BzgKfdB6FiJnLPbaJprQ8nAcQBSy5dDqpar9mjOLDN2NoqxClj5w3Ubz/W14fa95c6wZOPCM0fsknubd4tv/nklDDesK/lTZuxBYVj+zAiSSG/Iu2RfOjYb9vD9lgJh0x+w87e+gXUuYwHGVsDKzHBILmTI1iW8HWAN+hIA8j2oeJzRbWiBIOBnMfvBveDlL8Nj9lX9YqaFvGWBgFPryKB65cpHT0uYN3Ox7Bpfe51wZl+G4lHCFO5YSReGoy14E3lHqGzgDOItm2hNDYgtd8zR+Df1dmuZln5CD X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 883484d4-0758-4124-01e2-08da80bcebd7 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Aug 2022 01:56:52.6696 (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: XAPUs5ZnzApsfKMs399y1I0ZZjUEKaeguC6e+LeRsqa9vqLwa+ruLuTY49yfpu1NTyl55KwKP4gmrwOpxHZzYg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5286 ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=FrfsnxdY; spf=pass (imf25.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.59 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1660787814; a=rsa-sha256; cv=pass; b=bGCWxkNBEHo8T5H/ezs7AUDPIED+rhPgm0UNxMF80zC8zwaocPRh0qPv0VNwg88jTGSjYU WBLMNNv7TunNuxH5mzkStmO/fUxy0K/jhrIDuJl2N/ZSHPp2VxRnG8PxJxPoX1mDOY7g6r FxPZMIQo6DN5aVWDagPss7SK6C1Xi9I= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660787814; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MCKeJXLsIngQdXbGadMhinw8sGoEtHj2BAFX2EvUDKA=; b=pWAzssPpWqGZ4KpjH8AyJrSsGV/vBmuD+rJfM86DTh/iPsRN8I22U2YCCDBb6xSjTFBHLb MR9y/e79AW6l5Q0xp2MIC6B6vJcAb3ljP8jgp2Ld9nupNY8x1mO1R2XcAmgQu0e839L+h7 l0RWOi9KFWxSTuX0P0WtYXuFwPkaH/4= Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=FrfsnxdY; spf=pass (imf25.hostedemail.com: domain of apopple@nvidia.com designates 40.107.223.59 as permitted sender) smtp.mailfrom=apopple@nvidia.com; dmarc=pass (policy=reject) header.from=nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") X-Rspam-User: X-Stat-Signature: op1e4j3pb64owja9dpwsdfe4jkxhbecs X-Rspamd-Queue-Id: 3DB3FA01D3 X-Rspamd-Server: rspam05 X-HE-Tag: 1660787815-904622 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 pinning pages with FOLL_LONGTERM check_and_migrate_movable_pages() is called to migrate pages out of zones which should not contain any longterm pinned pages. When migration succeeds all pages will have been unpinned so pinning needs to be retried. Migration can also fail, in which case the pages will also have been unpinned but the operation should not be retried. If all pages are in the correct zone nothing will be unpinned and no retry is required. The logic in check_and_migrate_movable_pages() tracks unnecessary state and the return codes for each case are difficult to follow. Refactor the code to clean this up. No behaviour change is intended. Signed-off-by: Alistair Popple Reviewed-by: John Hubbard --- Changes for v5: - Minor formatting fixes. - Improved comments from John. - Cleaned up return code logic in check_and_migrate_movable_pages() Changes for v4: - Use folio directly instead of page based functions and folio->page. Changes for v3: - Improved comments (thanks John). - Fix up inconsistent int/long/unsigned long. - Rename (migrate|collect)_unpinnable_pages to (migrate|collect)_longterm_unpinnable_pages() as suggested by David. Changes for v2: - Split into different functions as suggested by John. - Made error handling more conventional as requested by Jason. Originally posted as "mm/gup.c: Simplify and fix check_and_migrate_movable_pages() return codes"[1]. Changes from that version: - Restore the original isolation failure behaviour and don't fail the pup. Instead retry indefinitely. - Unpin all pages on retry or failure rather than just failure. [1] https://lore.kernel.org/linux-mm/814dee5d3aadd38c3370eaaf438ba7eee9bf9d2b.1659399696.git-series.apopple@nvidia.com/ --- mm/gup.c | 192 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 122 insertions(+), 70 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index a2baa8b..6365005 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1901,19 +1901,16 @@ struct page *get_dump_page(unsigned long addr) #ifdef CONFIG_MIGRATION /* - * 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. - * Return negative error if migration fails. + * Returns the number of collected pages. Return value is always >= 0. */ -static long check_and_migrate_movable_pages(unsigned long nr_pages, - struct page **pages) +static unsigned long collect_longterm_unpinnable_pages( + struct list_head *movable_page_list, + unsigned long nr_pages, + struct page **pages) { - unsigned long isolation_error_count = 0, i; + unsigned long i, collected = 0; struct folio *prev_folio = NULL; - LIST_HEAD(movable_page_list); - bool drain_allow = true, coherent_pages = false; - int ret = 0; + bool drain_allow = true; for (i = 0; i < nr_pages; i++) { struct folio *folio = page_folio(pages[i]); @@ -1922,43 +1919,16 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, continue; prev_folio = 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. - */ - get_page(&folio->page); - unpin_user_page(&folio->page); + if (folio_is_longterm_pinnable(folio)) + continue; - ret = migrate_device_coherent_page(&folio->page); - if (ret) - goto unpin_pages; + collected++; + if (folio_is_device_coherent(folio)) continue; - } - if (folio_is_longterm_pinnable(folio)) - continue; - /* - * Try to move out any movable page before pinning the range. - */ if (folio_test_hugetlb(folio)) { - if (isolate_hugetlb(&folio->page, - &movable_page_list)) - isolation_error_count++; + isolate_hugetlb(&folio->page, movable_page_list); continue; } @@ -1967,59 +1937,133 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages, drain_allow = false; } - if (folio_isolate_lru(folio)) { - isolation_error_count++; + if (!folio_isolate_lru(folio)) continue; - } - list_add_tail(&folio->lru, &movable_page_list); + + list_add_tail(&folio->lru, movable_page_list); node_stat_mod_folio(folio, NR_ISOLATED_ANON + folio_is_file_lru(folio), folio_nr_pages(folio)); } - if (!list_empty(&movable_page_list) || isolation_error_count || - coherent_pages) - goto unpin_pages; + return collected; +} - /* - * If list is empty, and no isolation errors, means that all pages are - * in the correct zone. - */ - return nr_pages; +/* + * Unpins all pages and migrates device coherent pages and movable_page_list. + * Returns zero if all pages were successfully migrated or -errno for failure + * (or partial success). + */ +static int migrate_longterm_unpinnable_pages( + struct list_head *movable_page_list, + unsigned long nr_pages, + struct page **pages) +{ + int ret; + unsigned long i; -unpin_pages: - /* - * pages[i] might be NULL if any device coherent pages were found. - */ for (i = 0; i < nr_pages; i++) { - if (!pages[i]) + struct folio *folio = page_folio(pages[i]); + + if (folio_is_device_coherent(folio)) { + /* + * Migration will fail if the page is pinned, so convert + * the pin on the source page to a normal reference. + */ + pages[i] = NULL; + folio_get(folio); + gup_put_folio(folio, 1, FOLL_PIN); + + if (migrate_device_coherent_page(&folio->page)) { + ret = -EBUSY; + goto err; + } + continue; + } + /* + * We can't migrate pages with unexpected references, so drop + * the reference obtained by __get_user_pages_locked(). + * Migrating pages have been added to movable_page_list after + * calling folio_isolate_lru() which takes a reference so the + * page won't be freed if it's migrating. + */ unpin_user_page(pages[i]); + pages[i] = NULL; } - if (!list_empty(&movable_page_list)) { + if (!list_empty(movable_page_list)) { struct migration_target_control mtc = { .nid = NUMA_NO_NODE, .gfp_mask = GFP_USER | __GFP_NOWARN, }; - ret = migrate_pages(&movable_page_list, alloc_migration_target, - NULL, (unsigned long)&mtc, MIGRATE_SYNC, - MR_LONGTERM_PIN, NULL); - if (ret > 0) /* number of pages not migrated */ + if (migrate_pages(movable_page_list, alloc_migration_target, + NULL, (unsigned long)&mtc, MIGRATE_SYNC, + MR_LONGTERM_PIN, NULL)) { ret = -ENOMEM; + goto err; + } } - if (ret && !list_empty(&movable_page_list)) - putback_movable_pages(&movable_page_list); + putback_movable_pages(movable_page_list); + + return 0; + +err: + for (i = 0; i < nr_pages; i++) + if (pages[i]) + unpin_user_page(pages[i]); + putback_movable_pages(movable_page_list); + return ret; } + +/* + * Check whether all pages are *allowed* to be pinned. Rather confusingly, all + * pages in the range are required to be pinned via FOLL_PIN, before calling + * this routine. + * + * If any pages in the range are not allowed to be pinned, then this routine + * will migrate those pages away, unpin all the pages in the range and return + * -EAGAIN. The caller should re-pin the entire range with FOLL_PIN and then + * call this routine again. + * + * If an error other than -EAGAIN occurs, this indicates a migration failure. + * The caller should give up, and propagate the error back up the call stack. + * + * If everything is OK and all pages in the range are allowed to be pinned, then + * this routine leaves all pages pinned and returns zero for success. + */ +static long check_and_migrate_movable_pages(unsigned long nr_pages, + struct page **pages) +{ + int ret; + unsigned long collected; + LIST_HEAD(movable_page_list); + + collected = collect_longterm_unpinnable_pages(&movable_page_list, + nr_pages, pages); + if (!collected) + return 0; + + ret = migrate_longterm_unpinnable_pages(&movable_page_list, nr_pages, + pages); + if (ret) + return ret; + + /* + * We successfully migrated and unpinned all unpinnable pages. Caller + * should retry. + */ + return -EAGAIN; +} #else static long check_and_migrate_movable_pages(unsigned long nr_pages, struct page **pages) { - return nr_pages; + return 0; } #endif /* CONFIG_MIGRATION */ @@ -2040,7 +2084,15 @@ static long __gup_longterm_locked(struct mm_struct *mm, if (!(gup_flags & FOLL_LONGTERM)) return __get_user_pages_locked(mm, start, nr_pages, pages, vmas, NULL, gup_flags); - /* check_and_migrate_movable_pages() assumes pages have been pinned. */ + + /* + * If we get to this point then FOLL_LONGTERM is set, and FOLL_LONGTERM + * implies FOLL_PIN (although the reverse is not true). Therefore it is + * correct to unconditionally call check_and_migrate_movable_pages() + * which assumes pages have been pinned via FOLL_PIN. + * + * Enforce the above reasoning by asserting that FOLL_PIN is set. + */ if (WARN_ON(!(gup_flags & FOLL_PIN))) return -EINVAL; flags = memalloc_pin_save(); @@ -2051,10 +2103,10 @@ static long __gup_longterm_locked(struct mm_struct *mm, break; rc = check_and_migrate_movable_pages(rc, pages); - } while (!rc); + } while (rc == -EAGAIN); memalloc_pin_restore(flags); - return rc; + return rc ? rc : nr_pages; } static bool is_valid_gup_flags(unsigned int gup_flags)