Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4589746iob; Sun, 8 May 2022 18:25:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx9frU3bigF7gQlR42DJQbYFr48ENwBa6phEJ0eIXL4PnK6qGbsfa9aokA2T2uLnj1rA9GV X-Received: by 2002:a17:903:1108:b0:15f:bce:19f8 with SMTP id n8-20020a170903110800b0015f0bce19f8mr4221714plh.7.1652059539575; Sun, 08 May 2022 18:25:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652059539; cv=none; d=google.com; s=arc-20160816; b=w+TTAtiA9ZOs7fx8eXNN1c7+L79oeuRxSJqqQ1gGQgJIAulIeQJ7Ml5qn4xCs8W9Gs qg6ZvIIRuMOkS5YfjOjkwkSp2eLp30juuMYsgEfvUoAgR+rpzfnuKLMexRomR3jlsC5y d1Gr7AUiOVRGcKtc5X8Gbt5HEi+J8NHK/vvSEz8FdAxCrm7OQyBdEb4imXzWHO1zvfdR AIgMeIrcdrsnQ5TYE6mqmym9eEaPQBwB4HvNQNjYRo06x1BHevtqC6c4mKU22nW2jijh uM3AROybD6+ioGDdVTeql9EnE57Tmj90u6CK1Sv74OX2q+LUiMnM5uyfIo4G3b62BADy h9WA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Kb8f4M/l5z6uxpDp6qlCrIRzZnhYZpwv4r1elaJ2Id0=; b=bUZsMhEjtX9WeFJZ6ls68ruyQFDSjvqdpdKX8p8NRDnzLpQzL67QXoertFA9nh33M+ XlXD7lZfPNaTlGBi3hnLHTqDYe0wjrZzVAZizA8CFVX0lFJWJEOHqpgCqgvc+QnAHrFN m0CYMrMdXTrtsB3Zvkm42GnCgKownoZn+UArmbNnBNR1Ds3fv3u8ukLHGzJbhc4gStNH WVTeBs51LLWrmZ4hgl3qjCbqlvHDV6i2G4t1GhG9SWRTG0k2xs/du8wgVLsxqoGH7Lqc RBsodqJ9KJuzU6CaoOJ65ObFMjoGOKqa/osYhA5pueCmqcTv/AnlLgP0t8Anzv0EQxqs m9Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bxZRCmDQ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id x4-20020a1709029a4400b0015d1c611e6asi9407097plv.520.2022.05.08.18.25.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 May 2022 18:25:39 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bxZRCmDQ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DE6E113E87; Sun, 8 May 2022 18:25:25 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378562AbiEENms (ORCPT + 99 others); Thu, 5 May 2022 09:42:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229620AbiEENmr (ORCPT ); Thu, 5 May 2022 09:42:47 -0400 Received: from mail-oa1-x2d.google.com (mail-oa1-x2d.google.com [IPv6:2001:4860:4864:20::2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFA0E56C32 for ; Thu, 5 May 2022 06:39:07 -0700 (PDT) Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-d6e29fb3d7so4216425fac.7 for ; Thu, 05 May 2022 06:39:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=Kb8f4M/l5z6uxpDp6qlCrIRzZnhYZpwv4r1elaJ2Id0=; b=bxZRCmDQan2xGXp3qqVXNGx862ETbFFt31nV6CLZpdPipaMEq/oLSslZtPA08TKTgM QQLXHg6zP65ezFnRj2snvB65jWMXnqQR0oxAxiZiJVr5ucUPgtCpVnA5B1U1ydXnNV4O aW6bYQFeLUMU6/nPEXiWn7k1KcvHW+CLGoXfBQ/RB4Rho/9oF1VvJoPL7XoEQumg+g1H X1k5ys3IoDR0ZxusVW9AVeheT6/Cb7mECpjzufoOQqIcOUqnr4CrKvC+xb3LYoHgSMcl Wz6oGBXdFkHgYShZC1cPEJYkl4+qK8Fwy/BAfMi9dQyj8mhyXrwMLTmlrRmpMlkAa9/E Ha4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=Kb8f4M/l5z6uxpDp6qlCrIRzZnhYZpwv4r1elaJ2Id0=; b=MVj3xDMhfXOybzRmoQH44A6vZdnnNQu/u1nMa6wgBxXZVMZ7VSDIgmEQPLcqSe6Cy+ FxRtSwraLE0B09gE/2qDBWOzSL6/4PDxnlHKaR8I7rgThe7/C4qx1YAkaXdNe8Fccq1+ nzXUzj+I/lXYoqUTlFp9jG1b5dzRswC0put5KnYmVph1MgP9gSRJvdq0xYdpHqx1CgrE xEojZEr9AJCkuxj6mN0PX17QX6feJzBQUbfSBy2lse+xO8D6kE79+vhdgeTbNycyMcE9 Gys8UnuLGNYBCBIZpV0deqALofK2mIGnj7UXGbYd3ptJVCb0MSfPZMiTxfcev6PAXWdx pCdQ== X-Gm-Message-State: AOAM532xxRF3Z5uWXfCGCI3teVIxrt/wdusA/GKhE3g6GI6CIPkowb5a YxUoz2IAWnToX6s4ve1jeQjMDDHy9Dgt75aX X-Received: by 2002:a05:6870:d78d:b0:d4:4b29:7ccc with SMTP id bd13-20020a056870d78d00b000d44b297cccmr2253927oab.69.1651757947172; Thu, 05 May 2022 06:39:07 -0700 (PDT) Received: from myrica (cpc92880-cmbg19-2-0-cust679.5-4.cable.virginm.net. [82.27.106.168]) by smtp.gmail.com with ESMTPSA id x6-20020a4a8d46000000b0035eb4e5a6c3sm774557ook.25.2022.05.05.06.39.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 May 2022 06:39:06 -0700 (PDT) Date: Thu, 5 May 2022 14:38:39 +0100 From: Jean-Philippe Brucker To: Baolu Lu 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 Subject: Re: [PATCH v5 10/12] iommu: Prepare IOMMU domain for IOPF Message-ID: References: <20220502014842.991097-1-baolu.lu@linux.intel.com> <20220502014842.991097-11-baolu.lu@linux.intel.com> <9144a782-04d2-a09d-4ac1-7133e5986619@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9144a782-04d2-a09d-4ac1-7133e5986619@linux.intel.com> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Hi Baolu, On Thu, May 05, 2022 at 04:31:38PM +0800, Baolu Lu wrote: > 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? I wasn't clear, it's iommu_detach_device_pasid() that holds the group->mutex: iommu_sva_unbind_device() | iommu_detach_device_pasid() | mutex_lock(&group->mutex) | domain->ops->detach_dev_pasid() | iopf_handle_group() iopf_queue_flush_dev() | iommu_get_domain_for_dev_pasid() ... wait for IOPF work | mutex_lock(&group->mutex) | ... deadlock Thanks, Jean > > 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; > > > +} >