From patchwork Tue Oct 25 09:02:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tudor Ambarus X-Patchwork-Id: 13018851 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C7F7DFA373E for ; Tue, 25 Oct 2022 09:19:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=5mYCvq/ScnyaeyUL9MwToKz/KSb7ahJMrMaIpNnq6+M=; b=y3jkSqij5ZVQ39 GMIpEJ+G4Hfu34DZfmAkM8RExrMKJavhIsjxy44OaEJviAjhjO5UkUmVwqqUYN8WgKy+P3C2q5Rew wGYosjn2Sh0kACKNG8bsZSFD+Zyj1Y0e4N07DzGPowSvRQgXPvg+/94S9nYccAC2ap3FvBTfJogRw DUsqTOJlhJVbIbw9NJevsky6XBEQZN8fKqLvK2k97vtP6muABbLqewC4nizXeiusQ9x6NE5EguUoA dinsLb79kl8W8oJw1w9iZnJofnwzolujaUzHUpyIbIv92+AjN3P78eBtIvb5DCWUIjfmj1ioBuZc7 iogUw/+eUzSb2REKpIDg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1onG4u-004W4J-M2; Tue, 25 Oct 2022 09:18:32 +0000 Received: from esa.microchip.iphmx.com ([68.232.154.123]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1onFrB-004Pcq-KW for linux-arm-kernel@lists.infradead.org; Tue, 25 Oct 2022 09:04:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1666688661; x=1698224661; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=z9hQ1oZ5mjOGtj70JKEN2QunnSSqI2AP8MBA7pO0V/8=; b=FS8xAaJCKOoYv9qaYWyQkWqYiozJpUTBCbajHMgyGL5R5wdj/BAA4bmN 4IeYv8VyTriZ7M7pgYlpgTWCkFv5Stx75ySQoBXChhhbyA4zssVuBwfmK +XcH/uBsDBH30h2NFCm5JYiC/Xp7YN0LdgP1tn+mfW761ClAZv2Gm02B6 Oa74XDXpFCme89OIKEKjegqGfo1II2tiFpWFdMvJQ8CUDEk6YIDrarCPR X7cTfGWilOPSU5jVrZZpkhzhw+1d5A1/PgjwWGzgXckahK3gIagS2La8K IPCLfNAd1fhVOelEjfxWZW8YghckGgu9uiHNaeceSnagSKf1Ofb/j3Ye6 A==; X-IronPort-AV: E=Sophos;i="5.95,211,1661842800"; d="scan'208";a="120221371" Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa6.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 25 Oct 2022 02:04:19 -0700 Received: from chn-vm-ex03.mchp-main.com (10.10.85.151) by chn-vm-ex01.mchp-main.com (10.10.85.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.12; Tue, 25 Oct 2022 02:04:18 -0700 Received: from ROB-ULT-M18064N.mchp-main.com (10.10.115.15) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server id 15.1.2507.12 via Frontend Transport; Tue, 25 Oct 2022 02:04:15 -0700 From: Tudor Ambarus To: , , Subject: [PATCH v2 22/32] dmaengine: at_hdmac: Use devm_kzalloc() and struct_size() Date: Tue, 25 Oct 2022 12:02:56 +0300 Message-ID: <20221025090306.297886-23-tudor.ambarus@microchip.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221025090306.297886-1-tudor.ambarus@microchip.com> References: <20221025090306.297886-1-tudor.ambarus@microchip.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221025_020421_773511_EDB921DC X-CRM114-Status: GOOD ( 14.01 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tudor Ambarus , maciej.sosnowski@intel.com, linux-kernel@vger.kernel.org, mripard@kernel.org, linux-arm-kernel@lists.infradead.org, dmaengine@vger.kernel.org, torfl6749@gmail.com Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use the resource-managed kzalloc to simplify error logic. Memory allocated with this function is automatically freed on driver detach. Use struct_size() helper to calculate the size of the atdma structure with its trailing flexible array. While here, move the mem allocation higher in the probe method, as failing to allocate memory indicates a serious system issue, and everything else does not matter anyway. All these help the code look a bit cleaner. Signed-off-by: Tudor Ambarus --- drivers/dma/at_hdmac.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/drivers/dma/at_hdmac.c b/drivers/dma/at_hdmac.c index 6bd9e35db8f9..f3fbb0aa8b24 100644 --- a/drivers/dma/at_hdmac.c +++ b/drivers/dma/at_hdmac.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -1786,6 +1787,12 @@ static int __init at_dma_probe(struct platform_device *pdev) if (!plat_dat) return -ENODEV; + atdma = devm_kzalloc(&pdev->dev, + struct_size(atdma, chan, plat_dat->nr_channels), + GFP_KERNEL); + if (!atdma) + return -ENOMEM; + io = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!io) return -EINVAL; @@ -1794,21 +1801,13 @@ static int __init at_dma_probe(struct platform_device *pdev) if (irq < 0) return irq; - size = sizeof(struct at_dma); - size += plat_dat->nr_channels * sizeof(struct at_dma_chan); - atdma = kzalloc(size, GFP_KERNEL); - if (!atdma) - return -ENOMEM; - /* discover transaction capabilities */ atdma->dma_common.cap_mask = plat_dat->cap_mask; atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1; size = resource_size(io); - if (!request_mem_region(io->start, size, pdev->dev.driver->name)) { - err = -EBUSY; - goto err_kfree; - } + if (!request_mem_region(io->start, size, pdev->dev.driver->name)) + return -EBUSY; atdma->regs = ioremap(io->start, size); if (!atdma->regs) { @@ -1963,8 +1962,6 @@ static int __init at_dma_probe(struct platform_device *pdev) atdma->regs = NULL; err_release_r: release_mem_region(io->start, size); -err_kfree: - kfree(atdma); return err; } @@ -2003,8 +2000,6 @@ static int at_dma_remove(struct platform_device *pdev) io = platform_get_resource(pdev, IORESOURCE_MEM, 0); release_mem_region(io->start, resource_size(io)); - kfree(atdma); - return 0; }