Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp6094380rdb; Mon, 18 Sep 2023 04:13:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHQHRdVRCnol7if1YxzxV+xPQspr8zRRwr0/UzrxUmqyqRhHSYxLBfSh2qlyiXZXDewUrg3 X-Received: by 2002:a17:90a:6e09:b0:268:13c4:b800 with SMTP id b9-20020a17090a6e0900b0026813c4b800mr7930859pjk.21.1695035608440; Mon, 18 Sep 2023 04:13:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695035608; cv=none; d=google.com; s=arc-20160816; b=vVkNu84Yhs2568qoFogMOJMoZ1BJpgA7FubSC3bWuWH5NXMA/lb44+HoqI3mtErEDe t/DHbadGnz/eHU3LwISp/bPC9lzjZtqjO7eF8wWavkpvWfgwXDNrKgcT3Be3TPjC/+bq mS+fGbwXFE/L7i8JZPhGQHNHeQyunjGRpf93yykFFusQyJ0x8D/mvRWXlV+7CVucji6S YA3aa3HZSrBZ1lqZIOst1+/vjHTJ8i219XPK5edDoVMGd7et2kRecxkyBwxtMd+RaVox OCCmpXOUsba97DBuyxHPQw8/rhS50lEkJqAGvBeoUM10gcOegyHRKmeOKrDF250s+tIN OCQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=sCA2e0Nuh87n4xubXaFiv7/BmxkgZIXW03QdmaEXbuA=; fh=ADkLrnZ0P5w4aR8AOw9jcP6NdKVhxKDtfsZB8K4/d98=; b=P3KZlB8VO9H/Z50JVXGGV41oT8W9RyUrjfLmL+VUZ1vzxhCzJbRzlsrc5MwxB+OESe EG2pWSO4q653hHUHirU8HM1N262Ha2DQP1Q3eJFockPuvIznm2ofjvcZFUxdM/tt3AAa jF8GANEJjgybYJD8RG6dhiZLti3KpEXIYMh/wOkJnvxe+PkmzA9YUb5MrbAfo8jbF8qL MP47OWxk7Mz+9TJTSghD5rqQmf8KEhzElA87PrB9ZOeXYaqeo6LNK6W4fRtDomKOt6EJ qq1u2yTx37/e/Wix/VQKqT+TtbWKDhUBVdWPkY4YquJsT2Y1Kz0AS1pqDU70+02pCHrr 7DOw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id pf6-20020a17090b1d8600b00263cfc9753csi10673901pjb.5.2023.09.18.04.13.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 04:13:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id CC24E80BCBAD; Mon, 18 Sep 2023 03:38:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235450AbjIRKhr (ORCPT + 99 others); Mon, 18 Sep 2023 06:37:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241335AbjIRKho (ORCPT ); Mon, 18 Sep 2023 06:37:44 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0FEE0CDF for ; Mon, 18 Sep 2023 03:36:24 -0700 (PDT) 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 1A4BD1FB; Mon, 18 Sep 2023 03:37:01 -0700 (PDT) Received: from [10.57.94.165] (unknown [10.57.94.165]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 862883F67D; Mon, 18 Sep 2023 03:36:22 -0700 (PDT) Message-ID: <5c2746a3-ef3a-2747-c600-395159f668ba@arm.com> Date: Mon, 18 Sep 2023 11:36:18 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH v3 4/7] iommu: Switch __iommu_domain_alloc() to device ops Content-Language: en-GB To: Baolu Lu , joro@8bytes.org, will@kernel.org Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, jgg@nvidia.com References: <404d8395cf4252c6fe6d98f317f3570127451778.1694693889.git.robin.murphy@arm.com> From: Robin Murphy In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.2 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email 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 (pete.vger.email [0.0.0.0]); Mon, 18 Sep 2023 03:38:22 -0700 (PDT) On 2023-09-18 07:10, Baolu Lu wrote: > On 9/16/23 12:58 AM, Robin Murphy wrote: >> @@ -1997,16 +1995,13 @@ void iommu_set_fault_handler(struct >> iommu_domain *domain, >>   } >>   EXPORT_SYMBOL_GPL(iommu_set_fault_handler); >> -static struct iommu_domain *__iommu_domain_alloc(const struct >> bus_type *bus, >> +static struct iommu_domain *__iommu_domain_alloc(struct device *dev, >>                            unsigned type) >>   { >> -    const struct iommu_ops *ops = bus ? bus->iommu_ops : NULL; >> +    const struct iommu_ops *ops = dev_iommu_ops(dev); >>       struct iommu_domain *domain; >>       unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS; >> -    if (!ops) >> -        return NULL; >> - >>       domain = ops->domain_alloc(alloc_type); >>       if (!domain) >>           return NULL; >> @@ -2030,9 +2025,28 @@ static struct iommu_domain >> *__iommu_domain_alloc(const struct bus_type *bus, >>       return domain; >>   } >> +static int __iommu_domain_alloc_dev(struct device *dev, void *data) >> +{ >> +    struct device **alloc_dev = data; >> + >> +    if (!dev_has_iommu(dev)) >> +        return 0; >> + >> +    WARN_ONCE(*alloc_dev && dev_iommu_ops(dev) != >> dev_iommu_ops(*alloc_dev), >> +          "Multiple IOMMU drivers present, which the public IOMMU API >> can't fully support yet. You may still need to disable one or more to >> get the expected result here, sorry!\n"); >> + >> +    *alloc_dev = dev; >> +    return 0; >> +} >> + >>   struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) >>   { >> -    return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); >> +    struct device *dev = NULL; >> + >> +    if (bus_for_each_dev(bus, NULL, &dev, __iommu_domain_alloc_dev)) >> +        return NULL; > > __iommu_domain_alloc_dev() always returns 0. Hence above if condition > will never be true. Perhaps, in __iommu_domain_alloc_dev(), Oh bugger, seems I screwed up the unnecessarily overcomplicated rebase that I made for myself, and managed to put this back to the v1 code, so it's just wrong (bus_for_each_dev() can return an error itself if the bus isn't properly initialised, but it also returns success if the bus has no devices, which was handled properly in v2 that you actually R-b'd). > >     if (WARN_ON(*alloc_dev && dev_iommu_ops(dev) != >             dev_iommu_ops(*alloc_dev)) >         return -EPERM; I went back and forth on this initially, but in the end I figured since the other patches are meant to be making the rest of the public API sufficiently robust, then if someone does try it with multiple drivers before full support can be finished, they can at least have some chance of getting the desired result, rather than a guarantee of not. I am still open to being convinced otherwise, though. Thanks, Robin. > > ? > >> + >> +    return __iommu_domain_alloc(dev, IOMMU_DOMAIN_UNMANAGED); > > Is it possible that all devices on this bus have dev_has_iommu() to be > false? If so, we probably need something like below: > >     if (!dev_has_iommu(dev)) >         return -ENODEV; > > ? > >>   } >>   EXPORT_SYMBOL_GPL(iommu_domain_alloc); >> @@ -3228,13 +3242,17 @@ static int >> __iommu_group_alloc_blocking_domain(struct iommu_group *group) >>       if (group->blocking_domain) >>           return 0; >> -    group->blocking_domain = __iommu_domain_alloc(dev->bus, >> IOMMU_DOMAIN_BLOCKED); >> +    /* noiommu groups should never be here */ >> +    if (WARN_ON(!dev_has_iommu(dev))) >> +        return -ENODEV; >> + >> +    group->blocking_domain = __iommu_domain_alloc(dev, >> IOMMU_DOMAIN_BLOCKED); >>       if (!group->blocking_domain) { >>           /* >>            * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED >>            * create an empty domain instead. >>            */ >> -        group->blocking_domain = __iommu_domain_alloc(dev->bus, >> IOMMU_DOMAIN_UNMANAGED); >> +        group->blocking_domain = __iommu_domain_alloc(dev, >> IOMMU_DOMAIN_UNMANAGED); >>           if (!group->blocking_domain) >>               return -EINVAL; >>       } > > Best regards, > baolu