Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp2614363ybx; Fri, 8 Nov 2019 06:55:14 -0800 (PST) X-Google-Smtp-Source: APXvYqwk5SE4LixyjeSELjz7HTPDX1pfb5KBRkTzynq+1O1iYntO2SGQZ26ddmZVXjh8TfHW0pFh X-Received: by 2002:a50:8871:: with SMTP id c46mr10739019edc.24.1573224914666; Fri, 08 Nov 2019 06:55:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573224914; cv=none; d=google.com; s=arc-20160816; b=oAhfEE4DnYWtLATRQ4b1E9y5D9HWPOdPUXNx5lZiaqP5AI2NqiOrVgjmdfAn0+iLgm ZQypXg/fflamkQo5z64lzhCCo4SojuWzsbsKzFq8xLvR2YSbjoktgHIO4uGLOGgnJDiY JXCPpTkLYTfv/hoHQ+8X9/fKK36wQ7VktuXNxlRcG68gLKQDDsGjMFzyCzY/9nEay4LT g51Qvi4t4ZGxIcaV0IWQhzryPgO/usM+DG9Z+avad9WMuT43Ixh1bnkrCi+s8Kpra7mM m11VF8CwM6n+EcFtHX4/lhEuLYJXNkDBOosN4JqjST7BEP4g6xgbL2uPS2fCglIT0lza 7Iqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=8Mwho19DAfkN0DxBlUoOkWk7cyipUrRuHhIRNZnEDdo=; b=Gor11mGIOs8bYiMYboIKizperfVsrA8xzgt7+pI1/h7oj76dpIqORMXAQrDAWODeBz sFlA2WrRgeVnEeIuWU6TmSEOYyNVugoaN7xxqZxCYvnK7d5TdDAVE3epBSy8PBfA0bko Z7nPrbEoCqBPE7CXzjjsP2kE2vNjizaVXfTN6LqlIYcmReM5trA9c1d9DfLQhvu2vnJU j8vsRJ6v6tf2C8YK+NvVQR6bnRsypyk3XQiI1x3CVqdydWcRzhNEZGLE4iI84ew13iHk Mpr/spKGzGx4UJwX4F5SRNogFcsz3V1IFMzndwll9RHmaWBZp6LCMvS9wPK21SnyvmFv 5QMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nkvt+iRM; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q5si3641758ejb.174.2019.11.08.06.54.49; Fri, 08 Nov 2019 06:55:14 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nkvt+iRM; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726152AbfKHOyT (ORCPT + 99 others); Fri, 8 Nov 2019 09:54:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:60164 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725883AbfKHOyT (ORCPT ); Fri, 8 Nov 2019 09:54:19 -0500 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7CD6221882; Fri, 8 Nov 2019 14:54:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1573224857; bh=jWYLwllCUqtBV7+ph/RRP8l2m0z8U4KXT030/BVvZOA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=nkvt+iRMc3rdFHFWdBuMtoO6m4lR1p55i8Wiy6/DYfzZuuXhcv3YkJJ/z6p28gacd 1bOGxMcHIXM9y7iOHwDFwwKLvr9UEl2FiR0QXOKXqvPNCJy153NRYiVb1+zP7NfBnL McG6LFA56dt8cwTO7JE+kLQSaBJ55RiQG8AFnpOs= Date: Fri, 8 Nov 2019 14:54:13 +0000 From: Will Deacon To: Jean-Philippe Brucker Cc: Joerg Roedel , Bjorn Helgaas , iommu@lists.linux-foundation.org, Robin Murphy , linux-kernel@vger.kernel.org Subject: Re: [PATCH 5/7] iommu/arm-smmu-v3: Allow building as a module Message-ID: <20191108145407.GA20024@willie-the-truck> References: <20191030145112.19738-1-will@kernel.org> <20191030145112.19738-6-will@kernel.org> <20191030193148.GA8432@8bytes.org> <20191031154247.GB28061@willie-the-truck> <20191104191524.GA2786242@lophozonia> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191104191524.GA2786242@lophozonia> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Jean-Philippe, On Mon, Nov 04, 2019 at 08:15:24PM +0100, Jean-Philippe Brucker wrote: > On Thu, Oct 31, 2019 at 03:42:47PM +0000, Will Deacon wrote: > > > Sorry for the stupid question, but what prevents the iommu module from > > > being unloaded when there are active users? There are no symbol > > > dependencies to endpoint device drivers, because the interface is only > > > exposed through the iommu-api, right? Is some sort of manual module > > > reference counting needed? > > > > Generally, I think unloading the IOMMU driver module while there are > > active users is a pretty bad idea, much like unbinding the driver via > > /sys in the same situation would also be fairly daft. However, I *think* > > the code in __device_release_driver() tries to deal with this by > > iterating over the active consumers and ->remove()ing them first. > > > I'm without hardware access at the moment, so I haven't been able to > > test this myself. We could nobble the module_exit() hook, but there's > > still the "force unload" option depending on the .config. > > Shame that we can't completely prevent module unloading, because handling > rmmod cleanly is tricky. > > On module unload we also need to tidy up the bus->iommu_ops installed by > bus_set_iommu(), and remove the IOMMU groups (and probably other leaks I > missed). I have a solution for the bus->iommu_ops, which is simply adding > a bus_unset_iommu() counterpart with a refcount, but it doesn't deal with > the IOMMU groups cleanly. If there are multiple IOMMU instances managing > one bus, then we should only remove the IOMMU groups belonging to the > instance that is being removed. Hmm, but all of those IOMMU instances must be driven by the same driver, right, since bus_set_iommu() can only take one set of callbacks for a given bus? In which case, removing the driver module effectively removes all instances of the IOMMU for that bus and I think we're ok. If we couple that with Joerg's suggestion to take a reference to the driver module in add_device(), then I think that actually it's harmless to leave the bus ops installed and the groups should be sorted too. It means it's pretty difficult to unload the module, but that's probably not a bad thing. I'll post a v2 shortly... > I'll think about this more, but the simple solution is attached if you > want to test. It at least works with a single IOMMU now: > > $ modprobe virtio-iommu > [ 25.180965] virtio_iommu virtio0: input address: 64 bits > [ 25.181437] virtio_iommu virtio0: page mask: 0xfffffffffffff000 > [ 25.214493] virtio-pci 0000:00:03.0: Adding to iommu group 0 > [ 25.233252] virtio-pci 0000:00:03.0: enabling device (0000 -> 0003) > [ 25.334810] e1000e 0000:00:02.0: Adding to iommu group 1 > [ 25.348997] e1000e 0000:00:02.0: enabling device (0000 -> 0002) > ... net test etc > > $ rmmod virtio-iommu > [ 34.084816] e1000e: eth1 NIC Link is Down > [ 34.212152] pci 0000:00:02.0: Removing from iommu group 1 > [ 34.250558] pci 0000:00:03.0: Removing from iommu group 0 > [ 34.261570] virtio_iommu virtio0: device removed > > $ modprobe virtio-iommu > [ 34.828982] virtio_iommu virtio0: input address: 64 bits > [ 34.829442] virtio_iommu virtio0: page mask: 0xfffffffffffff000 > [ 34.844576] virtio-pci 0000:00:03.0: Adding to iommu group 0 > [ 34.916449] e1000e 0000:00:02.0: Adding to iommu group 1 > > Thanks, > Jean > From 5437fcaabe1d4671e2dc5b90b7898c0bf698111b Mon Sep 17 00:00:00 2001 > From: Jean-Philippe Brucker > Date: Mon, 4 Nov 2019 15:52:36 +0100 > Subject: [PATCH] iommu: Add bus_unset_iommu() > > Let modular IOMMU drivers undo bus_set_iommu(). Keep track of bus > registrations with a list and refcount, and remove the iommu_ops from > the bus when there are no IOMMU providers anymore. > > Signed-off-by: Jean-Philippe Brucker > --- > drivers/iommu/iommu.c | 101 ++++++++++++++++++++++++++++++++++-------- > include/linux/iommu.h | 1 + > 2 files changed, 84 insertions(+), 18 deletions(-) To be honest, I think we should be trying to move *away* from the bus-ops abstraction rather than extending it. We already don't need it for DMA domains on arm64, and I think it's really just a bit of a wart now because iommu_domain_alloc() takes a 'struct bus_type *' as its argument. Will