From patchwork Tue Jan 24 07:11:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manivannan Sadhasivam X-Patchwork-Id: 47573 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2011282wrn; Mon, 23 Jan 2023 23:19:25 -0800 (PST) X-Google-Smtp-Source: AMrXdXsTHvQMOSaXsABwlxR80HfGXsK+n5zNkkFKRiUFXJaHqtKSaSrBcwczGPkPwhuXQA87hEN+ X-Received: by 2002:a17:907:100c:b0:870:e329:5f3d with SMTP id ox12-20020a170907100c00b00870e3295f3dmr29177231ejb.19.1674544765056; Mon, 23 Jan 2023 23:19:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674544765; cv=none; d=google.com; s=arc-20160816; b=NU4D9siuQWsOnxCZ38+MBURxftR+5ooYrPY6wRWhIiXZUT+slKdkt39peoidE054Bm kHC/MsswE4giwYLo5t4AwRc9G8k87CFWxPJzYy9mJjSaXNPknzCPgtnzWjYJpBSkWBIP crMFZNpFez5Y9dYnvexDehjcVJtmoCiG7x81LNf0dvUwE91J15QFrNLEAqJezMTPjwbB 5/Odb9dV9Gjf3W3mMEunsfFFz7MQfCA4TxJ6BmiXXfg4rBkZ+95ff7QES6Q+3AGx6fpc gBGAbROwYgxsW0NMy3D9aV/qWJw9BnsmmO7OQwPj0wAES3+x6qXqVTGloX6frWfwUQJ0 zxUQ== 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=s99rLvDhf6ATGkjGrNxSr1n7f9HghXrJG2VH9aHevqTUahwUXQ0iQhvLc6Nx13C4yQ Keh3MBRsHc4qNyFFt1TVue+l6Btj8lvXpyP832+9SPxdXORo+s/fFqVfEIlXCGj8MNRT ygp5DWC1yLGky7PYit/UTJR/DQLyDgL2QVX/3d/REPg2hrxhRQWvsd+TW/W303fWYQf+ lFsC+TE8kFD07/iF7rDHtd3uZH6cXCxQcDrQXSa27ENw+L1KqE8NGJoulgqCI+3fN9ss LGwdsb+J1DYS1YT4Vx6TgmnliDGplx9xjU8nQZk7x/nW2tVlIdjq/C48Q6+oBTd6DH75 /m0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KpayRy1m; 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 fb2-20020a1709073a0200b0086b4343995bsi1555167ejc.629.2023.01.23.23.19.01; Mon, 23 Jan 2023 23:19:25 -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=KpayRy1m; 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 S233103AbjAXHMu (ORCPT + 99 others); Tue, 24 Jan 2023 02:12:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233467AbjAXHMq (ORCPT ); Tue, 24 Jan 2023 02:12:46 -0500 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 145EB3EC50 for ; Mon, 23 Jan 2023 23:12:37 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id t12-20020a17090aae0c00b00229f4cff534so760515pjq.1 for ; Mon, 23 Jan 2023 23:12:37 -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=KpayRy1mwChJZrnXae442WsM2cwAA8qXgwj4GzK6axTqDd6Gw56qrfgcyEzk+jiW1m lA/0YVULthaerJWfY2IDUDE4Nk7kG4Ils5j/PBdP/TgfGkfMb5haMfOf/jdiYXP3/r8i +Vpr6A0fZ3rIjOQUg9NBx8zJ9cOkYBIs+bRv/fmLG1U7hykUhmywmxtVyeCHIDNgUvJd DfeeqDLcfc2d7pFBYPWXY+03fdm19/EuJSEzPBTFqVKEflWr1nyiNacn9G1Uw1/T0v6m XvM1fm4Z+AEp0Jyfpf5LNu8c5fyhRlMssYkXQUqysPQwPOhofUpYspWu55ELGAgFFTYZ xAXg== 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=20QOfFNaxwxDXYNPpFZ49etcbeL2Zf4MqFvykmwfu1fL0G4UOhiOdFVOs2TOm00+31 cWl5+hJXjjRiDdEdwUIswM95UNgqJYRWaVjmHTBz97hFoqlYBOfcyAoo9tO2P8QiFHGQ DDAzKVO+LtUEHQJa58030CktBRt0kM56/GnyruHn/xpIQQDzyGh+P8pUSXP961Von6Wq CL0NJP880ayWf3aTPV4VA5houeml4U8wUke7cod/Zd1mI30mStNwhJPhHcUCiVRUELNa 0HjnxFXbIyqnAyayT2zove5mlFSTGR20GPXH6bTP3HkaZDpgP+dF8eMVkEUn3V4PWWam CXLQ== X-Gm-Message-State: AFqh2krfVuWb31E0UYK5g8s6uCpDvKzJcxe+Vqxqy/6m0mMfCSsQG3Md U4cqWewSQWgmFAJiU8LVsvL5 X-Received: by 2002:a05:6a20:3a82:b0:b8:7e6d:5b72 with SMTP id d2-20020a056a203a8200b000b87e6d5b72mr25572101pzh.36.1674544356275; Mon, 23 Jan 2023 23:12:36 -0800 (PST) Received: from localhost.localdomain ([117.193.209.165]) by smtp.gmail.com with ESMTPSA id 7-20020a17090a174700b00219220edf0dsm736041pjm.48.2023.01.23.23.12.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jan 2023 23:12:35 -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: [PATCH v5 3/5] PCI: endpoint: Use a separate lock for protecting epc->pci_epf list Date: Tue, 24 Jan 2023 12:41:56 +0530 Message-Id: <20230124071158.5503-4-manivannan.sadhasivam@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230124071158.5503-1-manivannan.sadhasivam@linaro.org> References: <20230124071158.5503-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?1755887451852409983?= X-GMAIL-MSGID: =?utf-8?q?1755887451852409983?= 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;