Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp175972rwi; Wed, 2 Nov 2022 10:35:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5P8amiHyiFdLlu3vKt0aYx70J7jNVBo3+sY99G1nGBzTMFUeg8W8fykcGt1XmgL3QrIZzp X-Received: by 2002:a17:90a:1b44:b0:213:1035:f913 with SMTP id q62-20020a17090a1b4400b002131035f913mr26261289pjq.133.1667410500044; Wed, 02 Nov 2022 10:35:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667410500; cv=none; d=google.com; s=arc-20160816; b=E26YdBJuAF+jcV82gmcEMK/vmNTZ+Lz/I0Shc4ouEJF+jYHDC+cuvAYNzXrx7TJjjB yRkemc4yDJfempPMfO0MA4iWjEYL9bEAW07LVdBKoYvpP1Yv2zIsA7J8Rk1JDDvHENj6 tTt+U6I08NDL/+0V0tOtfFVwJ7AXKWT5zYrUYtjNaaJ7FXjjPVG2CU1kDSLf77UaDkH/ MU5o4OepduL2QZlCMSXTZFW6HSoKTcySZQSmFJAc6WtWrRsyjewY8uMyM2nlR7Q3L2MU wPRWnnuILomlb5hiFvMnVfsHZ8C2e1jcflPHLeIVhEP/x85nw1kBbhXHPyinUIEpzJbe CosQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=9xXQl3ypMam93TQFZncK480oz+++LfFiwW9TqbQA7ow=; b=mbq4LPq9r5ijMAW8/nR5BlQ5hrWDEkFJ5adWPg6WToYlKDgFcBNRHkdI7LGfQm6b9O nwPlurlo/2edzNXwZjyjq1NDJdj7xAmfYnA8xbeYWChhqkApKpY3uGeNpazOOPVOVde3 odpWazODDhTVEleZcMA10HqS39OE0NaajTULF2i2GWuGmxE/LhWSSKTlo4IH5vqXlw4C kj/rKkn9LN1h97MS56pBJm7sJAPkNllCS6x41X9x/kBH8wS75gEj3UjXF9HQZi/gcsbi oS4NeHD1vi1F7IJeY1QQosG50leKu4MlD9x2+8imsfUWLk9uaacB5Ka21FH3kO9OH1nh 9LcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=WqN4PAWB; 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 l12-20020a17090270cc00b00187204b35besi10922428plt.203.2022.11.02.10.34.45; Wed, 02 Nov 2022 10:35:00 -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=WqN4PAWB; 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 S231420AbiKBROR (ORCPT + 98 others); Wed, 2 Nov 2022 13:14:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230500AbiKBRNz (ORCPT ); Wed, 2 Nov 2022 13:13:55 -0400 Received: from fllv0016.ext.ti.com (fllv0016.ext.ti.com [198.47.19.142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A0092D3; Wed, 2 Nov 2022 10:13:54 -0700 (PDT) Received: from lelv0266.itg.ti.com ([10.180.67.225]) by fllv0016.ext.ti.com (8.15.2/8.15.2) with ESMTP id 2A2HDeqq111380; Wed, 2 Nov 2022 12:13:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1667409220; bh=9xXQl3ypMam93TQFZncK480oz+++LfFiwW9TqbQA7ow=; h=Date:Subject:To:CC:References:From:In-Reply-To; b=WqN4PAWBfTgKqYPqWR9QfiQMUSEDkcgH7jMjSv6Cf7d6MMl030QQzz87vmOiCkhmW wch90C6cgTFlDbDD20D2mJ1zuddtVjIe7cBPvqx+dTZoly+wJdzW+xxvRFeLfdBPy4 azLS9WnhAuCZ1JiBBvlQf09MX1qlI/AwwaJw6Pgg= Received: from DLEE107.ent.ti.com (dlee107.ent.ti.com [157.170.170.37]) by lelv0266.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 2A2HDeBh090547 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 2 Nov 2022 12:13:40 -0500 Received: from DLEE109.ent.ti.com (157.170.170.41) by DLEE107.ent.ti.com (157.170.170.37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.6; Wed, 2 Nov 2022 12:13:40 -0500 Received: from lelv0326.itg.ti.com (10.180.67.84) by DLEE109.ent.ti.com (157.170.170.41) 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; Wed, 2 Nov 2022 12:13:40 -0500 Received: from [10.249.33.217] (ileaxei01-snat2.itg.ti.com [10.180.69.6]) by lelv0326.itg.ti.com (8.15.2/8.15.2) with ESMTP id 2A2HDd5N022592; Wed, 2 Nov 2022 12:13:39 -0500 Message-ID: Date: Wed, 2 Nov 2022 12:13:39 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: [PATCH v3] dma-buf: fix racing conflict of dma_heap_add() Content-Language: en-US To: Dawei Li , , CC: , , , , , , , , References: From: Andrew Davis In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 X-Spam-Status: No, score=-5.4 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 11/2/22 10:58 AM, Dawei Li wrote: > Racing conflict could be: > task A task B > list_for_each_entry > strcmp(h->name)) > list_for_each_entry > strcmp(h->name) > kzalloc kzalloc > ...... ..... > device_create device_create > list_add > list_add > > The root cause is that task B has no idea about the fact someone > else(A) has inserted heap with same name when it calls list_add, > so a potential collision occurs. > > v1: https://lore.kernel.org/all/TYCP286MB2323950197F60FC3473123B7CA349@TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM/ > > v1->v2: Narrow down locking scope, check the existence of heap before > insertion, as suggested by Andrew Davis. > > v2->v3: Remove double checking. The above version info should be in a cover letter or below the --- line so it doesn't end up in the commit message in tree. > > Fixes: c02a81fba74f ("dma-buf: Add dma-buf heaps framework") > > base-commit: 447fb14bf07905b880c9ed1ea92c53d6dd0649d7 > Same as above, plus this is an odd base, maybe just use "v6.1-rc2". > Signed-off-by: Dawei Li > --- > drivers/dma-buf/dma-heap.c | 29 +++++++++++++++-------------- > 1 file changed, 15 insertions(+), 14 deletions(-) > > diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c > index 8f5848aa144f..7a25e98259ea 100644 > --- a/drivers/dma-buf/dma-heap.c > +++ b/drivers/dma-buf/dma-heap.c > @@ -233,18 +233,6 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) > return ERR_PTR(-EINVAL); > } > > - /* check the name is unique */ > - mutex_lock(&heap_list_lock); > - list_for_each_entry(h, &heap_list, list) { > - if (!strcmp(h->name, exp_info->name)) { > - mutex_unlock(&heap_list_lock); > - pr_err("dma_heap: Already registered heap named %s\n", > - exp_info->name); > - return ERR_PTR(-EINVAL); > - } > - } > - mutex_unlock(&heap_list_lock); > - > heap = kzalloc(sizeof(*heap), GFP_KERNEL); > if (!heap) > return ERR_PTR(-ENOMEM); > @@ -283,13 +271,26 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) > err_ret = ERR_CAST(dev_ret); > goto err2; > } > - /* Add heap to the list */ > + > mutex_lock(&heap_list_lock); > + /* check the name is unique */ > + list_for_each_entry(h, &heap_list, list) { > + if (!strcmp(h->name, exp_info->name)) { > + mutex_unlock(&heap_list_lock); > + pr_err("dma_heap: Already registered heap named %s\n", > + exp_info->name); > + err_ret = ERR_PTR(-EINVAL); > + goto err3; > + } > + } > + > + /* Add heap to the list */ > list_add(&heap->list, &heap_list); > mutex_unlock(&heap_list_lock); > > return heap; > - Would like to keep this new line after the return statement. Andrew > +err3: > + device_destroy(dma_heap_class, heap->heap_devt); > err2: > cdev_del(&heap->heap_cdev); > err1: