From patchwork Sun Oct 30 11:37:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dawei Li X-Patchwork-Id: 620151 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 99479C38A02 for ; Sun, 30 Oct 2022 11:37:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229629AbiJ3LhV (ORCPT ); Sun, 30 Oct 2022 07:37:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229497AbiJ3LhU (ORCPT ); Sun, 30 Oct 2022 07:37:20 -0400 Received: from JPN01-OS0-obe.outbound.protection.outlook.com (mail-os0jpn01olkn2027.outbound.protection.outlook.com [40.92.98.27]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6576AE55; Sun, 30 Oct 2022 04:37:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CuTBaEmzOCxjLSJWXzLCCJkrgduIhexB/CjDrjGN4IcDzGO4EQsa6hPlzqXyKIfwDOB4EjY8ALWLvkdIksEzKEU1dEwNhl+mKvm/Oz3uOSbomC+EeXvdweGPmpWnyxOFbAFM25rpQwRnJUZ6yiXJNfg3obVNVX0ESbP/+3F0lssnC8gs8DtprlClQ2vx5ERiER2xWPY9S6fz/EB/4vtAJIm9LrGBcPurOX0+usKII0g4shsBjwj/fUtou9AcTPKTUR4vK/+DVAzzp0ZU/9Chr7Hnx289r9H4oIMYEVSPeyKZNx0tuJRAwMIvjW5khZCOytc7OufNr1DrrS/jic8o/w== 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=svsL4S9jfk3q5oc4G6+CXyigCp88sv9APcA7xWoQvzk=; b=fVIAlrOXPdSef1NDonDUPfCAVVmFTQAC7FeBD0abt2pGz29C6YOJbbuRFR727HaUUEsDrY7J9DJUjayc+dexDpvdhkr7rkOnWkICf+VUr8eUMkR1KOufXaR7UlTnhha3HfxZIiGffveoXcXIJIL9k6MEnDkYADVRma2M1YmIEx+9Y3Zhn65mTnrit3OVBci/xBCDSi9uHtdNv65OJYlpdGMnH+Q3+Fq1o7CWIevlKuU9cMy97U2wkIurcVDofX9MlS/rq5+Xh227BxE9cIGb+kVNjOmF7XFyEZ5vOadBzZxUGsk0CycAt6FtfiGAXWd1cilfqgym667tar9KR5hGxw== 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=svsL4S9jfk3q5oc4G6+CXyigCp88sv9APcA7xWoQvzk=; b=dD6Cda0KIGFIL+Rpuh60/dgKbd/WmEZ9DF2ULWpJGXK99mUl4HHGEnJKnScSalvcK2bpVXXw96HnePe0i7qIAmHYvg7ivGCAU2on91qH1kVbnBNd6ntIkwfabAKeCOLF4895avFat2jzXLihTiQqgOuHbn7bn6rlL67eTaVQ8lz15TL0oT/ZBsxATdl+c4JXQWL/o3Ln9XiHfHpYbXh2jCWd72XGzTXawGNHcIw/SBRkBDMFJimM2jrKsg7Z3JWQIOeJ/bx6kCRMU3RU9AGhaQfDY2nAYSC+FqF3dp5YOyaEaytDzHPDqC4st29IZPilKZYkjW33XmaqN532BXUZBQ== Received: from TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:152::9) by OS3P286MB2709.JPNP286.PROD.OUTLOOK.COM (2603:1096:604:1fc::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5769.16; Sun, 30 Oct 2022 11:37:12 +0000 Received: from TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM ([fe80::c90e:cbf3:c23d:43a5]) by TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM ([fe80::c90e:cbf3:c23d:43a5%7]) with mapi id 15.20.5769.018; Sun, 30 Oct 2022 11:37:12 +0000 From: Dawei Li To: sumit.semwal@linaro.org, christian.koenig@amd.com Cc: benjamin.gaignard@collabora.com, lmark@codeaurora.org, 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] dma-buf: fix racing conflict of dma_heap_add() Date: Sun, 30 Oct 2022 19:37:00 +0800 Message-ID: X-Mailer: git-send-email 2.25.1 X-TMN: [Um6xlQbCyYoT19WyxqearVE+xI9G5zJia2KmZRKIW1k=] X-ClientProxiedBy: TY1PR01CA0184.jpnprd01.prod.outlook.com (2603:1096:403::14) To TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:152::9) X-Microsoft-Original-Message-ID: <20221030113700.3300-1-set_pte_at@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYCP286MB2323:EE_|OS3P286MB2709:EE_ X-MS-Office365-Filtering-Correlation-Id: 4cdf1d64-4620-4054-bfbc-08daba6b15da X-MS-Exchange-SLBlob-MailProps: 70qbaZjg4mv1OqsmyvwVkO5pclI01FVpovJWLvgsAQ8XASbU4UOFaqFZL/PS2XbnOslcIt6Wz0uOSrOC9MGc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NHvb7SuV23PXUQduoHkrbpefxIC4OjrMEGdbYAjTHSGEe7kPT0HgJ1lv9Ay67mhUZPHU3oR6BPgyFoG6WJZENgqAEySlKIIBURtw4gtVMyOcbP3TF6R5Dm5EYWypwOdrK+N9Am0Kz/hXJepRVbFFxBrU7cnWBkXZ2grgxo3kYngX6QpIIVfJ48xloeBE+JuLOPE3xwCaRtepnyMIAhAXSgMjuZjUBnF4nYsOoahxl85YS2t5andR48zZrlRQRX9vSrF5K1RpQR8MPl6ivSS4DUg85IcG9RUiU77YvoxFKt1h0hyQG8fl6XfA5rWOtGNxSaynx+rNwmrsJE6igCwpWQ1Z4RgLvoEoL0S+MIDVKVa2O10uUaGWq4Cm1LMALyeVcJ0Ex7n9S2XXXLdis0NI48In9Dhb+qC+gS0/JOoz7WeRq56KLw0uI/TsXEuPtVtUlkT3ZFPYMIBqNwLA0q84nfjeME6v4d9DrfF8GTyWUuvQHtc/7G9Tk0iR0gMFN/SivhOHCsTQlKLtqGpPl0anElXMf+ZurA70b3sqwhXjs0LVB+1EX3lRnTuktl+UcbfH6YqawdQnV61zricy62yny3tSL+39j8A== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4cdf1d64-4620-4054-bfbc-08daba6b15da X-MS-Exchange-CrossTenant-AuthSource: TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Oct 2022 11:37:12.0082 (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: OS3P286MB2709 Precedence: bulk List-ID: X-Mailing-List: linux-media@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. Fixes: c02a81fba74f ("dma-buf: Add dma-buf heaps framework") base-commit: 447fb14bf07905b880c9ed1ea92c53d6dd0649d7 Signed-off-by: Dawei Li --- drivers/dma-buf/dma-heap.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index 8f5848aa144f..ff44c2777b04 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -243,11 +243,12 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) return ERR_PTR(-EINVAL); } } - mutex_unlock(&heap_list_lock); heap = kzalloc(sizeof(*heap), GFP_KERNEL); - if (!heap) + if (!heap) { + mutex_unlock(&heap_list_lock); return ERR_PTR(-ENOMEM); + } heap->name = exp_info->name; heap->ops = exp_info->ops; @@ -284,7 +285,6 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) goto err2; } /* Add heap to the list */ - mutex_lock(&heap_list_lock); list_add(&heap->list, &heap_list); mutex_unlock(&heap_list_lock); @@ -296,6 +296,7 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) xa_erase(&dma_heap_minors, minor); err0: kfree(heap); + mutex_unlock(&heap_list_lock); return err_ret; }