From patchwork Tue Oct 25 14:50:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manivannan Sadhasivam X-Patchwork-Id: 13019404 X-Patchwork-Delegate: lorenzo.pieralisi@arm.com 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 D918CC04A95 for ; Tue, 25 Oct 2022 14:51:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232910AbiJYOvf (ORCPT ); Tue, 25 Oct 2022 10:51:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232876AbiJYOva (ORCPT ); Tue, 25 Oct 2022 10:51:30 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9255E1A20AA for ; Tue, 25 Oct 2022 07:51:29 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id 130so1681281pfu.8 for ; Tue, 25 Oct 2022 07:51:29 -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 :message-id:reply-to; bh=jlONzOG1Sfig+UyBXL2V5B2sybs7pn6sgdhK0vy4e48=; b=L37DM/hgXDmzR5hLLsgWEwzvZg7/FD4jPz3U3YlRCDu+vIAHyT8zpE9qbH/LBNhaY/ 2N6lR1HgTyDoWSxWAD/FQi81drHaZpmOSqjamnWyDrqcCAJdy0B5qmwfPlC7JEbd6Rbr fwMOLbaUnp0HEkhTDEVgP4muugSNAgVjuPymKFl1EEJTyFub36iSPFbBcmGWm9ZBbE8W UNxF+9KsobFWG125JxOehz588g45HreQ1+LaQ9ivOmheEhQtkwJMNcgc8qbsQAVycpm9 Ksb5NPlU0c7oi6PJI8MfUYnracno5TH+b9Dy5BI9bcHlUJnhI+x06XcNYdhgYBM+YQRh y48A== 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=jlONzOG1Sfig+UyBXL2V5B2sybs7pn6sgdhK0vy4e48=; b=6jeTtPRoFlL+qY8j+5yJO1LC+BNPxfAsen4MB1+lmn2qINxshHoqCF9MM/eu2ARsKT q5e4Vufw5jEftXJG/utyiPR9HbRzhN60Oyn3oAeroNYsXe2qj3LXetXu3xMZhNCbf8UU aehx15KI0dNDXVzujtjAjA3oXKS6BI4H55Uu6o5t9Y+8JuhK0SlIOUlXk+oU0uz7FJ4j VJtmqsEUaZkYs3fVFjc+l5AL2s8eM1thL2PQuMjQz0oPZTf7irE/wBnWZAbMucdH/Bh0 7+e1orJ0MCq8VHfMJ4XpNcLU1eQ83d2UUeerJD4VxZ6r26FwJ0xSjJFs+g9hFyhDoGU4 ZxBQ== X-Gm-Message-State: ACrzQf3oWG+5V0rvSE/gIGEKas+BlYWnE3lDJYADRgWCHAnnZhxCKsfZ Rl6Wu/XliTm9v30yCFh9wx+7 X-Google-Smtp-Source: AMsMyM4dWj73Tinf1Jcql1LP/j08/Q+rLk42PwNzX3jkdB7U4c8Ta/ElbCQ8PQkbgpZ1oWAAFjTsfg== X-Received: by 2002:a63:a13:0:b0:440:a593:b79f with SMTP id 19-20020a630a13000000b00440a593b79fmr32533408pgk.557.1666709489059; Tue, 25 Oct 2022 07:51:29 -0700 (PDT) Received: from localhost.localdomain ([117.193.208.236]) by smtp.gmail.com with ESMTPSA id n14-20020a170903110e00b00180cf894b67sm1318765plh.130.2022.10.25.07.51.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Oct 2022 07:51:28 -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 , Kishon Vijay Abraham I Subject: [PATCH v4 3/5] PCI: endpoint: Use a separate lock for protecting epc->pci_epf list Date: Tue, 25 Oct 2022 20:20:59 +0530 Message-Id: <20221025145101.116393-4-manivannan.sadhasivam@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221025145101.116393-1-manivannan.sadhasivam@linaro.org> References: <20221025145101.116393-1-manivannan.sadhasivam@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@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. 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 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;