From: Jacob Pan <jacob.jun.pan@linux.intel.com>
To: Joerg Roedel <joro@8bytes.org>,
Alex Williamson <alex.williamson@redhat.com>,
"Lu Baolu" <baolu.lu@linux.intel.com>,
iommu@lists.linux-foundation.org,
LKML <linux-kernel@vger.kernel.org>,
David Woodhouse <dwmw2@infradead.org>,
Jean-Philippe Brucker <jean-philippe@linaro.com>
Cc: "Tian, Kevin" <kevin.tian@intel.com>, Raj Ashok <ashok.raj@intel.com>
Subject: [PATCH v2 3/3] iommu/uapi: Add helper function for size lookup
Date: Wed, 25 Mar 2020 16:17:07 -0700 [thread overview]
Message-ID: <1585178227-17061-4-git-send-email-jacob.jun.pan@linux.intel.com> (raw)
In-Reply-To: <1585178227-17061-1-git-send-email-jacob.jun.pan@linux.intel.com>
IOMMU UAPI can be extended in the future by adding new
fields at the end of each user data structure. Since we use
a unified UAPI version for compatibility checking, a lookup
function is needed to find the correct user data size to copy
from user.
This patch adds a helper function based on a 2D lookup with
version and type as input arguments.
---
v2: Clarify size lookup array extension rules, backfill -EINVAL
if new version introduce new union members.
---
Signed-off-by: Liu Yi L <yi.l.liu@intel.com>
Signed-off-by: Jacob Pan <jacob.jun.pan@linux.intel.com>
---
drivers/iommu/iommu.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/iommu.h | 6 +++++
2 files changed, 78 insertions(+)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index c476b58e0ffb..e91ced212653 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1724,6 +1724,78 @@ int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
}
EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);
+
+/**
+ * Maintain a UAPI version to user data structure size lookup for each
+ * API function types we support. e.g. bind guest pasid, cache invalidation.
+ * As data structures being extended with new members, offsetofend() is
+ * used to identify the size. In case of adding a new member to a union,
+ * offsetofend() applies to the largest member which may not be the newest.
+ *
+ * When new types are introduced with new versions, the new types for older
+ * version must be filled with -EINVAL.
+ *
+ * The table below documents UAPI revision history with the name of the
+ * newest member of each data structure. The largest member of a union was
+ * used for the initial version of each type.
+ *
+ * +--------------+---------------+
+ * | Type / | V1 |
+ * | UAPI Version | |
+ * +==============+===============+
+ * | BIND_GPASID | vtd |
+ * +--------------+---------------+
+ * | CACHE_INVAL | addr_info |
+ * +--------------+---------------+
+ * | PAGE_RESP | code |
+ * +--------------+---------------+
+ *
+ * Examples of future extensions:
+ * V2 addes new member to the union
+ * +--------------+---------------+---------------+
+ * | Type / | V1 | V2 |
+ * | UAPI Version | | |
+ * +==============+===============+===============+
+ * | BIND_GPASID | vtd | smmu_v3 |
+ * +--------------+---------------+---------------+
+ * | CACHE_INVAL | addr_info | new_info |
+ * +--------------+---------------+---------------+
+ * | PAGE_RESP | code | N/A |
+ * +--------------+---------------+---------------+
+ *
+ * V3 introduces a new UAPI data type: NEW_TYPE but with no new members
+ * added to the existing types.
+ * +--------------+---------------+---------------+---------------+
+ * | Type / | V1 | V2 | V3 |
+ * | UAPI Version | | | |
+ * +==============+===============+===============+===============+
+ * | BIND_GPASID | vtd | smmu_v3 | N/A |
+ * +--------------+---------------+---------------+---------------+
+ * | CACHE_INVAL | addr_info | new_info | N/A |
+ * +--------------+---------------+---------------+---------------+
+ * | PAGE_RESP | code | N/A | N/A |
+ * +--------------+---------------+---------------+---------------+
+ * | NEW_TYPE | -EINVAL | -EINVAL | largest_member|
+ * +--------------+---------------+---------------+---------------+
+ */
+const static int iommu_uapi_data_size[NR_IOMMU_UAPI_TYPE][IOMMU_UAPI_VERSION] = {
+ /* IOMMU_UAPI_BIND_GPASID */
+ {offsetofend(struct iommu_gpasid_bind_data, vtd)},
+ /* IOMMU_UAPI_CACHE_INVAL */
+ {offsetofend(struct iommu_cache_invalidate_info, addr_info)},
+ /* IOMMU_UAPI_PAGE_RESP */
+ {offsetofend(struct iommu_page_response, code)},
+};
+
+int iommu_uapi_get_data_size(int type, int version)
+{
+ if (type >= NR_IOMMU_UAPI_TYPE || version > IOMMU_UAPI_VERSION)
+ return -EINVAL;
+
+ return iommu_uapi_data_size[type][version - 1];
+}
+EXPORT_SYMBOL_GPL(iommu_uapi_get_data_size);
+
static void __iommu_detach_device(struct iommu_domain *domain,
struct device *dev)
{
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index d1b5f4d98569..4908919a98f1 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -510,6 +510,7 @@ extern int iommu_report_device_fault(struct device *dev,
struct iommu_fault_event *evt);
extern int iommu_page_response(struct device *dev,
struct iommu_page_response *msg);
+extern int iommu_uapi_get_data_size(int type, int version);
extern int iommu_group_id(struct iommu_group *group);
extern struct iommu_group *iommu_group_get_for_dev(struct device *dev);
@@ -897,6 +898,11 @@ static inline int iommu_page_response(struct device *dev,
return -ENODEV;
}
+static inline int iommu_uapi_get_data_size(int type, int version)
+{
+ return -ENODEV;
+}
+
static inline int iommu_group_id(struct iommu_group *group)
{
return -ENODEV;
--
2.7.4
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu
WARNING: multiple messages have this Message-ID (diff)
From: Jacob Pan <jacob.jun.pan@linux.intel.com>
To: Joerg Roedel <joro@8bytes.org>,
Alex Williamson <alex.williamson@redhat.com>,
"Lu Baolu" <baolu.lu@linux.intel.com>,
iommu@lists.linux-foundation.org,
LKML <linux-kernel@vger.kernel.org>,
David Woodhouse <dwmw2@infradead.org>,
Jean-Philippe Brucker <jean-philippe@linaro.com>
Cc: "Yi Liu" <yi.l.liu@intel.com>,
"Tian, Kevin" <kevin.tian@intel.com>,
Raj Ashok <ashok.raj@intel.com>,
Eric Auger <eric.auger@redhat.com>,
Jacob Pan <jacob.jun.pan@linux.intel.com>
Subject: [PATCH v2 3/3] iommu/uapi: Add helper function for size lookup
Date: Wed, 25 Mar 2020 16:17:07 -0700 [thread overview]
Message-ID: <1585178227-17061-4-git-send-email-jacob.jun.pan@linux.intel.com> (raw)
In-Reply-To: <1585178227-17061-1-git-send-email-jacob.jun.pan@linux.intel.com>
IOMMU UAPI can be extended in the future by adding new
fields at the end of each user data structure. Since we use
a unified UAPI version for compatibility checking, a lookup
function is needed to find the correct user data size to copy
from user.
This patch adds a helper function based on a 2D lookup with
version and type as input arguments.
---
v2: Clarify size lookup array extension rules, backfill -EINVAL
if new version introduce new union members.
---
Signed-off-by: Liu Yi L <yi.l.liu@intel.com>
Signed-off-by: Jacob Pan <jacob.jun.pan@linux.intel.com>
---
drivers/iommu/iommu.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/iommu.h | 6 +++++
2 files changed, 78 insertions(+)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index c476b58e0ffb..e91ced212653 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1724,6 +1724,78 @@ int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev,
}
EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid);
+
+/**
+ * Maintain a UAPI version to user data structure size lookup for each
+ * API function types we support. e.g. bind guest pasid, cache invalidation.
+ * As data structures being extended with new members, offsetofend() is
+ * used to identify the size. In case of adding a new member to a union,
+ * offsetofend() applies to the largest member which may not be the newest.
+ *
+ * When new types are introduced with new versions, the new types for older
+ * version must be filled with -EINVAL.
+ *
+ * The table below documents UAPI revision history with the name of the
+ * newest member of each data structure. The largest member of a union was
+ * used for the initial version of each type.
+ *
+ * +--------------+---------------+
+ * | Type / | V1 |
+ * | UAPI Version | |
+ * +==============+===============+
+ * | BIND_GPASID | vtd |
+ * +--------------+---------------+
+ * | CACHE_INVAL | addr_info |
+ * +--------------+---------------+
+ * | PAGE_RESP | code |
+ * +--------------+---------------+
+ *
+ * Examples of future extensions:
+ * V2 addes new member to the union
+ * +--------------+---------------+---------------+
+ * | Type / | V1 | V2 |
+ * | UAPI Version | | |
+ * +==============+===============+===============+
+ * | BIND_GPASID | vtd | smmu_v3 |
+ * +--------------+---------------+---------------+
+ * | CACHE_INVAL | addr_info | new_info |
+ * +--------------+---------------+---------------+
+ * | PAGE_RESP | code | N/A |
+ * +--------------+---------------+---------------+
+ *
+ * V3 introduces a new UAPI data type: NEW_TYPE but with no new members
+ * added to the existing types.
+ * +--------------+---------------+---------------+---------------+
+ * | Type / | V1 | V2 | V3 |
+ * | UAPI Version | | | |
+ * +==============+===============+===============+===============+
+ * | BIND_GPASID | vtd | smmu_v3 | N/A |
+ * +--------------+---------------+---------------+---------------+
+ * | CACHE_INVAL | addr_info | new_info | N/A |
+ * +--------------+---------------+---------------+---------------+
+ * | PAGE_RESP | code | N/A | N/A |
+ * +--------------+---------------+---------------+---------------+
+ * | NEW_TYPE | -EINVAL | -EINVAL | largest_member|
+ * +--------------+---------------+---------------+---------------+
+ */
+const static int iommu_uapi_data_size[NR_IOMMU_UAPI_TYPE][IOMMU_UAPI_VERSION] = {
+ /* IOMMU_UAPI_BIND_GPASID */
+ {offsetofend(struct iommu_gpasid_bind_data, vtd)},
+ /* IOMMU_UAPI_CACHE_INVAL */
+ {offsetofend(struct iommu_cache_invalidate_info, addr_info)},
+ /* IOMMU_UAPI_PAGE_RESP */
+ {offsetofend(struct iommu_page_response, code)},
+};
+
+int iommu_uapi_get_data_size(int type, int version)
+{
+ if (type >= NR_IOMMU_UAPI_TYPE || version > IOMMU_UAPI_VERSION)
+ return -EINVAL;
+
+ return iommu_uapi_data_size[type][version - 1];
+}
+EXPORT_SYMBOL_GPL(iommu_uapi_get_data_size);
+
static void __iommu_detach_device(struct iommu_domain *domain,
struct device *dev)
{
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index d1b5f4d98569..4908919a98f1 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -510,6 +510,7 @@ extern int iommu_report_device_fault(struct device *dev,
struct iommu_fault_event *evt);
extern int iommu_page_response(struct device *dev,
struct iommu_page_response *msg);
+extern int iommu_uapi_get_data_size(int type, int version);
extern int iommu_group_id(struct iommu_group *group);
extern struct iommu_group *iommu_group_get_for_dev(struct device *dev);
@@ -897,6 +898,11 @@ static inline int iommu_page_response(struct device *dev,
return -ENODEV;
}
+static inline int iommu_uapi_get_data_size(int type, int version)
+{
+ return -ENODEV;
+}
+
static inline int iommu_group_id(struct iommu_group *group)
{
return -ENODEV;
--
2.7.4
next prev parent reply other threads:[~2020-03-25 23:11 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-25 23:17 [PATCH v2 0/3] IOMMU user API enhancement Jacob Pan
2020-03-25 23:17 ` Jacob Pan
2020-03-25 23:17 ` [PATCH v2 1/3] iommu/uapi: Define uapi version and capabilities Jacob Pan
2020-03-25 23:17 ` Jacob Pan
2020-03-26 9:23 ` Christoph Hellwig
2020-03-26 9:23 ` Christoph Hellwig
2020-03-26 16:44 ` Jacob Pan
2020-03-26 16:44 ` Jacob Pan
2020-03-27 2:49 ` Tian, Kevin
2020-03-27 2:49 ` Tian, Kevin
2020-03-27 7:47 ` Christoph Hellwig
2020-03-27 7:47 ` Christoph Hellwig
2020-03-27 23:53 ` Jacob Pan
2020-03-27 23:53 ` Jacob Pan
2020-03-30 5:40 ` Tian, Kevin
2020-03-30 5:40 ` Tian, Kevin
2020-03-30 16:07 ` Jacob Pan
2020-03-30 16:07 ` Jacob Pan
2020-03-31 6:06 ` Tian, Kevin
2020-03-31 6:06 ` Tian, Kevin
2020-03-31 15:54 ` Jacob Pan
2020-03-31 15:54 ` Jacob Pan
2020-04-01 5:32 ` Tian, Kevin
2020-04-01 5:32 ` Tian, Kevin
2020-04-02 18:36 ` Jacob Pan
2020-04-02 18:36 ` Jacob Pan
2020-04-13 20:41 ` Jacob Pan
2020-04-13 20:41 ` Jacob Pan
2020-04-13 22:21 ` Alex Williamson
2020-04-13 22:21 ` Alex Williamson
2020-04-14 5:05 ` Jacob Pan
2020-04-14 5:05 ` Jacob Pan
2020-04-14 16:13 ` Alex Williamson
2020-04-14 16:13 ` Alex Williamson
2020-04-14 17:13 ` Jacob Pan
2020-04-14 17:13 ` Jacob Pan
2020-04-14 22:32 ` Jacob Pan
2020-04-14 22:32 ` Jacob Pan
2020-04-14 23:47 ` Tian, Kevin
2020-04-14 23:47 ` Tian, Kevin
2020-04-15 15:38 ` Jacob Pan
2020-04-15 15:38 ` Jacob Pan
2020-04-16 1:27 ` Tian, Kevin
2020-04-16 1:27 ` Tian, Kevin
2020-04-14 8:15 ` Christoph Hellwig
2020-04-14 8:15 ` Christoph Hellwig
2020-04-14 8:11 ` Christoph Hellwig
2020-04-14 8:11 ` Christoph Hellwig
2020-04-14 16:06 ` Jacob Pan
2020-04-14 16:06 ` Jacob Pan
2020-03-25 23:17 ` [PATCH v2 2/3] iommu/uapi: Use unified UAPI version Jacob Pan
2020-03-25 23:17 ` Jacob Pan
2020-03-25 23:17 ` Jacob Pan [this message]
2020-03-25 23:17 ` [PATCH v2 3/3] iommu/uapi: Add helper function for size lookup 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=1585178227-17061-4-git-send-email-jacob.jun.pan@linux.intel.com \
--to=jacob.jun.pan@linux.intel.com \
--cc=alex.williamson@redhat.com \
--cc=ashok.raj@intel.com \
--cc=baolu.lu@linux.intel.com \
--cc=dwmw2@infradead.org \
--cc=iommu@lists.linux-foundation.org \
--cc=jean-philippe@linaro.com \
--cc=joro@8bytes.org \
--cc=kevin.tian@intel.com \
--cc=linux-kernel@vger.kernel.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.