From patchwork Wed Jan 11 11:40:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manivannan Sadhasivam X-Patchwork-Id: 41927 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp3270586wrt; Wed, 11 Jan 2023 03:45:05 -0800 (PST) X-Google-Smtp-Source: AMrXdXsTED1q8YL8YJzMBYLqKM2rGm0k9VCYRruUg4r8+S9KQEc8xo5nSWdcgBfTFQ8MZ6FDBEDx X-Received: by 2002:a17:906:524b:b0:7c1:5098:907f with SMTP id y11-20020a170906524b00b007c15098907fmr61201144ejm.61.1673437505498; Wed, 11 Jan 2023 03:45:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673437505; cv=none; d=google.com; s=arc-20160816; b=qbVOWH6GnyQBLIetRkoFAjcpVqTRd6idrH0A8e6xNwAsbMESPnzFIZWThX/D1Fgbm7 zQSMX7Ej15t3jN0trR76h5DsFC4e8yCsjrUslCCJmtMD2gPkK0P5s1cgAzWWLsM+QJ9h 9djgsrJM5CJZqQX2aCx6qfmtrTetkcuGqWBV4jfQIapMENquQgGYpRvol6u1KnBorO3/ zkiq8rG22FVJ8YFW52K3gDSUj73YDjwYGUXkRTCmbErHwLSCIaWaAN0dzo42OLEJZ/Uj f/VXMnprK/Q1mggwPZex++dDE6b0K+e0LEGemoJbSfqLBU3GthJjK8XrQ1wDEJdG9hpo Y1lA== 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=oH9vt83FVTay5mm3rIS7SlgtDXXGxJxNj0BGxkKfd2s=; b=MslPFcFxShOF/hY4C4IEWALODP2NYGt0rqN0dE8YrNTymeaZWMB2aYFB3PrhHVPftD zghbhiL/KBlEWkmyAAFXme+RJkauOaKagjsaI5umze8zrueAvJfOfjGM3W/8xHpxmVWQ lqgduziJFtGVOe9POTAZLa2Fbik2zJgGTz4F/xxB5vJxzgAYcDDnLu8T1bRe7+dfKib7 3Jwp995aoXEetMtlvOfJibhZpgSiuS+qGSh6HpMMZHn0M2aOT/8bxGoPWQ4kFVD2hB/l Er6y9fEpdUNJFW+EIPn2eZUFWlFXwao3L41046bYBhWOy5ehBiarxslRtSuNumR1iDJ/ +7VA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Wg4x6bfv; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gt20-20020a1709072d9400b0078d4c9d77adsi16790422ejc.94.2023.01.11.03.44.41; Wed, 11 Jan 2023 03:45:05 -0800 (PST) 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=Wg4x6bfv; 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 S233284AbjAKLnd (ORCPT + 99 others); Wed, 11 Jan 2023 06:43:33 -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-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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754726405751525446?= X-GMAIL-MSGID: =?utf-8?q?1754726405751525446?= 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 == 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); @@ -777,6 +777,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;