Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp2186370rwi; Tue, 11 Oct 2022 06:03:29 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5KjZX1txLmMN+s3Lmi5bQb8XREUorrmJQdpiI2t0hiL7YuegfzKR8R/RqofI70T5DJu+hh X-Received: by 2002:a63:1349:0:b0:44b:2240:b311 with SMTP id 9-20020a631349000000b0044b2240b311mr20828315pgt.405.1665493409118; Tue, 11 Oct 2022 06:03:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665493409; cv=none; d=google.com; s=arc-20160816; b=iQI4nFtDU0ZLIXZreynDKNrDgZqaA0ruP4HBxciYZEfroZRYUSdtRD70ubP74FC+jH 3yjQD/VQJJkfO7esvv1bw5hLsToenxpu2oG5lchz3oFU7iFpJhD1BA2XpvLq9XRQB2S9 MNcmVCCzJI66DNP5GTc/LEuLq4x11BOuWrb2N+3ScuaKHXiVWWe1POLZaUZceQM5jdGZ 9Uk4QH0d5MNRlX4sgma4YjmvVoz1kDB0ZPUc0OnxzKkFb+NdJYaPHlMOEgEeZR+dcgyO nRNvP/zXc5zfPRHNy2dOY4HHS9H56nWxf5PgA8XTfKo+j1R21Ag/A6ZXB5oWV6oGYJAf owaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=tUmc3Z2gbFC5iJS3cRvN7b8XBwMTR68vu9QqBE9JhyU=; b=K1KytM2zhkK1gdAVVZPJ/KREpXPSb/pA8dvrF+7eaxk7I3bzBXPvCt7lLy69cjJglu uUuQ4c99i2S1j6TI0TRGkPpjgO0FzNpPY7yaS+KrnP1tGPFWp7PemiVdXztkDLJ8X9sd /bbdfPjxebkNrbS8eHOWEaK//Yqxq1REJgkCFHktaERArPAeFbiYaWorTTjAziiIuvIZ O6p9bp40VKFb5FBUEM/U4DOXyOgNvThtWh5gkZJkvsWkwklBiTWTHzmi81Tp+YxK4FxQ Uiz+bMMZ9bu7Jie749uHYAzXNIIh1PNeHBXWm3UvMBX5X9VNQYKn12pjBZYSfI3bVXzy As5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=wZB23Plw; 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=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j10-20020a056a00130a00b0055345e08029si15097450pfu.266.2022.10.11.06.03.14; Tue, 11 Oct 2022 06:03:29 -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=@ti.com header.s=ti-com-17Q1 header.b=wZB23Plw; 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=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229821AbiJKMkb (ORCPT + 99 others); Tue, 11 Oct 2022 08:40:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229703AbiJKMka (ORCPT ); Tue, 11 Oct 2022 08:40:30 -0400 Received: from fllv0015.ext.ti.com (fllv0015.ext.ti.com [198.47.19.141]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23E812661; Tue, 11 Oct 2022 05:40:29 -0700 (PDT) Received: from fllv0035.itg.ti.com ([10.64.41.0]) by fllv0015.ext.ti.com (8.15.2/8.15.2) with ESMTP id 29BCeCFA110994; Tue, 11 Oct 2022 07:40:12 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1665492012; bh=tUmc3Z2gbFC5iJS3cRvN7b8XBwMTR68vu9QqBE9JhyU=; h=Subject:To:CC:References:From:Date:In-Reply-To; b=wZB23PlwnzNSjFZw4oBh36lhRLWPg2FvHqBwbG3QYdQ74xJItLEns6ZdxmH1gHgXt xinUq3BFMS7RLL+vT9cDHNoI6v7lHsXEF7iKmceUH7W+RnuV9bKh4iMa6Bfcv6u35G yChn5xf4PIGJoEWn75G46tESzHr22yUk3YAx3MgY= Received: from DLEE115.ent.ti.com (dlee115.ent.ti.com [157.170.170.26]) by fllv0035.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 29BCeCKN120737 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 11 Oct 2022 07:40:12 -0500 Received: from DLEE114.ent.ti.com (157.170.170.25) by DLEE115.ent.ti.com (157.170.170.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.6; Tue, 11 Oct 2022 07:40:12 -0500 Received: from lelv0326.itg.ti.com (10.180.67.84) by DLEE114.ent.ti.com (157.170.170.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.6 via Frontend Transport; Tue, 11 Oct 2022 07:40:12 -0500 Received: from [172.24.147.145] (ileaxei01-snat2.itg.ti.com [10.180.69.6]) by lelv0326.itg.ti.com (8.15.2/8.15.2) with ESMTP id 29BCe89E026172; Tue, 11 Oct 2022 07:40:09 -0500 Subject: Re: [PATCH v3 3/5] PCI: endpoint: Use a separate lock for protecting epc->pci_epf list To: Manivannan Sadhasivam , , , CC: , , , , , References: <20221006134927.41437-1-manivannan.sadhasivam@linaro.org> <20221006134927.41437-4-manivannan.sadhasivam@linaro.org> From: Kishon Vijay Abraham I Message-ID: <5d041111-6ebc-4ac6-5693-443535545510@ti.com> Date: Tue, 11 Oct 2022 18:10:08 +0530 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <20221006134927.41437-4-manivannan.sadhasivam@linaro.org> Content-Type: text/plain; charset="utf-8"; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_MED,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 On 06/10/22 7:19 pm, Manivannan Sadhasivam wrote: > 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 Acked-by: Kishon Vijay Abraham I > --- > 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; >