From nobody Mon Sep 15 19:24:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54DA8C5479D for ; Wed, 11 Jan 2023 11:44:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238510AbjAKLne (ORCPT ); Wed, 11 Jan 2023 06:43:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229923AbjAKLm2 (ORCPT ); Wed, 11 Jan 2023 06:42:28 -0500 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 577BD1AA1B for ; Wed, 11 Jan 2023 03:41:27 -0800 (PST) Received: by mail-pj1-x1032.google.com with SMTP id o1-20020a17090a678100b00219cf69e5f0so19739237pjj.2 for ; Wed, 11 Jan 2023 03:41:27 -0800 (PST) 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 :message-id:reply-to; bh=oH9vt83FVTay5mm3rIS7SlgtDXXGxJxNj0BGxkKfd2s=; b=Wg4x6bfvkxTfPYAlmxDxvXJtxohXxtVWE6cbO6bG+wfAiroXDjoQq3gbHUJT2zTDQw E/UV7gwj4xckA4/JVrgrdY6CLiFH1c/0HQMFN9aqUCn/i3e4dPIMdAaohF7d9887/yvE cAxTsO6JIpTz2M89IuqxPUX04QGOeBw6jlkllTgVQJcj+QTtQgUZgx0oQ14eVfe8rjXD jViJwLb7gk8sILVD2hefT36N8F+Cj6l5JpO5pr7YP1QM/4etP81ewSNM777dv1QQr3Q7 x8OTqxog5q7+qnQ/NQhxU4JHjXCX+pe475L2HMmxJfy9kZDzRq/nRC19yxkpaEuIM39N 02KQ== 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:message-id:reply-to; bh=oH9vt83FVTay5mm3rIS7SlgtDXXGxJxNj0BGxkKfd2s=; b=720vwBAOeUYuHfEhoScKwksn8bFhr8EhOgYT8yKtY08YnxdD6aeR35r5gfuSxLDtmD Xzinx67MGRHDiYZeSicGIMmNKcWFVr8Ko6j1BOirmD65V9KwiC9j/OxQqkNoc554rgKH tua5vSNfGOWXqEReXFu7wx56gEtiX8OI+M6ebn1hlL2cy+RPacOG9UWA/Mttg9i7FjmX iYqGPZFu7PXf8ZcACYZVjuRPgyNq20nApsUt/CFSzSLgLoTk/Ba20+e1X1DR/OGUmQTf nIzopo4p/XXXC4eAQ9sXCcm/fXxq6od9KzUmJsuynLpvTmSaGbMy/5ItKcBPFZuVaAXv fX8w== X-Gm-Message-State: AFqh2kq87ucGlMXfLADc9Nx/P1EoNRKeci5sOzQ2t+a+hAtdRmWYYk17 rz8+QCm34wiA6iECffSlJYN5 X-Google-Smtp-Source: AMrXdXupiESlafMbBbpuY+/ZXnQBHBLh7EwSN1/svtwmT2KZuuvn8C/KLT5Ub0Al9vfL/2yOHhRtPg== X-Received: by 2002:a17:902:7d8e:b0:194:4981:2018 with SMTP id a14-20020a1709027d8e00b0019449812018mr1972566plm.60.1673437286659; Wed, 11 Jan 2023 03:41:26 -0800 (PST) Received: from localhost.localdomain ([117.217.177.1]) by smtp.gmail.com with ESMTPSA id u14-20020a170902e5ce00b0018958a913a2sm9942688plf.223.2023.01.11.03.41.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Jan 2023 03:41:25 -0800 (PST) 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 , Kishon Vijay Abraham I Subject: [RESEND v4 3/5] PCI: endpoint: Use a separate lock for protecting epc->pci_epf list Date: Wed, 11 Jan 2023 17:10:57 +0530 Message-Id: <20230111114059.6553-4-manivannan.sadhasivam@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230111114059.6553-1-manivannan.sadhasivam@linaro.org> References: <20230111114059.6553-1-manivannan.sadhasivam@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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. Acked-by: Kishon Vijay Abraham I 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 2542196e8c3d..2c023db8f51c 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 =3D=3D SECONDARY_INTERFACE && epf->sec_epc) return -EBUSY; =20 - mutex_lock(&epc->lock); + mutex_lock(&epc->list_lock); func_no =3D find_first_zero_bit(&epc->function_num_map, BITS_PER_LONG); if (func_no >=3D BITS_PER_LONG) { @@ -640,7 +640,7 @@ int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf= *epf, =20 list_add_tail(list, &epc->pci_epf); ret: - mutex_unlock(&epc->lock); + mutex_unlock(&epc->list_lock); =20 return ret; } @@ -672,11 +672,11 @@ void pci_epc_remove_epf(struct pci_epc *epc, struct p= ci_epf *epf, list =3D &epf->sec_epc_list; } =20 - mutex_lock(&epc->lock); + mutex_lock(&epc->list_lock); clear_bit(func_no, &epc->function_num_map); list_del(list); epf->epc =3D NULL; - mutex_unlock(&epc->lock); + mutex_unlock(&epc->list_lock); } EXPORT_SYMBOL_GPL(pci_epc_remove_epf); =20 @@ -777,6 +777,7 @@ __pci_epc_create(struct device *dev, const struct pci_e= pc_ops *ops, } =20 mutex_init(&epc->lock); + mutex_init(&epc->list_lock); INIT_LIST_HEAD(&epc->pci_epf); ATOMIC_INIT_NOTIFIER_HEAD(&epc->notifier); =20 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; --=20 2.25.1