Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp1741931rdd; Thu, 11 Jan 2024 08:01:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IE8FkyUuThehwgRBxrcPyy9vUZHjo0l25MSWbqIZucHdn/ikvmDSq4GdCXhvE9hsMHO4w3A X-Received: by 2002:a05:6808:e83:b0:3bc:3470:1750 with SMTP id k3-20020a0568080e8300b003bc34701750mr1246997oil.116.1704988877517; Thu, 11 Jan 2024 08:01:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704988877; cv=none; d=google.com; s=arc-20160816; b=o3F5VhKphfkbg8UZOq3TSHdiuGanLCUbhPb/dN8ptDgfnmtxofQJ/khL1Bu9qswdLj CyM6DgIIMqtBY15L5UMIU0pQmN7xTlhf8W57Tno+EHafKLD6RAMc4cL+P6bp9d5iKkrN GObC3wXXCJa401l0QTMit8O6f+yd+hLbDVYHdYbR4XPCNokv9qPGeJyosHsaCBWj5bER XI7Km+1BtT9xyXU0DUA/DQh9Z70svjxNsz5e1mq2EAoAirCGsQzqUvQm6rrF1vCxPWKa Lsh2HLoChUY0xjYzwr3UNRBoXfI1JxGAtnETANrBBiYijZNmCx9u3LWUi+lUBejJIC82 k13Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=zO6hUO3RJi2exjt1rl/NLdQ8g67c6DByrhsf9pi6vp0=; fh=rWYFYzW3JEEeiMls87MSHm9CRd1Yi4ULBQ0L1kY00ks=; b=owxXUe9B9GWJMIm/zmPG0vMHHvqGnqJm809GxvGVkJqlbuiNrw0mzU+pjImitNg3tO 7EaX17Ag1Sv0qHYkLUJwdYW2uSpeYxuicmQPJUknMvPQpXWlwEJ10zvLHPk5HAxwKuhq DGYEKt/y5Ah4JY8g6ex453p4uF5DK6J8B7haEhAiTdZXVL8esutzL68ofiT9CkUCq0YG RaFeqyKLnqzO4Oy91ePzRryAo5OELLhG7hXgLaBgqZ+VNVNEdX2qixPJLpx8uZyNoH1c x5+CxpvbglMugw29pL9JceaI5wbnvRoh8Z89wGHCmx6iyVlZQY1fURhg293goocGqbTd H87w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-23816-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23816-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id gl9-20020a0568083c4900b003bc2481fc35si507233oib.93.2024.01.11.08.01.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Jan 2024 08:01:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-23816-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-23816-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-23816-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id A698B287C59 for ; Thu, 11 Jan 2024 15:51:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1EA0C4F8B5; Thu, 11 Jan 2024 15:50:57 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D47C74F88D for ; Thu, 11 Jan 2024 15:50:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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 F2C842F4; Thu, 11 Jan 2024 07:51:39 -0800 (PST) Received: from [10.1.196.40] (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 33F133F5A1; Thu, 11 Jan 2024 07:50:53 -0800 (PST) Message-ID: <016e052e-8a99-4d60-b864-87c9859953f2@arm.com> Date: Thu, 11 Jan 2024 15:50:51 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/1] iommufd/selftest: Use right iommu_ops for mock device Content-Language: en-GB To: Jason Gunthorpe , Lu Baolu Cc: Kevin Tian , Joerg Roedel , Will Deacon , iommu@lists.linux.dev, linux-kernel@vger.kernel.org References: <20240111073213.180020-1-baolu.lu@linux.intel.com> <20240111144840.GW50608@ziepe.ca> From: Robin Murphy In-Reply-To: <20240111144840.GW50608@ziepe.ca> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 11/01/2024 2:48 pm, Jason Gunthorpe wrote: > On Thu, Jan 11, 2024 at 03:32:13PM +0800, Lu Baolu wrote: >> 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 will lookup the global iommu device list >> and use the iommu_ops of the first iommu device which has no >> dev->iommu->fwspec. This causes the wrong iommu_ops to be used for the mock >> device on x86 platforms where dev->iommu->fwspec is not used. >> >> Preallocate the fwspec for the mock device so that the right iommu ops can >> be used. > > I really don't like this. > > The lifecycle model for fwspec is already a bit confusing. Introducing > a new case where a driver pre-allocates the fwspec is making it worse, > not better. > > eg iommu_init_device() error unwind will free this allocated fwspec > leaving the device broken. We don't have the concept of a fwspec that > is owned by the device, it is really owned by the probing code. As I've tried to explain before, this is in fact the correct use of fwspec as originally designed, i.e. being set up by *bus code* before device_add() (remember this is not the "IOMMU driver" part of selftest.c). Indeed for perfect symmetry the bus code would free the fwspec after the corresponding device_del() returns, but there's no harm in that being factored into iommu_release_device() since the notifier call occurs sufficiently late in device_del() itself as to make no practical difference. I'm working to get things back to that model (wherein the dev_iommu and fwspec lifecycles become trivial), just with the slight tweak that these days it's going to make more sense to have the initialisation factored into device_add() itself (via iommu_probe_device()), rather than beforehand. Thanks, Robin. > The fundamental issue is we now have a special kind of driver: > > fwspec = dev_iommu_fwspec_get(dev); > if (fwspec && fwspec->ops) > ops = fwspec->ops; > else > ops = iommu_ops_from_fwnode(NULL); > ^^^^^^^^ > > Which represents a "global" non-fwspec using driver that will only > bind to devices that didn't parse into a fwspec. > > The code above supports only one of these drivers at time, but allows > more than one to be registered - it is inconsistent. > > I think the right/easy answer is to iterate over all the "global" > drivers and call their probe instead of just the first one. > > Especially since my approach over here migrates the whole thing to work > by iterating: > > https://lore.kernel.org/all/0-v2-f82a05539a64+5109-iommu_fwspec_p2_jgg@nvidia.com/ > > And this patch: > > https://lore.kernel.org/all/28-v2-f82a05539a64+5109-iommu_fwspec_p2_jgg@nvidia.com/ > > Is how I made the iterating logic, it could be pulled out and tidied a > bit. > > Jason