Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp5659286rwb; Tue, 22 Nov 2022 03:13:43 -0800 (PST) X-Google-Smtp-Source: AA0mqf4ifb5t5APdWeE1lsoFmQa6zpUm2wBMFWuoLmq27pr8FKbiVnRSnn5I6syja5UJAXY+7bxg X-Received: by 2002:a17:906:d1c7:b0:7ad:fd3e:7762 with SMTP id bs7-20020a170906d1c700b007adfd3e7762mr4742899ejb.717.1669115623763; Tue, 22 Nov 2022 03:13:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669115623; cv=none; d=google.com; s=arc-20160816; b=kbPT6tJrVGf7TLRImhZVjKTv+Ul16RWy08CLdd8WEPrVCliNAPoCoNPG24OQznVzEY /kaAscGjVCIamC+UmqTqfV+n3/VaXnCHwtO501Mhz3ot16ZU/1cvBiCWiy1teSLaery+ T9m6DPkRhnHnUpYQolRnXpBZskMmxj3zvhXSa46bijWhaAtfopPu7iQ2by6y3qBRvoCx YiLGeFE5Iwxctp9Z/pVHcQQ30tNTNjObal2Gk9/r4mNF/IBtkfmdNMkHq5PcGQTmuYOE 0jE7uACB7m1Q8h3K0IZZbSnF7xWA1vMfBzio7UTM4FEyvrGnnRlGcqPbGxDZXN1FwTsK 8tCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=eAU3eLi14ypt2c7b62+uOHX4p+XY4P0OQzHd4hzoaDg=; b=R8nKrkLQ4zbWdkheGpYLD5xtim2QvjhYSZlZvnzRUR8H5OWtipJoFyo+1nT3pcw8et MsHdor5kvhW2duHlMmYPe3IY05bChDmODkNeOereM/mcaHs2xvVOWLmNkj7jl2h82ClZ 3PTnfbyz/AFmwU2i+e8M9BL7taevmVzPwnwDq37C/rQE4mTgcY/z35TY86B0BSxEzmm/ YYRWrMagrX3xDJIYqfcyTHhEcEWwSRMpCZth5TlxZ2nH7LWqfOhbrGMJKZ8wbYWxuF5+ ozO3CrSttoPpPdM4Hedy688wuMZ0N2GA31AlI3Z0eT4HT+TPcy57QlV0/SW8DgFb1BCu Ib6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=I8sHtJ1V; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l20-20020a056402255400b004617bf36c3csi12629602edb.308.2022.11.22.03.13.21; Tue, 22 Nov 2022 03:13:43 -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=I8sHtJ1V; 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 S232500AbiKVKT2 (ORCPT + 92 others); Tue, 22 Nov 2022 05:19:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233085AbiKVKTC (ORCPT ); Tue, 22 Nov 2022 05:19:02 -0500 Received: from mail-yb1-xb29.google.com (mail-yb1-xb29.google.com [IPv6:2607:f8b0:4864:20::b29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0863543847 for ; Tue, 22 Nov 2022 02:17:58 -0800 (PST) Received: by mail-yb1-xb29.google.com with SMTP id n189so4351062yba.8 for ; Tue, 22 Nov 2022 02:17:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=eAU3eLi14ypt2c7b62+uOHX4p+XY4P0OQzHd4hzoaDg=; b=I8sHtJ1VMGWbKADlghW+RXorMBoSoGjLES4pamIlTJ84wNBgwc8sYMtmY465DWhztX 8T6TnLw+/ULQud/T+NeuVkPEFI2IbCg9VAga/SULPq5Nq/ZrU1KWxBjhWgSV7d+TikqR vploUK08ERDl4Ku/AOx/5xoK2Qou94qqW0nOH63KbmfBRr5L4kAsvt1aWsX12n58DwZZ JNVKU17khlLyeudPe3qFAs5NIKyB4B3OV41S7mlmAziAJmVlcvJGVMv5AWfcgNwIM8Qo PcS4IxvqboaMcDSFM1z5mbr1Fcdsb4U2c985YnUeuWjIRWww8+Cs2B5likipDQqp4fag lDtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eAU3eLi14ypt2c7b62+uOHX4p+XY4P0OQzHd4hzoaDg=; b=j2L/aXWDOcr+wjt/8vmTsljEvAH9ZSTd9H2KlmYh4clnec8+q0eRZasoX/fcsAW/a4 zQ49w173txFrLiFYuKHHg48tTn6NCSwhDRAANlezAc5k4wPLb71ySzYnPtr38hYD4W92 M9kMti9CN5S1nFJr5vF7zpSrhPceKI69F71uS9Ayubwq8iAbBjJ8g63hlPtvS3ag60cU Fzfc1e8knwHrQtJiQLvs3Eip64BHv6eGejk4miyWXj0zPgsR87LheyMNlEFNUnjCrXq2 2Ywzgik0SLBVfhe6GfGtgTfaHN3cZeFWytV0qOCEwgUg4MD1/SLPGKwzjuUmph4FM/J1 wddA== X-Gm-Message-State: ANoB5pkz8X2KH1GYk8xuS6cfs8/72xGuOeiQTvaVDgzRmP104luqV22F sM4JSFIW9oirT5yneh97P9jznsjSK57DR0X0TJ2Mlw== X-Received: by 2002:a25:cc0a:0:b0:6e6:f85a:da48 with SMTP id l10-20020a25cc0a000000b006e6f85ada48mr19451585ybf.369.1669112277056; Tue, 22 Nov 2022 02:17:57 -0800 (PST) MIME-Version: 1.0 References: <56393e84-485b-42ba-5fce-d4a0d0017653@amd.com> In-Reply-To: <56393e84-485b-42ba-5fce-d4a0d0017653@amd.com> From: Sumit Semwal Date: Tue, 22 Nov 2022 15:47:42 +0530 Message-ID: Subject: Re: [PATCH v4] dma-buf: fix racing conflict of dma_heap_add() To: =?UTF-8?Q?Christian_K=C3=B6nig?= Cc: Dawei Li , benjamin.gaignard@collabora.com, labbott@redhat.com, Brian.Starkey@arm.com, jstultz@google.com, afd@ti.com, sspatil@android.com, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=unavailable 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 Hi Dawei Li, On Mon, 21 Nov 2022 at 23:53, Christian K=C3=B6nig wrote: > > Hi Dawei, > > from the technical description, coding style etc.. it looks clean to me, > but I'm the completely wrong person to ask for a background check. > > I have a high level understanding of how dma-heaps work, but not a > single line of this code is from me. > > Feel free to add my Acked-by, but Laura, John and others do you have any > opinion? > > Regards, > Christian. > > Am 21.11.22 um 16:28 schrieb Dawei Li: > > On Sat, Nov 05, 2022 at 12:05:36AM +0800, Dawei Li wrote: > > > > Hi Christian, > > May I have your opinion on this change? > > > > Thanks, > > Dawei > > > >> 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. > >> > >> Fixes: c02a81fba74f ("dma-buf: Add dma-buf heaps framework") > >> Signed-off-by: Dawei Li Looks good to me as well. I will apply it over on drm-misc. Best, Sumit. > >> --- > >> v1: https://nam11.safelinks.protection.outlook.com/?url=3Dhttps%3A%2F%= 2Flore.kernel.org%2Fall%2FTYCP286MB2323950197F60FC3473123B7CA349%40TYCP286M= B2323.JPNP286.PROD.OUTLOOK.COM%2F&data=3D05%7C01%7Cchristian.koenig%40a= md.com%7C1989f31257fc458a27c508dacbd5078e%7C3dd8961fe4884e608e11a82d994e183= d%7C0%7C0%7C638046413707443203%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAi= LCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=3D= OWPUPrIHGnzwXyQE4WlIthThwSuSK2y3Eq2Wq5zAzbo%3D&reserved=3D0 > >> v1->v2: Narrow down locking scope, check the existence of heap before > >> insertion, as suggested by Andrew Davis. > >> v2->v3: Remove double checking. > >> v3->v4: Minor coding style and patch formatting adjustment. > >> --- > >> drivers/dma-buf/dma-heap.c | 28 +++++++++++++++------------- > >> 1 file changed, 15 insertions(+), 13 deletions(-) > >> > >> diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c > >> index 8f5848aa144f..59d158873f4c 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_he= ap_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 =3D kzalloc(sizeof(*heap), GFP_KERNEL); > >> if (!heap) > >> return ERR_PTR(-ENOMEM); > >> @@ -283,13 +271,27 @@ struct dma_heap *dma_heap_add(const struct dma_h= eap_export_info *exp_info) > >> err_ret =3D 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 =3D ERR_PTR(-EINVAL); > >> + goto err3; > >> + } > >> + } > >> + > >> + /* Add heap to the list */ > >> list_add(&heap->list, &heap_list); > >> mutex_unlock(&heap_list_lock); > >> > >> return heap; > >> > >> +err3: > >> + device_destroy(dma_heap_class, heap->heap_devt); > >> err2: > >> cdev_del(&heap->heap_cdev); > >> err1: > >> -- > >> 2.25.1 > >> > --=20 Thanks and regards, Sumit Semwal (he / him) Tech Lead - LCG, Vertical Technologies Linaro.org =E2=94=82 Open source software for ARM SoCs