Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753861AbdHRAuF (ORCPT ); Thu, 17 Aug 2017 20:50:05 -0400 Received: from lucky1.263xmail.com ([211.157.147.130]:42036 "EHLO lucky1.263xmail.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752872AbdHRAuE (ORCPT ); Thu, 17 Aug 2017 20:50:04 -0400 X-263anti-spam: KSV:0; X-MAIL-GRAY: 1 X-MAIL-DELIVERY: 0 X-KSVirus-check: 0 X-ABS-CHECKED: 4 X-RL-SENDER: shawn.lin@rock-chips.com X-FST-TO: linux-kernel@vger.kernel.org X-SENDER-IP: 58.22.7.114 X-LOGIN-NAME: shawn.lin@rock-chips.com X-UNIQUE-TAG: X-ATTACHMENT-NUM: 0 X-DNS-TYPE: 0 Cc: shawn.lin@rock-chips.com, iommu@lists.linux-foundation.org, marc.zyngier@arm.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH] iommu: Avoid NULL group dereference To: Robin Murphy , joro@8bytes.org References: <59a6c4f0a790eea40e2b2f2be840b63317dd44e4.1502966326.git.robin.murphy@arm.com> From: Shawn Lin Message-ID: <25ce000c-77d1-c01e-795d-0b52d2892318@rock-chips.com> Date: Fri, 18 Aug 2017 08:49:54 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <59a6c4f0a790eea40e2b2f2be840b63317dd44e4.1502966326.git.robin.murphy@arm.com> Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2106 Lines: 58 Hi On 2017/8/17 18:40, Robin Murphy wrote: > The recently-removed FIXME in iommu_get_domain_for_dev() turns out to > have been a little misleading, since that check is still worthwhile even > when groups *are* universal. We have a few IOMMU-aware drivers which > only care whether their device is already attached to an existing domain > or not, for which the previous behaviour of iommu_get_domain_for_dev() > was ideal, and who now crash if their device does not have an IOMMU. > It works, thanks! Tested-by: Shawn Lin > With IOMMU groups now serving as a reliable indicator of whether a > device has an IOMMU or not (barring false-positives from VFIO no-IOMMU > mode), drivers could arguably do this: > > group = iommu_group_get(dev); > if (group) { > domain = iommu_get_domain_for_dev(dev); > iommu_group_put(group); > } > > However, rather than duplicate that code across multiple callsites, > particularly when it's still only the domain they care about, let's skip > straight to the next step and factor out the check into the common place > it applies - in iommu_get_domain_for_dev() itself. Sure, it ends up > looking rather familiar, but now it's backed by the reasoning of having > a robust API able to do the expected thing for all devices regardless. > > Fixes: 05f80300dc8b ("iommu: Finish making iommu_group support mandatory") > Reported-by: Shawn Lin > Signed-off-by: Robin Murphy > --- > > As well as dma-iommu, there are at least the Cavium ThunderX and > Freescale DPAA2 ethernet drivers expecting this to work too. > > drivers/iommu/iommu.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c > index af69bf7e035a..5499a0387349 100644 > --- a/drivers/iommu/iommu.c > +++ b/drivers/iommu/iommu.c > @@ -1352,6 +1352,8 @@ struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) > struct iommu_group *group; > > group = iommu_group_get(dev); > + if (!group) > + return NULL; > > domain = group->domain; > >