Received: by 2002:ac0:8845:0:0:0:0:0 with SMTP id g63csp452864img; Tue, 26 Feb 2019 03:04:38 -0800 (PST) X-Google-Smtp-Source: AHgI3IbgaiXHlSISjNwGa88AThWU8bwHgrsNa3r6o6jzY7lw+YhWfytkIeSt0L3fOS+eaaXxS0RH X-Received: by 2002:a17:902:87:: with SMTP id a7mr15661900pla.295.1551179078687; Tue, 26 Feb 2019 03:04:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551179078; cv=none; d=google.com; s=arc-20160816; b=bkdYjaYhnbevbrJwi70N1/4G0hJYimts/mBubB2Lfj/eg/zqBKxKxzIUPIIFKEF5d2 /keVuhkL/nI5lSg9/plNZvwViPds6LYyTOfhAKBVcdroFvTFRa/bfB7/0uJFXfpU8UUL nSz8LIFwZPT6KQlYKrKa5HCF250la0R6uPOhtKDjJC7PXYzwdjNDCZzxljgKvbyfV1st DxHXoBunY2p4OX0YgSz1klYXwck0PubQP3OroczPyXXZIcLdx9KRoakKHopOLW1TwFmq u7s7L5WrVcMzxpCL3Xj4PG7+huVN/C5tjzpAT45nBSbRNC0S5VyUDlA1N9WHDXUyNkMv Us5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:organization:openpgp:from:references:cc:to:subject; bh=7tnM8Jow+S0O96B7HHIFBO5Xgq17jdxdb01rdWSGgy4=; b=WJxfmqa8ML3GFSZD0gOprBX3KilgSB02HGRpgbBKDUm2w9nBHwl3BRnATvKP6YCNFk dnz4j8gqgiMLWVtl6ZouZ4T1GH8YNzkKNWpN79ESyRAoXe0iI5RY9pX9H22YpEAzV1kq PfXkt3rQwbe+ze06a3eIGbR+nJSM8gtPkp0j7Pq3YY2j1PymfM/mFWSfzQtFymGA0Td2 GNRRIf0snwovLefUaMZDrqURQwu3toQgXUTLpCToH+nW0YTMxluz/w/UjtPGYhlO/OlI EDqTGA/6MB9Qv0a1IaZ1lATNu4wQyQ+PdOT0lv4dqEH52VLeSzDmbEhkKAtnkLh9n89o xxLQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h8si10372053pgq.35.2019.02.26.03.04.21; Tue, 26 Feb 2019 03:04:38 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728066AbfBZLDt (ORCPT + 99 others); Tue, 26 Feb 2019 06:03:49 -0500 Received: from mx2.suse.de ([195.135.220.15]:50172 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725908AbfBZLDt (ORCPT ); Tue, 26 Feb 2019 06:03:49 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id EB833ACBD; Tue, 26 Feb 2019 11:03:46 +0000 (UTC) Subject: Re: [PATCH] bcache: use kmemdup_nul for CACHED_LABEL buffer To: Geliang Tang Cc: Kent Overstreet , linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org References: <04ff7c6d5cdd8b389d471989704c2f9dc3def554.1548840161.git.geliangtang@gmail.com> <04ebd070-3c9c-0cd4-f2d8-d7b078ea2582@suse.de> <20190226100102.GA10363@OptiPlex> From: Coly Li Openpgp: preference=signencrypt Organization: SUSE Labs Message-ID: <44f39202-6dca-eb25-f8ca-60ca000503f1@suse.de> Date: Tue, 26 Feb 2019 19:03:41 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 MIME-Version: 1.0 In-Reply-To: <20190226100102.GA10363@OptiPlex> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019/2/26 6:01 下午, Geliang Tang wrote: > On Wed, Feb 06, 2019 at 04:37:36PM +0800, Coly Li wrote: >> On 2019/1/30 5:29 下午, Geliang Tang wrote: >>> This patch uses kmemdup_nul to create a NUL-terminated string from >>> dc->sb.label. This is better than open coding it. >>> >>> With this, we can move env[2] initialization into env[] array to make >>> code more elegant. >>> >>> Signed-off-by: Geliang Tang >> >> Hi Geliang, >> >> In general I am OK with your idea. But I feel there might be some >> regression with your change. I comment your patch in line, correct me if >> I am wrong. >> >> >>> --- >>> drivers/md/bcache/super.c | 10 ++++------ >>> 1 file changed, 4 insertions(+), 6 deletions(-) >>> >>> diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c >>> index 4dee119c3664..84ab241c8516 100644 >>> --- a/drivers/md/bcache/super.c >>> +++ b/drivers/md/bcache/super.c >>> @@ -906,21 +906,18 @@ static int cached_dev_status_update(void *arg) >>> void bch_cached_dev_run(struct cached_dev *dc) >>> { >>> struct bcache_device *d = &dc->disk; >>> - char buf[SB_LABEL_SIZE + 1]; >>> + char *buf = kmemdup_nul(dc->sb.label, SB_LABEL_SIZE, GFP_KERNEL); >> >> If kdumdup_null() is failed, buf will be NULL. >> >>> char *env[] = { >>> "DRIVER=bcache", >>> kasprintf(GFP_KERNEL, "CACHED_UUID=%pU", dc->sb.uuid), >>> - NULL, >>> + kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf ? : ""), >> >> If buf is NULL, env[2] here is pointed to "" which is allocated in >> read-only data segment, and not a dynamic memory. > > Hi Coly, > > Sorry for my late reply. > > If buf is NULL, env[2] is kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", ""); Oh, I overlooked the location of last bracket. > In this case, env[2] is also a dynamic memory, a string like this, "CACHED_LABEL=". > So we can use kfree() to free it. There is no problem. > Sure, it is OK to me. I will add it into my for-test directory. Thanks. Coly Li > And here is a test case: > > $ cat test.c > #include > #include > #include > > static int __init test_init(void) > { > char *env = kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", ""); > pr_info("env = [%s]\n", env); > kfree(env); > return 0; > } > > static void __exit test_exit(void) > { > } > > module_init(test_init); > module_exit(test_exit); > > MODULE_LICENSE("GPL"); > > $ sudo insmod test.ko > $ dmesg > [ 3026.072298] env = [CACHED_LABEL=] > $ sudo rmmod test > > Thanks. > > -Geliang > >> >>> NULL, >>> }; >>> >>> - memcpy(buf, dc->sb.label, SB_LABEL_SIZE); >>> - buf[SB_LABEL_SIZE] = '\0'; >>> - env[2] = kasprintf(GFP_KERNEL, "CACHED_LABEL=%s", buf); >>> - >>> if (atomic_xchg(&dc->running, 1)) { >>> kfree(env[1]); >>> kfree(env[2]); >> >> Then kfree() here will try to release a read-only memory segment. I >> guess this is problematic. >> >>> + kfree(buf); >>> return; >>> } >>> >>> @@ -944,6 +941,7 @@ void bch_cached_dev_run(struct cached_dev *dc) >>> kobject_uevent_env(&disk_to_dev(d->disk)->kobj, KOBJ_CHANGE, env); >>> kfree(env[1]); >>> kfree(env[2]); >> >> Same problem might happen here for 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")) >>> >> >> >> -- >> >> Coly Li > -- Coly Li