Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3597125ybi; Fri, 5 Jul 2019 10:11:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqzg5uStzyJGrOez6WfQD5cDioYMSOI94I6BU/fYXphV40yjRPhqeEjfFuxRkvEMpx5Zl9rl X-Received: by 2002:a17:90a:7787:: with SMTP id v7mr6813569pjk.143.1562346670495; Fri, 05 Jul 2019 10:11:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562346670; cv=none; d=google.com; s=arc-20160816; b=EYvB4nZpDD1ojykTZbCsbe7t3eyRqHFuthXIQk+4LQQk3BwL7JrSAOWqxFH8XcMyQo duoBQ4KXr2N1u4qYPJ7xIu/DePf/Hnr89oyD1RITSeNuoH6AjuyoLjvgRRmv5XHrb/JF 1AzCufc8kFw3jjMDebD2hvXjtl7kDzMhSk2a8MXN7lENnECdItsz4ZDduLqVNrORORn6 ycyDO/Fnj+BLoaeDzQ1OysUcdy5nndfilWhojnH80EasHy9vjSRxiXvJBPH6AVxKd/UI 52horhpW7TQVWE0ZGun4qQSXoV60cgA47fFHLg4RdcEDXvIy+Q5dEjGUXrhyVuWHXCcV G0bQ== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:references:cc:to:subject:from; bh=er/ItgiWSJXHBNpbMHkc7eSyXsBSgW/oRdNfeVxiWC8=; b=ZhFmkEp3nIRAzD+2RHKKD15Lh5YBO8uj3GMBBkNqaFLd6hAJTKD+WECooThGokUB52 Xi/XQ9b8M5UezhrxXhA5Aag51dh89756adGJjV94qqeH3ppLAsTCeaNhNnQoj3kCh8C1 4dci/SyPm/i2ooFtX3qzPJ1D+Z1WZXj5N+MeAhVRmsIJQCikbi7P3jvcxc1eT/B2a+hJ 5daKTSrZtE0u04+Z4RPR9wTSAt0WXKMD+m/axG0aEWWs7IwUOuQ/JuSrFYxPPs9YK1Zr dZwtR7eNGm6wCfJjnP5wrY5bE81l22WI4pH9gBsde66f5TrEkAe++vm2E+yCzqGEzVAw pjUw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d12si9479793pla.121.2019.07.05.10.10.54; Fri, 05 Jul 2019 10:11:10 -0700 (PDT) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728234AbfGEQ3L (ORCPT + 99 others); Fri, 5 Jul 2019 12:29:11 -0400 Received: from foss.arm.com ([217.140.110.172]:43616 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725917AbfGEQ3L (ORCPT ); Fri, 5 Jul 2019 12:29:11 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 736042B; Fri, 5 Jul 2019 09:29:10 -0700 (PDT) Received: from [10.1.38.41] (unknown [10.1.38.41]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D90FC3F246; Fri, 5 Jul 2019 09:29:07 -0700 (PDT) From: Jean-Philippe Brucker Subject: Re: [PATCH 6/8] iommu/arm-smmu-v3: Support auxiliary domains To: Will Deacon Cc: Will Deacon , "joro@8bytes.org" , "robh+dt@kernel.org" , Mark Rutland , Robin Murphy , "jacob.jun.pan@linux.intel.com" , "iommu@lists.linux-foundation.org" , "devicetree@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "eric.auger@redhat.com" References: <20190610184714.6786-1-jean-philippe.brucker@arm.com> <20190610184714.6786-7-jean-philippe.brucker@arm.com> <20190626175959.ubxvb2qn4taclact@willie-the-truck> Message-ID: <7cad065e-5eba-bd22-5c1d-c55ad315ace0@arm.com> Date: Fri, 5 Jul 2019 17:29:06 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.1 MIME-Version: 1.0 In-Reply-To: <20190626175959.ubxvb2qn4taclact@willie-the-truck> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 26/06/2019 18:59, Will Deacon wrote: >> +static void arm_smmu_aux_detach_dev(struct iommu_domain *domain, struct device *dev) >> +{ >> + struct iommu_domain *parent_domain; >> + struct arm_smmu_domain *parent_smmu_domain; >> + struct arm_smmu_master *master = dev_to_master(dev); >> + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); >> + >> + if (!arm_smmu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX)) >> + return; >> + >> + parent_domain = iommu_get_domain_for_dev(dev); >> + if (!parent_domain) >> + return; >> + parent_smmu_domain = to_smmu_domain(parent_domain); >> + >> + mutex_lock(&smmu_domain->init_mutex); >> + if (!smmu_domain->aux_nr_devs) >> + goto out_unlock; >> + >> + if (!--smmu_domain->aux_nr_devs) { >> + arm_smmu_write_ctx_desc(parent_smmu_domain, smmu_domain->ssid, >> + NULL); >> + /* >> + * TLB doesn't need invalidation since accesses from the device >> + * can't use this domain's ASID once the CD is clear. >> + * >> + * Sadly that doesn't apply to ATCs, which are PASID tagged. >> + * Invalidate all other devices as well, because even though >> + * they weren't 'officially' attached to the auxiliary domain, >> + * they could have formed ATC entries. >> + */ >> + arm_smmu_atc_inv_domain(smmu_domain, 0, 0); > > I've been struggling to understand the locking here, since both > arm_smmu_write_ctx_desc and arm_smmu_atc_inv_domain take and release the > devices_lock for the domain. Is there not a problem with devices coming and > going in-between the two calls? Yes it's a problem. I suppose we could take the parent's init_mutex (making sure that it protects detach_dev() as well. First I need to figure out how to prevent the parent domain from disappearing when auxiliary domains are attached, I seem to have forgotten that. I think checking if AUXD is enabled in the device passed to attach_dev() should be sufficient - that's what I do for SVA. But the IOMMU API isn't quite ready to handle failure in iommu_detach_device() at the moment. VFIO will free the domain even if it's still attached. > >> + } else { >> + struct arm_smmu_cmdq_ent cmd; >> + >> + /* Invalidate only this device's ATC */ >> + if (master->ats_enabled) { >> + arm_smmu_atc_inv_to_cmd(smmu_domain->ssid, 0, 0, &cmd); >> + arm_smmu_atc_inv_master(master, &cmd); >> + } >> + } >> +out_unlock: >> + mutex_unlock(&smmu_domain->init_mutex); >> +} >> + >> +static int arm_smmu_aux_get_pasid(struct iommu_domain *domain, struct device *dev) >> +{ >> + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); >> + >> + return smmu_domain->ssid ?: -EINVAL; >> +} >> + >> static struct iommu_ops arm_smmu_ops = { >> .capable = arm_smmu_capable, >> .domain_alloc = arm_smmu_domain_alloc, >> @@ -2539,6 +2772,13 @@ static struct iommu_ops arm_smmu_ops = { >> .of_xlate = arm_smmu_of_xlate, >> .get_resv_regions = arm_smmu_get_resv_regions, >> .put_resv_regions = arm_smmu_put_resv_regions, >> + .dev_has_feat = arm_smmu_dev_has_feature, >> + .dev_feat_enabled = arm_smmu_dev_feature_enabled, >> + .dev_enable_feat = arm_smmu_dev_enable_feature, >> + .dev_disable_feat = arm_smmu_dev_disable_feature, > > Why can't we use the existing ->capable and ->dev_{get,set}_attr callbacks > for this? ->capable isn't very useful because it applies to all SMMUs in the system. The existing ->{get,set}_attr callbacks apply to an iommu_domain. The main reason for doing it on endpoints was that it would be tedious to keep track of capabilities when attaching and detaching devices to a domain, especially for drivers that allow multiple IOMMUs per domain [1]. There were more discussions, and in the end Joerg proposed the current API for device attributes [2] [1] https://lore.kernel.org/lkml/aa1ff748-c2ec-acc0-f1d9-cdff2b131e58@linux.intel.com/ [2] https://lore.kernel.org/linux-iommu/20181207102926.GM16835@8bytes.org/ Thanks, Jean