mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jacob Pan <jacob.pan@linux.microsoft.com>
To: iommu@lists.linux.dev
Cc: Jason Gunthorpe <jgg@ziepe.ca>,
	Nicolin Chen <nicolinc@nvidia.com>,
	Kevin Tian <kevin.tian@intel.com>, Will Deacon <will@kernel.org>,
	Robin Murphy <robin.murphy@arm.com>, Wei Liu <wei.liu@kernel.org>,
	"K . Y . Srinivasan" <kys@microsoft.com>,
	Haiyang Zhang <haiyangz@microsoft.com>,
	Dexuan Cui <decui@microsoft.com>, Long Li <longli@microsoft.com>,
	linux-hyperv@vger.kernel.org, Joerg Roedel <joro@8bytes.org>,
	Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>,
	Vasant Hegde <vasant.hegde@amd.com>,
	Arnd Bergmann <arnd@arndb.de>,
	linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Aneesh Kumar K . V" <aneesh.kumar@kernel.org>,
	Mukesh Rathor <mukeshrathor@microsoft.com>,
	John Starks <John.Starks@microsoft.com>,
	Souradeep Chakrabarti <schakrabarti@microsoft.com>,
	Yu Zhang <zhangyu1@linux.microsoft.com>,
	Easwar Hariharan <eahariha@linux.microsoft.com>,
	Alex Williamson <alex@shazbot.org>
Subject: [PATCH RFC 5/9] mshv: Add partition file identity helper
Date: Fri, 25 Sep 2026 12:07:38 -0700	[thread overview]
Message-ID: <20260925190742.1575380-6-jacob.pan@linux.microsoft.com> (raw)
In-Reply-To: <20260925190742.1575380-1-jacob.pan@linux.microsoft.com>

Provide a helper to derive the immutable MSHV partition ID from a
partition file. IOMMU drivers can use this to bind a vIOMMU to the
partition object represented by a userspace fd instead of relying on
current-task lookup.

Keep the public helper in built-in Hyper-V common code and let the MSHV
root driver register the partition-file callbacks. This lets built-in
IOMMU callers avoid symbol_get() when MSHV_ROOT is built as a module.

Assisted-by: GPT-5.6 Sol
Signed-off-by: Jacob Pan <jacob.pan@linux.microsoft.com>
---
 drivers/hv/hv_common.c         | 69 ++++++++++++++++++++++++++++++++++
 drivers/hv/mshv_root_main.c    | 36 +++++++++++++++++-
 include/asm-generic/mshyperv.h | 35 +++++++++++++++++
 3 files changed, 139 insertions(+), 1 deletion(-)

diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c
index 31256cb22b39..d259cb833376 100644
--- a/drivers/hv/hv_common.c
+++ b/drivers/hv/hv_common.c
@@ -17,6 +17,7 @@
 #include <linux/export.h>
 #include <linux/bitfield.h>
 #include <linux/cpumask.h>
+#include <linux/fs.h>
 #include <linux/sched/task_stack.h>
 #include <linux/panic_notifier.h>
 #include <linux/ptrace.h>
@@ -24,6 +25,7 @@
 #include <linux/efi.h>
 #include <linux/kdebug.h>
 #include <linux/kmsg_dump.h>
+#include <linux/mutex.h>
 #include <linux/sizes.h>
 #include <linux/slab.h>
 #include <linux/dma-map-ops.h>
@@ -37,6 +39,73 @@ EXPORT_SYMBOL_GPL(hv_current_partition_id);
 enum hv_partition_type hv_curr_partition_type;
 EXPORT_SYMBOL_GPL(hv_curr_partition_type);
 
+static DEFINE_MUTEX(mshv_partition_file_ops_lock);
+static const struct mshv_partition_file_ops *mshv_partition_file_ops;
+
+int mshv_partition_file_ops_register(const struct mshv_partition_file_ops *ops)
+{
+	int ret = 0;
+
+	if (!ops || !ops->file_is_partition || !ops->get_partid)
+		return -EINVAL;
+
+	mutex_lock(&mshv_partition_file_ops_lock);
+	if (mshv_partition_file_ops)
+		ret = -EBUSY;
+	else
+		mshv_partition_file_ops = ops;
+	mutex_unlock(&mshv_partition_file_ops_lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mshv_partition_file_ops_register);
+
+void
+mshv_partition_file_ops_unregister(const struct mshv_partition_file_ops *ops)
+{
+	mutex_lock(&mshv_partition_file_ops_lock);
+	if (mshv_partition_file_ops == ops)
+		mshv_partition_file_ops = NULL;
+	mutex_unlock(&mshv_partition_file_ops_lock);
+}
+EXPORT_SYMBOL_GPL(mshv_partition_file_ops_unregister);
+
+bool file_is_mshv_partition(struct file *file)
+{
+	const struct mshv_partition_file_ops *ops;
+	bool ret = false;
+
+	if (!file)
+		return false;
+
+	mutex_lock(&mshv_partition_file_ops_lock);
+	ops = mshv_partition_file_ops;
+	if (ops)
+		ret = ops->file_is_partition(file);
+	mutex_unlock(&mshv_partition_file_ops_lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(file_is_mshv_partition);
+
+u64 mshv_partition_file_get_partid(struct file *file)
+{
+	const struct mshv_partition_file_ops *ops;
+	u64 ret = HV_PARTITION_ID_INVALID;
+
+	if (!file)
+		return HV_PARTITION_ID_INVALID;
+
+	mutex_lock(&mshv_partition_file_ops_lock);
+	ops = mshv_partition_file_ops;
+	if (ops && ops->file_is_partition(file))
+		ret = ops->get_partid(file);
+	mutex_unlock(&mshv_partition_file_ops_lock);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(mshv_partition_file_get_partid);
+
 /*
  * ms_hyperv and hv_nested are defined here with other
  * Hyper-V specific globals so they are shared across all architectures and are
diff --git a/drivers/hv/mshv_root_main.c b/drivers/hv/mshv_root_main.c
index 8b31e3948c09..838ea6397c9f 100644
--- a/drivers/hv/mshv_root_main.c
+++ b/drivers/hv/mshv_root_main.c
@@ -2160,6 +2160,33 @@ u64 mshv_current_partid(void)
 }
 EXPORT_SYMBOL_GPL(mshv_current_partid);
 
+static bool mshv_partition_file_is_valid(struct file *file)
+{
+	if (!file)
+		return false;
+
+	if (file->f_op == &mshv_partition_fops)
+		return true;
+
+	return false;
+}
+
+static u64 mshv_partition_file_get_partid_impl(struct file *file)
+{
+	if (file->f_op == &mshv_partition_fops) {
+		struct mshv_partition *partition = file->private_data;
+
+		return partition ? partition->pt_id : HV_PARTITION_ID_INVALID;
+	}
+
+	return HV_PARTITION_ID_INVALID;
+}
+
+static const struct mshv_partition_file_ops mshv_partition_file_ops = {
+	.file_is_partition = mshv_partition_file_is_valid,
+	.get_partid = mshv_partition_file_get_partid_impl,
+};
+
 static int
 add_partition(struct mshv_partition *partition)
 {
@@ -2599,10 +2626,14 @@ static int __init mshv_parent_partition_init(void)
 	if (hv_get_hypervisor_version(&version_info))
 		return -ENODEV;
 
-	ret = misc_register(&mshv_dev);
+	ret = mshv_partition_file_ops_register(&mshv_partition_file_ops);
 	if (ret)
 		return ret;
 
+	ret = misc_register(&mshv_dev);
+	if (ret)
+		goto unregister_file_ops;
+
 	dev = mshv_dev.this_device;
 
 	if (version_info.build_number < MSHV_HV_MIN_VERSION ||
@@ -2652,6 +2683,8 @@ static int __init mshv_parent_partition_init(void)
 	mshv_synic_exit();
 device_deregister:
 	misc_deregister(&mshv_dev);
+unregister_file_ops:
+	mshv_partition_file_ops_unregister(&mshv_partition_file_ops);
 	return ret;
 }
 
@@ -2661,6 +2694,7 @@ static void __exit mshv_parent_partition_exit(void)
 	mshv_port_table_fini();
 	mshv_debugfs_exit();
 	misc_deregister(&mshv_dev);
+	mshv_partition_file_ops_unregister(&mshv_partition_file_ops);
 	mshv_irqfd_wq_cleanup();
 	root_scheduler_deinit();
 	mshv_synic_exit();
diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h
index c7258cd72a89..ed7ab21d68a2 100644
--- a/include/asm-generic/mshyperv.h
+++ b/include/asm-generic/mshyperv.h
@@ -401,6 +401,41 @@ static inline u64 mshv_current_partid(void)
 }
 #endif /* CONFIG_MSHV_ROOT */
 
+struct file;
+struct mshv_partition_file_ops {
+	bool (*file_is_partition)(struct file *file);
+	u64 (*get_partid)(struct file *file);
+};
+
+#if IS_ENABLED(CONFIG_HYPERV)
+int mshv_partition_file_ops_register(const struct mshv_partition_file_ops *ops);
+void
+mshv_partition_file_ops_unregister(const struct mshv_partition_file_ops *ops);
+bool file_is_mshv_partition(struct file *file);
+u64 mshv_partition_file_get_partid(struct file *file);
+#else
+static inline int
+mshv_partition_file_ops_register(const struct mshv_partition_file_ops *ops)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void
+mshv_partition_file_ops_unregister(const struct mshv_partition_file_ops *ops)
+{
+}
+
+static inline bool file_is_mshv_partition(struct file *file)
+{
+	return false;
+}
+
+static inline u64 mshv_partition_file_get_partid(struct file *file)
+{
+	return HV_PARTITION_ID_INVALID;
+}
+#endif
+
 static inline int hv_deposit_memory(u64 partition_id, u64 status)
 {
 	return hv_deposit_memory_node(NUMA_NO_NODE, partition_id, status);
-- 
2.43.0


  parent reply	other threads:[~2026-09-25 19:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-25 19:07 [RFC PATCH 0/9] iommu/iommufd: Add hypervisor external attach Jacob Pan
2026-09-25 19:07 ` [PATCH RFC 1/9] iommu: Introduce external attach domain type Jacob Pan
2026-09-25 19:07 ` [PATCH RFC 2/9] iommufd: Introduce hypervisor vIOMMU type Jacob Pan
2026-09-25 19:07 ` [PATCH RFC 3/9] iommufd: Add external HWPT support Jacob Pan
2026-09-25 19:07 ` [PATCH RFC 4/9] iommufd/selftest: Add hypervisor external attach backend Jacob Pan
2026-09-25 19:07 ` Jacob Pan [this message]
2026-09-25 19:07 ` [PATCH RFC 6/9] mshv: Add prepare callback for external device attach Jacob Pan
2026-09-25 19:07 ` [PATCH RFC 7/9] iommu/hyperv: Split root IOMMU declarations Jacob Pan
2026-09-25 19:07 ` [PATCH RFC 8/9] iommu/hyperv: Add fd-backed vIOMMU support Jacob Pan
2026-09-25 19:07 ` [PATCH RFC 9/9] iommu/hyperv: Add IOMMUFD external domains Jacob Pan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260925190742.1575380-6-jacob.pan@linux.microsoft.com \
    --to=jacob.pan@linux.microsoft.com \
    --cc=John.Starks@microsoft.com \
    --cc=alex@shazbot.org \
    --cc=aneesh.kumar@kernel.org \
    --cc=arnd@arndb.de \
    --cc=decui@microsoft.com \
    --cc=eahariha@linux.microsoft.com \
    --cc=haiyangz@microsoft.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kys@microsoft.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longli@microsoft.com \
    --cc=mukeshrathor@microsoft.com \
    --cc=nicolinc@nvidia.com \
    --cc=robin.murphy@arm.com \
    --cc=schakrabarti@microsoft.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=vasant.hegde@amd.com \
    --cc=wei.liu@kernel.org \
    --cc=will@kernel.org \
    --cc=zhangyu1@linux.microsoft.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®