Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp472537rdb; Mon, 29 Jan 2024 07:54:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IGDOUZYXvgC2jF4rgkodfk5ir9jn7KjE9gCFXwhAgv5MvYB1vMG5ORaNkdSRAOq+Vto2qID X-Received: by 2002:a05:6358:89e:b0:176:d522:76b4 with SMTP id m30-20020a056358089e00b00176d52276b4mr3288330rwj.14.1706543672761; Mon, 29 Jan 2024 07:54:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706543672; cv=pass; d=google.com; s=arc-20160816; b=BaaSqu9ne6LoEZsaM4msp3bxnrCH36CQHUN0IMYKCO3DFZBRJ9TXgaq1iHxZ3/ZJS3 K04hrdDwZZk6byVePWF5bI6RXWPPx451qY+kuM4H2ONpaWsJCRQnDziqp9xMl0+2TJh6 z5DFfmKBvXg/bOP+Nl7xnbemI43kig+rEHJB+3gKvtIR+uYZ1nqNtnTGgIheK1l6Fpd1 hccFnfS/1TLmmOoOQ3agKTO8ioxv/JoYQKudChu703ToYE+gaJ3eheAvBDAii7dzOBHz yLdHxsPEga4wJq7BTSpbsiKIjMFQB1jMsoj8DV0rDGyCN2iWyanaySquU3YE5P3ZCxaV oG3w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=+u6y5RGZUaOEJts7DaeBk/aybPKOo63QfRuk7QPhhAo=; fh=LT1N1ZCSZYBnOAkF6DzKXsWwjGIYe0osw9rG1pBFBmI=; b=k1cyLA2pt0JtvZwZsKZr8N6vkzVvL//yj2XuLX43oL3ixz0GyWcsS1wU8vk/IHCtk8 zVyDLymK1uf1Pdkqs8WolKFkKvteonDECauJLOY4Lw78AlUrPpAn65Q/O1hSsQKEM9CH udyeGkg8BpV6j3hig1W4xrcqvbh4dK7D3T3Cs3BrxMzm3ZMWuWLaxitiZh2h0gfvfg1t rmYczpBY74omoLW35lds6XxDLWKeWn4ceaeh4lIY6+BXlC9tRfzokxjc152g9XC7+vlX F76xe0I9fuy+wDPxowf5GrhraWSsGQ6278XKuAPBltbrJdmi0k1UyYdm3cFe4bkUyg7J MWQg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=owqHGQGq; arc=pass (i=1 spf=pass spfdomain=ziepe.ca dkim=pass dkdomain=ziepe.ca); spf=pass (google.com: domain of linux-kernel+bounces-43009-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43009-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id ck27-20020a056a02091b00b005cfbc9de943si6005043pgb.382.2024.01.29.07.54.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 07:54:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-43009-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=owqHGQGq; arc=pass (i=1 spf=pass spfdomain=ziepe.ca dkim=pass dkdomain=ziepe.ca); spf=pass (google.com: domain of linux-kernel+bounces-43009-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-43009-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 62CDDB238B2 for ; Mon, 29 Jan 2024 15:38:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AE60A154446; Mon, 29 Jan 2024 15:37:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="owqHGQGq" Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 53800154426 for ; Mon, 29 Jan 2024 15:37:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706542677; cv=none; b=kXj8POOVtc5o7tQEEJ5VEVuRTJwpAVHfRHB4i2uF9WbzkMEQ70DV4Caihmaw42vIIGNvjfZWcJv//c5sI5QkwXYbGXoTidiwLVkYHh647hXYhBRj2zmFI8S7DhRGIxKNF9Jsd6ztVvd9uYl63FBS66Ph4zRy4DeQvm4inxRkKw8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706542677; c=relaxed/simple; bh=dv7rvRTXCssC2Ucy3ETYL9O+pACUSn657yVE+v0ZMSE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=t8lxoPu4h3RygMEB7uxjDv6SAPG+EQ4NuMMCQ4uu6tint/Km0BLeYbLTlxXS5xMX8QsZ36NYhN805m794p/DwwbEVtqLvJ0CY+3Rpp+/fDcXgbE+PuDfg8ag/jg3kjLxJVSrRxoLWUM/MW5vfGkfQsDfZrKJwLoOvf9J9KPsL7w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca; spf=pass smtp.mailfrom=ziepe.ca; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b=owqHGQGq; arc=none smtp.client-ip=209.85.210.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ziepe.ca Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-6e11faeb125so581929a34.2 for ; Mon, 29 Jan 2024 07:37:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; t=1706542674; x=1707147474; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=+u6y5RGZUaOEJts7DaeBk/aybPKOo63QfRuk7QPhhAo=; b=owqHGQGqtbUf8a/Ho4QsD2/LPQhfOhR7WY0o2qDk6nIl1pmaEem9f3jleLmuH9xL+R r2pIqwgrc3RnyvlTrYurSDufJwdm8Doy70TvefYh+MlAgihzO81X6C/yfhyqbvcZLf61 Vx+LAkc01OZE96jEO7j0v88l7iYeZagJpC9cDl8akCmP0knyd6cMu/Rqa5yRcGbI4zCY zSZB6FOR1hkDkwwizyDk3VqXRUEe4q9JaE32HLZsPClYudxswxp0a/hyLIWUAoYoDRJ7 Hx73ANqu63r6vfK44l/t2fBSc/OQv+kIVYceWfAobtZp6tYXQBfOB5ut0GrooD2w54BK vW/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706542674; x=1707147474; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=+u6y5RGZUaOEJts7DaeBk/aybPKOo63QfRuk7QPhhAo=; b=lyCOV1vdcqIrTmCDlMmV5Z/ofIQUPIDSa7ZoCxXjNZR86bK3b/0rHd35BkC2J6JOBD gRRkLWdX0++pSROKnlrstoQDGCPlEvumZ/nbbny83E9RYFEIsrkioCW1Rm+tNvBn+/VS AtGxuDP7yy5tubayxUkXGC9Kb3ShPyY0oWrCciHUennqEehLgJ7khdd8HDo6nM9WqIor lz7PE4YaZo0ow2e/9UYpFp7/bdamRpi1j+PuSGS9jyF1lfth4ayqfREuh8ydk2dyp+mV m0Y9vvMKscxSGuqY2dmGuemZCltVYmDFsFPxyirqnd4wg+0SrsZh/2V4JkHYhh52St7S OBQQ== X-Gm-Message-State: AOJu0Yw+MFkdwT5tjQ3JwVWOyobO8Y/oyIWNHTN02WddldETJZ0R6uxO SeLjD962Q9+juL6E2QFgpSnDKsJFD0PduSWsBYx7h9LDJAu0oWCdc5TUwbwbyYk= X-Received: by 2002:a9d:685a:0:b0:6e0:ba7c:4203 with SMTP id c26-20020a9d685a000000b006e0ba7c4203mr5515941oto.1.1706542674389; Mon, 29 Jan 2024 07:37:54 -0800 (PST) Received: from ziepe.ca (hlfxns017vw-142-68-80-239.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.68.80.239]) by smtp.gmail.com with ESMTPSA id z21-20020a05683008d500b006e12266433csm750687otg.27.2024.01.29.07.37.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 07:37:53 -0800 (PST) Received: from jgg by wakko with local (Exim 4.95) (envelope-from ) id 1rUTho-00A9vk-Ij; Mon, 29 Jan 2024 11:37:52 -0400 Date: Mon, 29 Jan 2024 11:37:52 -0400 From: Jason Gunthorpe To: Lu Baolu Cc: Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , iommu@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/2] iommu: Probe right iommu_ops for device Message-ID: <20240129153752.GE50608@ziepe.ca> References: <20240126105341.78086-1-baolu.lu@linux.intel.com> <20240126105341.78086-3-baolu.lu@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240126105341.78086-3-baolu.lu@linux.intel.com> On Fri, Jan 26, 2024 at 06:53:41PM +0800, Lu Baolu wrote: > Previously, in the iommu probe device path, __iommu_probe_device() gets > the iommu_ops for the device from dev->iommu->fwspec if this field has > been initialized before probing. Otherwise, it is assumed that only one > of Intel, AMD, s390, PAMU or legacy SMMUv2 can be present, hence it's > feasible to lookup the global iommu device list and use the iommu_ops of > the first iommu device which has no dev->iommu->fwspec. > > The assumption mentioned above is no longer correct with the introduction > of the IOMMUFD mock device on x86 platforms. There might be multiple > instances of iommu drivers, none of which have the dev->iommu->fwspec > field populated. > > Probe the right iommu_ops for device by iterating over all the global > drivers and call their probe function to find a match. > > Fixes: 17de3f5fdd35 ("iommu: Retire bus ops") > Cc: Robin Murphy > Signed-off-by: Lu Baolu > --- > drivers/iommu/iommu.c | 76 +++++++++++++++++++++++++++---------------- > 1 file changed, 48 insertions(+), 28 deletions(-) > > diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c > index 0af0b5544072..2cdb01e411fa 100644 > --- a/drivers/iommu/iommu.c > +++ b/drivers/iommu/iommu.c > @@ -396,30 +396,69 @@ void dev_iommu_priv_set(struct device *dev, void *priv) > } > EXPORT_SYMBOL_GPL(dev_iommu_priv_set); > > +static struct iommu_device * > +__iommu_do_probe_device(struct device *dev, const struct iommu_ops *ops) > +{ > + struct iommu_device *iommu_dev; > + > + if (!try_module_get(ops->owner)) > + return ERR_PTR(-EINVAL); > + > + iommu_dev = ops->probe_device(dev); > + if (IS_ERR(iommu_dev)) > + module_put(ops->owner); This doesn't really do enough to protect against races, to do that fully we need to have iommu_device_unregister() do some better locking, and fix fwspec->ops lifecycle somehow.. Remember that module ref counts don't prevent iommu_device_unregister_bus() concurrency. So, it would be simpler to leave the try_module_get in the iommu_init_device(), just move it after the probe_device call. > +static struct iommu_device *iommu_probe_device_ops(struct device *dev) > +{ > + struct iommu_device *iter, *iommu_dev = ERR_PTR(-ENODEV); > + struct iommu_fwspec *fwspec; > + > + /* > + * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU > + * instances with non-NULL fwnodes, and client devices should have been > + * identified with a fwspec by this point. Otherwise, we will iterate > + * over all the global drivers and call their probe function to find a > + * match. > + */ > + fwspec = dev_iommu_fwspec_get(dev); > + if (fwspec && fwspec->ops) > + return __iommu_do_probe_device(dev, fwspec->ops); > + > + mutex_lock(&iommu_device_lock); > + list_for_each_entry(iter, &iommu_device_list, list) { > + iommu_dev = __iommu_do_probe_device(dev, iter->ops); > + if (!IS_ERR(iommu_dev)) > + break; This does need to skip duplicate ops though (see my version), we don't want to call the same driver many times. And Kevin and Robin are right, since we recently removed a bunch of fwpsec checks from drivers the core code must now never call a fwspec expecting driver without a fwspec. (check for !iommu->fwnode) Otherwise this looks fine for me, thanks Jason