Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp85159rwi; Wed, 2 Nov 2022 09:34:13 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6XndlFrtBKqsSEjTY+M2q/hkqGyvf62/0ipqFeiVHLXyTl1Ow88mZK6yGRsQRjnwmqpRob X-Received: by 2002:a17:907:8a23:b0:78d:a24e:a9aa with SMTP id sc35-20020a1709078a2300b0078da24ea9aamr25222963ejc.404.1667406853090; Wed, 02 Nov 2022 09:34:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1667406853; cv=pass; d=google.com; s=arc-20160816; b=w+NqVIYP5ZXLFf1OkE3ppZeaV/6FYevZ/5wghwc9G3C9XGjLkcu22jRidEZaGox1wJ 69MElmNeUzZg72/FPFE5wFihMtcYLt+GdkdwZ1hukYW5s+y0FUxAgI62cbxvmgOgosbx KjSb1whwW6Hdg/QQtN2PKG1KC35b08GpnFT3OC9bsfiSHunvRspuh/TJiOObmcJ0dGGZ YMrv/BT8ymki71elUPX4G+AUWYDq/6yU06bung8sbf67/8tlegkC4NtBEXk1anwk6TC7 qfIf5fzf9wGGFVyiL5pItNqLghaJu4Mzsh1Fa5hGItFTHYg2BZ6no/25KjovjBFrOrI3 qhmQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :message-id:date:subject:cc:to:from:dkim-signature; bh=/xQxRB+vXWAaN6lf7I9+zqAqmt3lidWLUa0xSH7X+hw=; b=hfgt+ZiH1vVHDReAwX3q4XKzhQ5pKqRgJyGz5cd3ljqn8BVinmxROpGwN38iijtcpM IjoNXtZH9i6RnytBB/X68ykxBEqb4X/lFBTYQbDVC1FOdcLJbE3VLIRxKs1F8CANjtJz SLZEuPSU36XU3IiCDHKlD9D5K0xudxEQVft5ncLfH1lp/JLrAav5kdxaMeAsK0shIhfF aWvNqVpv5FX/VEDH2vnzArtBfWxptxg2kYfYimn0zgw2DgmE3SknMtL5/h9DSJvHewdx YYsfMHFsHavl7r+DM+a+gG+/V8MEaXhCykfPU5zxsMIQ1hwYH6Jzv7hu/5+O3o42bn8i ntrQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=YWpC3q2U; arc=pass (i=1); 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=QUARANTINE dis=NONE) header.from=outlook.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l14-20020a056402254e00b00459060fce1bsi18302227edb.574.2022.11.02.09.33.43; Wed, 02 Nov 2022 09:34:13 -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=@outlook.com header.s=selector1 header.b=YWpC3q2U; arc=pass (i=1); 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=QUARANTINE dis=NONE) header.from=outlook.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230242AbiKBP7M (ORCPT + 99 others); Wed, 2 Nov 2022 11:59:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230193AbiKBP6u (ORCPT ); Wed, 2 Nov 2022 11:58:50 -0400 Received: from JPN01-TYC-obe.outbound.protection.outlook.com (mail-tycjpn01olkn2017.outbound.protection.outlook.com [40.92.99.17]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22E542BB0A; Wed, 2 Nov 2022 08:58:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=D8zx23gSFiKXhU9W7qBQf44Tj0qxEZXpqPzqHhky0l4wCoHzvH44jaUy67zRohyCV3LHUZvc3zxTnmQzzf77PJjPnpuSdSlD4W9j/8ivpQU7bKGYiVySh9ypB6+7ZTtJuSp35ngSnLLIQUOvpGiLh9z4JmVme3vsrEB7w4v7SnFBn8zdjDIMyb2uN56va8TKA/lGbnd8xbdQHVUMBec/kjAWDT8p1fNXUUjHCu6w7v4UFAfgVEHWXrmvXzowy4syMkzZZkCA7J+bTQOGcqtxD4qzDswTPmCVmrJGq5hNDY/KUqzA4JW2Rznor1vhIdmk/w3bOmRGn6uh3I9Ca9tNCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=/xQxRB+vXWAaN6lf7I9+zqAqmt3lidWLUa0xSH7X+hw=; b=kJIaBC99mT7RxyQu5XkVZoPPxjvkIikZMOkPCGsVf3Tstvm08VUGtgalp5WcNmXFdrSeP5i8EnhV1gQlR57+qdCCiDrcfV2n97qzvnu8VtLpLNauvGtqoFhIAqy2yrEUUDkGxB9apRO2Pc+qhxYlsXXQZe9Qb51cwd9G8JcB+YxLJEwiDIX3ttT0cjNXzZ1mRyFgjpU6GreKwSiQIstw1fjL7ZPppjsXjmlCNtbS3WIIMvye3Skckk108MTfi0+W6VtKgsDXr6JCqy3r9cu9F4vgODlqq3qB47indn2eNX/NqOYLtSfpKZ5mhin1DnJolBeqwmBpU7BYIYA0cohQoQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/xQxRB+vXWAaN6lf7I9+zqAqmt3lidWLUa0xSH7X+hw=; b=YWpC3q2UCJljzO5os7oRs2r1qTqlhvWxP9WKS/Icy5RVAL/T4cVID/nsnUdvD9jIxTlx66D4LOGMCKfGFt7rHMy4LKvJ1YVBPA8HieAJVq/Usuf3WrdSkZI4ElpxnnmQSyn5NXIty44Un3ihQev+El4NKh5O/fyzvrKNwu7irdyWTRzQnE8mAqmYZBFLdXOt/pC/wQdiss/pD78C2WjjA6bfWqjk51PTpViflsU/FlUCXxr3hd0TPZXHYqPAcQub0ATjm7YjwADjSdxsmU8+Wcvg/TsOFGOTiR4Kjg6UOPfc8V0jcvvSpkGYw0O9zCx6vJnokPReil2Jl0vOEu1ULQ== Received: from TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:152::9) by OS3P286MB2422.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:151::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5769.19; Wed, 2 Nov 2022 15:58:42 +0000 Received: from TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM ([fe80::c90e:cbf3:c23d:43a5]) by TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM ([fe80::c90e:cbf3:c23d:43a5%9]) with mapi id 15.20.5791.022; Wed, 2 Nov 2022 15:58:42 +0000 From: Dawei Li To: sumit.semwal@linaro.org, christian.koenig@amd.com Cc: 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, Dawei Li Subject: [PATCH v3] dma-buf: fix racing conflict of dma_heap_add() Date: Wed, 2 Nov 2022 23:58:23 +0800 Message-ID: X-Mailer: git-send-email 2.25.1 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-TMN: [Hb13McbHy8iya2slT4EkVubLNUyTNzO/] X-ClientProxiedBy: SGAP274CA0006.SGPP274.PROD.OUTLOOK.COM (2603:1096:4:b6::18) To TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:152::9) X-Microsoft-Original-Message-ID: <20221102155823.406044-1-set_pte_at@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYCP286MB2323:EE_|OS3P286MB2422:EE_ X-MS-Office365-Filtering-Correlation-Id: 52dd0f8a-61d7-4684-9574-08dabceb1d59 X-MS-Exchange-SLBlob-MailProps: ZLYX9kf+sFZrWcqlYuxsQ4SinJ0dkCiqXcsavXO7jaSL3FwrSbAMOCLBPddQdbgOgPvnX69oxlNgKJ13hRvJznrLWDHsVacx/W3igzW+0IarXyvR90JSYQOssUT4M9UQ9pn45aEYld6rL6Z7thebJwH4CTCPdVsI8sOSlhCLLx7EkaIJV1CVvk3qPXrGFGxqhyQH70hgYXtXNCAx/ZbqE5GzvwcM23aX2CfS34ueYVW06gNOy/1Xk5AqIc3hemdUfECHrkBoNspWEhHy6blrgUjbi/a5dJxqgIqGIjTRktm9zTFXH13YIGDwLThb+rxl3jSzeeYOqJxZkp2wZ9X3tF5CKL1a4K792NeH0deN+nHJ+Vmc6ZFZmzb/ccXfZdTwEAjUKbBd9KW7MQx6apt2OKbaD4juwuREzkziQs2SWcr6heVQbMkrL2TbDAQpVyMyEE3JAPK3FqXjTTJkI9qWYdlVTBTTwl1iVSIcCr0eRCNOyt+GIYVr9n827wU2ZzbmpgDh8/rCTtSKTljLlSQTJ0YcO61dUo3OFoi1ZsX+1xppMIcvFqo+HceSmjRCVq9vaYd/DF0uUAhYSStRRNKfeB1Zaed/7iC7aNy6lCnfkQ+GMl8vXFcxlCxBNZW8XbIQpYh/vBN6Gn0+0gXyKyhSvSO6zyQr01qUar4rlh2bzz9AdPwaIwkuWEjZ373L7Dnn5bJbEnQvIl7QXI1JTy4I7j9L8DqoPYDgqUINEpSmCK1tQGfl2FUgwgbP+u8MHzj/BtV3uP/ggMeXHCNxyRnWLU0QQrb7RTT2guMROYrqzStA/zMSrFyZOA== X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9yBYn2OGZXdgfuBEJGo+7riIFDC5SgdaYSlouJwrH0jR9JzJADrhs9ziS3SSjIVaQsHL0bXHXBxRoNfluDm77FenEagX4z/uqrJ3pfuFD0Q+G1Q6bDOTwKutM5bNEknezWABkbKSuW+1hfXNErVCmv1KmjAgTQwzNSnlinusp88TcDcMiNKt3/Bau/04hdcPN31r5DbAEnoRdjhmQeu+wsgYTvTxH8FfPi7klhQ298uqxW+u6LYhbjqrgDDKRY9kt8QaRH+3mvRhYf2RGgm5xhBNVPE+GSSf5RXChKVb1Ev57RUhAbPNN/1XZRnjm3uLaL+IUwR8JPnNHtOoqKp3iIC1+txKP9cjvnRROtaECkpPxkGZnnCCaTmwYxfaTGkOhTv4t8CGlAH/TSlOCiwlI02u//jR9xcQUU7IdMJJzTSOrZI0O16YCiopHABEzH17rRwmhMXmtWuLVE5OE6B0z28qVFjtSMBXiWJktUO64MdJFCZWUNeILnnko7ZFf9IQ6UgxpENQFm4P8zNUgdOJr77H+W8d6jphoTX5fTsMTgcbF36CgKl9vg+UazqlfWPv21luXXYXoQqMZc21+rcNnqRKXx0EY6d3Vz7ZgbYfzlMxw4AOae6hBQdAjFGK5nI0l0mpAzeXXrClo2pePF8tMjGxYscrnjmSCf96y5pM0rG1ZE1f8YdV+bErkxOF+8cY X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?h48OKdY6rzvSrL0/xp2r3+NSu0oqJ1WsAou+sGycyCUnWj0VQ42s/Y1gGzRd?= =?us-ascii?Q?fx4KblE7IYeIyxh4w630lQqQf+0Xnm+0jdtJaE0c2pKo+VbcyR28iGyMS8v0?=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spo05o1ng2bB5kh3F5lUFU?= =?us-ascii?Q?1KlivwVX1ObOVLufGwKcIVWLOs18sRMx7D2S/MPq4ItzhvGwY/WQZ+u3cf7s?= =?us-ascii?Q?5p0Fr7opWXBYPDxSm2CXvMD3VSi3aqTWAM5SH+R0f2aCHY0n/3bxmF96+GuA?= =?us-ascii?Q?IuXYNyfFUp4eSjb5QR1Y/dMe/ZOzwpSbPfej/KURGStKVnUzRCuelRFKRH/O?= =?us-ascii?Q?5wZNsUUCSU4SHy4Z0XpToj6WYx10poST0xChP4VxIt/pIXWU9VyYqNfuCedU?= =?us-ascii?Q?FdBPByMplf8LyG/5vG4RVWgsT/dJYaA20Q0zAx8y3Q=3D=3D?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 52dd0f8a-61d7-4684-9574-08dabceb1d59 X-MS-Exchange-CrossTenant-AuthSource: TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Nov 2022 15:58:42.4905 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: OS3P286MB2422 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,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 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. Fixes: c02a81fba74f ("dma-buf: Add dma-buf heaps framework") base-commit: 447fb14bf07905b880c9ed1ea92c53d6dd0649d7 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; - +err3: + device_destroy(dma_heap_class, heap->heap_devt); err2: cdev_del(&heap->heap_cdev); err1: -- 2.25.1