From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A62EC43461 for ; Tue, 8 Sep 2020 07:46:27 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 07D7B21D43 for ; Tue, 8 Sep 2020 07:46:27 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="SCQBCO8n"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Xw/xqnPD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 07D7B21D43 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date:Message-ID:From: References:To:Subject:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=+8hahQbW14KCuK6CHOeIJ6U8M5BK8GJUUKq2ZoY7lV4=; b=SCQBCO8ni/k837ygaviKXjMG4 4hbjPN7Abc5foN/D6WNNedkoncCeC3Fz9iYrCO5J0s1O+MFeRTew8LXK2l5ilt+8owgv8kb510VwO QJWqmkVQwY/U7L0fDRlxf6ZM55/mZHPu+bn7Agp53CPKecSHwBVmqRizFDplz118DjrKTU45VnuSC 1DnykyOm4k+hx3H7j6wI4jx4XnOyXIgxt41vmBTLLxrdvJZERtUIrGedKJ6yye7NoUiX9DDXfut2y zJI5FLt72CUWDYGLA/R2Q8lWdqx2dgl74DJyrxajulYQXYL8viN2Bgo9Gj2/E3bmWqfEn1O0xmMaz 8TaFkJchQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kFYJX-0005je-7q; Tue, 08 Sep 2020 07:45:15 +0000 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kFYJU-0005ig-4D for linux-arm-kernel@lists.infradead.org; Tue, 08 Sep 2020 07:45:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1599551111; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UORcFQ1IG5Tr44HvdIPG+Z5dSXuiK6yNKBHVDJGNA60=; b=Xw/xqnPDLBLGyMXsAT6y0LKus8yQ3z1cVDe1Ljof0Mtr0PBg5FouWEzoguMPGjL35jFsaa UyCnCdKpeYdNuDSewfXdlWtd/Fqs1ElCTU3e2W5iyz0BwSqCzxS22FsWqoXcrMc1jKGcSW SmFc7zEqBeI5bbdFr930E+iru5Hufmk= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-288-Fr1lcA3YM462hrs_4_daFQ-1; Tue, 08 Sep 2020 03:45:07 -0400 X-MC-Unique: Fr1lcA3YM462hrs_4_daFQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2223C1DDF1; Tue, 8 Sep 2020 07:45:06 +0000 (UTC) Received: from [10.36.112.51] (ovpn-112-51.ams2.redhat.com [10.36.112.51]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BA15582463; Tue, 8 Sep 2020 07:45:03 +0000 (UTC) Subject: Re: [PATCH RESEND v9 03/13] iommu/sva: Add PASID helpers To: Jean-Philippe Brucker , iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org References: <20200817171558.325917-1-jean-philippe@linaro.org> <20200817171558.325917-4-jean-philippe@linaro.org> From: Auger Eric Message-ID: Date: Tue, 8 Sep 2020 09:45:02 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <20200817171558.325917-4-jean-philippe@linaro.org> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eric.auger@redhat.com X-Mimecast-Spam-Score: 0.002 X-Mimecast-Originator: redhat.com Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200908_034512_254696_774DD881 X-CRM114-Status: GOOD ( 27.79 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fenghua.yu@intel.com, will@kernel.org, zhengxiang9@huawei.com, catalin.marinas@arm.com, zhangfei.gao@linaro.org, robin.murphy@arm.com Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Jean, On 8/17/20 7:15 PM, Jean-Philippe Brucker wrote: > Let IOMMU drivers allocate a single PASID per mm. Store the mm in the > IOASID set to allow refcounting and searching mm by PASID, when handling > an I/O page fault. > > Reviewed-by: Lu Baolu > Signed-off-by: Jean-Philippe Brucker > --- > drivers/iommu/Kconfig | 5 +++ > drivers/iommu/Makefile | 1 + > drivers/iommu/iommu-sva-lib.h | 15 +++++++ > drivers/iommu/iommu-sva-lib.c | 85 +++++++++++++++++++++++++++++++++++ > 4 files changed, 106 insertions(+) > create mode 100644 drivers/iommu/iommu-sva-lib.h > create mode 100644 drivers/iommu/iommu-sva-lib.c > > diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig > index bef5d75e306b..fb1787377eb6 100644 > --- a/drivers/iommu/Kconfig > +++ b/drivers/iommu/Kconfig > @@ -103,6 +103,11 @@ config IOMMU_DMA > select IRQ_MSI_IOMMU > select NEED_SG_DMA_LENGTH > > +# Shared Virtual Addressing library > +config IOMMU_SVA_LIB > + bool > + select IOASID > + > config FSL_PAMU > bool "Freescale IOMMU support" > depends on PCI > diff --git a/drivers/iommu/Makefile b/drivers/iommu/Makefile > index 11f1771104f3..61bd30cd8369 100644 > --- a/drivers/iommu/Makefile > +++ b/drivers/iommu/Makefile > @@ -27,3 +27,4 @@ obj-$(CONFIG_FSL_PAMU) += fsl_pamu.o fsl_pamu_domain.o > obj-$(CONFIG_S390_IOMMU) += s390-iommu.o > obj-$(CONFIG_HYPERV_IOMMU) += hyperv-iommu.o > obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o > +obj-$(CONFIG_IOMMU_SVA_LIB) += iommu-sva-lib.o > diff --git a/drivers/iommu/iommu-sva-lib.h b/drivers/iommu/iommu-sva-lib.h > new file mode 100644 > index 000000000000..b40990aef3fd > --- /dev/null > +++ b/drivers/iommu/iommu-sva-lib.h > @@ -0,0 +1,15 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * SVA library for IOMMU drivers > + */ > +#ifndef _IOMMU_SVA_LIB_H > +#define _IOMMU_SVA_LIB_H > + > +#include > +#include > + > +int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max); > +void iommu_sva_free_pasid(struct mm_struct *mm); > +struct mm_struct *iommu_sva_find(ioasid_t pasid); > + > +#endif /* _IOMMU_SVA_LIB_H */ > diff --git a/drivers/iommu/iommu-sva-lib.c b/drivers/iommu/iommu-sva-lib.c > new file mode 100644 > index 000000000000..db7e6c104d6b > --- /dev/null > +++ b/drivers/iommu/iommu-sva-lib.c > @@ -0,0 +1,85 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Helpers for IOMMU drivers implementing SVA > + */ > +#include > +#include > + > +#include "iommu-sva-lib.h" > + > +static DEFINE_MUTEX(iommu_sva_lock); > +static DECLARE_IOASID_SET(iommu_sva_pasid); > + > +/** > + * iommu_sva_alloc_pasid - Allocate a PASID for the mm > + * @mm: the mm > + * @min: minimum PASID value (inclusive) > + * @max: maximum PASID value (inclusive) > + * > + * Try to allocate a PASID for this mm, or take a reference to the existing one > + * provided it fits within the [min, max] range. On success the PASID is > + * available in mm->pasid, and must be released with iommu_sva_free_pasid(). > + * > + * Returns 0 on success and < 0 on error. > + */ > +int iommu_sva_alloc_pasid(struct mm_struct *mm, ioasid_t min, ioasid_t max) > +{ > + int ret = 0; > + ioasid_t pasid; > + > + if (min == INVALID_IOASID || max == INVALID_IOASID || > + min == 0 || max < min) you may add a comment explaining why min == 0 is forbidden. > + return -EINVAL; > + > + mutex_lock(&iommu_sva_lock); > + if (mm->pasid) { > + if (mm->pasid >= min && mm->pasid <= max) > + ioasid_get(mm->pasid); > + else > + ret = -EOVERFLOW; > + } else { > + pasid = ioasid_alloc(&iommu_sva_pasid, min, max, mm); > + if (pasid == INVALID_IOASID) > + ret = -ENOMEM; > + else > + mm->pasid = pasid; > + } > + mutex_unlock(&iommu_sva_lock); > + return ret; > +} > +EXPORT_SYMBOL_GPL(iommu_sva_alloc_pasid); > + > +/** > + * iommu_sva_free_pasid - Release the mm's PASID > + * @mm: the mm. > + * > + * Drop one reference to a PASID allocated with iommu_sva_alloc_pasid() > + */ > +void iommu_sva_free_pasid(struct mm_struct *mm) > +{ > + mutex_lock(&iommu_sva_lock); > + if (ioasid_put(mm->pasid)) > + mm->pasid = 0; ditto: 0 versus INVALID_IOASID > + mutex_unlock(&iommu_sva_lock); > +} > +EXPORT_SYMBOL_GPL(iommu_sva_free_pasid); > + > +/* ioasid wants a void * argument */ shouldn't it be: ioasid_find getter() requires a void *arg? > +static bool __mmget_not_zero(void *mm) > +{ > + return mmget_not_zero(mm); > +} > + > +/** > + * iommu_sva_find() - Find mm associated to the given PASID > + * @pasid: Process Address Space ID assigned to the mm > + * > + * On success a reference to the mm is taken, and must be released with mmput(). > + * > + * Returns the mm corresponding to this PASID, or an error if not found. > + */ > +struct mm_struct *iommu_sva_find(ioasid_t pasid) > +{ > + return ioasid_find(&iommu_sva_pasid, pasid, __mmget_not_zero); > +} > +EXPORT_SYMBOL_GPL(iommu_sva_find); > Thanks Eric _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel