From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.231.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796051311; b=nTGooYyZZY7huUiKHgM1Obt8iXPXtonYg4sDtrT4lSKdbwTNak/PFdkjLbwtFUMlAe5h NBZI/pmIbO0T5EKGHWIeMgeyeenHqdhDxC087vXB5T1LhmUkzimCdcAGtrNRD5EePhCBK FQHNHbDy/+V374l/3cGn9jFkR3jCPDNMtfO9ZqVXY8kNyc4alNBMbaEBoWzxcm3HDTk3T gq1TThslx0WoXzP/nXKpP1NuCeDQEWZFcAAHfEXXoiDTaZMP8YKH/pEUorGo7FsYjkXcY 1YVXR8e3wRWGhPwN/d/q8+AFQMyupYXbbBUEGV5LkjcJ/pB7v5XaF6QJbCO8+cGOOGw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796051311; h=DMARC-Filter:DKIM-Signature:Subject:Mime-Version:X-Original-From: In-Reply-To:X-Lms-Return-Path:To:Message-Id:Received: Content-Disposition:Content-Type:Cc:From:Date:References: Content-Transfer-Encoding; bh=cBzT46kfXjMhUAOs1JeGSkYsa5T/uxFq4zaoAlcVCBM=; b=tGPt866g7P12DhcLwbBaJNvhCmXA1eFyNfo34iUH7hLq1u19tgAXKoQLsdDfxbu91S1/ Iz3IsI6p2AwiRPp2Wb2DPpg3sY2cy3S+U1tWyyfH9z8k+AaLD9rhE+53cp95T0YPt1p3e 0VngZYtWhWiM/XZtxGtH9ijLiNhG0EG75sEaypBZIbjIywIjyY7eNM8mhXNY9U/UrdfCe ijAQbZ0ruWR9k6KLvtUpW8YPFrI7d11A4dbC6EQmicKbXB1upp2JFwNebL62b1Lf1WgkR VLg5kjp2Ukvg3i0QoeJgOR52sH/aXPsiPjiaA+8vV/PI9qwTjmPjHJxvlBeO8IhR/Ng== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=fygo.io; spf=pass smtp.mailfrom=fygo.io; dkim=pass header.d=fygo-io.20200929.dkim.larksuite.com header.i=@fygo-io.20200929.dkim.larksuite.com header.a=rsa-sha256 header.s=s1 header.b=RMtlZ5FX; arc=none smtp.remote-ip=209.127.231.56 Received: from va-2-56.ptr.blmpb.com (va-2-56.ptr.blmpb.com [209.127.231.56]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E4F3B606A325 for ; Mon, 30 Nov 2026 15:08:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E4F3B606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=fygo.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fygo.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=fygo-io.20200929.dkim.larksuite.com header.i=@fygo-io.20200929.dkim.larksuite.com header.a=rsa-sha256 header.s=s1 header.b=RMtlZ5FX DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=s1; d=fygo-io.20200929.dkim.larksuite.com; t=1796051303; h=from:subject:mime-version:from:date:message-id:subject:to:cc: reply-to:content-type:mime-version:in-reply-to:message-id; bh=cBzT46kfXjMhUAOs1JeGSkYsa5T/uxFq4zaoAlcVCBM=; b=RMtlZ5FX1vj1vUTXcqLynN555enoyrQa5jVHJ0NZKG6fwSDiiryy8Ah05aMsULiae+UsHH LPRsPBorBiuOZce/qacsilKK9XmYCe4S6n6fKZYh1VwIJOPLnVEmHa82nKRVGI6mjcgjGq YCWpAw/bdYUoyz6z2WAUX5heRcKaZ2IV4O6jcmysxF38sc3IAxlTfDsEjAOJHoXI8Ui6ik 7m3WMXqWDi4i06Vg0a0gtnPm04r282oKEc5Prm8wfjMeFmu36dbOqW+T4+7osR3u8aM5M8 XMkXh2V5fATR8t/oe1RHk/8gzM4ZES3KcCxTZPHr/3irhYxaXvVpWPCgFZB4oA== Subject: Re: [PATCH 4/4] bcache: Release the cache kobject when cache_alloc() fails X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Original-From: Coly Li In-Reply-To: <20261103094627.1289665-1-vulab@iscas.ac.cn> X-Lms-Return-Path: To: "Wentao Liang" Message-Id: Received: from studio.local ([120.245.64.92]) by smtp.larksuite.com with ESMTPS; Mon, 30 Nov 2026 15:08:22 +0000 Content-Disposition: inline Content-Type: text/plain; charset=UTF-8 Cc: "Kent Overstreet" , , , From: "Coly Li" Date: Mon, 30 Nov 2026 23:08:20 +0800 References: <20261103094627.1289665-1-vulab@iscas.ac.cn> Content-Transfer-Encoding: 7bit Hi Wentao, Thanks for the fix. IMHO the fix is valid, but still add some comments inline. On Tue, Nov 03, 2026 at 09:46:27AM +0800, Wentao Liang wrote: > cache_alloc() initializes ca->kobj with kobject_init(), which takes the Which function does the above 'which' mean? cache_alloc() or kobject_init()? If it is cache_alloc(), correct; if it is kboject_init() mistake. I suggest you the make it more explict and clear. > initial reference on the kobject, and takes a reference on the bcache > module. The object is released later by bch_cache_release(), which also > drops the block device, puts the super block folio, frees ca and drops > the module reference. I suggest you to list the exact object name explicitly. > When one of the allocations in cache_alloc() fails, the function frees > only the buffers it managed to allocate and returns an error. I also suggest you to list the exact names of the 'buffers' explicitly. > register_cache() then only calls fput() on the block device and returns, > so the reference taken by kobject_init() is never dropped: ca is leaked Again, you should explicitly point out the reference of ca->kobj. > together with the super block folio and the module reference. The > comment on that path still claims that ca->kobj is not initialized, but > kobject_init() is the first thing cache_alloc() does. Also should explain why "ca is leaked together with the super block folio and the module reference". Because when ca->kobj reference counter reaches 0 by the last kobject_put(&ca->kobj), then its .release callback bch_cache_release() is called. If kobject_put(&ca->kobj) is skipped, then bch_cache_release() will never be called, then "ca is leaked together with the super block folio and the module reference". BTW, if I remember correctly you are not the first one tried to fix this but you are the firstly correctly understand how bch_cache_release() works as a callback, and make me understand how to fix it. > > Put ca->kobj on that path so that bch_cache_release() runs and releases > everything. bch_cache_release() frees ca->buckets and ca->prio_buckets > as well, so clear those pointers in cache_alloc() after freeing them, and > drop the module_put() from the error path because the module reference is > now released by bch_cache_release(). Skip the bucket page free in > bch_cache_release() when cache_alloc() failed before allocating them. > Again, I expect explict description how you fix the issue, not the general words like "Put ca->kobj on that path". Normally the implicit and general descriptions are LLM styles and should not be welcomed in kernel bug fix commit log. > Fixes: cafe56359144 ("bcache: A block layer cache") > Cc: stable@vger.kernel.org > Assisted-by: DeepSeek-V4.1-Flash and OpenAI Codex > Signed-off-by: Wentao Liang > --- > drivers/md/bcache/super.c | 22 ++++++++++++++-------- > 1 file changed, 14 insertions(+), 8 deletions(-) > > diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c > index 97d9adb0bf96..5d4975873c2e 100644 > --- a/drivers/md/bcache/super.c > +++ b/drivers/md/bcache/super.c > @@ -2221,7 +2221,9 @@ void bch_cache_release(struct kobject *kobj) > ca->set->cache = NULL; > } > > - free_pages((unsigned long) ca->disk_buckets, ilog2(meta_bucket_pages(&ca->sb))); > + if (ca->disk_buckets) > + free_pages((unsigned long) ca->disk_buckets, > + ilog2(meta_bucket_pages(&ca->sb))); > kfree(ca->prio_buckets); > vfree(ca->buckets); For the above 2 lines, also kfree() and vfree() checks NULL inside, normally I would suggest to check the following checks, if (ca->prio_buckets) kfree(ca->prio_buckets); if (ca->buckets) vfree(ca->buckets); Then the extra function jumps to kfree() and vfree() can be avoided. > > @@ -2364,8 +2366,10 @@ static int cache_alloc(struct cache *ca) > > err_disk_buckets_alloc: > kfree(ca->prio_buckets); > + ca->prio_buckets = NULL; > err_prio_buckets_alloc: > vfree(ca->buckets); > + ca->buckets = NULL; > err_buckets_alloc: > free_heap(&ca->heap); > err_heap_alloc: > @@ -2380,7 +2384,10 @@ static int cache_alloc(struct cache *ca) > free_fifo(&ca->free[RESERVE_BTREE]); > err_btree_alloc: > err_free: > - module_put(THIS_MODULE); > + /* > + * The module reference taken above is dropped by > + * bch_cache_release() when the caller puts ca->kobj. > + */ The above code comments can be removed. > if (err) > pr_notice("error %pg: %s\n", ca->bdev, err); > return ret; > @@ -2408,13 +2415,12 @@ static int register_cache(struct cache_sb *sb, struct cache_sb_disk *sb_disk, > err = "cache_alloc(): unknown error"; > pr_notice("error %pg: %s\n", file_bdev(bdev_file), err); > /* > - * If we failed here, it means ca->kobj is not initialized yet, > - * kobject_put() won't be called and there is no chance to > - * call fput() to bdev in bch_cache_release(). So > - * we explicitly call fput() on the block device here. > + * cache_alloc() has already released the buffers it > + * allocated, so go through out: to drop the reference > + * taken by kobject_init(); bch_cache_release() then frees > + * ca and drops the block device and the super block folio. > */ I feel the above code comments is mistaken and misleading. Maybe you may consider something like this, /* * ca->kobj was initialized by kobject_init() in cache_alloc(), * so the initial reference o ca->kobj must be dropped when * cache_alloc() fails. goto out will drop the initial reference * by call kobject_put(&ca->kobj) and invoke ca->kobj release * callback fucntion bch_cache_release(). This callback will * free ca, release ca->bdev_file and ca->sb_disk. */ For your reference. > - fput(bdev_file); > - return ret; > + goto out; > } > > if (kobject_add(&ca->kobj, bdev_kobj(file_bdev(bdev_file)), "bcache")) { The patch does fix thing, just I want a few commit log, code and comments to be improved. Thanks. Coly Li