Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp5708824pxb; Tue, 16 Feb 2021 05:49:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJxoGCaUNGwIHDVXjj1SL3uy+wsauWWS8+pGWvPo2qC8kzTN26EcETT1LuvV4Okfaa1w9ReI X-Received: by 2002:aa7:cc98:: with SMTP id p24mr21553811edt.126.1613483349055; Tue, 16 Feb 2021 05:49:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613483349; cv=none; d=google.com; s=arc-20160816; b=j53iE9grF8i3B2I10OW5dHp9iQH/WlYZ1Z6dqjD9sQ3UkzJfm0GaL5aZt+KIOPEFKO 77IZFrZW30CJMHd8jtiqCweQyULkgt0ezmW0tmIuND2ajnPQ2aIJbs3h9tByJ7V6AoDX OIBsyjvLKPKCo44l61UseSCSRiSKd1/ixQwAvhgpDShu8cCLQ7hQM6KFBTMiOyZal8yI LMxLjJMkrWL3/BoYLDiMKFdWQXHrkNfokgz2jLyZ/dQElf9KQ41FfKyARkIGSQwgQXbE mfrGbWZzFMAbW+/OS6v/nqCSd40szJcSJieuYIZ0V3gddMc9U9+F6D548ry3qdHqeMzf 2eWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=fVHe1Y653+5pKXE7oRswRcCz+UREmQSL1WDQDrNBNoY=; b=Folo56OEzsdAW9QtcyhTiVnlr8mZ+9Tcpv9PujOs4pON7TaQRV1E/rPd4wa1xeaf1A a5MnYoYBlPK9tg7RuuoL5dxFN2SOPowiurGx7N8rtfgg76w7iQ6CgE6cgTVCigmOqT9C O+cw5WsRS1lSFD0Nd+PNYFFJDw+2bcsJWRt/IQ2nMCti/g7PEq7LZqIKZDYhoO2JSr1D h44ebcH+d6yTyKrXRaqCKD166RYQZ7lOsknVXEtNepbzt4LmCPOe3MnZDBhjQMgyihc/ tDVpjQKHnC7a2VHgRQjkNdXIECDeBjGPOMfZQdb2VnxgsrLBfyO6/bzHQyPdCZjmysUf 83yg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n26si13827521ejg.362.2021.02.16.05.48.46; Tue, 16 Feb 2021 05:49:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229916AbhBPNqF (ORCPT + 99 others); Tue, 16 Feb 2021 08:46:05 -0500 Received: from foss.arm.com ([217.140.110.172]:35456 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229713AbhBPNqC (ORCPT ); Tue, 16 Feb 2021 08:46:02 -0500 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 1CC0E31B; Tue, 16 Feb 2021 05:45:16 -0800 (PST) Received: from [10.57.48.219] (unknown [10.57.48.219]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E96FF3F694; Tue, 16 Feb 2021 05:45:14 -0800 (PST) Subject: Re: [PATCH v2] iommu: Check dev->iommu in iommu_dev_xxx functions To: Shameerali Kolothum Thodi , "linux-kernel@vger.kernel.org" , "iommu@lists.linux-foundation.org" Cc: "jean-philippe@linaro.org" , "will@kernel.org" , "linuxarm@openeuler.org" , "Zengtao (B)" References: <33cf95925cfb47dda3ee472e00b9846c@huawei.com> From: Robin Murphy Message-ID: Date: Tue, 16 Feb 2021 13:45:10 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: <33cf95925cfb47dda3ee472e00b9846c@huawei.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2021-02-12 17:28, Shameerali Kolothum Thodi wrote: > > >> -----Original Message----- >> From: Shameerali Kolothum Thodi >> Sent: 12 February 2021 16:45 >> To: 'Robin Murphy' ; linux-kernel@vger.kernel.org; >> iommu@lists.linux-foundation.org >> Cc: joro@8bytes.org; jean-philippe@linaro.org; will@kernel.org; Zengtao (B) >> ; linuxarm@openeuler.org >> Subject: RE: [PATCH v2] iommu: Check dev->iommu in iommu_dev_xxx functions >> >> >> >>> -----Original Message----- >>> From: Robin Murphy [mailto:robin.murphy@arm.com] >>> Sent: 12 February 2021 16:39 >>> To: Shameerali Kolothum Thodi ; >>> linux-kernel@vger.kernel.org; iommu@lists.linux-foundation.org >>> Cc: joro@8bytes.org; jean-philippe@linaro.org; will@kernel.org; Zengtao (B) >>> ; linuxarm@openeuler.org >>> Subject: Re: [PATCH v2] iommu: Check dev->iommu in iommu_dev_xxx >> functions >>> >>> On 2021-02-12 14:54, Shameerali Kolothum Thodi wrote: >>>> Hi Robin/Joerg, >>>> >>>>> -----Original Message----- >>>>> From: Shameer Kolothum >> [mailto:shameerali.kolothum.thodi@huawei.com] >>>>> Sent: 01 February 2021 12:41 >>>>> To: linux-kernel@vger.kernel.org; iommu@lists.linux-foundation.org >>>>> Cc: joro@8bytes.org; robin.murphy@arm.com; jean-philippe@linaro.org; >>>>> will@kernel.org; Zengtao (B) ; >>>>> linuxarm@openeuler.org >>>>> Subject: [Linuxarm] [PATCH v2] iommu: Check dev->iommu in >>> iommu_dev_xxx >>>>> functions >>>>> >>>>> The device iommu probe/attach might have failed leaving dev->iommu >>>>> to NULL and device drivers may still invoke these functions resulting >>>>> in a crash in iommu vendor driver code. Hence make sure we check that. >>>>> >>>>> Also added iommu_ops to the "struct dev_iommu" and set it if the dev >>>>> is successfully associated with an iommu. >>>>> >>>>> Fixes: a3a195929d40 ("iommu: Add APIs for multiple domains per >> device") >>>>> Signed-off-by: Shameer Kolothum >>> >>>>> --- >>>>> v1 --> v2: >>>>> -Added iommu_ops to struct dev_iommu based on the discussion with >>> Robin. >>>>> -Rebased against iommu-tree core branch. >>>> >>>> A gentle ping on this... >>> >>> Is there a convincing justification for maintaining yet another copy of >>> the ops pointer rather than simply dereferencing iommu_dev->ops at point >>> of use? >>> >> >> TBH, nothing I can think of now. That was mainly the way I interpreted your >> suggestion >> from the v1. Now it looks like you didn’t mean it :). I am Ok to rework it to >> dereference >> it from iommu_dev. Please let me know. > > So we can do something like this, > > index fd76e2f579fe..5fd31a3cec18 100644 > --- a/drivers/iommu/iommu.c > +++ b/drivers/iommu/iommu.c > @@ -2865,10 +2865,12 @@ EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); > */ > int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat) > { > - const struct iommu_ops *ops = dev->bus->iommu_ops; > + if (dev->iommu && dev->iommu->iommu_dev && dev->iommu->iommu_dev->ops) > + struct iommu_ops *ops = dev->iommu->iommu_dev->ops; > > - if (ops && ops->dev_enable_feat) > - return ops->dev_enable_feat(dev, feat); > + if (ops->dev_enable_feat) > + return ops->dev_enable_feat(dev, feat); > + } > > return -ENODEV; > } > > Again, not sure we need to do the checking for iommu->dev and ops here. If the > dev->iommu is set, is it safe to assume that we have a valid iommu->iommu_dev > and ops always? (May be it is safer to do the checking in case something > else breaks this assumption in future). Please let me know your thoughts. I think it *could* happen that dev->iommu is set by iommu_fwspec_init() but iommu_probe_device() later refuses the device for whatever reason, so we would still need to check iommu->iommu_dev to be completely safe. We can assume iommu_dev->ops is valid, since if the IOMMU driver has returned something bogus from .probe_device then it's a major bug in that driver and crashing is the best indicator :) Robin. > > Thanks, > Shameer > > > _______________________________________________ > iommu mailing list > iommu@lists.linux-foundation.org > https://lists.linuxfoundation.org/mailman/listinfo/iommu >