Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp1939319pxb; Fri, 18 Feb 2022 20:27:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJwVBRXlWS5Zgteqtfv8yvrKTkVqSUeo6A1oe89dS9/j8X25G8FFwgLU3LX7boI0HVgfMKMc X-Received: by 2002:a17:906:fcbb:b0:6cd:f8fa:e356 with SMTP id qw27-20020a170906fcbb00b006cdf8fae356mr8625891ejb.436.1645244839288; Fri, 18 Feb 2022 20:27:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645244839; cv=none; d=google.com; s=arc-20160816; b=HfVsShq5gD5dWSam0jSQDNOn1Np87Vkd9fC53kx1XXNYfunx3OYhTBYkks+diKMue/ NsA7cIDT+Xa1nyRHHQmR+doMhZ09wNIdtpgLaYdsU+KebRKDDZJmWB+FGrMit5+Q0CSd z57XpjHbAgFohJm9NRTQ0AbuN/gbzmHZQWCgfDOcrQZTdbyv7XEJLdDZdBOh4NA0IRG/ vZKbiIZaFmYC9tf4rRgUJhCCWV8+QRrldcqW0MUJlu4HpZxD+fQ0PilNeS/teFlK0LH9 zsCBbxPu51NzJE5toOOnyQLN9g0ZnZJlFLFBJGHJCmVmEe/O7VsdG/T2LTtsrGkP4l82 pGLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=EELMh9yLzKq2YhWaWmX86e66jZD5FrMdWsd0v+UKIFQ=; b=wWc/aaAuj1Oya2dOt5HP8DYxylO19Ot4cY7F5K84hTtMTgeFf8k196LvdW3yOialRp JaAb7JOuqWYliqJgJB+T9sEYy3KHCyVZzCfiYWLzv2s8oTKLo0B26YhY23UoeIseIjKg xTJpNVrFg2gwfJBZCooaroq5maGFtcyFpdaSK/VLpq7lLw7su6oHdqlpFGTVdGUBLhbZ j4JdW3fFj1Mkxnhmn5/eC3MEaxJE9wYjEHFgrpHgbuNAQGB8bRvVyCP4oFPF/9e+gRcq zlnrIFMUBoUyDZpmkj6SlxyGuGe0Dj0FXQGPCnX0VDWGMAf7hHwTgtHQlx6D+3oL7Mjy Cf5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SUMjYzcT; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p23si8435489edw.108.2022.02.18.20.26.44; Fri, 18 Feb 2022 20:27:19 -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=@redhat.com header.s=mimecast20190719 header.b=SUMjYzcT; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241104AbiBSA6f (ORCPT + 99 others); Fri, 18 Feb 2022 19:58:35 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:39856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240988AbiBSA5n (ORCPT ); Fri, 18 Feb 2022 19:57:43 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id DFB1628BF5F for ; Fri, 18 Feb 2022 16:56:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645232218; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=EELMh9yLzKq2YhWaWmX86e66jZD5FrMdWsd0v+UKIFQ=; b=SUMjYzcTLrTPEZDwLtdtcE8hyPi4rD3LI5WECT0lQFZRIWcqUngGXlBRY/jzN39LJye3Ap 414ga08N6O17uAxkIIXslZ2QdhfxGf//Z4hIYCCbDEcIuQmucuoUg2D6WOqyeIJXz0mA3q clq+P0FbvQHvQccv6Xv1DNvDupZj6BY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-594-407jO7cuM6-rsOc9SkRHhg-1; Fri, 18 Feb 2022 19:56:55 -0500 X-MC-Unique: 407jO7cuM6-rsOc9SkRHhg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3DA1B1091DA1; Sat, 19 Feb 2022 00:56:52 +0000 (UTC) Received: from MiWiFi-R3L-srv.redhat.com (ovpn-12-39.pek2.redhat.com [10.72.12.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0C6CE62D60; Sat, 19 Feb 2022 00:56:37 +0000 (UTC) From: Baoquan He To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, akpm@linux-foundation.org, hch@lst.de, cl@linux.com, 42.hyeyoo@gmail.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, vbabka@suse.cz, David.Laight@ACULAB.COM, david@redhat.com, herbert@gondor.apana.org.au, davem@davemloft.net, linux-crypto@vger.kernel.org, steffen.klassert@secunet.com, netdev@vger.kernel.org, hca@linux.ibm.com, gor@linux.ibm.com, agordeev@linux.ibm.com, borntraeger@linux.ibm.com, svens@linux.ibm.com, linux-s390@vger.kernel.org, michael@walle.cc, linux-i2c@vger.kernel.org, wsa@kernel.org Subject: [PATCH 19/22] ethernet: rocker: Use dma_alloc_noncoherent() for dma buffer Date: Sat, 19 Feb 2022 08:52:18 +0800 Message-Id: <20220219005221.634-20-bhe@redhat.com> In-Reply-To: <20220219005221.634-1-bhe@redhat.com> References: <20220219005221.634-1-bhe@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Use dma_alloc_noncoherent() instead to get the DMA buffer. [ 42.hyeyoo@gmail.com: Use dma_alloc_noncoherent() instead of __get_free_pages. Fix memory leak. ] Signed-off-by: Baoquan He Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Jiri Pirko Cc: davem@davemloft.net Cc: Jakub Kicinski Cc: netdev@vger.kernel.org --- drivers/net/ethernet/rocker/rocker_main.c | 59 +++++++++-------------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/drivers/net/ethernet/rocker/rocker_main.c b/drivers/net/ethernet/rocker/rocker_main.c index 3fcea211716c..b23dd9b70d8d 100644 --- a/drivers/net/ethernet/rocker/rocker_main.c +++ b/drivers/net/ethernet/rocker/rocker_main.c @@ -193,20 +193,17 @@ static int rocker_dma_test_offset(const struct rocker *rocker, int i; int err; - alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset, - GFP_KERNEL | GFP_DMA); + alloc = dma_alloc_noncoherent(&pdev->dev, + ROCKER_TEST_DMA_BUF_SIZE * 2 + offset, + &dma_handle, + DMA_BIDIRECTIONAL, + GFP_KERNEL); if (!alloc) return -ENOMEM; + buf = alloc + offset; expect = buf + ROCKER_TEST_DMA_BUF_SIZE; - dma_handle = dma_map_single(&pdev->dev, buf, ROCKER_TEST_DMA_BUF_SIZE, - DMA_BIDIRECTIONAL); - if (dma_mapping_error(&pdev->dev, dma_handle)) { - err = -EIO; - goto free_alloc; - } - rocker_write64(rocker, TEST_DMA_ADDR, dma_handle); rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE); @@ -215,14 +212,14 @@ static int rocker_dma_test_offset(const struct rocker *rocker, dma_handle, buf, expect, ROCKER_TEST_DMA_BUF_SIZE); if (err) - goto unmap; + goto free; memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE); err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR, dma_handle, buf, expect, ROCKER_TEST_DMA_BUF_SIZE); if (err) - goto unmap; + goto free; prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE); for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++) @@ -231,14 +228,11 @@ static int rocker_dma_test_offset(const struct rocker *rocker, dma_handle, buf, expect, ROCKER_TEST_DMA_BUF_SIZE); if (err) - goto unmap; - -unmap: - dma_unmap_single(&pdev->dev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE, - DMA_BIDIRECTIONAL); -free_alloc: - kfree(alloc); + goto free; +free: + dma_free_noncoherent(&pdev->dev, ROCKER_TEST_DMA_BUF_SIZE * 2 + offset, + alloc, dma_handle, DMA_BIDIRECTIONAL); return err; } @@ -500,20 +494,13 @@ static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker, dma_addr_t dma_handle; char *buf; - buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA); + buf = dma_alloc_noncoherent(&pdev->dev, buf_size, + &dma_handle, direction, GFP_KERNEL); if (!buf) { err = -ENOMEM; goto rollback; } - dma_handle = dma_map_single(&pdev->dev, buf, buf_size, - direction); - if (dma_mapping_error(&pdev->dev, dma_handle)) { - kfree(buf); - err = -EIO; - goto rollback; - } - desc_info->data = buf; desc_info->data_size = buf_size; dma_unmap_addr_set(desc_info, mapaddr, dma_handle); @@ -526,11 +513,10 @@ static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker, rollback: for (i--; i >= 0; i--) { const struct rocker_desc_info *desc_info = &info->desc_info[i]; - - dma_unmap_single(&pdev->dev, - dma_unmap_addr(desc_info, mapaddr), - desc_info->data_size, direction); - kfree(desc_info->data); + dma_free_noncoherent(&pdev->dev, desc_info->data_size, + desc_info->data, + dma_unmap_addr(desc_info, mapaddr), + direction); } return err; } @@ -548,10 +534,11 @@ static void rocker_dma_ring_bufs_free(const struct rocker *rocker, desc->buf_addr = 0; desc->buf_size = 0; - dma_unmap_single(&pdev->dev, - dma_unmap_addr(desc_info, mapaddr), - desc_info->data_size, direction); - kfree(desc_info->data); + dma_free_noncoherent(&pdev->dev, + desc_info->data_size, + desc_info->data, + dma_unmap_addr(desc_info, mapaddr), + direction); } } -- 2.17.2