From patchwork Mon Feb 5 12:01:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gowans, James" X-Patchwork-Id: 13545353 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 77A8CC48291 for ; Mon, 5 Feb 2024 12:04:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 18EEE6B0095; Mon, 5 Feb 2024 07:04:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 166926B0096; Mon, 5 Feb 2024 07:04:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 02E246B0098; Mon, 5 Feb 2024 07:03:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E60BA6B0095 for ; Mon, 5 Feb 2024 07:03:59 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id AA40BA0A75 for ; Mon, 5 Feb 2024 12:03:59 +0000 (UTC) X-FDA: 81757616598.30.17B0D23 Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) by imf26.hostedemail.com (Postfix) with ESMTP id 7E179140002 for ; Mon, 5 Feb 2024 12:03:57 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=ptSRFBg0; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf26.hostedemail.com: domain of "prvs=75897cb1d=jgowans@amazon.com" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=75897cb1d=jgowans@amazon.com" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707134637; a=rsa-sha256; cv=none; b=nKezBeS7ptKciWnQGf1YAqhIyYXTKhC4ebNcjiotGTQAuji9pvP27SWRBd4g3XXRXeKM29 gJYw1FZ4SFVcSMkyr6T2zSNIF3VdMJbCbQuHTOCO/b2P9cFPPlN8doevqOvcYti7ts+0EN YMMS0L4rumfsvycuNeIZOT69f+DNqHs= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=ptSRFBg0; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf26.hostedemail.com: domain of "prvs=75897cb1d=jgowans@amazon.com" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=75897cb1d=jgowans@amazon.com" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707134637; 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=iY0J0IZ5qTqLG1CI5oIteOfffrC6hlfw7IE0MCx8M3k=; b=taeFBKoCHXKdQfGzwcREYF+hRhhBSkcUXTFfsAkSKLyBehjajh0hU5qt/IOEg32mihp/cc Dv7GtiJEWIF2w/1b2fvr9ojwlFZE1G79mv5bF2QGBC7BJOZpUoqFDtWlm7+7TgrbiOEY8T 58Ws7jB5RQoMBELlvObzA/EHzjjpriY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1707134638; x=1738670638; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iY0J0IZ5qTqLG1CI5oIteOfffrC6hlfw7IE0MCx8M3k=; b=ptSRFBg0C/qJvi/Fz97DIkGMS40huEmuwtGGN1sPJK6E9721GxqS63RF F0aTAEQJJJ3WIm4T+Xa3kzPxIvzaAi0Y0k/NNAb9r4RljJGn9xD2FhP18 KjZkvtWO2n3QfyvPkdzi13eI133/3pAxeeqCjkKLHe7KCGSqPZYhIS3Jl U=; X-IronPort-AV: E=Sophos;i="6.05,245,1701129600"; d="scan'208";a="63724432" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80009.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2024 12:03:55 +0000 Received: from EX19MTAEUC001.ant.amazon.com [10.0.10.100:51867] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.28.192:2525] with esmtp (Farcaster) id 0b48539d-2334-4d72-bccb-1f938dcbdb04; Mon, 5 Feb 2024 12:03:53 +0000 (UTC) X-Farcaster-Flow-ID: 0b48539d-2334-4d72-bccb-1f938dcbdb04 Received: from EX19D014EUC004.ant.amazon.com (10.252.51.182) by EX19MTAEUC001.ant.amazon.com (10.252.51.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Mon, 5 Feb 2024 12:03:53 +0000 Received: from dev-dsk-jgowans-1a-a3faec1f.eu-west-1.amazon.com (172.19.112.191) by EX19D014EUC004.ant.amazon.com (10.252.51.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Mon, 5 Feb 2024 12:03:46 +0000 From: James Gowans To: CC: Eric Biederman , , "Joerg Roedel" , Will Deacon , , Alexander Viro , "Christian Brauner" , , Paolo Bonzini , Sean Christopherson , , Andrew Morton , , Alexander Graf , David Woodhouse , "Jan H . Schoenherr" , Usama Arif , Anthony Yznaga , Stanislav Kinsburskii , , , Subject: [RFC 08/18] iommu: Add allocator for pgtables from persistent region Date: Mon, 5 Feb 2024 12:01:53 +0000 Message-ID: <20240205120203.60312-9-jgowans@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205120203.60312-1-jgowans@amazon.com> References: <20240205120203.60312-1-jgowans@amazon.com> MIME-Version: 1.0 X-Originating-IP: [172.19.112.191] X-ClientProxiedBy: EX19D033UWC001.ant.amazon.com (10.13.139.218) To EX19D014EUC004.ant.amazon.com (10.252.51.182) X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 7E179140002 X-Stat-Signature: n7nnp9bkc4ksu5n8it56pf4nfouas3ug X-HE-Tag: 1707134637-178402 X-HE-Meta: U2FsdGVkX1/aYxT4EZLjs+qnVdJMdRqEmdB7UTW9rV8WLne9bcnVoYLlISfYIuEFWqKG36HwNKjNQwE5YqXtcDz/QWhSAJwW8P1ASMzS7hnFo9dYUQJka6BPlbqmYUQ8Ezk6DUHv35ccVrGr0xgFv0Z18erHiRC01h60ul2fv6K+bS75wunxQbYzLtVNedB1m5CpkzSQBzrtY2EUCJmcpH07iiMlpOHBlQSOSNDzj7/WMyJBHgT8mJQ1YlkSIouRbqoZaIvmJ1MRrz7dBQBpkn3vrDePlecnaR6jj5P8r/DMhjd0sf3n/jf2bff0pbTvZ85GT0MWubMydc2ZZysJfwYA2VrwXtVXchWQytRxV5WpnNMVOLdDSqBitcELxH3RWBIvSOsnywuPvYYgcYPL4YpekHo+P5CwJCFuxHK2kfSGUSr3X4MxItyQNXjPGUZXkJuzi/8W7t7/xhsuHTgN2XN0CHnCyCusJtwIzK4XezMmpAI4HDRyNcojcLr+g7yEDwFAI8k02DCziFO1RSNUmEZXSEsjeopNhJmGaw85UlOISuLcPFCdngaYUay+urry6YmFYsqbw8xdUe2SSrTq2Mp55fhj3U3neJH3qWvfQqy649xZeJsySRpdQKl9LNQQcTFL9A6Wm8EjZQYZTpnc60Bbk7ce/daR4neHwsiXnKqx/MP+xNpKdnd2uUiQrKsV8Jqv0jKUWx0aaWS+T+TIOFDAaWcjRcNjt3joQDVBkfNeYxaw67DMM1PD5dcp6BR2kQdg7gq4oJVTJBHlcsLMTbw2dsL03cd8SmYRDEqXk+MEdb7YyNciwbKVBP0n4PuhyOgH8H+6RG2X9335TBTLqqTYTF6G2sa02bz6BovK3RQPVNygLr0NYwPwbaeb+gz2N7fP827E0FtSrbdax+77Zphhe4fIB19teG0xcGo7jimBlHs5QIam4lIC6VezpSey71AyhuJyatoNZy3hz/Z +zpr0gUA ovHl0e8E02jJbwSikwMHvkRcLBl3bZj7ORygVieFrGsvycWk+XOE2Pk/jMC2jl/w/dOdku86mmr8E1x03jjnZ+xYawKrbmnCkBVFw3UzbBuarLXEFjXU6WpDV9is64P/zlZP+jtI5UT8vICO5jXnNt7OjSDXUiW6G9CLtkjxZ8pUk/lhz/gy/Zb7FCI6JB01nPIaMFMVSVzhicx3HnPd+QG8jKxaGu/XHFVg7FmE+JWB+vyKQLvjXBBu4x8VNMA3VAgWFAuahAqv7T9GUS0jaqWM7BQcP81Tp1Eoey4vPgSNKABAxpjGKExzuJtTsPxKW7gQptO4QUTItVk3J5f5HWm7FTG81iIh70jr2o5fnqwY9+ruGbztop6VmNNmeVHOD4n4kx8IF5/zyPmPLji2nOAe1rv1s85IyGx/61QnrCYheRmUs3JTlcTGFdhX0d9PSJICnjh6AgCEfZkmphpxfHsJXZy1oI6ccgOA8Eo/ZCsENZBx30i19HZfMFQg/KBQ2epIf746f1h4+nXytQSQUQLFXfIKCyXYcKWjUguD8zyZJIL8= 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: List-Subscribe: List-Unsubscribe: The specific IOMMU drivers will need to ability to allocate pages from a pkernfs IOMMU pgtable file for their pgtables. Also, the IOMMU drivers will need to ability to consistent get the same page for the root PGD page - add a specific function to get this PGD "root" page. This is different to allocating regular pgtable pages because the exact same page needs to be *restored* after kexec into the pgd pointer on the IOMMU domain struct. To support this sort of allocation the pkernfs region is treated as an array of 512 4 KiB pages, the first of which is an allocation bitmap. --- drivers/iommu/Makefile | 1 + drivers/iommu/pgtable_alloc.c | 36 +++++++++++++++++++++++++++++++++++ drivers/iommu/pgtable_alloc.h | 9 +++++++++ 3 files changed, 46 insertions(+) create mode 100644 drivers/iommu/pgtable_alloc.c create mode 100644 drivers/iommu/pgtable_alloc.h diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile index 769e43d780ce..cadebabe9581 100644 --- a/drivers/iommu/Makefile +++ b/drivers/iommu/Makefile @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 obj-y += amd/ intel/ arm/ iommufd/ +obj-y += pgtable_alloc.o obj-$(CONFIG_IOMMU_API) += iommu.o obj-$(CONFIG_IOMMU_API) += iommu-traces.o obj-$(CONFIG_IOMMU_API) += iommu-sysfs.o diff --git a/drivers/iommu/pgtable_alloc.c b/drivers/iommu/pgtable_alloc.c new file mode 100644 index 000000000000..f0c2e12f8a8b --- /dev/null +++ b/drivers/iommu/pgtable_alloc.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "pgtable_alloc.h" +#include + +/* + * The first 4 KiB is the bitmap - set the first bit in the bitmap. + * Scan bitmap to find next free bits - it's next free page. + */ + +void iommu_alloc_page_from_region(struct pkernfs_region *region, void **vaddr, unsigned long *paddr) +{ + int page_idx; + + page_idx = bitmap_find_free_region(region->vaddr, 512, 0); + *vaddr = region->vaddr + (page_idx << PAGE_SHIFT); + if (paddr) + *paddr = region->paddr + (page_idx << PAGE_SHIFT); +} + + +void *pgtable_get_root_page(struct pkernfs_region *region, bool liveupdate) +{ + /* + * The page immediately after the bitmap is the root page. + * It would be wrong for the page to be allocated if we're + * NOT doing a liveupdate, or for a liveupdate to happen + * with no allocated page. Detect this mismatch. + */ + if (test_bit(1, region->vaddr) ^ liveupdate) { + pr_err("%sdoing a liveupdate but root pg bit incorrect", + liveupdate ? "" : "NOT "); + } + set_bit(1, region->vaddr); + return region->vaddr + PAGE_SIZE; +} diff --git a/drivers/iommu/pgtable_alloc.h b/drivers/iommu/pgtable_alloc.h new file mode 100644 index 000000000000..c1666a7be3d3 --- /dev/null +++ b/drivers/iommu/pgtable_alloc.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include + +void iommu_alloc_page_from_region(struct pkernfs_region *region, + void **vaddr, unsigned long *paddr); + +void *pgtable_get_root_page(struct pkernfs_region *region, bool liveupdate);