From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6148F3E8C52 for ; Wed, 25 Feb 2026 17:11:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772039477; cv=none; b=paS7AAKZ4ydn3xIOHUlYC4pCLn4adlEAnEtuyRqVUejoUGSaeqJ6R8cWGK0r3gyBeXT2ENs6t5IgmpnXCZBlNF85c0hacPsiaID9ad2oI+kkXEzfpmit+NgUPsXW1HIzDCFAvKtWnHViVP0BXNvq/0nUqPgGMvtgm1HAHzafguo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772039477; c=relaxed/simple; bh=uPTqY9JgO/N3hTjOibB1QSaxUILQmEOeelzPayjTk/k=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Sp001OW2dJ3aY9xFwFe8NIjC0sTpOMJIyHSAhNH8mcdlW5Uzp6mdYyicKgWJOnyEoX98kCg54hFf7XzRj+Bd4U3lFCKUDHGjjWdQaSnoz/VPgvKoKJSpF8A8LWZtpPMggiCLoldLgKL9I1IitrZeM6lqoSJdTVtTHSC7HtyiP/o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LQr6CjY8; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LQr6CjY8" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-48371bb515eso87751455e9.1 for ; Wed, 25 Feb 2026 09:11:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772039473; x=1772644273; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=FIssFQ2vzW/kmoN9DVV+0qSa2AsjIM2tskzI7Qx/Me8=; b=LQr6CjY8TtDOQx+Vv1A1W28IXTraePHaxC8M6KKLC2Y2NFFORZ2UZcPvzcdpzBeXyD w4VKkLyHQ0PGc70TWaCEKbKJwFvxuP9XDad0FxLKqjcqHfOmcQ+UihE1pYpFgGiuWt13 fT/Qgl3lybJbsOKir8FkNcNGRuqtPIdBVLQgP4HGyLS4EgqciKiAP5XeW4cKreKNuzCY sawai24HqKBCZSoqPpVC+Wknfg3fwvV/JYWURcvWIqiprrecOZZ8wkCg4M0YX8X80cea gh+pKhoxL6nslmtmN9fd6GrNEuUiqIejXM8QYpFhrX8f+W2RCJ3tUYzkwc9KkM3BqO8G 0PZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772039473; x=1772644273; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=FIssFQ2vzW/kmoN9DVV+0qSa2AsjIM2tskzI7Qx/Me8=; b=PcIC+VVjDCyEDiKpi4LRiFeeML2HHtXdVsfrSgWqP7uenblb+VJkBK0VL4VJJMx5C9 hNYCZZBjL3CA2xYn/7jUWHB1AqlF6M3WjcyzsmJFiFw/Hku1Bc6vqILBQ4n3LSM1X6PD hlhr1U46K7M3YId6e1ckFXiE+z1UkRTft5J7HID8t03MXvTayutN5pCUMCNBqCNBUPW4 Ynhamo5Bvx6BlWTOdB+V3JxNCGrgTUfcSWp0dgQ0Q7buBvdEHOct8dNpj4KNTg1qQfDm lAjPlzA3KU4CkCLfbWNG9M5Ml/M+FvyZL+yh00yvLcpK3kppMrYpjnxnHJe9ollOkWvI n8gA== X-Forwarded-Encrypted: i=1; AJvYcCVWocmbh+6QImU2cTVe+uZ5eubxDx92soVP/2w/6SaBwz8QQRmp6UOzkLocIJRm3FljYYTZC45xOywjRCc=@vger.kernel.org X-Gm-Message-State: AOJu0Yzprn2LMm9KT+Tw2ruQH6zS3q/yE8TUWHonzvGB4u0jaczBqi8O ETf1DUAqVCd8vbr0SaOx1BkiWnkiuG9/8STgyh3Ws6ltKTAngWHaQalu X-Gm-Gg: ATEYQzyhGFUov/YqsQM+u/Dleyzj/kCSEnhIr2mXVhN086ShvnpuHuBzx+psv3IPkeM qSu5B7nrvAeQny9fE+g0sNJopUKNqBN5go2leIY3kNfh60KkrzjqsSkaie/3PZWS37PuQQJILwu gJCuwaG3nZe3SF5UhyNLFrmufPYN5hpjQwGZ1+AMi/k8C/Hx/s7eoAeIMNLN2l4Q5FdPzezcoBV AvHWTAGmxuETDK82smLwephcp2nItQCozhjezTc76ZcFcmK/CPIigkItoixP+AEILVdUpSaKiEH qE7nb2TQ4os3Cxco6BAI3V9cxutUyOYx/fyDwFMltR8LV96aBdfVKEjwvVT5sU+Qh59fWiOSRY2 Hjl42cfZw+Tt/4cmEno4AopSsDKy8cixB6tbQofFS5FUApCMnuD3d+pkq8BZTa32Z5oBleLlezn aforDMvEjREujTwxRP7e2IekM3c5WGBHA3TO0s4nbt/hsPbY8nqEk287DjCvCdC7jH X-Received: by 2002:a05:600c:450c:b0:47d:73a4:45a7 with SMTP id 5b1f17b1804b1-483a96377b1mr274706305e9.24.1772039472364; Wed, 25 Feb 2026 09:11:12 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-483bd68826asm128499385e9.0.2026.02.25.09.11.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Feb 2026 09:11:11 -0800 (PST) Date: Wed, 25 Feb 2026 17:11:10 +0000 From: David Laight To: David Sterba Cc: Qu Wenruo , Miquel =?UTF-8?B?U2FiYXTDqSBTb2w=?= =?UTF-8?B?w6A=?= , dsterba@suse.com, clm@fb.com, naohiro.aota@wdc.com, linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, kees@kernel.org Subject: Re: [PATCH] btrfs: replace kcalloc() calls to kzalloc_objs() Message-ID: <20260225171110.171fad8d@pumpkin> In-Reply-To: <20260225144446.GE26902@twin.jikos.cz> References: <20260223234451.277369-1-mssola@mssola.com> <69c16813-5ac1-4756-ad42-41b4275e6aee@gmx.com> <20260224145555.44a8096d@pumpkin> <20260225144446.GE26902@twin.jikos.cz> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Wed, 25 Feb 2026 15:44:46 +0100 David Sterba wrote: > On Tue, Feb 24, 2026 at 02:55:55PM +0000, David Laight wrote: > > On Tue, 24 Feb 2026 15:07:10 +1030 > > Qu Wenruo wrote: > > =20 > > > =E5=9C=A8 2026/2/24 10:14, Miquel Sabat=C3=A9 Sol=C3=A0 =E5=86=99=E9= =81=93: =20 > > > > Commit 2932ba8d9c99 ("slab: Introduce kmalloc_obj() and family") > > > > introduced, among many others, the kzalloc_objs() helper, which has= some > > > > benefits over kcalloc(). > > > >=20 > > > > Cc: Kees Cook > > > > Signed-off-by: Miquel Sabat=C3=A9 Sol=C3=A0 > > > > --- > > > > fs/btrfs/block-group.c | 2 +- > > > > fs/btrfs/raid56.c | 8 ++++---- > > > > fs/btrfs/tests/zoned-tests.c | 2 +- > > > > fs/btrfs/volumes.c | 6 ++---- > > > > fs/btrfs/zoned.c | 5 ++--- > > > > 5 files changed, 10 insertions(+), 13 deletions(-) > > > >=20 > > > > diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c > > > > index 37bea850b3f0..8d85b4707690 100644 > > > > --- a/fs/btrfs/block-group.c > > > > +++ b/fs/btrfs/block-group.c > > > > @@ -2239,7 +2239,7 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs= _info, u64 chunk_start, > > > > if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) > > > > io_stripe_size =3D btrfs_stripe_nr_to_offset(nr_data_stripes(ma= p)); > > > > =20 > > > > - buf =3D kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); > > > > + buf =3D kzalloc_objs(*buf, map->num_stripes, GFP_NOFS); =20 > > >=20 > > > Not sure if we should use *buf for the type. > > >=20 > > > I still remember we had some bugs related to incorrect type usage. =20 > >=20 > > The global change really ought to have used u64 to add the type-check. > > Otherwise it will have added 'very hard to find' bugs in the very code > > it is trying to make better. > >=20 > > Using *buf for the type might be a reasonable pattern for new code. =20 >=20 > I find this a bit contradictory: I agree that using *buf as the argument > can cause bugs hard to find, yet the next sentence recommends to use it. The issue is that mechanically changing: buf =3D kzalloc(sizeof(type),...); to: buf =3D kzalloc_obj(*buf, ...); is that you've silently changed the size of the allocated memory if 'type' wasn't actually the correct type. Whereas changing it so: buf =3D kzalloc_obj(type, ...); will give a compiler error if/when the types don't match. (There may be places where this is exactly what is intended.) For a big mechanical change you really want to err on the side of caution. For new code it is a bit different. kzalloc_obj() will pick up silly mistakes, so both: auto buf =3D kzalloc_obj(type, ...); and: type *buf =3D kzalloc_obj(*buf, ...); are reasonable patterns. The former may actually read better as 'allocate an object of this type' and doesn't require that you replicate the type. David >=20 > This kzalloc_obj way is new I'm analyzing what would be a good pattern > and so far I don't like the "*buf" style of 1st argument. As the > function is really a macro it does not dereference it but it still > appears as it does. >=20 > Writing the type explicitly looks still more like a C to me. Types in > arguments are in helpers like container_of or rb_entry and it makes it > obvious that there's something special while for the kzalloc_obj I need > to remember it. >=20 > The whole thing would read better as "allocate object of type", so I'm > probably going to convert it to this pattern in btrfs code.