Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp990094ybl; Fri, 31 Jan 2020 11:48:08 -0800 (PST) X-Google-Smtp-Source: APXvYqzfsdUskbpJWzYfEWFCFUQyGLKdpJNlAcmNkdr8ZniKQ5ltc3sJUBF4h7GY3rZIBJmxs68B X-Received: by 2002:a9d:811:: with SMTP id 17mr9126994oty.369.1580500088373; Fri, 31 Jan 2020 11:48:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580500088; cv=none; d=google.com; s=arc-20160816; b=PIaQt1b8g0OdEEFE8krfumkw8bbVMcOH3yNRJrx6z+wHD829gvJ6vxxaXbXSGZwrPD k+du5ouEqoYpBlderp6ApEDLxLLhxi+bh0TVD/bxrLjt4F9htyPUxY6dL3fMprVoElZE CmRFg8bSKb+JHOzGvtMgsfyY95OnLIzE7XWECwLOeoM4lDzIQM9Ndn1xyY2HtJG2TyLL shsm4YG6uqA54VseWG9ZXwsQk3TTDZFmquusu4ZkpGY6LkV3rPLeb8O7FLBTO410XXNM id+iHBr9qKOqw0lngrhAosDuF+7VhoYpPjCx5fmx8ur2EShyx6j+tkofoDtKjjv1i9QX Wjrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date; bh=7IR1+uURhjP3ciZxLQMFePkR0vswFyrmyK0tfY0Vj5Y=; b=dRES4jeiUdXfrOY+hOX2pwMMpyOc864RfgFHf645CMsrKetXasrZOV258cYnoErgxF nIIm7N3fo0aYS/YhELRhkUAZd1A3iCxma4G1IXDHYpCM9FR58YV9OjcnRWTuNv/L2E/o t5xTHsY5F7rfp4THERnmnv+26I1RTlxu3hSAh1wsDLv8/8WZTv9hFHW8ueRyHZeVIq9b AnZc+vw4d8/AERr/LClD70QLHJbReAeMBpc8mNsIu+Fp0HgcxBfYB/1cGPeuLQNY2R0l JGbYDZXENeRNuJK4+jM0HNqIrMeUOD+z8zf7FqaKR74sejUrEdfhoRKx9Gb0sz3OWlpm nAbg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 128si4760064oih.78.2020.01.31.11.47.55; Fri, 31 Jan 2020 11:48:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726295AbgAaTp4 (ORCPT + 99 others); Fri, 31 Jan 2020 14:45:56 -0500 Received: from mga17.intel.com ([192.55.52.151]:21848 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726001AbgAaTp4 (ORCPT ); Fri, 31 Jan 2020 14:45:56 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 31 Jan 2020 11:45:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,386,1574150400"; d="scan'208";a="218708109" Received: from jacob-builder.jf.intel.com (HELO jacob-builder) ([10.7.199.155]) by orsmga007.jf.intel.com with ESMTP; 31 Jan 2020 11:45:55 -0800 Date: Fri, 31 Jan 2020 11:51:10 -0800 From: Jacob Pan To: Alex Williamson Cc: iommu@lists.linux-foundation.org, LKML , "Lu Baolu" , Joerg Roedel , David Woodhouse , "Yi Liu" , "Tian, Kevin" , Raj Ashok , "Christoph Hellwig" , Jean-Philippe Brucker , Jonathan Cameron , Eric Auger , jacob.jun.pan@linux.intel.com Subject: Re: [PATCH 3/3] iommu/uapi: Add helper function for size lookup Message-ID: <20200131115110.1de98286@jacob-builder> In-Reply-To: <20200129151951.2e354e37@w520.home> References: <1580277724-66994-1-git-send-email-jacob.jun.pan@linux.intel.com> <1580277724-66994-4-git-send-email-jacob.jun.pan@linux.intel.com> <20200129144046.3f91e4c1@w520.home> <20200129151951.2e354e37@w520.home> Organization: OTC X-Mailer: Claws Mail 3.13.2 (GTK+ 2.24.30; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 29 Jan 2020 15:19:51 -0700 Alex Williamson wrote: > On Wed, 29 Jan 2020 14:40:46 -0700 > Alex Williamson wrote: > > > On Tue, 28 Jan 2020 22:02:04 -0800 > > Jacob Pan wrote: > > > > > 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. > > > > > > Signed-off-by: Liu Yi L > > > Signed-off-by: Jacob Pan > > > --- > > > drivers/iommu/iommu.c | 22 ++++++++++++++++++++++ > > > include/linux/iommu.h | 6 ++++++ > > > 2 files changed, 28 insertions(+) > > > > > > diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c > > > index 7dd51c5d2ba1..9e5de9abebdf 100644 > > > --- a/drivers/iommu/iommu.c > > > +++ b/drivers/iommu/iommu.c > > > @@ -1696,6 +1696,28 @@ 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, the > > > offsetofend() > > > + * will identify the new sizes. > > > + */ > > > +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)}, > > This seems prone to errors in future revisions. Both of the above > reference the end of fields within an anonymous union. When a new > field is added, it's not necessarily the newest field that needs to be > listed here, but the largest at the time. > So should the current > version always use sizeof instead (or name the union so we can > reference it)? I'm not sure of an error proof way to make sure we > keep the N-1 version consistent when we add a new version though. > More comments? > yes. we must be very careful the new size has to be the largest of the union not the newest. I agree that using sizeof() would make current version size straightforward. But we have to find the size for N-1 with offsetofend, seems more risk to the existing N-1 code when bump up version. I was thinking this array also serve as documentation of the revision history, but as you pointed out offsetofend may not be the newest member of the union. So it cannot document which member was added in which version. Seems more comments is the only way. How about comments as below with example future extensions? /** * 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. * * Current version: V1 * +--------------+---------------+ * | Type / | V1 | * | UAPI Version | | * +==============+===============+ * | BIND_GPASID | vtd | * +--------------+---------------+ * | CACHE_INVAL | addr_info | * +--------------+---------------+ * | PAGE_RESP | code | * +--------------+---------------+ * * Future extension examples: * * V2 adds new members to bind_gpasid and cache_invalidate API but not * page response. * +--------------+---------------+---------------+ * | 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| * +--------------+---------------+---------------+---------------+ * > Also, is the 12-bytes of padding in struct iommu_gpasid_bind_data > excessive with this new versioning scheme? Per rule #2 I'm not sure > if we're allowed to repurpose those padding bytes, but if we add > fields to the end of the structure as the scheme suggests, we're > stuck with not being able to expand the union for new fields. > > Thanks, > Alex > > > > + /* IOMMU_UAPI_PAGE_RESP */ > > > + {offsetofend(struct iommu_page_response, code)}, > > > +}; > > > + > > > +int iommu_uapi_get_data_size(int type, int version) > > > +{ > > > > Seems like this is asking for a bounds check, > > > > if (type >= NR_IOMMU_UAPI_TYPE || version > IOMMU_UAPI_VERSION) > > return -EINVAL; > > > > If we add new types in future versions, I assume we'd back fill the > > table with -EINVAL as well (rather than zero). Thanks, > > > > Alex > > > > > + 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 9718c109ea0a..416fe02160ba 100644 > > > --- a/include/linux/iommu.h > > > +++ b/include/linux/iommu.h > > > @@ -500,6 +500,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); @@ -885,6 +886,11 @@ static inline int > > > iommu_page_response(struct device *dev, return -ENODEV; > > > } > > > > > > +static int iommu_uapi_get_data_size(int type, int version) > > > +{ > > > + return -ENODEV; > > > +} > > > + > > > static inline int iommu_group_id(struct iommu_group *group) > > > { > > > return -ENODEV; > > > [Jacob Pan]