From patchwork Mon Sep 26 06:03:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alistair Popple X-Patchwork-Id: 12988240 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D868FC07E9D for ; Mon, 26 Sep 2022 06:04:36 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6F7EC10E4F4; Mon, 26 Sep 2022 06:04:26 +0000 (UTC) Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2046.outbound.protection.outlook.com [40.107.94.46]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2D79710E394; Mon, 26 Sep 2022 06:04:20 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Oca6pW+6+KtG/XgCOK0VrJvZ6IhDf/Nyfx/5a8Mmk63oyuThRZP1J+mGiuqYAk/5RhG8aUYgwFYjJ376bIKV/2RaLYxMMGYEWTPzSC6qslwWKWR7h0f8cv30ahReMc3jE4pv+G3oP58CGVRJ3rCIylPVkGKAWSp/nyQUuZRiAtjrwTwIhw0g7Xt8dMyKJZLljNgS8KxMV51bgn6aPuG0Kj/bBLVPlUUmGNYBkO9VR/6e65z4Yen8SiOZBRL9sMdAMfryQ//cDjjuSHx+NC4sUcxtk3Q9TmFRFz8AAqqzJEcNclncMirxISFUSx7EMvaFjUj5nBZlUxGSJC7mUpinaA== 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=lpUR8gcIPaAR7nD1dKeC+83we5Ll0QdDdKicRyU/2VU=; b=Fhop26GLvxmPg1GIiy6s7+0gK9ChzQqUbXlK7SR5s2IauiEC666hVU2OP0uDZBpyaVWAHBFoimP/Nv5pet9OUaXm4DDYjXihmmjuXSSNLB213PT6urGg0OTuxNSxHXdAY3ETSEQuUms93krHl4W5FlO6GrURiZ+04Y8VM+l2fA1kLGuVgCb1Q531JoKWQrUzpB9j6YMZhSFpko1EPplTZNP3OoW9QsmqlDxcmLJmMotrCK+FGVPX/Zs+CZQuOZ0xs9/t+heTiHGIN4tA/P7hxhDeAlC1IOIp085qhvnYundWbWPSUt5TL5yre60mjigEPobtaBQS5AQrnzaDJWKCNA== 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=lpUR8gcIPaAR7nD1dKeC+83we5Ll0QdDdKicRyU/2VU=; b=PMHcNX1AcNm68fswb4wqfJoo6IGL+BWzkRMjjMdIa7gj5tXlvdnuBp3XWC1q/p3cvsfuh/l9f4VPo/p2oT3uDedlJ8xugwlEQxBDlanEdSBVw9C4fZZJp9CWAQbmutX53+sg+LmJoHvH2SMSS/qYUBA7aYsZ10kDa+ur0Gr5kFDoxnteawoWHIuIap6SNaJM93GHXoGq+1W3iShdwMXA3WQtxMLA+8NlO5t77uWKM1i6SKlG5plvh2RF/K4txTbw7G11atKzhaTBVPIqdQqYUcXfqEkThG4ASMY/gKvA03kn9ZpqrC0inHhn+AnWhoE0nljFeOW1fZNpIpo9bI3QdA== 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 MW4PR12MB7359.namprd12.prod.outlook.com (2603:10b6:303:219::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.25; Mon, 26 Sep 2022 06:04:18 +0000 Received: from BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::ed0:a520:ac8e:9966]) by BYAPR12MB3176.namprd12.prod.outlook.com ([fe80::ed0:a520:ac8e:9966%6]) with mapi id 15.20.5654.025; Mon, 26 Sep 2022 06:04:18 +0000 From: Alistair Popple To: linux-mm@kvack.org, Andrew Morton Subject: [PATCH 2/7] mm: Free device private pages have zero refcount Date: Mon, 26 Sep 2022 16:03:06 +1000 Message-Id: <3d74bb439723c7e46cbe47d1711795308aee4ae3.1664171943.git-series.apopple@nvidia.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: References: X-ClientProxiedBy: SY0PR01CA0006.ausprd01.prod.outlook.com (2603:10c6:10:1bb::22) To BYAPR12MB3176.namprd12.prod.outlook.com (2603:10b6:a03:134::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR12MB3176:EE_|MW4PR12MB7359:EE_ X-MS-Office365-Filtering-Correlation-Id: 42dc79fb-aca4-4410-816b-08da9f84f2bc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Sg8pk3ObyJPpGQXkXCrSAZrlI2JfuD7xYLEcvcWnSqNBC5l/zr/eIlMA3v5g6cNqFpPIBZk6le8SP+1o/FzBAL4i298q7XU4Kp9UvfXgJAAazaL5Y0LgRpKIT6vGoMrUJ6k83kfQiAfhQLcFQIZ3fdeKxeX9YB1BtbvXFM5Lm02EmMDaNfd/sWVTogWERr+z3BIjIbdaBtCYJP7TkUY/ejuUd3E9huIAFtn8wqeP5VL0egDMcAThpaErJjkr22/RjMXX0C9W298V96AgEpdLVPSQhHN4J6sAs5tjbO1olnfHB4XS7KLZHzA90D8yqDbkZS0RAwW9M1m8+03MajgeToulqBnox/cdDpW4BKDlRInUqrswG9FkTGJS8KYn+lKcHGpzU5FmLnSMOR+690JQiMJM0XDAe6x/8Il11R+32tILcxb+Ixsx5235bOE5DVZDhEhq1sGsAHejmdLZvVLsNyZzDGt37RBfyI34D0qol+lPRmRUC64DXhPG9thIZglx7UWSbk2kRXXYePRu1B0OEPvF7+a2v0n9VsYg84I48jx6H7oSJ6ahBzRofXCsytfXeDWYF3z1INZJJsLOvVBrlNvOmfzzNKfaP0YoYH/j0tmhBprpZi1PJwxHo3H7G1oF8ssOicZY4CBSYRlzlq/MNDSFawTCgGpnaDv6EwDnyMs/hixWEyIXMpuV0vZY9/X/43jPLnYlfV1eXqgcMT1isUQP8//m/8to+Mwhjhyy22+23EH3Sl1JLVgwrZkP6cio 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:(13230022)(4636009)(39860400002)(136003)(366004)(376002)(396003)(346002)(451199015)(8936002)(86362001)(7416002)(2906002)(5660300002)(83380400001)(6666004)(41300700001)(6512007)(186003)(107886003)(26005)(6506007)(478600001)(6486002)(2616005)(6916009)(66946007)(4326008)(66556008)(66476007)(8676002)(316002)(38100700002)(54906003)(36756003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VzBdNO6pyTug0f53OiDET+Rk1JMLTpnAA29eaaVmvQqdoMusNTsQgP71Jb0gK/+grlWM0NnjIFLOFpmEmc0nXC4M/aINMh1iXnNqtZ0fkl/vQJ/CLZNEZGHOcEcy9lJBqmAgQQm0guSUiTuImpDL+dIBBDc8dlGTSWGYE3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omxSsqwYlueEA1Ps3YO0Q6fzhD2t2MLePgr4PprgH4lNHigstCWuEtH7L3DY8yq4VDEMn/f31wpTiIjTjFG/M3TtpW2wmLKFXlx1BD68gk+s0ihbZhs4xgrskwjxsCPVZgBoD2q+5Hz0Bok9/mJLKLpLsVcXzRGsqJ+LFpqPMd6EApW8sjmOKeFalYPA7RFvDowp2eFK9CvBqVr6L5bY+8sD4+/M6BDFvi8k83r5LoPowU95+td4ia1QdCG1tRUy8zTtVIeUwzyDGuU6yp2Sebi+iAFsld31UWJNhkTdpoLhrKLHKxneFtWKLxbpAO4s7hg6AJz7V9MJ3DA7Bl8M57P0Zr4gN9onksrqXmZZoAer1F X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 42dc79fb-aca4-4410-816b-08da9f84f2bc X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3176.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Sep 2022 06:04:18.5160 (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: pSJfaFl+AoP2iXV+KdZPR0OQ/OuPImsGV0AlcHXWV5stObXND6Q/pax0LvM2JKiC8tBfzane1jtZP79gVwCvAw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7359 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alex Sierra , Karol Herbst , David Airlie , nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org, Michael Ellerman , Alistair Popple , "Matthew Wilcox \(Oracle\)" , Ben Skeggs , Jason Gunthorpe , Ralph Campbell , John Hubbard , Nicholas Piggin , Dan Williams , Felix Kuehling , "Pan, Xinhui" , linux-kernel@vger.kernel.org, Alex Deucher , linuxppc-dev@lists.ozlabs.org, =?utf-8?q?Christian_K=C3=B6nig?= Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Since 27674ef6c73f ("mm: remove the extra ZONE_DEVICE struct page refcount") device private pages have no longer had an extra reference count when the page is in use. However before handing them back to the owning device driver we add an extra reference count such that free pages have a reference count of one. This makes it difficult to tell if a page is free or not because both free and in use pages will have a non-zero refcount. Instead we should return pages to the drivers page allocator with a zero reference count. Kernel code can then safely use kernel functions such as get_page_unless_zero(). Signed-off-by: Alistair Popple --- arch/powerpc/kvm/book3s_hv_uvmem.c | 1 + drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 1 + drivers/gpu/drm/nouveau/nouveau_dmem.c | 1 + lib/test_hmm.c | 1 + mm/memremap.c | 5 ----- mm/page_alloc.c | 6 ++++++ 6 files changed, 10 insertions(+), 5 deletions(-) diff --git a/arch/powerpc/kvm/book3s_hv_uvmem.c b/arch/powerpc/kvm/book3s_hv_uvmem.c index d4eacf4..08d2f7d 100644 --- a/arch/powerpc/kvm/book3s_hv_uvmem.c +++ b/arch/powerpc/kvm/book3s_hv_uvmem.c @@ -718,6 +718,7 @@ static struct page *kvmppc_uvmem_get_page(unsigned long gpa, struct kvm *kvm) dpage = pfn_to_page(uvmem_pfn); dpage->zone_device_data = pvt; + set_page_count(dpage, 1); lock_page(dpage); return dpage; out_clear: diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c index 776448b..05c2f4d 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c @@ -223,6 +223,7 @@ svm_migrate_get_vram_page(struct svm_range *prange, unsigned long pfn) page = pfn_to_page(pfn); svm_range_bo_ref(prange->svm_bo); page->zone_device_data = prange->svm_bo; + set_page_count(page, 1); lock_page(page); } diff --git a/drivers/gpu/drm/nouveau/nouveau_dmem.c b/drivers/gpu/drm/nouveau/nouveau_dmem.c index 1635661..f9234ed 100644 --- a/drivers/gpu/drm/nouveau/nouveau_dmem.c +++ b/drivers/gpu/drm/nouveau/nouveau_dmem.c @@ -326,6 +326,7 @@ nouveau_dmem_page_alloc_locked(struct nouveau_drm *drm) return NULL; } + set_page_count(page, 1); lock_page(page); return page; } diff --git a/lib/test_hmm.c b/lib/test_hmm.c index 89463ff..2bd3a67 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -627,6 +627,7 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice) goto error; } + set_page_count(dpage, 1); dpage->zone_device_data = rpage; lock_page(dpage); return dpage; diff --git a/mm/memremap.c b/mm/memremap.c index 25029a4..e065171 100644 --- a/mm/memremap.c +++ b/mm/memremap.c @@ -501,11 +501,6 @@ void free_zone_device_page(struct page *page) */ page->mapping = NULL; page->pgmap->ops->page_free(page); - - /* - * Reset the page count to 1 to prepare for handing out the page again. - */ - set_page_count(page, 1); } #ifdef CONFIG_FS_DAX diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 9d49803..67eaab5 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -6744,6 +6744,12 @@ static void __ref __init_zone_device_page(struct page *page, unsigned long pfn, set_pageblock_migratetype(page, MIGRATE_MOVABLE); cond_resched(); } + + /* + * ZONE_DEVICE pages are released directly to the driver page allocator + * which will set the page count to 1 when allocating the page. + */ + set_page_count(page, 0); } /*