Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3328747pxk; Mon, 28 Sep 2020 14:38:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysawZkig0OCksXrk94ECqeOx3s7A1v0kK+Jnzgh0GA3nYqRHyegPX4Xw4j1MByx+cUpLUX X-Received: by 2002:a17:906:bc47:: with SMTP id s7mr790247ejv.354.1601329114894; Mon, 28 Sep 2020 14:38:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601329114; cv=none; d=google.com; s=arc-20160816; b=BGgtwZ0orlYsd0Hud6qt06dLkqK1Rr0G2s2W40vF1mz023cFaJqVCwP5cKIMBgLLOE CkYQUXuLWirks3PgwcUpC9uOSbOk40wvKO7rUlY4h0y3wSqWvU1b0DFATtW3bgZNvJnF YRrCYmE1jDvlkR3Lp3eOHXfsNPRCUqDNhj3Ul0/Gra1zZ7IXAottBaZSy7rtf1fR2cp2 Iw2DGtILC18Z2PNQ6Ovn1ZDeqGsyAbiir00oX6EkaVRkKpSdMg23Bf761I/WwfyiHKfx ciaqUx1icMOvBqW0jSOgYIzMD81vh8Pzivg37M1pR1t/3Tt67R/bLPl4tAZDkTvyLGm8 7yFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=bwi3i0q/cn+NLcsJ/OschTx3Q/x+51Mi2oWAxj4yj8g=; b=ygW481tyo+sd14TsWUBCO3NkXousCw2wlE4QR6Ix/Jz2EPR62lT9f9gap3r5AYlQR8 3OUi1dfZezoGfTAuBVtz3DyiP3eUbh2yC4AxxIgp5Jjv3GQCPYaa0CPLUtGj+beERsfS G35wfxdM8icFdmzDqIHN5eKrFW0zxgFoe7zt4fWwCrALqFdeUmI3hLP4TknCjx39jvHf u5yTQqMcKmOCBABlQFne47Zu/70z7cQe4NPAahRF2NYkIE2jcgA82DRY0DWd5Kg1R5O0 0FWMR9FnUbQC4Ovrwu+r981XenpZjZYvknFnHlCycom+k3VCZDvKArm1ws+DfkKN5ZwU zkdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=NL596sY6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p7si1511086edq.28.2020.09.28.14.38.12; Mon, 28 Sep 2020 14:38:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=NL596sY6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727049AbgI1Vgz (ORCPT + 99 others); Mon, 28 Sep 2020 17:36:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727027AbgI1Vgt (ORCPT ); Mon, 28 Sep 2020 17:36:49 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0EDFC061755; Mon, 28 Sep 2020 14:36:47 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id h23so337202pjv.5; Mon, 28 Sep 2020 14:36:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bwi3i0q/cn+NLcsJ/OschTx3Q/x+51Mi2oWAxj4yj8g=; b=NL596sY6bqU+rbeX7xBtMeb+xWj2cDDR5UT9FJx8xhU4w82SByKy6IfuXP0HnbkVTo jLJdAIFGrnLu983EZyts0X8Jx98i7P1nF+vNcbAR78d0T3mX25+RcLGZ/RRIppFIEXOJ 3knEe5CgXferUTfrddi6u3uasBCAlAgDBnKIc1+PSX9ozsQscj5WSmkXnOWhbNijRv3Y RTZZsQHyMR5doENwOQw752lpfrLhoyQyFgyjNszTqq9X2dg/fS7leywkuYfv9/YrZ7f4 30TBFIIO6T+Q7Ij/rUqfCntf8cMIo7WIfo6jRZMz80yd9h/AmsnUQywiY7VwX1mivYL5 i5LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bwi3i0q/cn+NLcsJ/OschTx3Q/x+51Mi2oWAxj4yj8g=; b=T9AMp83cWtvmM7tW0PGLp0i+owDGhqEGNrqnZWATZ0xIQcdubdXuycKMKFL0v3ipIZ /p1fi+Irnw63DdaASTV0aFsF/6DEFXYeTUdS9QJ4aSxb/cWamD2gu3Ym7XICGXuYDnD6 eo89zL1wf5IAw4cK97BiewDFIPr4Hnqc36LeaAwIAy+QoahLjpkChEYqOFxYm7IJ4tqD /Lx5rcBJD7fEQlcFsnhc2GJqKKXmESBwGLtDgwpkTeGvkJYZ56elrweto+e4ed1H/7B9 /8U5/iJsoM2Y7qvi8U1RozTXR9ac+NiaDqkgyDK7HwfZtcdSELHr9LwSmIYHc5tYqLxM LrnQ== X-Gm-Message-State: AOAM530T7NGirB966fIpwHAmbjZsegf+plRc4sfiJc0+auyErw44GP1C Rl3SboAbIsfnKhLSe66h+mA= X-Received: by 2002:a17:90a:3d0e:: with SMTP id h14mr1067667pjc.34.1601329007377; Mon, 28 Sep 2020 14:36:47 -0700 (PDT) Received: from jacob-builder.jf.intel.com (jfdmzpr04-ext.jf.intel.com. [134.134.137.73]) by smtp.gmail.com with ESMTPSA id l11sm2220864pjf.17.2020.09.28.14.36.46 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Sep 2020 14:36:46 -0700 (PDT) From: Jacob Pan X-Google-Original-From: Jacob Pan To: iommu@lists.linux-foundation.org, LKML , Joerg Roedel , Alex Williamson , "Lu Baolu" , David Woodhouse , Jonathan Corbet Cc: linux-api@vger.kernel.org, Jean-Philippe Brucker , Eric Auger , Jacob Pan , Yi Liu , "Tian, Kevin" , Raj Ashok , Wu Hao , Yi Sun , Dave Jiang , Randy Dunlap Subject: [PATCH v3 03/14] iommu/ioasid: Add a separate function for detach data Date: Mon, 28 Sep 2020 14:38:30 -0700 Message-Id: <1601329121-36979-4-git-send-email-jacob.jun.pan@linux.intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1601329121-36979-1-git-send-email-jacob.jun.pan@linux.intel.com> References: <1601329121-36979-1-git-send-email-jacob.jun.pan@linux.intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org IOASID private data can be cleared by ioasid_attach_data() with a NULL data pointer. A common use case is for a caller to free the data afterward. ioasid_attach_data() calls synchronize_rcu() before return such that free data can be sure without outstanding readers. However, since synchronize_rcu() may sleep, ioasid_attach_data() cannot be used under spinlocks. This patch adds ioasid_detach_data() as a separate API where synchronize_rcu() is called only in this case. ioasid_attach_data() can then be used under spinlocks. In addition, this change makes the API symmetrical. Signed-off-by: Jacob Pan --- drivers/iommu/intel/svm.c | 4 ++-- drivers/iommu/ioasid.c | 54 ++++++++++++++++++++++++++++++++++++++--------- include/linux/ioasid.h | 5 ++++- 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/drivers/iommu/intel/svm.c b/drivers/iommu/intel/svm.c index 2c5645f0737a..06a16bee7b65 100644 --- a/drivers/iommu/intel/svm.c +++ b/drivers/iommu/intel/svm.c @@ -398,7 +398,7 @@ int intel_svm_bind_gpasid(struct iommu_domain *domain, struct device *dev, list_add_rcu(&sdev->list, &svm->devs); out: if (!IS_ERR_OR_NULL(svm) && list_empty(&svm->devs)) { - ioasid_attach_data(data->hpasid, NULL); + ioasid_detach_data(data->hpasid); kfree(svm); } @@ -441,7 +441,7 @@ int intel_svm_unbind_gpasid(struct device *dev, int pasid) * the unbind, IOMMU driver will get notified * and perform cleanup. */ - ioasid_attach_data(pasid, NULL); + ioasid_detach_data(pasid); kfree(svm); } } diff --git a/drivers/iommu/ioasid.c b/drivers/iommu/ioasid.c index 5f63af07acd5..6cfbdfb492e0 100644 --- a/drivers/iommu/ioasid.c +++ b/drivers/iommu/ioasid.c @@ -272,24 +272,58 @@ int ioasid_attach_data(ioasid_t ioasid, void *data) spin_lock(&ioasid_allocator_lock); ioasid_data = xa_load(&active_allocator->xa, ioasid); - if (ioasid_data) - rcu_assign_pointer(ioasid_data->private, data); - else + + if (!ioasid_data) { ret = -ENOENT; - spin_unlock(&ioasid_allocator_lock); + goto done_unlock; + } - /* - * Wait for readers to stop accessing the old private data, so the - * caller can free it. - */ - if (!ret) - synchronize_rcu(); + if (ioasid_data->private) { + ret = -EBUSY; + goto done_unlock; + } + rcu_assign_pointer(ioasid_data->private, data); + +done_unlock: + spin_unlock(&ioasid_allocator_lock); return ret; } EXPORT_SYMBOL_GPL(ioasid_attach_data); /** + * ioasid_detach_data - Clear the private data of an ioasid + * + * @ioasid: the IOASIDD to clear private data + */ +void ioasid_detach_data(ioasid_t ioasid) +{ + struct ioasid_data *ioasid_data; + + spin_lock(&ioasid_allocator_lock); + ioasid_data = xa_load(&active_allocator->xa, ioasid); + + if (!ioasid_data) { + pr_warn("IOASID %u not found to detach data from\n", ioasid); + goto done_unlock; + } + + if (ioasid_data->private) { + rcu_assign_pointer(ioasid_data->private, NULL); + goto done_unlock; + } + +done_unlock: + spin_unlock(&ioasid_allocator_lock); + /* + * Wait for readers to stop accessing the old private data, + * so the caller can free it. + */ + synchronize_rcu(); +} +EXPORT_SYMBOL_GPL(ioasid_detach_data); + +/** * ioasid_alloc - Allocate an IOASID * @set: the IOASID set * @min: the minimum ID (inclusive) diff --git a/include/linux/ioasid.h b/include/linux/ioasid.h index 9c44947a68c8..c7f649fa970a 100644 --- a/include/linux/ioasid.h +++ b/include/linux/ioasid.h @@ -40,7 +40,7 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, int ioasid_register_allocator(struct ioasid_allocator_ops *allocator); void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator); int ioasid_attach_data(ioasid_t ioasid, void *data); - +void ioasid_detach_data(ioasid_t ioasid); #else /* !CONFIG_IOASID */ static inline ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, void *private) @@ -72,5 +72,8 @@ static inline int ioasid_attach_data(ioasid_t ioasid, void *data) return -ENOTSUPP; } +static inline void ioasid_detach_data(ioasid_t ioasid) +{ +} #endif /* CONFIG_IOASID */ #endif /* __LINUX_IOASID_H */ -- 2.7.4