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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 868A0C433F5 for ; Thu, 28 Oct 2021 12:10:56 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3543F610CA for ; Thu, 28 Oct 2021 12:10:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 3543F610CA Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.217951.378186 (Exim 4.92) (envelope-from ) id 1mg4F1-00022e-10; Thu, 28 Oct 2021 12:10:43 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 217951.378186; Thu, 28 Oct 2021 12:10:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mg4F0-00022X-Ss; Thu, 28 Oct 2021 12:10:42 +0000 Received: by outflank-mailman (input) for mailman id 217951; Thu, 28 Oct 2021 12:10:41 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mg4Ez-00022L-MY for xen-devel@lists.xenproject.org; Thu, 28 Oct 2021 12:10:41 +0000 Received: from foss.arm.com (unknown [217.140.110.172]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTP id 109b19dc-37e8-11ec-849e-12813bfff9fa; Thu, 28 Oct 2021 12:10:39 +0000 (UTC) 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 01BB61063; Thu, 28 Oct 2021 05:10:39 -0700 (PDT) Received: from [10.57.24.217] (unknown [10.57.24.217]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B26863F5A1; Thu, 28 Oct 2021 05:10:37 -0700 (PDT) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 109b19dc-37e8-11ec-849e-12813bfff9fa Subject: Re: [patch-4.16] arm/smmuv1,v2: Protect smmu master list with a lock To: Julien Grall , xen-devel@lists.xenproject.org Cc: Stefano Stabellini , Volodymyr Babchuk , bertrand.marquis@arm.com, Ian Jackson References: <20211026122903.15042-1-michal.orzel@arm.com> <70c30a6c-b779-805e-079a-41bb484894b9@xen.org> <01545115-e82e-2a9d-a8e4-da9676080c0f@xen.org> From: Michal Orzel Message-ID: Date: Thu, 28 Oct 2021 14:10:15 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <01545115-e82e-2a9d-a8e4-da9676080c0f@xen.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Hi Julien, On 27.10.2021 19:02, Julien Grall wrote: > > > On 27/10/2021 11:41, Michal Orzel wrote: >> Hi Julien, > > Hi Michal, > >> On 26.10.2021 18:56, Julien Grall wrote: >>> Hi, >>> >>> On 26/10/2021 17:28, Julien Grall wrote: >>>> On 26/10/2021 13:29, Michal Orzel wrote: >>>>> If a device is added to SMMUv1/v2 from DT and PCI >>>>> at the same time, there is a concurrent access >>>>> to a smmu master list. This could lead to a >>>>> scenario where one is looking into a list that >>>>> is being modified at the same time. Add a lock >>>>> to prevent this issue. >>>> >>>> Did you intend to say "Hold" rather than "Add"? >>>> >> Yes, this is what I meant. I will change it. >> >>>>> >>>>> Reuse the existing spinlock arm_smmu_devices_lock >>>>> as it is already protecting find_smmu_master. >>>>> >>>>> ipmmu-smmu and smmuv3 are not impacted by this >>>>> issue as there is no access or modification of >>>>> a global resource during add_device. >>>>> >>>>> Signed-off-by: Michal Orzel >>>>> --- >>>>> This patch aims for 4.16 release. >>>>> Benefits: >>>>> Remove a bug that could lead to a corruption of the >>>>> smmu master list, which would be very hard to debug. >>>> >>>>   From my understanding, this corruption would only happen with CONFIG_HAS_PCI. At the moment, this is a experimental feature as it is not fully complete. >>> >>> Actually, digging through the code, I noticed that iommu_add_device() assume that it can only be called with platform/DT. Thankfully, AFAICT, the function would return -ENXIO because the fwspec would not be allocated for PCI device. >>> >>> So was this patch tested with additional patch on top? >>> >> The purpose of this patch is to fix the issue that is present and which you thankfully noticed. >> There is still a lot of patches to be added that aim to support PCI passthrough. >> The complete PCI series is tested with SMMU and it works. >> But there is no chance to test this patch standalone as iommu_add_device is not in the correct form for PCI as of now. > > Ok. I would suggest to say this is a latent bug so it make clear that the patch is more for hardening at the moment. > Ok I will mention it in the commit msg. >>>>>    xen/drivers/passthrough/arm/smmu.c | 25 +++++++++++++++++++------ >>>>>    1 file changed, 19 insertions(+), 6 deletions(-) >>>>> >>>>> diff --git a/xen/drivers/passthrough/arm/smmu.c b/xen/drivers/passthrough/arm/smmu.c >>>>> index c9dfc4caa0..be62a66a28 100644 >>>>> --- a/xen/drivers/passthrough/arm/smmu.c >>>>> +++ b/xen/drivers/passthrough/arm/smmu.c >>>>> @@ -820,21 +820,25 @@ static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu, >>>>>                         struct device *dev, >>>>>                         struct iommu_fwspec *fwspec) >>>>>    { >>>>> -    int i; >>>>> +    int i, ret; >>>>>        struct arm_smmu_master *master; >>>>>        struct device_node *dev_node = dev_get_dev_node(dev); >>>>> +    spin_lock(&arm_smmu_devices_lock); >>>>>        master = find_smmu_master(smmu, dev_node); >>>>>        if (master) { >>>>>            dev_err(dev, >>>>>                "rejecting multiple registrations for master device %s\n", >>>>>                dev_node->name); >>>>> -        return -EBUSY; >>>>> +        ret = -EBUSY; >>>>> +        goto out_unlock; >>>>>        } >>>>>        master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL); >>>>> -    if (!master) >>>>> -        return -ENOMEM; >>>>> +    if (!master) { >>>>> +        ret = -ENOMEM; >>>>> +        goto out_unlock; >>>>> +    } >>>>>        master->of_node = dev_node; >>>>>        /* Xen: Let Xen know that the device is protected by an SMMU */ >>>>> @@ -846,11 +850,17 @@ static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu, >>>>>                dev_err(dev, >>>>>                    "stream ID for master device %s greater than maximum allowed (%d)\n", >>>>>                    dev_node->name, smmu->num_mapping_groups); >>>>> -            return -ERANGE; >>>>> +            ret = -ERANGE; >>>>> +            goto out_unlock; >>>>>            } >>>>>            master->cfg.smendx[i] = INVALID_SMENDX; >>>>>        } >>>>> -    return insert_smmu_master(smmu, master); >>>>> + >>>>> +    ret = insert_smmu_master(smmu, master); >>>>> + >>>>> +out_unlock: >>>>> +    spin_unlock(&arm_smmu_devices_lock); >>>> >>>> Please add a newline here. >>>> >> Ok. >> >>>>> +    return ret; >>>>>    } >>>>>    static int register_smmu_master(struct arm_smmu_device *smmu, >>>>> @@ -2056,7 +2066,10 @@ static int arm_smmu_add_device(struct device *dev) >>>>>        } else { >>>>>            struct arm_smmu_master *master; >>>>> +        spin_lock(&arm_smmu_devices_lock); >>>>>            master = find_smmu_master(smmu, dev->of_node); >>>>> +        spin_unlock(&arm_smmu_devices_lock); >>>> >>>> At the moment, unlocking here is fine because we don't remove the device. However, there are a series to supporting removing a device (see [1]). So I think it would be preferable to unlock after the last use of 'cfg'. >>>> >> Ok. I will move unlocking to the end of this else {} block. I was not aware of the patch you are referring to. > > I think the end of the else is still too early. This needs to at least be past iommu_group_set_iommudata() because we store cfg. > > Potentially, the lock wants to also englobe arm_smmu_master_alloc_smes(). So I am wondering whether it would be simpler to hold the lock for the whole duration of arm_smmu_add_device() (I can see value when we will want to interlock with the remove code). > >> >>>> Looking at the code, I also noticed that the error path is not correct for at least the PCI device and we would leak memory. We also assume that Stream ID == Requester ID. Are both of them in your radar for PCI passthrough? >>>> >> I agree with you. I also noticed it. However this is going to be fixed with the next patch series when Rahul gets back. > > Good. Do you have a todo list for PCI passthrough that can be shared? > As I am not working on the PCI stuff, I do not have such a list but I can point you to the branch on which both ARM and EPAM are working: https://gitlab.com/xen-project/fusa/xen-integration/-/tree/integration/pci-passthrough > Cheers, > Cheers, Michal