Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp984635rwb; Thu, 6 Oct 2022 07:07:45 -0700 (PDT) X-Google-Smtp-Source: AMsMyM50AMPPV3nRzZ8CcyXGCVt2VlcxA9lCYZiQIFodwum+0rMlDQ2s5sn42YL+ytPYgPlhDVLM X-Received: by 2002:a05:6a00:168e:b0:53b:3f2c:3213 with SMTP id k14-20020a056a00168e00b0053b3f2c3213mr129063pfc.56.1665065265466; Thu, 06 Oct 2022 07:07:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665065265; cv=none; d=google.com; s=arc-20160816; b=Q8eUgl1aP44o7VnDf5dGEF63WhYf9pKmv5C0ks/w/aYSvioQZctfrO5rAB/IOlJ2a1 PLZFBCs7HGuSZ1aVWxed4tCqLIbbSNHdKgNOkRlMv3PpbCiEaq6o+4cwWT41Lmb+obSy HrjNEpQHGK+7NpHVvFN13Q9VZ6oY2rNH91AECRL0qpJkDf8Yn16IsazwvMEyWmgMTnRj Ab9d28mTBq8Fk1RiYCOWFsCfnKVhwmKHrRwANbusOqPcZzcbtoEZv5+wu2vS8UG78iHY Y54XsycCQ5hyskpzjBxZrX9Nxtnxo+jmTxPsE5IEwL73a9apR69r2oCQWfCyiQ1/9NAA d69A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=mWMJic3+3eVweR7Hb3wJwKAsDypmbh3PTgAfppiGim0=; b=P05ZREChdl8VFC4C3ayQZodh/oipVddwGkmbpa0o/kYQsn6Wr5eTIaWczgPe8o59ML MXipNS8IUIRiUp2C+bJXFDtPHnHx+jH3+tTr8CmKeY+aD+VEy2vwX9jn2Q3kVuCDlZEF A8txLft3ciHQ/qZW3rlSDu3ZA9FAuwqo3n9Pq5HR3f1bLMckswlwroaXM9UVz5nceYRS VHXIhtVLTWD/1AXPTvYKlz/ydE5XVcOxiuWNuXzsy72CXSQI35fY2KsU9Vq9vnXUA+Xv Qi5s5g/NoJpM0siczLihSIjofHKfGlPJ1EES1TtWQHkgzg3fb4/gszR4jLS/QuZjpxlz VFWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZkJ3eSSv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 136-20020a63028e000000b00452a7b4f50bsi11060939pgc.48.2022.10.06.07.07.32; Thu, 06 Oct 2022 07:07:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZkJ3eSSv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231417AbiJFNuR (ORCPT + 99 others); Thu, 6 Oct 2022 09:50:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230472AbiJFNuJ (ORCPT ); Thu, 6 Oct 2022 09:50:09 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33255286CA for ; Thu, 6 Oct 2022 06:49:56 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id a5-20020a17090aa50500b002008eeb040eso5000066pjq.1 for ; Thu, 06 Oct 2022 06:49:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=mWMJic3+3eVweR7Hb3wJwKAsDypmbh3PTgAfppiGim0=; b=ZkJ3eSSvWEQHG4zrCYO/rtAQMC+rTCblUgkXT/sojUfeAz6djcfzmaQhTE1VFhHlb5 c1atXYEjU3xEGE7hI13KaDEcIgKWkEtN1hvwHwEilxv96++sncyPLWqzhVGEXpdUs06G CdnNu3apyqKTZeVSCSOWHTmHzGnFe3F2KnJM3MWKtya9Y3YnYefumWTirYL9USS1lYub A/mEPLmhBfwN3a8nKOKM2rGPY5Mu7KrZF82KX4Ex4dyspcvVZE3FoiZPzuE8M8MOv+bw OGNw6BEO2hAZNd93A/lo5mHGoPx0ZTYnxKuuVNn8KZcYJUWz6h7H+Lqu3yOpWNU2Gh62 93nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=mWMJic3+3eVweR7Hb3wJwKAsDypmbh3PTgAfppiGim0=; b=svI6PWebPisnhrMk85wGt/9l2v6BKfSSU1yQG5+bgV5EsV4zvtc/BrwMeLFImzS5bB X7jEeAhsLGJQhCqVUgVjMtSs7dkB2UGl9MlRu3oybFmLzP6jUvcWIniX4G6932UXsboi K6EdJU1GtlU3bK+5AdFqYfP+cvx1wvk7sOwW0wERIOpKDQofqeSm4wsyyZiR4THEYPtB lWFYirN/Ipj0pY5JcKi4GxC75qGW6sA+nzmbxsxXVIuMMO/sadm+0hCyj1EL9crPl0b3 +qF87NVRMl93E7kz75xxEYpqVENcZrnkjAL4d1H2L/h5vLbemSsFEJ3qEHdh6RqE4JN6 jnag== X-Gm-Message-State: ACrzQf1jIT99/Oopa6o/T7e5/UDISlpDI7tcQmJWC7TgbS1vVyLxI/fz +jsDYCGHZ+Yl0PP2gcP8imRk X-Received: by 2002:a17:90a:4b47:b0:20a:e47c:1450 with SMTP id o7-20020a17090a4b4700b0020ae47c1450mr10742123pjl.198.1665064195847; Thu, 06 Oct 2022 06:49:55 -0700 (PDT) Received: from localhost.localdomain ([220.158.158.220]) by smtp.gmail.com with ESMTPSA id k25-20020a635a59000000b00434760ee36asm1874053pgm.16.2022.10.06.06.49.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 06:49:55 -0700 (PDT) From: Manivannan Sadhasivam To: kishon@kernel.org, lpieralisi@kernel.org, bhelgaas@google.com Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, kw@linux.com, robh@kernel.org, vidyas@nvidia.com, vigneshr@ti.com, Manivannan Sadhasivam Subject: [PATCH v3 3/5] PCI: endpoint: Use a separate lock for protecting epc->pci_epf list Date: Thu, 6 Oct 2022 19:19:25 +0530 Message-Id: <20221006134927.41437-4-manivannan.sadhasivam@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221006134927.41437-1-manivannan.sadhasivam@linaro.org> References: <20221006134927.41437-1-manivannan.sadhasivam@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=ham 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 The EPC controller maintains a list of EPF drivers added to it. For protecting this list against the concurrent accesses, the epc->lock (used for protecting epc_ops) has been used so far. Since there were no users trying to use epc_ops and modify the pci_epf list simultaneously, this was not an issue. But with the addition of callback mechanism for passing the events, this will be a problem. Because the pci_epf list needs to be iterated first for getting hold of the EPF driver and then the relevant event specific callback needs to be called for the driver. If the same epc->lock is used, then it will result in a deadlock scenario. For instance, ... mutex_lock(&epc->lock); list_for_each_entry(epf, &epc->pci_epf, list) { epf->event_ops->core_init(epf); | |-> pci_epc_set_bar(); | |-> mutex_lock(&epc->lock) # DEADLOCK ... So to fix this issue, use a separate lock called "list_lock" for protecting the pci_epf list against the concurrent accesses. This lock will also be used by the callback mechanism. Signed-off-by: Manivannan Sadhasivam --- drivers/pci/endpoint/pci-epc-core.c | 9 +++++---- include/linux/pci-epc.h | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/pci/endpoint/pci-epc-core.c b/drivers/pci/endpoint/pci-epc-core.c index 3bc9273d0a08..6cce430d431b 100644 --- a/drivers/pci/endpoint/pci-epc-core.c +++ b/drivers/pci/endpoint/pci-epc-core.c @@ -613,7 +613,7 @@ int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf, if (type == SECONDARY_INTERFACE && epf->sec_epc) return -EBUSY; - mutex_lock(&epc->lock); + mutex_lock(&epc->list_lock); func_no = find_first_zero_bit(&epc->function_num_map, BITS_PER_LONG); if (func_no >= BITS_PER_LONG) { @@ -640,7 +640,7 @@ int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf, list_add_tail(list, &epc->pci_epf); ret: - mutex_unlock(&epc->lock); + mutex_unlock(&epc->list_lock); return ret; } @@ -672,11 +672,11 @@ void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf, list = &epf->sec_epc_list; } - mutex_lock(&epc->lock); + mutex_lock(&epc->list_lock); clear_bit(func_no, &epc->function_num_map); list_del(list); epf->epc = NULL; - mutex_unlock(&epc->lock); + mutex_unlock(&epc->list_lock); } EXPORT_SYMBOL_GPL(pci_epc_remove_epf); @@ -773,6 +773,7 @@ __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, } mutex_init(&epc->lock); + mutex_init(&epc->list_lock); INIT_LIST_HEAD(&epc->pci_epf); ATOMIC_INIT_NOTIFIER_HEAD(&epc->notifier); diff --git a/include/linux/pci-epc.h b/include/linux/pci-epc.h index a48778e1a4ee..fe729dfe509b 100644 --- a/include/linux/pci-epc.h +++ b/include/linux/pci-epc.h @@ -122,6 +122,7 @@ struct pci_epc_mem { * struct pci_epc - represents the PCI EPC device * @dev: PCI EPC device * @pci_epf: list of endpoint functions present in this EPC device + * list_lock: Mutex for protecting pci_epf list * @ops: function pointers for performing endpoint operations * @windows: array of address space of the endpoint controller * @mem: first window of the endpoint controller, which corresponds to @@ -139,6 +140,7 @@ struct pci_epc_mem { struct pci_epc { struct device dev; struct list_head pci_epf; + struct mutex list_lock; const struct pci_epc_ops *ops; struct pci_epc_mem **windows; struct pci_epc_mem *mem; -- 2.25.1