From patchwork Wed Dec 13 16:04:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amelie Delaunay X-Patchwork-Id: 178145 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp7881308dys; Wed, 13 Dec 2023 08:05:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IGwpvi+wV2VRM1Fh9QUJ1XeX3CGvy1fQMcd1FmE3PMX5QE3R3rdEZPvNFF3z9tQW4adNplr X-Received: by 2002:a05:6a00:b51:b0:6ce:3f02:108c with SMTP id p17-20020a056a000b5100b006ce3f02108cmr4222551pfo.22.1702483531845; Wed, 13 Dec 2023 08:05:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702483531; cv=none; d=google.com; s=arc-20160816; b=r0GFpn7OdUNiitvoL2zvbYGUhCMoysJpIuHMTdg0Yn3VYXoj1YJYwrX0qmGSatp6yE eCeNsx5g5SNwFIZ2LzC+hxsVQ+pUHiW2K56TKjfwwOy8jJkjpEHvtOHUODpKgtYVYGKI eC1/9276StFt+lyDpdS97B+NwE20oyZwii7VjY8la2l7jlSXcT++fLSuC3ZlylF/tHQ2 eJiQ41yBQsta5Zc3BTUtGYkp2qrsLUip/MgbEqXOmZpciedAnkmOVYccPdO6XIxB9eGA ceggyPdqJnxMXU/reCz+tHUZwmGqZV7W/h4+8rZcZrsU6Uu879YBax1mYNFG+t3jKwzc wFEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=vP0CuvtnE+r/ntZNTtv47/xL+GFWmIspcly1Iqw0j54=; fh=Pds6JOqLhIxYGh60MD8BxQAK2lsDexWaUz5dqOS89hc=; b=bF2J8blfoQJ5GxPS01Lpck2pjxE+puW4zbl97VTx022rdUEAUiQmF7hQE1/PvOVPC8 c4hCtYzchUt1ef7gDnRPDXWQOwLcYNVOEtHnKoaZOywcMPScpjg7ZXrHcZbQd4jHN5Te Bh2ct+ByiSvetzrNXWdbsaqO0kHYbm39FK2qoTgVLa5MEcmhlKEfYwQo37pUC2kbEdIb +cpb2z6UM/rumLaeTpUQhXNcuFRneMql1dGDUB1Cacar0XMz77IBg9LZn9W0sxGqepZs zYRfxffJSg/a2fdxPq1pv03o1RiJaxhkUEcTRajILccSPlp9dEkzarD9D/qwdv1OhMfL 5psA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foss.st.com header.s=selector1 header.b=54WpcFRk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foss.st.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id d6-20020a056a0010c600b006ce826158f0si9526062pfu.111.2023.12.13.08.05.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 08:05:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@foss.st.com header.s=selector1 header.b=54WpcFRk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foss.st.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 485BB802B175; Wed, 13 Dec 2023 08:05:15 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233240AbjLMQFG (ORCPT + 99 others); Wed, 13 Dec 2023 11:05:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233262AbjLMQFE (ORCPT ); Wed, 13 Dec 2023 11:05:04 -0500 Received: from mx07-00178001.pphosted.com (mx08-00178001.pphosted.com [91.207.212.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66933AC; Wed, 13 Dec 2023 08:05:10 -0800 (PST) Received: from pps.filterd (m0046660.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3BDAeIhK020074; Wed, 13 Dec 2023 17:04:54 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h= from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding:content-type; s=selector1; bh=vP0Cuvt nE+r/ntZNTtv47/xL+GFWmIspcly1Iqw0j54=; b=54WpcFRkZY3+P0q8YGNKJ1L 3/vcUxpmMxvEPoMd4eASPOSK4gswANmkfFk33VIp8PBPN1flMJ8cdpNRDhpjtuKm MeXMSIwMUVzwo3n8p+c19CSPDDqMMr9N36CiyFD0chWuCK8opJx8aQvAemes3DvG Wms6WMBRrPT9EE5ENnb50SPvNhU5mfUKnkWkvEQ67Px/szRmrTJl0kYAzXh8R/ma zirqhNkc4ef3bniaAVjesfFFiLEWAiT3I5WwGQMcLdXj2Mf16RtD20L2WdtpRKak iQoNF0LJQEXSW/kE3pkNpkdW5KAicYTP2o2ZO6ZN8JAvRuqfGPUVs6QcLcLaGVQ= = Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3uyb2k1by1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 13 Dec 2023 17:04:54 +0100 (CET) Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 5BF0B100057; Wed, 13 Dec 2023 17:04:53 +0100 (CET) Received: from Webmail-eu.st.com (shfdag1node3.st.com [10.75.129.71]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 28F7A24B88D; Wed, 13 Dec 2023 17:04:53 +0100 (CET) Received: from localhost (10.252.16.151) by SHFDAG1NODE3.st.com (10.75.129.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Wed, 13 Dec 2023 17:04:52 +0100 From: Amelie Delaunay To: Vinod Koul , Dave Jiang CC: , Amelie Delaunay , , Subject: [PATCH] dmaengine: fix NULL pointer in channel unregistration function Date: Wed, 13 Dec 2023 17:04:52 +0100 Message-ID: <20231213160452.2598073-1-amelie.delaunay@foss.st.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Originating-IP: [10.252.16.151] X-ClientProxiedBy: EQNCAS1NODE3.st.com (10.75.129.80) To SHFDAG1NODE3.st.com (10.75.129.71) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-12-13_09,2023-12-13_01,2023-05-22_02 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 13 Dec 2023 08:05:15 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785183372176137069 X-GMAIL-MSGID: 1785183372176137069 __dma_async_device_channel_register() can fail. In case of failure, chan->local is freed (with free_percpu()), and chan->local is nullified. When dma_async_device_unregister() is called (because of managed API or intentionally by DMA controller driver), channels are unconditionally unregistered, leading to this NULL pointer: [ 1.318693] Unable to handle kernel NULL pointer dereference at virtual address 00000000000000d0 [...] [ 1.484499] Call trace: [ 1.486930] device_del+0x40/0x394 [ 1.490314] device_unregister+0x20/0x7c [ 1.494220] __dma_async_device_channel_unregister+0x68/0xc0 Look at dma_async_device_register() function error path, channel device unregistration is done only if chan->local is not NULL. Then add the same condition at the beginning of __dma_async_device_channel_unregister() function, to avoid NULL pointer issue whatever the API used to reach this function. Fixes: d2fb0a043838 ("dmaengine: break out channel registration") Signed-off-by: Amelie Delaunay Reviewed-by: Dave Jiang --- drivers/dma/dmaengine.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c index b7388ae62d7f..491b22240221 100644 --- a/drivers/dma/dmaengine.c +++ b/drivers/dma/dmaengine.c @@ -1103,6 +1103,9 @@ EXPORT_SYMBOL_GPL(dma_async_device_channel_register); static void __dma_async_device_channel_unregister(struct dma_device *device, struct dma_chan *chan) { + if (chan->local == NULL) + return; + WARN_ONCE(!device->device_release && chan->client_count, "%s called while %d clients hold a reference\n", __func__, chan->client_count);