Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4033316ybl; Mon, 3 Feb 2020 11:13:41 -0800 (PST) X-Google-Smtp-Source: APXvYqxp6YJHipwy0D7Us5oaaWO2XTynCznWyicAB51R73fw4BHHexPFoXo2TBvVYmz/JTw8Y+4/ X-Received: by 2002:a9d:7696:: with SMTP id j22mr19692109otl.188.1580757221264; Mon, 03 Feb 2020 11:13:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580757221; cv=none; d=google.com; s=arc-20160816; b=ANYvlbEQ3Yu3/Cvg9YrpF3gbQo33MEI8ke9XTJNj9JUVOkKJSWwakG+cgE9OnYCAVT 3EfQVoBV6yf3TsPxGuXrypGABzG0pQaCgfXb4orIQKsn68vW/zcbhxO/aYaKO9U+3XYI 7/vN4Izbp1MwaSmxxWVMGUv4T6Xlv71aw8iItOmRvBBFoG+rxjxxKWJhgJXSezEeUEvq e0CXo0YWZvvES4e7Z9+C/DB6QmN8yvPSYbZU7xS1+4mIkvC95rKsXG9VAKEKXeeECQwf xr/wS/NO8+vQLRHW0wDV2MGqfkOggQv7YJM2NFl6mTxgPU0nJxcdVTK6Q2Y9LTgqURKn IGJA== 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 :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=ScQAELNsrhFhctu+0tG8n84o4vjKl6zh78J/ARkQSJ4=; b=QzVuCdRdDxQHw+b7hztbjX0szMbiqVuOPIi2poTYCq0gdJEOGynHzQlPn1X+fok6pa lKjtl032BHZr9/5GZgIfYFibA4qEYd/JwobD5QY9KvhF5q0Plr+TgocGbxqChpt2/1/9 v7pCsnenhijjM0gVEcM8sITl4YfDJAPyWwPmAYJtA5AwvRQTIJtpqdIHAsf3AcTptVZ6 LrnHSZyzFALSSEfrB8LE8/4Mi9fkP2jSu/EMEugOCJPfo9PQ+nVu4pF9IvHqQyz+IVqJ E9qzPmeOg2bgWdqJTSVLVusEYYr+RG6ABdC2tf8hdKHCWIXQcB7wV3qZ1a6yTHM8UwBo fXrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=L0ZbVzFH; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h203si8934346oif.3.2020.02.03.11.13.28; Mon, 03 Feb 2020 11:13:41 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=L0ZbVzFH; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729688AbgBCS1V (ORCPT + 98 others); Mon, 3 Feb 2020 13:27:21 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:32849 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727124AbgBCS1U (ORCPT ); Mon, 3 Feb 2020 13:27:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580754438; h=from:from: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; bh=ScQAELNsrhFhctu+0tG8n84o4vjKl6zh78J/ARkQSJ4=; b=L0ZbVzFHGo/ihcvLl9vN2fGAt/2y1bR+jeGs7ulQotVVGty3sVkPFecmenZ5dXPvM3/Npo 1jTejyoNm6y3oyLzqoWxSUT+MVINvWbHVBzi+o0qtnLKgjz4lxdw3BVVCABXeuZpWbk5u3 AlCb/dTnEnEE1szyvgA/wdGLILix3Pw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-266-3Fq-OHPgNXirtzdTnknZfQ-1; Mon, 03 Feb 2020 13:27:14 -0500 X-MC-Unique: 3Fq-OHPgNXirtzdTnknZfQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 99696A0CC4; Mon, 3 Feb 2020 18:27:12 +0000 (UTC) Received: from w520.home (ovpn-116-28.phx2.redhat.com [10.3.116.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 509F719C7F; Mon, 3 Feb 2020 18:27:09 +0000 (UTC) Date: Mon, 3 Feb 2020 11:27:08 -0700 From: Alex Williamson To: Jacob Pan 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 Subject: Re: [PATCH 3/3] iommu/uapi: Add helper function for size lookup Message-ID: <20200203112708.14174ce2@w520.home> In-Reply-To: <20200131155125.53475a72@jacob-builder> 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> <20200131155125.53475a72@jacob-builder> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 31 Jan 2020 15:51:25 -0800 Jacob Pan wrote: > Hi Alex, > Sorry I missed this part in the previous reply. Comments below. > > On Wed, 29 Jan 2020 15:19:51 -0700 > Alex Williamson wrote: > > > 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, > We can still use the padding bytes as long as there is a new flag bit > to indicate the validity of the new filed within the padding. > I should have made it clear in rule #2 when mentioning the flags bits. > Should define what extension constitutes. > How about this? > " > * 2. Data structures are open to extension but closed to modification. > * Extension should leverage the padding bytes first where a new > * flag bit is required to indicate the validity of each new member. > * The above rule for padding bytes also applies to adding new union > * members. > * After padding bytes are exhausted, new fields must be added at the > * end of each data structure with 64bit alignment. Flag bits can be > * added without size change but existing ones cannot be altered. > * > " > So if we add new field by doing re-purpose of padding bytes, size > lookup result will remain the same. New code would recognize the new > flag, old code stays the same. > > VFIO layer checks for UAPI compatibility and size to copy, version > sanity check and flag usage are done in the IOMMU code. > > > 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. > Good point, it does sound contradictory. I hope the rewritten rule #2 > address that. > Adding data after the union should be extremely rare. Do you see any > issues with the example below? > > offsetofend() can still find the right size. > e.g. > V1 > struct iommu_gpasid_bind_data { > __u32 version; > #define IOMMU_PASID_FORMAT_INTEL_VTD 1 > __u32 format; > #define IOMMU_SVA_GPASID_VAL (1 << 0) /* guest PASID valid */ > __u64 flags; > __u64 gpgd; > __u64 hpasid; > __u64 gpasid; > __u32 addr_width; > __u8 padding[12]; > /* Vendor specific data */ > union { > struct iommu_gpasid_bind_data_vtd vtd; > }; > }; > > 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)}, ... > }; > > V2, Add new_member at the end (forget padding for now). > struct iommu_gpasid_bind_data { > __u32 version; > #define IOMMU_PASID_FORMAT_INTEL_VTD 1 > __u32 format; > #define IOMMU_SVA_GPASID_VAL (1 << 0) /* guest PASID valid */ > #define IOMMU_NEW_MEMBER_VAL (1 << 1) /* new member added */ > __u64 flags; > __u64 gpgd; > __u64 hpasid; > __u64 gpasid; > __u32 addr_width; > __u8 padding[12]; > /* Vendor specific data */ > union { > struct iommu_gpasid_bind_data_vtd vtd; > }; > __u64 new_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), offsetofend(struct iommu_gpasid_bind_data,new_member)}, > > }; > > V3, Add smmu to the union,larger than vtd > > struct iommu_gpasid_bind_data { > __u32 version; > #define IOMMU_PASID_FORMAT_INTEL_VTD 1 > #define IOMMU_PASID_FORMAT_INTEL_SMMU 2 > __u32 format; > #define IOMMU_SVA_GPASID_VAL (1 << 0) /* guest PASID valid */ > #define IOMMU_NEW_MEMBER_VAL (1 << 1) /* new member added */ > #define IOMMU_SVA_SMMU_SUPP (1 << 2) /* SMMU data supported */ > __u64 flags; > __u64 gpgd; > __u64 hpasid; > __u64 gpasid; > __u32 addr_width; > __u8 padding[12]; > /* Vendor specific data */ > union { > struct iommu_gpasid_bind_data_vtd vtd; > struct iommu_gpasid_bind_data_smmu smmu; > }; > __u64 new_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), > offsetofend(struct iommu_gpasid_bind_data, new_member), > offsetofend(struct iommu_gpasid_bind_data, new_member)}, > ... > }; > How are you not breaking rule #3, "Versions are backward compatible" with this? If the kernel is at version 3 and userspace is at version 2 then new_member exists at different offsets of the structure. The kernels iommu_uapi_data_size for V2 changed between version 2 and 3. Thanks, Alex