Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4646499iob; Sun, 8 May 2022 20:28:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwDoLV9Ob8U3slN6g55pujD113nmAYHU0exrGpDumH90/qhbaSu5eQBLUnkbM2eviqm2Q6/ X-Received: by 2002:a17:90b:1811:b0:1dc:8d37:b4cb with SMTP id lw17-20020a17090b181100b001dc8d37b4cbmr24499333pjb.101.1652066934241; Sun, 08 May 2022 20:28:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652066934; cv=none; d=google.com; s=arc-20160816; b=K7JNnACXcD+uroVjk37xx5Z1/b7RSEkXvPkTAUbP0C7ohLKqFMQzLi43hKbvybI51T LEdbbwxi4wvUgb3ss/Yx5HvxYzf8aI0TfKHESmRIuqJKGgkjk79o6p6jWqp6ibdnSgj9 HvAYCBT1TWfJdt3Ns6T3LFtAHHn+/5a6iHwymcqLhhNKBGzQgtUoNHC0R5MhyIY//tBe jY3yJXTVIVXcuOuL1H69DNwHQx9XHhXIVh9BPDH0Shde4J32ojmyAgEcpygAfZuy+SeU swdWeWGe/17t178AWaOiVbGsSAoaoWPEuMfwZODJSM4e+6xNFR0nHNjqLWKsjlzkcBAK Jg8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=XU3txU7U3eblbJoXMqwCW6h3mfiL5X23+hj1V+28Rto=; b=n9Gx7sdrO6e/gl9aNki+e/UbKr6C0I5vKtxkwxEvkDuv3kNtqER5LtNeRwdVljov5s g8vNrquts/zbOKa0daRmt/hCpMeUwDbfTbRohLYSZfnAPqosXntvDPgLVhk7nMT0CZNf UTupZURnMUakwIGosqQxY1/B01eYmlTADBzlPXmNw/92d4N56SvTcRYO56B5D8VNz1Kp gW4ngi9UNao3aA/mYGmq93f1+CFqDA6/9AH/D8R24QQYRjCN6e+VqpENy/h5HMUMDel1 jW2rrUy4ps9GOyGAUs9jjhWkUSV6+8ZxMwTyLWdsIyVcF0+E064D6TfDapeVEeDKM86P M/Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZX4xoxoF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id c188-20020a6335c5000000b003c165f258e9si13132203pga.665.2022.05.08.20.28.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 20:28:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZX4xoxoF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4677884A1D; Sun, 8 May 2022 20:28:37 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349289AbiEEIfa (ORCPT + 99 others); Thu, 5 May 2022 04:35:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245568AbiEEIf2 (ORCPT ); Thu, 5 May 2022 04:35:28 -0400 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2DA00DF51 for ; Thu, 5 May 2022 01:31:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1651739509; x=1683275509; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=roh0ARsU0A0vGZ1wvEez4gzYA90rB5c2aWwIVA4MWDc=; b=ZX4xoxoFxFi7W0qH0LGW3/waUotLisH8j0s6bjK0+xOl87sV0CUbdeXR NJya+2A6LAEy/GQTWQ6XSDGZxnbHwHiJv3AaOzaCcQAFwdbznBHBukm6J 0BabLdbV8Qdhw79o312JBzdHEmOydJsMdIMxhfnwL9tU0WnheA6wkDX8d xogmUxYZL/23fcvQipUcnC94NF3+3B+cMTU49Dv1KULCHBLRPuwXwJnUq FhM/6b/mqbZErSD2oUfwN69d46HStZH19pLfxFMJ1e9iKy3VRT93YeSlt 1MDdO65TjQ80Tvu8TYZMXe7nYeVJX7p0Pb+ZOf+v1402IgAbVxlRxCe4N w==; X-IronPort-AV: E=McAfee;i="6400,9594,10337"; a="267638993" X-IronPort-AV: E=Sophos;i="5.91,200,1647327600"; d="scan'208";a="267638993" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2022 01:31:48 -0700 X-IronPort-AV: E=Sophos;i="5.91,200,1647327600"; d="scan'208";a="563139126" Received: from minhaowa-mobl.ccr.corp.intel.com (HELO [10.255.30.75]) ([10.255.30.75]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 May 2022 01:31:41 -0700 Message-ID: <9144a782-04d2-a09d-4ac1-7133e5986619@linux.intel.com> Date: Thu, 5 May 2022 16:31:38 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1 Subject: Re: [PATCH v5 10/12] iommu: Prepare IOMMU domain for IOPF Content-Language: en-US To: Jean-Philippe Brucker Cc: Joerg Roedel , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Ashok Raj , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Dave Jiang , Vinod Koul , Eric Auger , Liu Yi L , Jacob jun Pan , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org References: <20220502014842.991097-1-baolu.lu@linux.intel.com> <20220502014842.991097-11-baolu.lu@linux.intel.com> From: Baolu Lu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/5/4 02:20, Jean-Philippe Brucker wrote: >> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c >> index 7cae631c1baa..33449523afbe 100644 >> --- a/drivers/iommu/iommu.c >> +++ b/drivers/iommu/iommu.c >> @@ -3174,3 +3174,24 @@ void iommu_detach_device_pasid(struct iommu_domain *domain, >> >> iommu_group_put(group); >> } >> + >> +struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev, >> + ioasid_t pasid) >> +{ >> + struct iommu_domain *domain; >> + struct iommu_group *group; >> + >> + if (!pasid_valid(pasid)) >> + return NULL; >> + >> + group = iommu_group_get(dev); >> + if (!group) >> + return NULL; >> + >> + mutex_lock(&group->mutex); > Unfortunately this still causes the deadlock when unbind() flushes the > IOPF queue while holding the group mutex. Sorry, I didn't get your point here. Do you mean unbind() could hold group mutex before calling this helper? The group mutex is only available in iommu.c. The unbind() has no means to hold this lock. Or, I missed anything? Best regards, baolu > > If we make this function private to IOPF, then we can get rid of this > mutex_lock(). It's OK because: > > * xarray protects its internal state with RCU, so we can call > xa_load() outside the lock. > > * The domain obtained from xa_load is finalized. Its content is valid > because xarray stores the domain using rcu_assign_pointer(), which has a > release memory barrier, which pairs with data dependencies in IOPF > (domain->sva_ioas etc). > > We'll need to be careful about this when allowing other users to install > a fault handler. Should be fine as long as the handler and data are > installed before the domain is added to pasid_array. > > * We know the domain is valid the whole time IOPF is using it, because > unbind() waits for pending faults. > > We just need a comment explaining the last point, something like: > > /* > * Safe to fetch outside the group mutex because: > * - xarray protects its internal state with RCU > * - the domain obtained is either NULL or fully formed > * - the IOPF work is the only caller and is flushed before the > * domain is freed. > */ > > Thanks, > Jean > >> + domain = xa_load(&group->pasid_array, pasid); >> + mutex_unlock(&group->mutex); >> + iommu_group_put(group); >> + >> + return domain; >> +}