[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240205120203.60312-9-jgowans@amazon.com>
Date: Mon, 5 Feb 2024 12:01:53 +0000
From: James Gowans <jgowans@...zon.com>
To: <linux-kernel@...r.kernel.org>
CC: Eric Biederman <ebiederm@...ssion.com>, <kexec@...ts.infradead.org>,
"Joerg Roedel" <joro@...tes.org>, Will Deacon <will@...nel.org>,
<iommu@...ts.linux.dev>, Alexander Viro <viro@...iv.linux.org.uk>, "Christian
Brauner" <brauner@...nel.org>, <linux-fsdevel@...r.kernel.org>, Paolo Bonzini
<pbonzini@...hat.com>, Sean Christopherson <seanjc@...gle.com>,
<kvm@...r.kernel.org>, Andrew Morton <akpm@...ux-foundation.org>,
<linux-mm@...ck.org>, Alexander Graf <graf@...zon.com>, David Woodhouse
<dwmw@...zon.co.uk>, "Jan H . Schoenherr" <jschoenh@...zon.de>, Usama Arif
<usama.arif@...edance.com>, Anthony Yznaga <anthony.yznaga@...cle.com>,
Stanislav Kinsburskii <skinsburskii@...ux.microsoft.com>,
<madvenka@...ux.microsoft.com>, <steven.sistare@...cle.com>,
<yuleixzhang@...cent.com>
Subject: [RFC 08/18] iommu: Add allocator for pgtables from persistent region
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 <linux/mm.h>
+
+/*
+ * 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 <linux/types.h>
+#include <linux/pkernfs.h>
+
+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);
--
2.40.1
Powered by blists - more mailing lists