Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp6014821pxb; Mon, 14 Feb 2022 13:10:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJztO8mNxvbqr4foAdy5ZGinTHZXogzY4x38r9v5jaWxpWgQEzfGcfCTgqHbowZlQLjEnUZ/ X-Received: by 2002:a63:fa41:: with SMTP id g1mr795988pgk.224.1644873018592; Mon, 14 Feb 2022 13:10:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644873018; cv=none; d=google.com; s=arc-20160816; b=WUrASIuCC3UW4ep4EwICdzwRvj0SkMLdVy6NPek5doe+to9ujzX2XOL9LgFW3Z+huP zzrjE2x0u0AXwVcDH/XjNVjSQHSkhMVxWtSR+WgX/kcCO6ys+JHfOngFledIEmRkqSjz 5My6u7qQx/gp3kK3B1ZnMBVF4HDdPg1dGKSUqIzXjwUZBAgBPtTQEldfmox3uc3NmoXh YEgwJT/4m/9nXY29fwHe1p3R42lY/0/wTedy1BxceuircY2Apa50SBqvVZlTvBBbXZS6 Odd8UNfAhAd/izGoxy2zBsh4SWKpGS0QlCNNsJZ5z/LXVRgYfI8Bo6s/kkM6HN+uDH1n d+ag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=UfeDkDvoLoeHnSR3M/8TjPostzibODFLAPTt4YTFYyk=; b=B1qkRtFvs+0a2JH3VPqRc/0dbIXJ34QC1GBLgkGtImYYrepvvHJ+HjKm/kgFQjjJma XqRIQkOyFjvAHUpH8BbwySL0jakIh50tJ67zQf7iyjt4A1CTtNeO5paVW/RbgJe/QlBo dfLaOX99yrr0dtdOdiynHoDvZB9X3Y42+K8i1dPmQGzIhi0whPG38zQGsrXFldszUfBE 8KB3HWOV1jlbFAi7tkP+0QxO0Pm0SUVDLU+cfeq4nDMpRYTzqErY01XW1Q0HpmPJWeZU nZjhKprmRGilcP82R5OUhDQnsZS0DfYblZqSUrezWQF5mCKyPw5anvCV5L4VG1h4yx3h cZaQ== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id y6si14920698plg.343.2022.02.14.13.10.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Feb 2022 13:10:18 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DD73120D363; Mon, 14 Feb 2022 12:30:16 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352661AbiBNLpE (ORCPT + 99 others); Mon, 14 Feb 2022 06:45:04 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:46722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352730AbiBNLoo (ORCPT ); Mon, 14 Feb 2022 06:44:44 -0500 Received: from theia.8bytes.org (8bytes.org [IPv6:2a01:238:4383:600:38bc:a715:4b6d:a889]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F6E7CFD; Mon, 14 Feb 2022 03:39:39 -0800 (PST) Received: by theia.8bytes.org (Postfix, from userid 1000) id 727E836D; Mon, 14 Feb 2022 12:39:37 +0100 (CET) Date: Mon, 14 Feb 2022 12:39:36 +0100 From: Joerg Roedel To: Lu Baolu Cc: Alex Williamson , Robin Murphy , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Ashok Raj , Greg Kroah-Hartman , Bjorn Helgaas , Will Deacon , Dan Williams , rafael@kernel.org, Diana Craciun , Cornelia Huck , Eric Auger , Liu Yi L , Jacob jun Pan , Chaitanya Kulkarni , Stuart Yoder , Laurentiu Tudor , Thierry Reding , David Airlie , Daniel Vetter , Jonathan Hunter , Li Yang , Dmitry Osipenko , iommu@lists.linux-foundation.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v1 3/8] iommu: Extend iommu_at[de]tach_device() for multi-device groups Message-ID: References: <20220106022053.2406748-1-baolu.lu@linux.intel.com> <20220106022053.2406748-4-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220106022053.2406748-4-baolu.lu@linux.intel.com> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 On Thu, Jan 06, 2022 at 10:20:48AM +0800, Lu Baolu wrote: > int iommu_attach_device(struct iommu_domain *domain, struct device *dev) > { > struct iommu_group *group; > - int ret; > + int ret = 0; > + > + if (domain->type != IOMMU_DOMAIN_UNMANAGED) > + return -EINVAL; > > group = iommu_group_get(dev); > if (!group) > return -ENODEV; > > - /* > - * Lock the group to make sure the device-count doesn't > - * change while we are attaching > - */ > mutex_lock(&group->mutex); > - ret = -EINVAL; > - if (iommu_group_device_count(group) != 1) > - goto out_unlock; > + if (group->owner_cnt) { > + /* > + * Group has been used for kernel-api dma or claimed explicitly > + * for exclusive occupation. For backward compatibility, device > + * in a singleton group is allowed to ignore setting the > + * drv.no_kernel_api_dma field. > + */ > + if ((group->domain == group->default_domain && > + iommu_group_device_count(group) != 1) || > + group->owner) { > + ret = -EBUSY; > + goto unlock_out; > + } > + } > > - ret = __iommu_attach_group(domain, group); > + if (!group->attach_cnt) { > + ret = __iommu_attach_group(domain, group); > + if (ret) > + goto unlock_out; > + } else { > + if (group->domain != domain) { > + ret = -EPERM; > + goto unlock_out; > + } > + } > > -out_unlock: > + group->owner_cnt++; > + group->attach_cnt++; > + > +unlock_out: > mutex_unlock(&group->mutex); > iommu_group_put(group); This extends iommu_attach_device() to behave as iommu_attach_group(), changing the domain for the whole group. Wouldn't it be better to scrap the iommu_attach_device() interface instead and only rely on iommu_attach_group()? This way it is clear that a call changes the whole group. IIUC this work is heading towards allowing multiple domains in one group as long as the group is owned by one entity. That is a valid requirement, but the way to get there is in my eyes: 1) Introduce a concept of a sub-group (or whatever we want to call it), which groups devices together which must be in the same domain because they use the same request ID and thus look all the same to the IOMMU. 2) Keep todays IOMMU groups to group devices together which can bypass the IOMMU when talking to each other, like multi-function devices and devices behind a no-ACS bridge. 3) Rework group->domain and group->default_domain, eventually moving them to sub-groups. This is an important distinction to make and also the reason the iommu_attach/detach_device() interface will always be misleading. Item 1) in this list will also be beneficial to other parts of the iommu code, namely iommu-dma code which can have finer-grained DMA-API domains with sub-groups instead of groups. Regards, Joerg