Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp183121pxb; Fri, 5 Mar 2021 18:54:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJzihGkpkarN0Zy8GTorlA46V5vfMzroiAgJeTah6gBBin54Th+OYSvRm077i3Z/BdnrwxUR X-Received: by 2002:a17:906:bccc:: with SMTP id lw12mr5105573ejb.268.1614999285461; Fri, 05 Mar 2021 18:54:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614999285; cv=none; d=google.com; s=arc-20160816; b=kRNXSv1MRELjnDyv8Vqc8zkjxxWiFI3Xf2Gukx4V0PjWsBP77Xo1qfxDGiYbToNA+C jfKX8D3hyy4uNtdo0UYaoq/amNsGBp+TsupsDd012lmduIvrWwv/bbjXJbzz+Y1Nl24Y m+XrUH18Rh0u0meiyojtMuEkKJb/G0EaLuO5PgPJBgu8zDcEzaiU8clKeunmTO4S2nSL E22lQo34W5V9Cw/prPItVoJGEbkZtTwHQs6BWp+Lv5Q1fY23YpyuXoEzTHCfFyFnXM1c p92tprIvDbj/+eAiHO7ESAtizBU+kxkRiqgZccdc4pELV071QeB+NipxhiFOJ2p7rppE j6TA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :mime-version:user-agent:date:message-id:subject:from:cc:to; bh=WfwVkuo6I9MLHLnrhqNN3Iab7bMDCQp3sIHAV00Botg=; b=gyciGEWwVRc+5esm76daG6dDZykz4CSpsHhm+6xYRlwbzjm47yBLzD1Zo2nMB4Pnir O4mhLrgPrQm9M1i8RRl+Q2zgPM1b3IrlckVMbEooq8WCZVhihrfxc757H3YID9JErsSM BtSBxGLzpH7yhnD8MXE6ei0rYdUiDFRt4HJyB/Pr0SKDk0oAxVv86yYE87RpUxqH/Ljf lSNgZ+izGs1H37yHrNDtXV/KG5mquNR+ZR71uX33jms6jlVTtz7s3wPL+yVsIQwssDu1 1gF7KL1iv6OPE+WKWDUXjixOzO53/hN0I/1QmQtVrGbKRzIDTELyA6vTamjMH7pyfK42 se4A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w24si2703391eds.559.2021.03.05.18.54.23; Fri, 05 Mar 2021 18:54:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229759AbhCFCod (ORCPT + 99 others); Fri, 5 Mar 2021 21:44:33 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:12701 "EHLO szxga04-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229616AbhCFCoM (ORCPT ); Fri, 5 Mar 2021 21:44:12 -0500 Received: from DGGEMS404-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4Dspll23CTzlSgl; Sat, 6 Mar 2021 10:41:55 +0800 (CST) Received: from [127.0.0.1] (10.174.176.117) by DGGEMS404-HUB.china.huawei.com (10.3.19.204) with Microsoft SMTP Server id 14.3.498.0; Sat, 6 Mar 2021 10:44:00 +0800 To: , , "linux-kernel@vger.kernel.org" CC: linfeilong , , lixiaokeng , lihaotian , From: Zhiqiang Liu Subject: [PATCH] bcache: reduce redundant code in bch_cached_dev_run() Message-ID: <84551e06-0a99-218f-a3b7-2694e1804d6e@huawei.com> Date: Sat, 6 Mar 2021 10:43:59 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.176.117] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In bch_cached_dev_run(), free(env[1])|free(env[2])|free(buf) show up three times. This patch introduce out tag in which free(env[1])|free(env[2])|free(buf) are only called one time. If we need to call free() when errors occur, we can set error code to ret, and then goto out tag directly. Signed-off-by: Zhiqiang Liu --- drivers/md/bcache/super.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c index 71691f32959b..1a8748050087 100644 --- a/drivers/md/bcache/super.c +++ b/drivers/md/bcache/super.c @@ -1052,6 +1052,7 @@ static int cached_dev_status_update(void *arg) int bch_cached_dev_run(struct cached_dev *dc) { + int ret = 0; struct bcache_device *d = &dc->disk; char *buf = kmemdup_nul(dc->sb.label, SB_LABEL_SIZE, GFP_KERNEL); char *env[] = { @@ -1064,19 +1065,15 @@ int bch_cached_dev_run(struct cached_dev *dc) if (dc->io_disable) { pr_err("I/O disabled on cached dev %s\n", dc->backing_dev_name); - kfree(env[1]); - kfree(env[2]); - kfree(buf); - return -EIO; + ret = -EIO; + goto out; } if (atomic_xchg(&dc->running, 1)) { - kfree(env[1]); - kfree(env[2]); - kfree(buf); pr_info("cached dev %s is running already\n", dc->backing_dev_name); - return -EBUSY; + ret = -EBUSY; + goto out; } if (!d->c && @@ -1097,15 +1094,13 @@ int bch_cached_dev_run(struct cached_dev *dc) * only class / kset properties are persistent */ kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env); - kfree(env[1]); - kfree(env[2]); - kfree(buf); if (sysfs_create_link(&d->kobj, &disk_to_dev(d->disk)->kobj, "dev") || sysfs_create_link(&disk_to_dev(d->disk)->kobj, &d->kobj, "bcache")) { pr_err("Couldn't create bcache dev <-> disk sysfs symlinks\n"); - return -ENOMEM; + ret = -ENOMEM; + goto out; } dc->status_update_thread = kthread_run(cached_dev_status_update, @@ -1114,7 +1109,11 @@ int bch_cached_dev_run(struct cached_dev *dc) pr_warn("failed to create bcache_status_update kthread, continue to run without monitoring backing device status\n"); } - return 0; +out: + kfree(env[1]); + kfree(env[2]); + kfree(buf); + return ret; } /* -- 2.19.1