From patchwork Thu Oct 26 04:39:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 158380 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp423289vqb; Wed, 25 Oct 2023 21:40:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEPHq3To4xBt/h/ZmEYYEXiWSgxGzSwa/4jZQ6dzRumNf8zWiYobL7mllhskZYIkZvbzM8O X-Received: by 2002:a25:50c7:0:b0:d9a:e337:b6a with SMTP id e190-20020a2550c7000000b00d9ae3370b6amr15889924ybb.61.1698295231778; Wed, 25 Oct 2023 21:40:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698295231; cv=none; d=google.com; s=arc-20160816; b=urlSY1J25X7V1uZ9k+rCnQ9A9XnATiZmP3H9Xct11rnWljMGUqb9dBjsbmqiAWc/Q4 y5EmPc37O0+/USQfXiS3yiKdvUz1FppzGLtrL7HEz7jE8NcsYVRbN4Dn8zcRVEMTHbjJ ZYFvwR+gsTTeiUtD5s5iYWhJli4jHcvsyCPrS0A5BO/C0fvjFdFyWvXana3fUndnJP2x jx5ebHPoFWeYp7v6F+6bRkik2P0+NVcJz0kCuvktdsivCxx1VnXTqVYwWrNK7xNsf1kl 72a/uxnaMMJyPwQLX4OxV4kBmSumVa066IJSvJ5AvpDyXDTdQqFq28wfxQn8Cif2GLg9 UTcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=M3HqrdrUP/8+ESOYQ9hu+2Z0lc/2AqysQRqav7IRS+A=; fh=AYfEg8UxxCZNlRHxnJWvHuqsheVQzM8ymhb/+J0VVdk=; b=uDQNNgx0HcUMv1zoo/BcWw2uBnwJW9hqNE9fflnzDQQglbCnkYjgTh9bZ2T9DYWhqb Cy3vQLGFR4TrAzMtnGcYF1q91sa/jUh0M/OM/+A+XCwkp37jSKWbbuml2IN291UAl0Jd QSHSdsgAMdwPUTE7ZRK+gjJ0gnfkIyA0IlFIimPZMvaTnlZ4uBRomlhRmcH9LFRjff7x Ynea4n5GhGnk+dpW66J0PhF0XdAd+dITWtWrCNz/6SKmjVWFZ68go+Yk4BV95SitC5cv /mw6SH7POtII4I9lYbDtcgn4bZcUNrJbmGUOUEw+EdgS+IJvojXmu2rASRYytyXn0E9p JTDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=PJ+0Kqok; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id e11-20020a25d30b000000b00d7e34de1692si13772139ybf.250.2023.10.25.21.40.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 21:40:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=PJ+0Kqok; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id F139081B94D0; Wed, 25 Oct 2023 21:40:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235005AbjJZEkW (ORCPT + 26 others); Thu, 26 Oct 2023 00:40:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343709AbjJZEjz (ORCPT ); Thu, 26 Oct 2023 00:39:55 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 137EB199; Wed, 25 Oct 2023 21:39:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698295191; x=1729831191; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=aSH3E8hLAKJHAaGomN3035I8KujEFt92c5kdITF4+n4=; b=PJ+0KqokgKsph6j0KwneF4qZazSaW9uKHKLhWPBoYMK1+gvLCyureoS2 YlDxuF5+6298sRx8Fi6ZtqLOsEWJBs+D2pMv7Tzt2aPKuCnKinOBUOFij TGDTaTZyQbiCo3JtvaqGo96qxpU4c79UiAF0F/HnlSWJn1RkNdQXQ5soo j1hntkl55L+UQ+t8acSP88Fb2L2nvwZT1/pCxl+v7qnz9ouHggX62gmTV WGuPZ21CDQEPjjhVR4rUINKJQ15eA/ctHLdPTkIz1DrkkOIk5kRc0gun/ /G2yI1jthsS+5TMgsNx/vQqiIw5g5gM9wPjFdnuqMcz+Ws9BQ6JQwKscm Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10874"; a="9014534" X-IronPort-AV: E=Sophos;i="6.03,252,1694761200"; d="scan'208";a="9014534" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Oct 2023 21:39:47 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10874"; a="708931342" X-IronPort-AV: E=Sophos;i="6.03,252,1694761200"; d="scan'208";a="708931342" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by orsmga003.jf.intel.com with ESMTP; 25 Oct 2023 21:39:45 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com, xin.zeng@intel.com Subject: [PATCH v7 08/10] iommu: Add iommu_copy_struct_from_user helper Date: Wed, 25 Oct 2023 21:39:36 -0700 Message-Id: <20231026043938.63898-9-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231026043938.63898-1-yi.l.liu@intel.com> References: <20231026043938.63898-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 25 Oct 2023 21:40:30 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780791620901535065 X-GMAIL-MSGID: 1780791620901535065 From: Nicolin Chen Wrap up the data type/pointer/len sanity and a copy_struct_from_user call for iommu drivers to copy driver specific data via struct iommu_user_data. And expect it to be used in the domain_alloc_user op for example. Signed-off-by: Nicolin Chen Co-developed-by: Yi Liu Signed-off-by: Yi Liu Reviewed-by: Kevin Tian Reviewed-by: Jason Gunthorpe Signed-off-by: Jason Gunthorpe --- include/linux/iommu.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 2ddd99f55471..8fb1b41b4d15 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -280,6 +280,46 @@ struct iommu_user_data { size_t len; }; +/** + * __iommu_copy_struct_from_user - Copy iommu driver specific user space data + * @dst_data: Pointer to an iommu driver specific user data that is defined in + * include/uapi/linux/iommufd.h + * @src_data: Pointer to a struct iommu_user_data for user space data info + * @data_type: The data type of the @dst_data. Must match with @src_data.type + * @data_len: Length of current user data structure, i.e. sizeof(struct _dst) + * @min_len: Initial length of user data structure for backward compatibility. + * This should be offsetofend using the last member in the user data + * struct that was initially added to include/uapi/linux/iommufd.h + */ +static inline int __iommu_copy_struct_from_user( + void *dst_data, const struct iommu_user_data *src_data, + unsigned int data_type, size_t data_len, size_t min_len) +{ + if (src_data->type != data_type) + return -EINVAL; + if (WARN_ON(!dst_data || !src_data)) + return -EINVAL; + if (src_data->len < min_len || data_len < src_data->len) + return -EINVAL; + return copy_struct_from_user(dst_data, data_len, src_data->uptr, + src_data->len); +} + +/** + * iommu_copy_struct_from_user - Copy iommu driver specific user space data + * @kdst: Pointer to an iommu driver specific user data that is defined in + * include/uapi/linux/iommufd.h + * @user_data: Pointer to a struct iommu_user_data for user space data info + * @data_type: The data type of the @kdst. Must match with @user_data->type + * @min_last: The last memember of the data structure @kdst points in the + * initial version. + * Return 0 for success, otherwise -error. + */ +#define iommu_copy_struct_from_user(kdst, user_data, data_type, min_last) \ + __iommu_copy_struct_from_user(kdst, user_data, data_type, \ + sizeof(*kdst), \ + offsetofend(typeof(*kdst), min_last)) + /** * struct iommu_ops - iommu ops and capabilities * @capable: check capability