From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6158FC369D5 for ; Mon, 28 Apr 2025 21:37:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ZsfJi5GV4gNJPXGtyMAi/qAe8sWnYVRwN1YewUo6rSc=; b=VwViC7KpHmSTUhx6iJuNIT61nd e4ICDFxn/Mcg/psh7g2eS6mPg3hTjU7XgOkZmnH5JeoEx6/ifBPtTPcTQAXWRwjNR2HXFDsimecf4 vXSypRKJh6RPwP12XhbqknsibrOMllN3CiQ3YfgTMUfE2L2vQj00buqP/tq7TWFH/HOP3jqyzOdGB BJkbkghtd/oiFAQoYIsNLGBIEDrAzSInGYPykItSYG8f3lSfO40fZSnwyIMmm3cFQn3iTEDM+OCKJ XzDmnRw21X0R+mdrLrqdDEh6ws9NpfensNrCmawjy4HDTqnpSS3FYP8mmoX+M2yzDx34Q/FVBa791 dMZTQtiQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1u9WAf-00000007djW-33cb; Mon, 28 Apr 2025 21:37:49 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1u9W7Q-00000007d23-3AWz for linux-arm-kernel@bombadil.infradead.org; Mon, 28 Apr 2025 21:34:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=ZsfJi5GV4gNJPXGtyMAi/qAe8sWnYVRwN1YewUo6rSc=; b=mBRTJvcnJs3q1Y6LyJHdXYVjCN cMtIkZP3jQ50UtgOaQwW2D3DzAMRS9zosZpSRUqpLGv25m+xsCGlnJvgMu/0MegB5WhrFvEISc+3s KbyWnRUsbTLHGcru5VzTKsHuZbpmwfkro9oMCXuaHuoYh/deie4D3II2vcdPl1lgahZ5s0sd9EjHP Z5HYp44wriP57F7Eb0o1emnjMfT73RjEsUWueMjlueFkkYoic4+MOuMS/J9ukM1Jo9pLGIANgmLbM GmLBfP9VYuls7zgewN07SXvVA2QlML4FsJM0rKrAjkVsL5PACd27aqjCQHaJ17bnBktp4ad4aXJsj L0p78xaA==; Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by desiato.infradead.org with esmtps (Exim 4.98.1 #2 (Red Hat Linux)) id 1u9W7H-0000000D3vU-32dc for linux-arm-kernel@lists.infradead.org; Mon, 28 Apr 2025 21:34:26 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-2264c9d0295so3985ad.0 for ; Mon, 28 Apr 2025 14:34:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1745876057; x=1746480857; darn=lists.infradead.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=ZsfJi5GV4gNJPXGtyMAi/qAe8sWnYVRwN1YewUo6rSc=; b=Qzod3WS3VNnonR28aDfzo9Mag8k5eUR9l4ycyB8S2eewhAZMPzYmKyy1w2pEl+TvtW azEZPtCWrjeAR3rZWogV1GDDE+Mc9Et941oYAUSulV5RjTuyplmaoE1oNDlLzDitZ8mR 5Vj5cSeRCD7VI8zyDgOY4bHtER0PQl1+HiWJzWMSIll94nj5TfSZxaEcS/U6L8bu6f43 yeEYFrsamslYk3EMEynLjUhxWRPX8k7+Cuo3XUM3pVh8ZFJqxVCtAx/zxOFDgcZfPx5V 3/9zP0ajORo78ZWQpk1OAqUOU/JHI27noIskwAuKdYGncBEJL4BvwEcAI94geCLq4cc0 1Yrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745876057; x=1746480857; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ZsfJi5GV4gNJPXGtyMAi/qAe8sWnYVRwN1YewUo6rSc=; b=M4XfOih4fLmvxLX6Qz4mu37T+iYze9mFtYzay61SKNkZVaoAIoNe0eKHPbLBitgfjN FUR3zdgJlhfeDsGqo6e1qT6zf4ZY4vzkjJrOkewXm3X2ec+t9ban7asq3FWQRHo5tUYh DQt8yNvXVJPs01XqhzcM5i5WAa4FSgT+wbhaVOVOLB4U5eQkRTIfwbOmy4eVfC8ZuVeq rVTFov4LFHfNp4nw80CEpZMqLPUwZlhEd2Gq1Hm7T9lio+jx7+9XGPUKjnaTPRDeMNMv 4cC7WogwwUkYoaiCoqRDUI5xRTRMx0fKqRpgvCa9tVZL1qTnsY3udQff/t5Yn3Y96ehC hwHg== X-Forwarded-Encrypted: i=1; AJvYcCXrgq9lA9e8airMIqDUpJRX4Pu1aHeRPO2faYcCodIJ1cwpf/8SlXQ3u1ebrGrAeCj7OKHU89DZb5q7PgylfAo/@lists.infradead.org X-Gm-Message-State: AOJu0YxuXs50y7MTkru802kyqAf9GQuV/EZcVZhw9/kjQUsw9C7/jBwi NMQdGs3YJM8MwpHn0bV9fk/zjYbWiUNToNeP++fsZK1ObRAKo/mFi9DvLzklgQ== X-Gm-Gg: ASbGnctCnjpYWTftap0mJWCe06H5t0CaAlxSAGDghwpppezeU+ei04ZFbCavUbLYbfY JDK+0umnx+e86VbtmdnBdi7VNUHGqK0IpCbQwDB7EA0ZG0qCI7ZktwCOPNe4BTWlflJKbCKubPE ur7iGPRGPegNTR+9q7fp6/sONpbm3MLrXhzlgjGRtuScc1MLyXyr7XHhWHLTr8zrPiGHT74JQjq xzzVVf9DR1RtHkSsJX9bEfmFVxhWAvHq659P4+542rm6Q5lcKCMnRGyAiFqv6bGWUR/Ocu247Yd OLepYqbqxT++XMboDJccg4GZpPL0YUt4mPxYT6QKJBqf3DPKNzLJ7xl5TfRLutaClDB0GsjF X-Google-Smtp-Source: AGHT+IE+KaoUm6FAcva/asMGpdAdvSvxi6I/nzVOjZdBDcnh+kzvq6u61j/01qvHClHVKIVW/FIR3A== X-Received: by 2002:a17:903:124d:b0:223:37ec:63be with SMTP id d9443c01a7336-22de6c49626mr880365ad.4.1745876056773; Mon, 28 Apr 2025 14:34:16 -0700 (PDT) Received: from google.com (2.210.143.34.bc.googleusercontent.com. [34.143.210.2]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22db50e75fdsm88595955ad.120.2025.04.28.14.34.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Apr 2025 14:34:16 -0700 (PDT) Date: Mon, 28 Apr 2025 21:34:05 +0000 From: Pranjal Shrivastava To: Nicolin Chen Cc: jgg@nvidia.com, kevin.tian@intel.com, corbet@lwn.net, will@kernel.org, bagasdotme@gmail.com, robin.murphy@arm.com, joro@8bytes.org, thierry.reding@gmail.com, vdumpa@nvidia.com, jonathanh@nvidia.com, shuah@kernel.org, jsnitsel@redhat.com, nathan@kernel.org, peterz@infradead.org, yi.l.liu@intel.com, mshavit@google.com, zhangzekun11@huawei.com, iommu@lists.linux.dev, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-tegra@vger.kernel.org, linux-kselftest@vger.kernel.org, patches@lists.linux.dev, mochs@nvidia.com, alok.a.tiwari@oracle.com, vasant.hegde@amd.com Subject: Re: [PATCH v2 10/22] iommufd/viommmu: Add IOMMUFD_CMD_VCMDQ_ALLOC ioctl Message-ID: References: <094992b874190ffdcf6012104b419c8649b5e4b4.1745646960.git.nicolinc@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <094992b874190ffdcf6012104b419c8649b5e4b4.1745646960.git.nicolinc@nvidia.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250428_223424_314433_D19BB26A X-CRM114-Status: GOOD ( 35.97 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, Apr 25, 2025 at 10:58:05PM -0700, Nicolin Chen wrote: > Introduce a new IOMMUFD_CMD_VCMDQ_ALLOC ioctl for user space to allocate > a vCMDQ for a vIOMMU object. Simply increase the refcount of the vIOMMU. > > Signed-off-by: Nicolin Chen > --- > drivers/iommu/iommufd/iommufd_private.h | 2 + > include/uapi/linux/iommufd.h | 41 +++++++++++ > drivers/iommu/iommufd/main.c | 6 ++ > drivers/iommu/iommufd/viommu.c | 94 +++++++++++++++++++++++++ > 4 files changed, 143 insertions(+) > > diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h > index 79160b039bc7..b974c207ae8a 100644 > --- a/drivers/iommu/iommufd/iommufd_private.h > +++ b/drivers/iommu/iommufd/iommufd_private.h > @@ -611,6 +611,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd); > void iommufd_viommu_destroy(struct iommufd_object *obj); > int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd); > void iommufd_vdevice_destroy(struct iommufd_object *obj); > +int iommufd_vcmdq_alloc_ioctl(struct iommufd_ucmd *ucmd); > +void iommufd_vcmdq_destroy(struct iommufd_object *obj); > > #ifdef CONFIG_IOMMUFD_TEST > int iommufd_test(struct iommufd_ucmd *ucmd); > diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h > index cc90299a08d9..06a763fda47f 100644 > --- a/include/uapi/linux/iommufd.h > +++ b/include/uapi/linux/iommufd.h > @@ -56,6 +56,7 @@ enum { > IOMMUFD_CMD_VDEVICE_ALLOC = 0x91, > IOMMUFD_CMD_IOAS_CHANGE_PROCESS = 0x92, > IOMMUFD_CMD_VEVENTQ_ALLOC = 0x93, > + IOMMUFD_CMD_VCMDQ_ALLOC = 0x94, > }; > > /** > @@ -1147,4 +1148,44 @@ struct iommu_veventq_alloc { > __u32 __reserved; > }; > #define IOMMU_VEVENTQ_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VEVENTQ_ALLOC) > + > +/** > + * enum iommu_vcmdq_type - Virtual Command Queue Type > + * @IOMMU_VCMDQ_TYPE_DEFAULT: Reserved for future use > + */ > +enum iommu_vcmdq_type { > + IOMMU_VCMDQ_TYPE_DEFAULT = 0, > +}; > + > +/** > + * struct iommu_vcmdq_alloc - ioctl(IOMMU_VCMDQ_ALLOC) > + * @size: sizeof(struct iommu_vcmdq_alloc) > + * @flags: Must be 0 > + * @viommu_id: Virtual IOMMU ID to associate the virtual command queue with > + * @type: One of enum iommu_vcmdq_type > + * @index: The logical index to the virtual command queue per virtual IOMMU, for > + * a multi-queue model > + * @out_vcmdq_id: The ID of the new virtual command queue > + * @addr: Base address of the queue memory in the guest physical address space > + * @length: Length of the queue memory in the guest physical address space > + * > + * Allocate a virtual command queue object for a vIOMMU-specific HW-accelerated > + * feature that can access a guest queue memory described by @addr and @length. > + * It's suggested for VMM to back the queue memory using a single huge page with > + * a proper alignment for its contiguity in the host physical address space. The > + * call will fail, if the queue memory is not contiguous in the physical address > + * space. Upon success, its underlying physical pages will be pinned to prevent > + * VMM from unmapping them in the IOAS, until the virtual CMDQ gets destroyed. > + */ > +struct iommu_vcmdq_alloc { > + __u32 size; > + __u32 flags; > + __u32 viommu_id; > + __u32 type; > + __u32 index; > + __u32 out_vcmdq_id; > + __aligned_u64 addr; > + __aligned_u64 length; > +}; > +#define IOMMU_VCMDQ_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VCMDQ_ALLOC) > #endif > diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c > index 2b9ee9b4a424..ac51d5cfaa61 100644 > --- a/drivers/iommu/iommufd/main.c > +++ b/drivers/iommu/iommufd/main.c > @@ -303,6 +303,7 @@ union ucmd_buffer { > struct iommu_ioas_map map; > struct iommu_ioas_unmap unmap; > struct iommu_option option; > + struct iommu_vcmdq_alloc vcmdq; > struct iommu_vdevice_alloc vdev; > struct iommu_veventq_alloc veventq; > struct iommu_vfio_ioas vfio_ioas; > @@ -358,6 +359,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { > IOCTL_OP(IOMMU_IOAS_UNMAP, iommufd_ioas_unmap, struct iommu_ioas_unmap, > length), > IOCTL_OP(IOMMU_OPTION, iommufd_option, struct iommu_option, val64), > + IOCTL_OP(IOMMU_VCMDQ_ALLOC, iommufd_vcmdq_alloc_ioctl, > + struct iommu_vcmdq_alloc, length), > IOCTL_OP(IOMMU_VDEVICE_ALLOC, iommufd_vdevice_alloc_ioctl, > struct iommu_vdevice_alloc, virt_id), > IOCTL_OP(IOMMU_VEVENTQ_ALLOC, iommufd_veventq_alloc, > @@ -501,6 +504,9 @@ static const struct iommufd_object_ops iommufd_object_ops[] = { > [IOMMUFD_OBJ_IOAS] = { > .destroy = iommufd_ioas_destroy, > }, > + [IOMMUFD_OBJ_VCMDQ] = { > + .destroy = iommufd_vcmdq_destroy, > + }, > [IOMMUFD_OBJ_VDEVICE] = { > .destroy = iommufd_vdevice_destroy, > }, When do we expect the VMM to use this ioctl? While it's spawning a new VM? IIUC, one vintf can have multiple lvcmdqs and looking at the series it looks like the vcmdq_alloc allocates a single lvcmdq. Is the plan to dedicate one lvcmdq to per VM? Which means VMs can share a vintf? Or do we plan to trap access to trap the access everytime the VM accesses an lvcmdq base register? > diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c > index a65153458a26..02a111710ffe 100644 > --- a/drivers/iommu/iommufd/viommu.c > +++ b/drivers/iommu/iommufd/viommu.c > @@ -170,3 +170,97 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) > iommufd_put_object(ucmd->ictx, &viommu->obj); > return rc; > } > + > +void iommufd_vcmdq_destroy(struct iommufd_object *obj) > +{ > + struct iommufd_vcmdq *vcmdq = > + container_of(obj, struct iommufd_vcmdq, obj); > + struct iommufd_viommu *viommu = vcmdq->viommu; > + > + if (viommu->ops->vcmdq_destroy) > + viommu->ops->vcmdq_destroy(vcmdq); > + iopt_unpin_pages(&viommu->hwpt->ioas->iopt, vcmdq->addr, vcmdq->length); > + refcount_dec(&viommu->obj.users); > +} > + > +int iommufd_vcmdq_alloc_ioctl(struct iommufd_ucmd *ucmd) > +{ > + struct iommu_vcmdq_alloc *cmd = ucmd->cmd; > + struct iommufd_viommu *viommu; > + struct iommufd_vcmdq *vcmdq; > + struct page **pages; > + int max_npages, i; > + dma_addr_t end; > + int rc; > + > + if (cmd->flags || cmd->type == IOMMU_VCMDQ_TYPE_DEFAULT) > + return -EOPNOTSUPP; The cmd->type check is a little confusing here, I think we could re-order the series and add this check when we have the CMDQV type. Alternatively, we could keep this in place and add the driver-specific vcmdq_alloc op calls when it's added/available for Tegra CMDQV while stubbing out the rest of this function accordingly. > + if (!cmd->addr || !cmd->length) > + return -EINVAL; > + if (check_add_overflow(cmd->addr, cmd->length - 1, &end)) > + return -EOVERFLOW; > + > + max_npages = DIV_ROUND_UP(cmd->length, PAGE_SIZE); > + pages = kcalloc(max_npages, sizeof(*pages), GFP_KERNEL); > + if (!pages) > + return -ENOMEM; > + > + viommu = iommufd_get_viommu(ucmd, cmd->viommu_id); > + if (IS_ERR(viommu)) { > + rc = PTR_ERR(viommu); > + goto out_free; > + } > + > + if (!viommu->ops || !viommu->ops->vcmdq_alloc) { > + rc = -EOPNOTSUPP; > + goto out_put_viommu; > + } > + > + /* Quick test on the base address */ > + if (!iommu_iova_to_phys(viommu->hwpt->common.domain, cmd->addr)) { > + rc = -ENXIO; > + goto out_put_viommu; > + } > + > + /* The underlying physical pages must be pinned in the IOAS */ > + rc = iopt_pin_pages(&viommu->hwpt->ioas->iopt, cmd->addr, cmd->length, > + pages, 0); > + if (rc) > + goto out_put_viommu; > + > + /* Validate if the underlying physical pages are contiguous */ > + for (i = 1; i < max_npages && pages[i]; i++) { > + if (page_to_pfn(pages[i]) == page_to_pfn(pages[i - 1]) + 1) > + continue; > + rc = -EFAULT; > + goto out_unpin; > + } > + > + vcmdq = viommu->ops->vcmdq_alloc(viommu, cmd->type, cmd->index, > + cmd->addr, cmd->length); > + if (IS_ERR(vcmdq)) { > + rc = PTR_ERR(vcmdq); > + goto out_unpin; > + } > + > + vcmdq->viommu = viommu; > + refcount_inc(&viommu->obj.users); > + vcmdq->addr = cmd->addr; > + vcmdq->ictx = ucmd->ictx; > + vcmdq->length = cmd->length; > + cmd->out_vcmdq_id = vcmdq->obj.id; > + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); > + if (rc) > + iommufd_object_abort_and_destroy(ucmd->ictx, &vcmdq->obj); > + else > + iommufd_object_finalize(ucmd->ictx, &vcmdq->obj); > + goto out_put_viommu; > + > +out_unpin: > + iopt_unpin_pages(&viommu->hwpt->ioas->iopt, cmd->addr, cmd->length); > +out_put_viommu: > + iommufd_put_object(ucmd->ictx, &viommu->obj); > +out_free: > + kfree(pages); > + return rc; > +} > -- > 2.43.0 >