From patchwork Wed Jul 3 16:27:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuqian Huang X-Patchwork-Id: 11031093 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E713A138B for ; Thu, 4 Jul 2019 07:26:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D1B9928A33 for ; Thu, 4 Jul 2019 07:26:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C5BF928A3F; Thu, 4 Jul 2019 07:26:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_SORBS_WEB autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 57EF028A33 for ; Thu, 4 Jul 2019 07:26:05 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D7E406E2A4; Thu, 4 Jul 2019 07:25:19 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0A57189815; Wed, 3 Jul 2019 16:27:27 +0000 (UTC) Received: by mail-pf1-x442.google.com with SMTP id 81so1512996pfy.13; Wed, 03 Jul 2019 09:27:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=UaCfWwscnCZ9X19G2iox4r2DvMJhQMjdmw+itwepCYk=; b=pN4nGl4EdztUmJTPL4JiyJSPXmGXyW0xFHfOn0tb7mjc23kxglPUy8ABN3W7MW7yzI 4WAqVHZTwyzLy4X6fuaV0X8XiY0p6RpP/rhJ4o99eWc2/BAFCwyW2skYinuoLwRI91ic KNOULRoev6arE/YWqXa5Qo3GT2YBFNZB1gSO92pJoLxaR/FUZwwW+cd9vJPoHymKeseF Fb1el4EbqmGGRfNrK+Bm/Rht7aJJvuhAJtRMwbTt4+2a6ExzI9pzjV9CPAxEIf6700rY mZwRkuzMUBGB7NfwEPY1TJGhBRzBQy9tXwUlazhLKP5iJV/KylIVceaWymATBKsbWb9v 98AA== X-Gm-Message-State: APjAAAW765ZyVPhgk0WnV+t93wFaZmHhClU1YWQmvMX/s7jpWcuMtZca TToPZ82lindyubOK8rSKI5Q= X-Google-Smtp-Source: APXvYqzkQ6bcPEfIil3HUDVMahzt6JJS97amraTDfjGTSEJJKqnMP0+6Q5Q+ENguuAWIV/uuzywivQ== X-Received: by 2002:a65:6656:: with SMTP id z22mr36489848pgv.197.1562171247573; Wed, 03 Jul 2019 09:27:27 -0700 (PDT) Received: from hfq-skylake.ipads-lab.se.sjtu.edu.cn ([202.120.40.82]) by smtp.googlemail.com with ESMTPSA id m31sm2914550pjb.6.2019.07.03.09.27.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 Jul 2019 09:27:27 -0700 (PDT) From: Fuqian Huang To: Subject: [PATCH v2 07/35] drm/amdgpu: Use kmemdup rather than duplicating its implementation Date: Thu, 4 Jul 2019 00:27:18 +0800 Message-Id: <20190703162718.32184-1-huangfq.daxian@gmail.com> X-Mailer: git-send-email 2.11.0 X-Mailman-Approved-At: Thu, 04 Jul 2019 07:25:13 +0000 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=UaCfWwscnCZ9X19G2iox4r2DvMJhQMjdmw+itwepCYk=; b=Kj4HgwcHyaagPTCY8GhUqTvrvQSy4Yd+XUxgRU9gQUNdmbYhG4XDtk8WUcRjmpRseD cHr9wS1oPEnjO/48xT1JxFaCEEsq0rz0E+rlHYKwklKLfz0kRbRKX3Fs8WJ5v5BoDvkK QOTQYF0wBSTLgBB2rIiCwmSpe4mdR5Jx83Ceb9cfnbxVjUMO1k6YIr587NYmWFnfnkvm 7Bnzi4Q69NLxvijF3X2ZFEtuLDkVPpHd4QbKF3/aZre9Q+vjFlPloeoo2TlcwX1eQHWp 5+maYUdfZuYjWCV7jkr+s4o+XsFf7BY0s9fP3RjOd78o6LuLHPiS5omMtK+jjVuvgr2M w0fQ== X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: David Airlie , linux-kernel@vger.kernel.org, amd-gfx@lists.freedesktop.org, Leo Li , dri-devel@lists.freedesktop.org, Fuqian Huang , Alex Deucher , =?utf-8?q?Christian_K=C3=B6nig?= MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP kmemdup is introduced to duplicate a region of memory in a neat way. Rather than kmalloc/kzalloc + memcpy, which the programmer needs to write the size twice (sometimes lead to mistakes), kmemdup improves readability, leads to smaller code and also reduce the chances of mistakes. Suggestion to use kmemdup rather than using kmalloc/kzalloc + memcpy. Reviewed-by: Emil Velikov Signed-off-by: Fuqian Huang --- Changes in v2: - Fix a typo in commit message (memset -> memcpy) drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c | 5 ++--- drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c | 5 ++--- drivers/gpu/drm/amd/display/dc/core/dc.c | 6 ++---- drivers/gpu/drm/amd/display/dc/core/dc_stream.c | 4 +--- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c index 02955e6e9dd9..48e38479d634 100644 --- a/drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c @@ -3925,11 +3925,10 @@ static int gfx_v8_0_init_save_restore_list(struct amdgpu_device *adev) int list_size; unsigned int *register_list_format = - kmalloc(adev->gfx.rlc.reg_list_format_size_bytes, GFP_KERNEL); + kmemdup(adev->gfx.rlc.register_list_format, + adev->gfx.rlc.reg_list_format_size_bytes, GFP_KERNEL); if (!register_list_format) return -ENOMEM; - memcpy(register_list_format, adev->gfx.rlc.register_list_format, - adev->gfx.rlc.reg_list_format_size_bytes); gfx_v8_0_parse_ind_reg_list(register_list_format, RLC_FormatDirectRegListLength, diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c index b610e3b30d95..09d901ef216d 100644 --- a/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c @@ -2092,11 +2092,10 @@ static int gfx_v9_1_init_rlc_save_restore_list(struct amdgpu_device *adev) u32 tmp = 0; u32 *register_list_format = - kmalloc(adev->gfx.rlc.reg_list_format_size_bytes, GFP_KERNEL); + kmemdup(adev->gfx.rlc.register_list_format, + adev->gfx.rlc.reg_list_format_size_bytes, GFP_KERNEL); if (!register_list_format) return -ENOMEM; - memcpy(register_list_format, adev->gfx.rlc.register_list_format, - adev->gfx.rlc.reg_list_format_size_bytes); /* setup unique_indirect_regs array and indirect_start_offsets array */ unique_indirect_reg_count = ARRAY_SIZE(unique_indirect_regs); diff --git a/drivers/gpu/drm/amd/display/dc/core/dc.c b/drivers/gpu/drm/amd/display/dc/core/dc.c index 18c775a950cc..6ced3b9cdce2 100644 --- a/drivers/gpu/drm/amd/display/dc/core/dc.c +++ b/drivers/gpu/drm/amd/display/dc/core/dc.c @@ -1263,14 +1263,12 @@ struct dc_state *dc_create_state(struct dc *dc) struct dc_state *dc_copy_state(struct dc_state *src_ctx) { int i, j; - struct dc_state *new_ctx = kzalloc(sizeof(struct dc_state), - GFP_KERNEL); + struct dc_state *new_ctx = kmemdup(src_ctx, + sizeof(struct dc_state), GFP_KERNEL); if (!new_ctx) return NULL; - memcpy(new_ctx, src_ctx, sizeof(struct dc_state)); - for (i = 0; i < MAX_PIPES; i++) { struct pipe_ctx *cur_pipe = &new_ctx->res_ctx.pipe_ctx[i]; diff --git a/drivers/gpu/drm/amd/display/dc/core/dc_stream.c b/drivers/gpu/drm/amd/display/dc/core/dc_stream.c index 96e97d25d639..d4b563a2e220 100644 --- a/drivers/gpu/drm/amd/display/dc/core/dc_stream.c +++ b/drivers/gpu/drm/amd/display/dc/core/dc_stream.c @@ -167,12 +167,10 @@ struct dc_stream_state *dc_copy_stream(const struct dc_stream_state *stream) { struct dc_stream_state *new_stream; - new_stream = kzalloc(sizeof(struct dc_stream_state), GFP_KERNEL); + new_stream = kzalloc(stream, sizeof(struct dc_stream_state), GFP_KERNEL); if (!new_stream) return NULL; - memcpy(new_stream, stream, sizeof(struct dc_stream_state)); - if (new_stream->sink) dc_sink_retain(new_stream->sink);