From nobody Sun Feb 8 17:42:40 2026 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8E8B30F808; Fri, 24 Oct 2025 10:22:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761301353; cv=none; b=t96nKTf5l3cfA4OTQFvB5331HjubkvB9nLDLCpr/GAU2Qdi3tA5SPKEKRGEadhKNYp5d2Vn8ayr8WLB2PJ/7DxnFIrC4LrqKu6LbOU7Yf9KLzHzaPV/zoLTx7ruatW+z3v9oLm+WeGVhgTnZb1Xm+MDndWQNUZ9rl1cfGTD72qE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761301353; c=relaxed/simple; bh=c71nAgtZEwYwr/Xb1hy8wiAuRLeKp2bV0BRppPPjFFo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AyAgrLjgplLzXq+BuREVhgYI1s7p05wYLkul0ceAmmNyne6q7xLL2PhD9UIJUE0xdsCNgeAStgh0Eb8tFeZ7z79ibZhfD1WR/hDNXsYhlfc6kScijZSy2z+ISRC+2DlpebY7EUMMyu2uj4OvjYmLVibtlC4KNcDl/J53NvdAnXw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=mssola.com; spf=fail smtp.mailfrom=mssola.com; dkim=pass (2048-bit key) header.d=mssola.com header.i=@mssola.com header.b=flhPnHJW; arc=none smtp.client-ip=80.241.56.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=mssola.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=mssola.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mssola.com header.i=@mssola.com header.b="flhPnHJW" Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4ctJqj6G9Hz9tlw; Fri, 24 Oct 2025 12:22:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mssola.com; s=MBO0001; t=1761301345; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DkO8Z9YMpnZxb4tpF0jgCNPp6scHTvIUwxqCdezCx+E=; b=flhPnHJWSYYi8sU5Nr1rQKl2Mb0oO5pyNAv+Q4zlacP4iYK+36BLwuaDXn+XgGBDjgcBsK gp4UppmNnFh9XXshK63taY8QKp90XnP9BcGA8q7ONTlL/4hEOsXX9UxEW0jrTxfqVKskte aH2+EIAE/RX3HhHFBjTGnRfhxJEurXzVKuxShX/1JMhv0EBHvQfBGbycX3iCBEWQS2qGQX caJjdggze97efmqiCiy44Tx6gYgd0lMgvjn/3TgTJ6y3JYWbg1XOflV3Gh5yVWo3wjqtKw J6D7/qZAZ6sawBeSWCfV3taxraEIiTpf4gaiF/0HgHHZzrtwx/ibQU/xdrbj6w== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=softfail (outgoing_mbo_mout: 2001:67c:2050:b231:465::102 is neither permitted nor denied by domain of mssola@mssola.com) smtp.mailfrom=mssola@mssola.com From: =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= To: linux-btrfs@vger.kernel.org Cc: clm@fb.com, dsterba@suse.com, johannes.thumshirn@wdc.com, fdmanana@suse.com, boris@bur.io, wqu@suse.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= Subject: [PATCH v2 1/4] btrfs: declare free_ipath() via DEFINE_FREE() Date: Fri, 24 Oct 2025 12:21:40 +0200 Message-ID: <20251024102143.236665-2-mssola@mssola.com> In-Reply-To: <20251024102143.236665-1-mssola@mssola.com> References: <20251024102143.236665-1-mssola@mssola.com> 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 X-Rspamd-Queue-Id: 4ctJqj6G9Hz9tlw The free_ipath() function was being used as a cleanup function everywhere. Declare it via DEFINE_FREE() so we can use this function with the __free() helper. The name has also been adjusted so it's closer to the type's name. Signed-off-by: Miquel Sabat=C3=A9 Sol=C3=A0 --- fs/btrfs/backref.c | 10 +--------- fs/btrfs/backref.h | 7 ++++++- fs/btrfs/inode.c | 4 +--- fs/btrfs/ioctl.c | 3 +-- fs/btrfs/scrub.c | 4 +--- 5 files changed, 10 insertions(+), 18 deletions(-) diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c index e050d0938dc4..eff2d388a706 100644 --- a/fs/btrfs/backref.c +++ b/fs/btrfs/backref.c @@ -2785,7 +2785,7 @@ struct btrfs_data_container *init_data_container(u32 = total_bytes) * allocates space to return multiple file system paths for an inode. * total_bytes to allocate are passed, note that space usable for actual p= ath * information will be total_bytes - sizeof(struct inode_fs_paths). - * the returned pointer must be freed with free_ipath() in the end. + * the returned pointer must be freed with __free_inode_fs_paths() in the = end. */ struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_r= oot, struct btrfs_path *path) @@ -2810,14 +2810,6 @@ struct inode_fs_paths *init_ipath(s32 total_bytes, s= truct btrfs_root *fs_root, return ifp; } =20 -void free_ipath(struct inode_fs_paths *ipath) -{ - if (!ipath) - return; - kvfree(ipath->fspath); - kfree(ipath); -} - struct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *= fs_info) { struct btrfs_backref_iter *ret; diff --git a/fs/btrfs/backref.h b/fs/btrfs/backref.h index 25d51c246070..1d009b0f4c69 100644 --- a/fs/btrfs/backref.h +++ b/fs/btrfs/backref.h @@ -241,7 +241,12 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, st= ruct btrfs_path *path, struct btrfs_data_container *init_data_container(u32 total_bytes); struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_r= oot, struct btrfs_path *path); -void free_ipath(struct inode_fs_paths *ipath); + +DEFINE_FREE(inode_fs_paths, struct inode_fs_paths *, + if (_T) { + kvfree(_T->fspath); + kfree(_T); + }) =20 int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, u64 start_off, struct btrfs_path *path, diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 79732756b87f..b9116a40ee9c 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -130,7 +130,7 @@ static int data_reloc_print_warning_inode(u64 inum, u64= offset, u64 num_bytes, struct btrfs_fs_info *fs_info =3D warn->fs_info; struct extent_buffer *eb; struct btrfs_inode_item *inode_item; - struct inode_fs_paths *ipath =3D NULL; + struct inode_fs_paths *ipath __free(inode_fs_paths) =3D NULL; struct btrfs_root *local_root; struct btrfs_key key; unsigned int nofs_flag; @@ -193,7 +193,6 @@ static int data_reloc_print_warning_inode(u64 inum, u64= offset, u64 num_bytes, } =20 btrfs_put_root(local_root); - free_ipath(ipath); return 0; =20 err: @@ -201,7 +200,6 @@ static int data_reloc_print_warning_inode(u64 inum, u64= offset, u64 num_bytes, "checksum error at logical %llu mirror %u root %llu inode %llu offset %llu= , path resolving failed with ret=3D%d", warn->logical, warn->mirror_num, root, inum, offset, ret); =20 - free_ipath(ipath); return ret; } =20 diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 692016b2b600..a65b67dab055 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -3298,7 +3298,7 @@ static long btrfs_ioctl_ino_to_path(struct btrfs_root= *root, void __user *arg) u64 rel_ptr; int size; struct btrfs_ioctl_ino_path_args *ipa =3D NULL; - struct inode_fs_paths *ipath =3D NULL; + struct inode_fs_paths *ipath __free(inode_fs_paths) =3D NULL; struct btrfs_path *path; =20 if (!capable(CAP_DAC_READ_SEARCH)) @@ -3346,7 +3346,6 @@ static long btrfs_ioctl_ino_to_path(struct btrfs_root= *root, void __user *arg) =20 out: btrfs_free_path(path); - free_ipath(ipath); kfree(ipa); =20 return ret; diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c index fe266785804e..317858844413 100644 --- a/fs/btrfs/scrub.c +++ b/fs/btrfs/scrub.c @@ -505,7 +505,7 @@ static int scrub_print_warning_inode(u64 inum, u64 offs= et, u64 num_bytes, struct btrfs_inode_item *inode_item; struct scrub_warning *swarn =3D warn_ctx; struct btrfs_fs_info *fs_info =3D swarn->dev->fs_info; - struct inode_fs_paths *ipath =3D NULL; + struct inode_fs_paths *ipath __free(inode_fs_paths) =3D NULL; struct btrfs_root *local_root; struct btrfs_key key; =20 @@ -569,7 +569,6 @@ static int scrub_print_warning_inode(u64 inum, u64 offs= et, u64 num_bytes, (char *)(unsigned long)ipath->fspath->val[i]); =20 btrfs_put_root(local_root); - free_ipath(ipath); return 0; =20 err: @@ -580,7 +579,6 @@ static int scrub_print_warning_inode(u64 inum, u64 offs= et, u64 num_bytes, swarn->physical, root, inum, offset, ret); =20 - free_ipath(ipath); return 0; } =20 --=20 2.51.1 From nobody Sun Feb 8 17:42:40 2026 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 646CD30F943; Fri, 24 Oct 2025 10:22:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761301354; cv=none; b=GYvRiMPIOmUESWLvykS8yppC16OUQa2U/HZWld4VmolpDS81CbFWX3M4OAN061+HRYCtPAaY0sdEHqLuZO0bcMB3/ypi90/07hkJhK+ukA4htWEnrzl++dmvCOse0d/361u8udfGGICV7plLsjWu1EeDOYfoig9Eveg+rExDGhE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761301354; c=relaxed/simple; bh=AYexWKgjlC/6/XZ+rMCz8Yfsdbd8Oc2v0vGOxZNjBXM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IAt4PGHqrpfjrZmjgRPVbSUAFOCRA8/BT1SZOAOPjHcG98GTQJqz87yG/U3k1AiAnb5m1ulNrbxxazZoT95SlCK6ArYrPGcVkyzy2QD53cppugpxrygfirjIOjj4liOpT8V3dvcRgTmP+ktfKfCCBTVp/4xJBH4GvExwY9VGJHQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=mssola.com; spf=fail smtp.mailfrom=mssola.com; dkim=pass (2048-bit key) header.d=mssola.com header.i=@mssola.com header.b=DevviGX8; arc=none smtp.client-ip=80.241.56.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=mssola.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=mssola.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mssola.com header.i=@mssola.com header.b="DevviGX8" Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4ctJqm5MMjz9v3b; Fri, 24 Oct 2025 12:22:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mssola.com; s=MBO0001; t=1761301348; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NSAxISSN6q/2TqVC51fgp56Z4tqCi+4yr5cLFM6xPMo=; b=DevviGX8z6r1TIaKDwug0FvawULqpf6R+3l13Pu6aeSFdk0gOuN92LKf7m3ccWXgV3lEQb R4+FBtyecaiBtio3udOEO1WMwwQoZmO+6/eOC6qB6EneMzUXQ/jCZrLpSUNj/zvcYUAzIE pgcOkh0w51V6/IrWPYmgBfn7z1OaAq9BsKV/MXiZ0wfEbbjCQ/OrQ4v/9ztf7Id11JnX21 sBa92ScWd17KrB0hs+Oy1hFcbmC0A/XCbOiZ7GZsWqNv55+dfiJypDp9G4YCrAHVtf7R37 3YP7ey+n6JhWSNQcbbjRJRZ2zJiBeL2ZAWwLbP5EcP5MSgkCuevDbXGCyS4HfA== From: =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= To: linux-btrfs@vger.kernel.org Cc: clm@fb.com, dsterba@suse.com, johannes.thumshirn@wdc.com, fdmanana@suse.com, boris@bur.io, wqu@suse.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= , David Sterba Subject: [PATCH v2 2/4] btrfs: define the AUTO_K(V)FREE helper macros Date: Fri, 24 Oct 2025 12:21:41 +0200 Message-ID: <20251024102143.236665-3-mssola@mssola.com> In-Reply-To: <20251024102143.236665-1-mssola@mssola.com> References: <20251024102143.236665-1-mssola@mssola.com> 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 These are two simple macros which ensure that a pointer is initialized to NULL and with the proper cleanup attribute for it. Suggested-by: David Sterba Signed-off-by: Miquel Sabat=C3=A9 Sol=C3=A0 --- fs/btrfs/misc.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/fs/btrfs/misc.h b/fs/btrfs/misc.h index 60f9b000d644..4d0a417af77e 100644 --- a/fs/btrfs/misc.h +++ b/fs/btrfs/misc.h @@ -13,6 +13,13 @@ #include #include =20 +/* + * Convenient macros to define a pointer with the __free(kfree) and + * __free(kvfree) cleanup attributes and initialized to NULL. + */ +#define AUTO_KFREE(name) *name __free(kfree) =3D NULL +#define AUTO_KVFREE(name) *name __free(kvfree) =3D NULL + /* * Enumerate bits using enum autoincrement. Define the @name as the n-th b= it. */ --=20 2.51.1 From nobody Sun Feb 8 17:42:40 2026 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA5CC30F939; Fri, 24 Oct 2025 10:22:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.152 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761301363; cv=none; b=qwpFT7YkoL/GAnPCis0S9iTuTYcYMkMHJwKSsC802tX6V//x3IRl+4d/6j1K3c8mmLx3xOwn3B3fUGxOQMdIKiEg6SYiha9wZy8fHhrKNB0J8I6YG6aDE/ASjQjSJcQYWM+/Jk+eUOUR/hba5VPmG4Ti3/FwJ7XAM0w2+L7RC3M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761301363; c=relaxed/simple; bh=oPw5Nw4lHmLEYTzuFswxhdv6HFHFCk/658sbQt0c9Oc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MDhLGux3T20Pb+EIAnwoohgwrdmVoio5LpQup66bJRNqLIBXsfJ+R+/ZO3yYLI0c1IkB7jUhn9Rg3JQJ2fM0SztjEyxvQZHPuEBfXADnmbjwggxMWNu2JExXupkRG8VwILPFk5ILeZ6iT3LjOklVkhmvQex6O9/wIcAyrY/nR/o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=mssola.com; spf=fail smtp.mailfrom=mssola.com; dkim=pass (2048-bit key) header.d=mssola.com header.i=@mssola.com header.b=TLmBMrZL; arc=none smtp.client-ip=80.241.56.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=mssola.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=mssola.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mssola.com header.i=@mssola.com header.b="TLmBMrZL" Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4ctJqq4S6Qz9tWv; Fri, 24 Oct 2025 12:22:31 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mssola.com; s=MBO0001; t=1761301351; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LWat3oXpWjt7Nw1hP+sXkqR0e2tefMTtNV2D03LSEFA=; b=TLmBMrZLlMeO/aYdvMS4ijB2RQbQBePDyZWBbUZObxMDXcyZGt37T7nVAbMi/RghEFVsOl ZHWHSeGh3+btJn57Cv7lgMC5yTZRpeD3lhEJz1XOswDGVhJjM/ZJ3zfRLXdQSVBcz1QbCl H7g9GA3zfvkSue4zBGN60rREDlA24lOaCJ181IHf4b1AN7+RMZbLnGWC/pzl6EbMGLAHIO 4kQccn5ZPSHv3/iC3x0KnEEk2Fs7FhXxBuJ6Y1dK4XUhgGSBKNMGHsMt5RlQ/ihH5ofTqf llqBE76OYnuzhLrUy2J8a1kVpoewne5VyYQjvCzp2TZ+Qi2wVsVVbAa1zXXaVA== From: =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= To: linux-btrfs@vger.kernel.org Cc: clm@fb.com, dsterba@suse.com, johannes.thumshirn@wdc.com, fdmanana@suse.com, boris@bur.io, wqu@suse.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= Subject: [PATCH v2 3/4] btrfs: apply the AUTO_K(V)FREE macros throughout the tree Date: Fri, 24 Oct 2025 12:21:42 +0200 Message-ID: <20251024102143.236665-4-mssola@mssola.com> In-Reply-To: <20251024102143.236665-1-mssola@mssola.com> References: <20251024102143.236665-1-mssola@mssola.com> 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 Apply the AUTO_KFREE and AUTO_KVFREE macros wherever it makes sense. Since this macro is expected to improve code readability, it has been avoided in places where the lifetime of objects wasn't easy to follow and a cleanup attribute would've made things worse; or when the cleanup section of a function involved many other things and thus there was no readability impact anyways. This change has also not been applied in extremely short functions where readability was clearly not an issue. Signed-off-by: Miquel Sabat=C3=A9 Sol=C3=A0 --- fs/btrfs/acl.c | 29 ++++++++---------- fs/btrfs/delayed-inode.c | 17 +++++------ fs/btrfs/extent-tree.c | 17 +++++------ fs/btrfs/ioctl.c | 31 +++++++------------ fs/btrfs/qgroup.c | 3 +- fs/btrfs/raid-stripe-tree.c | 14 +++------ fs/btrfs/reflink.c | 7 ++--- fs/btrfs/relocation.c | 34 ++++++++------------- fs/btrfs/send.c | 50 ++++++++++++------------------- fs/btrfs/super.c | 3 +- fs/btrfs/tests/extent-io-tests.c | 3 +- fs/btrfs/tests/extent-map-tests.c | 6 ++-- fs/btrfs/tree-log.c | 46 +++++++++++----------------- fs/btrfs/volumes.c | 28 +++++------------ fs/btrfs/zoned.c | 3 +- 15 files changed, 105 insertions(+), 186 deletions(-) diff --git a/fs/btrfs/acl.c b/fs/btrfs/acl.c index e0ba00d64ea0..2033b37348dd 100644 --- a/fs/btrfs/acl.c +++ b/fs/btrfs/acl.c @@ -14,12 +14,13 @@ #include "ctree.h" #include "xattr.h" #include "acl.h" +#include "misc.h" =20 struct posix_acl *btrfs_get_acl(struct inode *inode, int type, bool rcu) { int size; const char *name; - char *value =3D NULL; + char AUTO_KFREE(value); struct posix_acl *acl; =20 if (rcu) @@ -49,7 +50,6 @@ struct posix_acl *btrfs_get_acl(struct inode *inode, int = type, bool rcu) acl =3D NULL; else acl =3D ERR_PTR(size); - kfree(value); =20 return acl; } @@ -57,9 +57,9 @@ struct posix_acl *btrfs_get_acl(struct inode *inode, int = type, bool rcu) int __btrfs_set_acl(struct btrfs_trans_handle *trans, struct inode *inode, struct posix_acl *acl, int type) { - int ret, size =3D 0; + int ret =3D 0, size =3D 0; const char *name; - char *value =3D NULL; + char AUTO_KFREE(value); =20 switch (type) { case ACL_TYPE_ACCESS: @@ -85,34 +85,29 @@ int __btrfs_set_acl(struct btrfs_trans_handle *trans, s= truct inode *inode, nofs_flag =3D memalloc_nofs_save(); value =3D kmalloc(size, GFP_KERNEL); memalloc_nofs_restore(nofs_flag); - if (!value) { - ret =3D -ENOMEM; - goto out; - } + if (!value) + return -ENOMEM; =20 ret =3D posix_acl_to_xattr(&init_user_ns, acl, value, size); if (ret < 0) - goto out; + return ret; } =20 if (trans) ret =3D btrfs_setxattr(trans, inode, name, value, size, 0); else ret =3D btrfs_setxattr_trans(inode, name, value, size, 0); + if (ret < 0) + return ret; =20 -out: - kfree(value); - - if (!ret) - set_cached_acl(inode, type, acl); - - return ret; + set_cached_acl(inode, type, acl); + return 0; } =20 int btrfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, struct posix_acl *acl, int type) { - int ret; + int ret =3D 0; struct inode *inode =3D d_inode(dentry); umode_t old_mode =3D inode->i_mode; =20 diff --git a/fs/btrfs/delayed-inode.c b/fs/btrfs/delayed-inode.c index 41e37f7f67cc..bf7c89135441 100644 --- a/fs/btrfs/delayed-inode.c +++ b/fs/btrfs/delayed-inode.c @@ -668,8 +668,8 @@ static int btrfs_insert_delayed_item(struct btrfs_trans= _handle *trans, struct btrfs_key first_key; const u32 first_data_size =3D first_item->data_len; int total_size; - char *ins_data =3D NULL; - int ret; + char AUTO_KFREE(ins_data); + int ret =3D 0; bool continuous_keys_only =3D false; =20 lockdep_assert_held(&node->mutex); @@ -740,10 +740,8 @@ static int btrfs_insert_delayed_item(struct btrfs_tran= s_handle *trans, =20 ins_data =3D kmalloc_array(batch.nr, sizeof(u32) + sizeof(struct btrfs_key), GFP_NOFS); - if (!ins_data) { - ret =3D -ENOMEM; - goto out; - } + if (!ins_data) + return -ENOMEM; ins_sizes =3D (u32 *)ins_data; ins_keys =3D (struct btrfs_key *)(ins_data + batch.nr * sizeof(u32)); batch.keys =3D ins_keys; @@ -759,7 +757,7 @@ static int btrfs_insert_delayed_item(struct btrfs_trans= _handle *trans, =20 ret =3D btrfs_insert_empty_items(trans, root, path, &batch); if (ret) - goto out; + return ret; =20 list_for_each_entry(curr, &item_list, tree_list) { char *data_ptr; @@ -814,9 +812,8 @@ static int btrfs_insert_delayed_item(struct btrfs_trans= _handle *trans, list_del(&curr->tree_list); btrfs_release_delayed_item(curr); } -out: - kfree(ins_data); - return ret; + + return 0; } =20 static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans, diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index ae2c3dc9957e..c846894cbb78 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -6060,7 +6060,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root, bool= update_ref, bool for_reloc struct btrfs_trans_handle *trans; struct btrfs_root *tree_root =3D fs_info->tree_root; struct btrfs_root_item *root_item =3D &root->root_item; - struct walk_control *wc; + struct walk_control AUTO_KFREE(wc); struct btrfs_key key; const u64 rootid =3D btrfs_root_id(root); int ret =3D 0; @@ -6078,9 +6078,8 @@ int btrfs_drop_snapshot(struct btrfs_root *root, bool= update_ref, bool for_reloc =20 wc =3D kzalloc(sizeof(*wc), GFP_NOFS); if (!wc) { - btrfs_free_path(path); ret =3D -ENOMEM; - goto out; + goto out_free; } =20 /* @@ -6290,7 +6289,6 @@ int btrfs_drop_snapshot(struct btrfs_root *root, bool= update_ref, bool for_reloc =20 btrfs_end_transaction_throttle(trans); out_free: - kfree(wc); btrfs_free_path(path); out: if (!ret && root_dropped) { @@ -6333,7 +6331,7 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *tra= ns, { struct btrfs_fs_info *fs_info =3D root->fs_info; BTRFS_PATH_AUTO_FREE(path); - struct walk_control *wc; + struct walk_control AUTO_KFREE(wc); int level; int parent_level; int ret =3D 0; @@ -6372,18 +6370,17 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *t= rans, while (1) { ret =3D walk_down_tree(trans, root, path, wc); if (ret < 0) - break; + return ret; =20 ret =3D walk_up_tree(trans, root, path, wc, parent_level); if (ret) { - if (ret > 0) - ret =3D 0; + if (ret < 0) + return ret; break; } } =20 - kfree(wc); - return ret; + return 0; } =20 /* diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index a65b67dab055..a33185a457f9 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -503,7 +503,7 @@ static noinline int create_subvol(struct mnt_idmap *idm= ap, struct btrfs_fs_info *fs_info =3D inode_to_fs_info(dir); struct btrfs_trans_handle *trans; struct btrfs_key key; - struct btrfs_root_item *root_item; + struct btrfs_root_item AUTO_KFREE(root_item); struct btrfs_inode_item *inode_item; struct extent_buffer *leaf; struct btrfs_root *root =3D BTRFS_I(dir)->root; @@ -516,7 +516,7 @@ static noinline int create_subvol(struct mnt_idmap *idm= ap, .subvol =3D true, }; unsigned int trans_num_items; - int ret; + int ret =3D 0; dev_t anon_dev; u64 objectid; u64 qgroup_reserved =3D 0; @@ -527,20 +527,18 @@ static noinline int create_subvol(struct mnt_idmap *i= dmap, =20 ret =3D btrfs_get_free_objectid(fs_info->tree_root, &objectid); if (ret) - goto out_root_item; + return ret; =20 /* * Don't create subvolume whose level is not zero. Or qgroup will be * screwed up since it assumes subvolume qgroup's level to be 0. */ - if (btrfs_qgroup_level(objectid)) { - ret =3D -ENOSPC; - goto out_root_item; - } + if (btrfs_qgroup_level(objectid)) + return -ENOSPC; =20 ret =3D get_anon_bdev(&anon_dev); if (ret < 0) - goto out_root_item; + return ret; =20 new_inode_args.inode =3D btrfs_new_subvol_inode(idmap, dir); if (!new_inode_args.inode) { @@ -692,8 +690,7 @@ static noinline int create_subvol(struct mnt_idmap *idm= ap, out_anon_dev: if (anon_dev) free_anon_bdev(anon_dev); -out_root_item: - kfree(root_item); + return ret; } =20 @@ -2956,7 +2953,7 @@ static long btrfs_ioctl_space_info(struct btrfs_fs_in= fo *fs_info, struct btrfs_ioctl_space_args space_args =3D { 0 }; struct btrfs_ioctl_space_info space; struct btrfs_ioctl_space_info *dest; - struct btrfs_ioctl_space_info *dest_orig; + struct btrfs_ioctl_space_info AUTO_KFREE(dest_orig); struct btrfs_ioctl_space_info __user *user_dest; struct btrfs_space_info *info; static const u64 types[] =3D { @@ -3077,9 +3074,8 @@ static long btrfs_ioctl_space_info(struct btrfs_fs_in= fo *fs_info, (arg + sizeof(struct btrfs_ioctl_space_args)); =20 if (copy_to_user(user_dest, dest_orig, alloc_size)) - ret =3D -EFAULT; + return -EFAULT; =20 - kfree(dest_orig); out: if (ret =3D=3D 0 && copy_to_user(arg, &space_args, sizeof(space_args))) ret =3D -EFAULT; @@ -3610,7 +3606,7 @@ static long btrfs_ioctl_balance_ctl(struct btrfs_fs_i= nfo *fs_info, int cmd) static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, void __user *arg) { - struct btrfs_ioctl_balance_args *bargs; + struct btrfs_ioctl_balance_args AUTO_KFREE(bargs); int ret =3D 0; =20 if (!capable(CAP_SYS_ADMIN)) @@ -3632,8 +3628,6 @@ static long btrfs_ioctl_balance_progress(struct btrfs= _fs_info *fs_info, =20 if (copy_to_user(arg, bargs, sizeof(*bargs))) ret =3D -EFAULT; - - kfree(bargs); out: mutex_unlock(&fs_info->balance_mutex); return ret; @@ -4227,7 +4221,7 @@ static int check_feature_bits(const struct btrfs_fs_i= nfo *fs_info, u64 safe_set, u64 safe_clear) { const char *type =3D btrfs_feature_set_name(set); - char *names; + const char AUTO_KFREE(names); u64 disallowed, unsupported; u64 set_mask =3D flags & change_mask; u64 clear_mask =3D ~flags & change_mask; @@ -4239,7 +4233,6 @@ static int check_feature_bits(const struct btrfs_fs_i= nfo *fs_info, btrfs_warn(fs_info, "this kernel does not support the %s feature bit%s", names, strchr(names, ',') ? "s" : ""); - kfree(names); } else btrfs_warn(fs_info, "this kernel does not support %s bits 0x%llx", @@ -4254,7 +4247,6 @@ static int check_feature_bits(const struct btrfs_fs_i= nfo *fs_info, btrfs_warn(fs_info, "can't set the %s feature bit%s while mounted", names, strchr(names, ',') ? "s" : ""); - kfree(names); } else btrfs_warn(fs_info, "can't set %s bits 0x%llx while mounted", @@ -4269,7 +4261,6 @@ static int check_feature_bits(const struct btrfs_fs_i= nfo *fs_info, btrfs_warn(fs_info, "can't clear the %s feature bit%s while mounted", names, strchr(names, ',') ? "s" : ""); - kfree(names); } else btrfs_warn(fs_info, "can't clear %s bits 0x%llx while mounted", diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c index 51d696e49768..6919f429c6b3 100644 --- a/fs/btrfs/qgroup.c +++ b/fs/btrfs/qgroup.c @@ -4792,7 +4792,7 @@ int btrfs_qgroup_trace_subtree_after_cow(struct btrfs= _trans_handle *trans, struct btrfs_fs_info *fs_info =3D root->fs_info; struct btrfs_tree_parent_check check =3D { 0 }; struct btrfs_qgroup_swapped_blocks *blocks =3D &root->swapped_blocks; - struct btrfs_qgroup_swapped_block *block; + struct btrfs_qgroup_swapped_block AUTO_KFREE(block); struct extent_buffer *reloc_eb =3D NULL; struct rb_node *node; bool swapped =3D false; @@ -4849,7 +4849,6 @@ int btrfs_qgroup_trace_subtree_after_cow(struct btrfs= _trans_handle *trans, ret =3D qgroup_trace_subtree_swap(trans, reloc_eb, subvol_eb, block->last_snapshot, block->trace_leaf); free_out: - kfree(block); free_extent_buffer(reloc_eb); out: if (ret < 0) { diff --git a/fs/btrfs/raid-stripe-tree.c b/fs/btrfs/raid-stripe-tree.c index cc6f6095cc9f..f5c616115254 100644 --- a/fs/btrfs/raid-stripe-tree.c +++ b/fs/btrfs/raid-stripe-tree.c @@ -19,7 +19,7 @@ static int btrfs_partially_delete_raid_extent(struct btrf= s_trans_handle *trans, u64 newlen, u64 frontpad) { struct btrfs_root *stripe_root =3D trans->fs_info->stripe_root; - struct btrfs_stripe_extent *extent, *newitem; + struct btrfs_stripe_extent *extent, AUTO_KFREE(newitem); struct extent_buffer *leaf; int slot; size_t item_size; @@ -53,14 +53,10 @@ static int btrfs_partially_delete_raid_extent(struct bt= rfs_trans_handle *trans, =20 ret =3D btrfs_del_item(trans, stripe_root, path); if (ret) - goto out; + return ret; =20 btrfs_release_path(path); - ret =3D btrfs_insert_item(trans, stripe_root, &newkey, newitem, item_size= ); - -out: - kfree(newitem); - return ret; + return btrfs_insert_item(trans, stripe_root, &newkey, newitem, item_size); } =20 int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, = u64 length) @@ -299,7 +295,7 @@ int btrfs_insert_one_raid_extent(struct btrfs_trans_han= dle *trans, struct btrfs_key stripe_key; struct btrfs_root *stripe_root =3D fs_info->stripe_root; const int num_stripes =3D btrfs_bg_type_to_factor(bioc->map_type); - struct btrfs_stripe_extent *stripe_extent; + struct btrfs_stripe_extent AUTO_KFREE(stripe_extent); const size_t item_size =3D struct_size(stripe_extent, strides, num_stripe= s); int ret; =20 @@ -336,8 +332,6 @@ int btrfs_insert_one_raid_extent(struct btrfs_trans_han= dle *trans, btrfs_abort_transaction(trans, ret); } =20 - kfree(stripe_extent); - return ret; } =20 diff --git a/fs/btrfs/reflink.c b/fs/btrfs/reflink.c index 1bbe3bb7e1bb..775a32a7953a 100644 --- a/fs/btrfs/reflink.c +++ b/fs/btrfs/reflink.c @@ -343,7 +343,7 @@ static int btrfs_clone(struct inode *src, struct inode = *inode, BTRFS_PATH_AUTO_FREE(path); struct extent_buffer *leaf; struct btrfs_trans_handle *trans; - char *buf =3D NULL; + char AUTO_KVFREE(buf); struct btrfs_key key; u32 nritems; int slot; @@ -358,10 +358,8 @@ static int btrfs_clone(struct inode *src, struct inode= *inode, return ret; =20 path =3D btrfs_alloc_path(); - if (!path) { - kvfree(buf); + if (!path) return ret; - } =20 path->reada =3D READA_FORWARD; /* Clone data */ @@ -611,7 +609,6 @@ static int btrfs_clone(struct inode *src, struct inode = *inode, } =20 out: - kvfree(buf); clear_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &BTRFS_I(inode)->runtime_flags); =20 return ret; diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 96539e8b7b4b..739fca944296 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -511,7 +511,7 @@ static void __del_reloc_root(struct btrfs_root *root) { struct btrfs_fs_info *fs_info =3D root->fs_info; struct rb_node *rb_node; - struct mapping_node *node =3D NULL; + struct mapping_node AUTO_KFREE(node); struct reloc_control *rc =3D fs_info->reloc_ctl; bool put_ref =3D false; =20 @@ -544,7 +544,6 @@ static void __del_reloc_root(struct btrfs_root *root) spin_unlock(&fs_info->trans_lock); if (put_ref) btrfs_put_root(root); - kfree(node); } =20 /* @@ -586,10 +585,9 @@ static struct btrfs_root *create_reloc_root(struct btr= fs_trans_handle *trans, struct btrfs_fs_info *fs_info =3D root->fs_info; struct btrfs_root *reloc_root; struct extent_buffer *eb; - struct btrfs_root_item *root_item; + struct btrfs_root_item AUTO_KFREE(root_item); struct btrfs_key root_key; int ret =3D 0; - bool must_abort =3D false; =20 root_item =3D kmalloc(sizeof(*root_item), GFP_NOFS); if (!root_item) @@ -617,15 +615,14 @@ static struct btrfs_root *create_reloc_root(struct bt= rfs_trans_handle *trans, btrfs_err(fs_info, "cannot relocate partially dropped subvolume %llu, drop progress key " BT= RFS_KEY_FMT, objectid, BTRFS_KEY_FMT_VALUE(&cpu_key)); - ret =3D -EUCLEAN; - goto fail; + return ERR_PTR(-EUCLEAN); } =20 /* called by btrfs_init_reloc_root */ ret =3D btrfs_copy_root(trans, root, root->commit_root, &eb, BTRFS_TREE_RELOC_OBJECTID); if (ret) - goto fail; + return ERR_PTR(ret); =20 /* * Set the last_snapshot field to the generation of the commit @@ -648,14 +645,13 @@ static struct btrfs_root *create_reloc_root(struct bt= rfs_trans_handle *trans, ret =3D btrfs_copy_root(trans, root, root->node, &eb, BTRFS_TREE_RELOC_OBJECTID); if (ret) - goto fail; + return ERR_PTR(ret); } =20 /* * We have changed references at this point, we must abort the - * transaction if anything fails. + * transaction if anything fails (i.e. 'goto abort'). */ - must_abort =3D true; =20 memcpy(root_item, &root->root_item, sizeof(*root_item)); btrfs_set_root_bytenr(root_item, eb->start); @@ -675,9 +671,7 @@ static struct btrfs_root *create_reloc_root(struct btrf= s_trans_handle *trans, ret =3D btrfs_insert_root(trans, fs_info->tree_root, &root_key, root_item); if (ret) - goto fail; - - kfree(root_item); + goto abort; =20 reloc_root =3D btrfs_read_tree_root(fs_info->tree_root, &root_key); if (IS_ERR(reloc_root)) { @@ -687,11 +681,9 @@ static struct btrfs_root *create_reloc_root(struct btr= fs_trans_handle *trans, set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state); btrfs_set_root_last_trans(reloc_root, trans->transid); return reloc_root; -fail: - kfree(root_item); + abort: - if (must_abort) - btrfs_abort_transaction(trans, ret); + btrfs_abort_transaction(trans, ret); return ERR_PTR(ret); } =20 @@ -2947,7 +2939,7 @@ static int relocate_file_extent_cluster(struct reloc_= control *rc) const struct file_extent_cluster *cluster =3D &rc->cluster; u64 offset =3D BTRFS_I(inode)->reloc_block_group_start; u64 cur_file_offset =3D cluster->start - offset; - struct file_ra_state *ra; + struct file_ra_state AUTO_KFREE(ra); int cluster_nr =3D 0; int ret =3D 0; =20 @@ -2960,13 +2952,13 @@ static int relocate_file_extent_cluster(struct relo= c_control *rc) =20 ret =3D prealloc_file_extent_cluster(rc); if (ret) - goto out; + return ret; =20 file_ra_state_init(ra, inode->i_mapping); =20 ret =3D setup_relocation_extent_mapping(rc); if (ret) - goto out; + return ret; =20 while (cur_file_offset < cluster->end - offset) { ret =3D relocate_one_folio(rc, ra, &cluster_nr, &cur_file_offset); @@ -2975,8 +2967,6 @@ static int relocate_file_extent_cluster(struct reloc_= control *rc) } if (ret =3D=3D 0) WARN_ON(cluster_nr !=3D cluster->nr); -out: - kfree(ra); return ret; } =20 diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c index caeaa50f2f44..9312d74400a3 100644 --- a/fs/btrfs/send.c +++ b/fs/btrfs/send.c @@ -2458,7 +2458,7 @@ static int send_subvol_begin(struct send_ctx *sctx) struct btrfs_key key; struct btrfs_root_ref *ref; struct extent_buffer *leaf; - char *name =3D NULL; + char AUTO_KFREE(name); int namelen; =20 path =3D btrfs_alloc_path(); @@ -2476,18 +2476,15 @@ static int send_subvol_begin(struct send_ctx *sctx) ret =3D btrfs_search_slot_for_read(send_root->fs_info->tree_root, &key, path, 1, 0); if (ret < 0) - goto out; - if (ret) { - ret =3D -ENOENT; - goto out; - } + return ret; + if (ret) + return -ENOENT; =20 leaf =3D path->nodes[0]; btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); if (key.type !=3D BTRFS_ROOT_BACKREF_KEY || key.objectid !=3D btrfs_root_id(send_root)) { - ret =3D -ENOENT; - goto out; + return -ENOENT; } ref =3D btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); namelen =3D btrfs_root_ref_name_len(leaf, ref); @@ -2497,11 +2494,11 @@ static int send_subvol_begin(struct send_ctx *sctx) if (parent_root) { ret =3D begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); if (ret < 0) - goto out; + return ret; } else { ret =3D begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); if (ret < 0) - goto out; + return ret; } =20 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); @@ -2529,8 +2526,6 @@ static int send_subvol_begin(struct send_ctx *sctx) ret =3D send_cmd(sctx); =20 tlv_put_failure: -out: - kfree(name); return ret; } =20 @@ -4077,7 +4072,7 @@ static int update_ref_path(struct send_ctx *sctx, str= uct recorded_ref *ref) */ static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *re= f) { - char *name; + char AUTO_KFREE(name); int ret; =20 name =3D kmemdup(ref->name, ref->name_len, GFP_KERNEL); @@ -4087,17 +4082,16 @@ static int refresh_ref_path(struct send_ctx *sctx, = struct recorded_ref *ref) fs_path_reset(ref->full_path); ret =3D get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path); if (ret < 0) - goto out; + return ret; =20 ret =3D fs_path_add(ref->full_path, name, ref->name_len); if (ret < 0) - goto out; + return ret; =20 /* Update the reference's base name pointer. */ set_ref_path(ref, ref->full_path); -out: - kfree(name); - return ret; + + return 0; } =20 static int rbtree_check_dir_ref_comp(const void *k, const struct rb_node *= node) @@ -5006,8 +5000,8 @@ static int __process_changed_new_xattr(int num, struc= t btrfs_key *di_key, { int ret; struct send_ctx *sctx =3D ctx; - char *found_data =3D NULL; - int found_data_len =3D 0; + char AUTO_KFREE(found_data); + int found_data_len =3D 0; =20 ret =3D find_xattr(sctx->parent_root, sctx->right_path, sctx->cmp_key, name, name_len, &found_data, @@ -5025,7 +5019,6 @@ static int __process_changed_new_xattr(int num, struc= t btrfs_key *di_key, } } =20 - kfree(found_data); return ret; } =20 @@ -5762,7 +5755,7 @@ static int send_capabilities(struct send_ctx *sctx) struct btrfs_dir_item *di; struct extent_buffer *leaf; unsigned long data_ptr; - char *buf =3D NULL; + char AUTO_KFREE(buf); int buf_len; int ret =3D 0; =20 @@ -5774,28 +5767,23 @@ static int send_capabilities(struct send_ctx *sctx) XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0); if (!di) { /* There is no xattr for this inode */ - goto out; + return 0; } else if (IS_ERR(di)) { - ret =3D PTR_ERR(di); - goto out; + return PTR_ERR(di); } =20 leaf =3D path->nodes[0]; buf_len =3D btrfs_dir_data_len(leaf, di); =20 buf =3D kmalloc(buf_len, GFP_KERNEL); - if (!buf) { - ret =3D -ENOMEM; - goto out; - } + if (!buf) + return -ENOMEM; =20 data_ptr =3D (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di); read_extent_buffer(leaf, buf, data_ptr, buf_len); =20 ret =3D send_set_xattr(sctx, XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), buf, buf_len); -out: - kfree(buf); return ret; } =20 diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 5cd8d8185a29..4ffd7059e27a 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -1614,7 +1614,7 @@ static inline void btrfs_descending_sort_devices( static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_inf= o, u64 *free_bytes) { - struct btrfs_device_info *devices_info; + struct btrfs_device_info AUTO_KFREE(devices_info); struct btrfs_fs_devices *fs_devices =3D fs_info->fs_devices; struct btrfs_device *device; u64 type; @@ -1712,7 +1712,6 @@ static inline int btrfs_calc_avail_data_space(struct = btrfs_fs_info *fs_info, nr_devices--; } =20 - kfree(devices_info); *free_bytes =3D avail_space; return 0; } diff --git a/fs/btrfs/tests/extent-io-tests.c b/fs/btrfs/tests/extent-io-te= sts.c index b19328d077d3..a0187d6163df 100644 --- a/fs/btrfs/tests/extent-io-tests.c +++ b/fs/btrfs/tests/extent-io-tests.c @@ -505,7 +505,7 @@ static int __test_eb_bitmaps(unsigned long *bitmap, str= uct extent_buffer *eb) static int test_eb_bitmaps(u32 sectorsize, u32 nodesize) { struct btrfs_fs_info *fs_info; - unsigned long *bitmap =3D NULL; + unsigned long AUTO_KFREE(bitmap); struct extent_buffer *eb =3D NULL; int ret; =20 @@ -551,7 +551,6 @@ static int test_eb_bitmaps(u32 sectorsize, u32 nodesize) ret =3D __test_eb_bitmaps(bitmap, eb); out: free_extent_buffer(eb); - kfree(bitmap); btrfs_free_dummy_fs_info(fs_info); return ret; } diff --git a/fs/btrfs/tests/extent-map-tests.c b/fs/btrfs/tests/extent-map-= tests.c index 42af6c737c6e..0b9f25dd1a68 100644 --- a/fs/btrfs/tests/extent-map-tests.c +++ b/fs/btrfs/tests/extent-map-tests.c @@ -1013,7 +1013,7 @@ static int test_rmap_block(struct btrfs_fs_info *fs_i= nfo, struct rmap_test_vector *test) { struct btrfs_chunk_map *map; - u64 *logical =3D NULL; + u64 AUTO_KFREE(logical); int i, out_ndaddrs, out_stripe_len; int ret; =20 @@ -1046,7 +1046,7 @@ static int test_rmap_block(struct btrfs_fs_info *fs_i= nfo, if (ret) { test_err("error adding chunk map to mapping tree"); btrfs_free_chunk_map(map); - goto out_free; + return ret; } =20 ret =3D btrfs_rmap_block(fs_info, map->start, btrfs_sb_offset(1), @@ -1079,8 +1079,6 @@ static int test_rmap_block(struct btrfs_fs_info *fs_i= nfo, ret =3D 0; out: btrfs_remove_chunk_map(fs_info, map); -out_free: - kfree(logical); return ret; } =20 diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 65079eb651da..0bb38e191e4e 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -3281,7 +3281,7 @@ static inline void btrfs_remove_log_ctx(struct btrfs_= root *root, mutex_unlock(&root->log_mutex); } =20 -/*=20 +/* * Invoked in log mutex context, or be sure there is no other task which * can access the list. */ @@ -4015,7 +4015,7 @@ static int flush_dir_items_batch(struct btrfs_trans_h= andle *trans, int count) { struct btrfs_root *log =3D inode->root->log_root; - char *ins_data =3D NULL; + char AUTO_KFREE(ins_data); struct btrfs_item_batch batch; struct extent_buffer *dst; unsigned long src_offset; @@ -4060,7 +4060,7 @@ static int flush_dir_items_batch(struct btrfs_trans_h= andle *trans, =20 ret =3D btrfs_insert_empty_items(trans, log, dst_path, &batch); if (ret) - goto out; + return ret; =20 dst =3D dst_path->nodes[0]; /* @@ -4092,8 +4092,6 @@ static int flush_dir_items_batch(struct btrfs_trans_h= andle *trans, =20 if (btrfs_get_first_dir_index_to_log(inode) =3D=3D 0) btrfs_set_first_dir_index_to_log(inode, batch.keys[0].offset); -out: - kfree(ins_data); =20 return ret; } @@ -4760,7 +4758,7 @@ static noinline int copy_items(struct btrfs_trans_han= dle *trans, struct btrfs_key *ins_keys; u32 *ins_sizes; struct btrfs_item_batch batch; - char *ins_data; + char AUTO_KFREE(ins_data); int dst_index; const bool skip_csum =3D (inode->flags & BTRFS_INODE_NODATASUM); const u64 i_size =3D i_size_read(&inode->vfs_inode); @@ -4888,7 +4886,7 @@ static noinline int copy_items(struct btrfs_trans_han= dle *trans, disk_bytenr + extent_num_bytes - 1, &ordered_sums, false); if (ret < 0) - goto out; + return ret; ret =3D 0; =20 list_for_each_entry_safe(sums, sums_next, &ordered_sums, list) { @@ -4898,7 +4896,7 @@ static noinline int copy_items(struct btrfs_trans_han= dle *trans, kfree(sums); } if (ret) - goto out; + return ret; =20 add_to_batch: ins_sizes[dst_index] =3D btrfs_item_size(src, src_slot); @@ -4912,11 +4910,11 @@ static noinline int copy_items(struct btrfs_trans_h= andle *trans, * so we don't need to do anything. */ if (batch.nr =3D=3D 0) - goto out; + return 0; =20 ret =3D btrfs_insert_empty_items(trans, log, dst_path, &batch); if (ret) - goto out; + return ret; =20 dst_index =3D 0; for (int i =3D 0; i < nr; i++) { @@ -4969,8 +4967,6 @@ static noinline int copy_items(struct btrfs_trans_han= dle *trans, } =20 btrfs_release_path(dst_path); -out: - kfree(ins_data); =20 return ret; } @@ -5689,9 +5685,8 @@ static int btrfs_check_ref_name_override(struct exten= t_buffer *eb, struct btrfs_inode *inode, u64 *other_ino, u64 *other_parent) { - int ret; BTRFS_PATH_AUTO_FREE(search_path); - char *name =3D NULL; + char AUTO_KFREE(name); u32 name_len =3D 0; u32 item_size =3D btrfs_item_size(eb, slot); u32 cur_offset =3D 0; @@ -5734,10 +5729,8 @@ static int btrfs_check_ref_name_override(struct exte= nt_buffer *eb, char *new_name; =20 new_name =3D krealloc(name, this_name_len, GFP_NOFS); - if (!new_name) { - ret =3D -ENOMEM; - goto out; - } + if (!new_name) + return -ENOMEM; name_len =3D this_name_len; name =3D new_name; } @@ -5755,28 +5748,24 @@ static int btrfs_check_ref_name_override(struct ext= ent_buffer *eb, di, &di_key); if (di_key.type =3D=3D BTRFS_INODE_ITEM_KEY) { if (di_key.objectid !=3D key->objectid) { - ret =3D 1; *other_ino =3D di_key.objectid; *other_parent =3D parent; + return 1; } else { - ret =3D 0; + return 0; } } else { - ret =3D -EAGAIN; + return -EAGAIN; } - goto out; } else if (IS_ERR(di)) { - ret =3D PTR_ERR(di); - goto out; + return PTR_ERR(di); } btrfs_release_path(search_path); =20 cur_offset +=3D this_len; } - ret =3D 0; -out: - kfree(name); - return ret; + + return 0; } =20 /* @@ -8044,4 +8033,3 @@ void btrfs_log_new_name(struct btrfs_trans_handle *tr= ans, btrfs_end_log_trans(root); free_extent_buffer(ctx.scratch_eb); } - diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 45d89b12025b..75a34ed95c74 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -739,7 +739,7 @@ static bool is_same_device(struct btrfs_device *device,= const char *new_path) { struct path old =3D { .mnt =3D NULL, .dentry =3D NULL }; struct path new =3D { .mnt =3D NULL, .dentry =3D NULL }; - char *old_path =3D NULL; + char AUTO_KFREE(old_path); bool is_same =3D false; int ret; =20 @@ -765,7 +765,6 @@ static bool is_same_device(struct btrfs_device *device,= const char *new_path) if (path_equal(&old, &new)) is_same =3D true; out: - kfree(old_path); path_put(&old); path_put(&new); return is_same; @@ -4384,7 +4383,7 @@ static void describe_balance_start_or_resume(struct b= trfs_fs_info *fs_info) { u32 size_buf =3D 1024; char tmp_buf[192] =3D {'\0'}; - char *buf; + char AUTO_KFREE(buf); char *bp; u32 size_bp =3D size_buf; int ret; @@ -4432,8 +4431,6 @@ static void describe_balance_start_or_resume(struct b= trfs_fs_info *fs_info) btrfs_info(fs_info, "balance: %s %s", (bctl->flags & BTRFS_BALANCE_RESUME) ? "resume" : "start", buf); - - kfree(buf); } =20 /* @@ -5562,9 +5559,8 @@ struct btrfs_block_group *btrfs_create_chunk(struct b= trfs_trans_handle *trans, { struct btrfs_fs_info *info =3D trans->fs_info; struct btrfs_fs_devices *fs_devices =3D info->fs_devices; - struct btrfs_device_info *devices_info =3D NULL; + struct btrfs_device_info AUTO_KFREE(devices_info); struct alloc_chunk_ctl ctl; - struct btrfs_block_group *block_group; int ret; =20 lockdep_assert_held(&info->chunk_mutex); @@ -5597,22 +5593,14 @@ struct btrfs_block_group *btrfs_create_chunk(struct= btrfs_trans_handle *trans, return ERR_PTR(-ENOMEM); =20 ret =3D gather_device_info(fs_devices, &ctl, devices_info); - if (ret < 0) { - block_group =3D ERR_PTR(ret); - goto out; - } + if (ret < 0) + return ERR_PTR(ret); =20 ret =3D decide_stripe_size(fs_devices, &ctl, devices_info); - if (ret < 0) { - block_group =3D ERR_PTR(ret); - goto out; - } - - block_group =3D create_chunk(trans, &ctl, devices_info); + if (ret < 0) + return ERR_PTR(ret); =20 -out: - kfree(devices_info); - return block_group; + return create_chunk(trans, &ctl, devices_info); } =20 /* diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c index 2e3145c1a102..020d3e951653 100644 --- a/fs/btrfs/zoned.c +++ b/fs/btrfs/zoned.c @@ -1631,7 +1631,7 @@ int btrfs_load_block_group_zone_info(struct btrfs_blo= ck_group *cache, bool new) struct btrfs_chunk_map *map; u64 logical =3D cache->start; u64 length =3D cache->length; - struct zone_info *zone_info =3D NULL; + struct zone_info AUTO_KFREE(zone_info); int ret; int i; unsigned long *active =3D NULL; @@ -1786,7 +1786,6 @@ int btrfs_load_block_group_zone_info(struct btrfs_blo= ck_group *cache, bool new) cache->physical_map =3D NULL; } bitmap_free(active); - kfree(zone_info); =20 return ret; } --=20 2.51.1 From nobody Sun Feb 8 17:42:40 2026 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40D0030F808; Fri, 24 Oct 2025 10:22:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761301360; cv=none; b=Hj7vo2LcAlCODLBIWJErn/NfRHgtVnM7Gjwn33fuuwfd78S2te0jC93EA7n7V+TXL0pz+HRAgEaflUSr81tVFxxmH6YnrFPUC+rcMdM91vdJwsX2/BxC8tu+57zgzFEKABE05S9/yEToiK0/t36HqXRHMCSlZMj1clenqMpk1S4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761301360; c=relaxed/simple; bh=dfYrRcCz625GU5Hv4qF7R5hDzF21+y53LzlCMNULDzs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FGZssqviwCVN2b3DhsjT1k7HpZCPJ0+qAZO8zp4wqy+eLLMLOXU3rSrv6cvbUJKHj16OcxOfgYRVFkrTy4LgLuQD/DEOkljYaL6usXm+anj/N3g25wEwDQI3p2IuOpN2y0g+GKs8AEpK8/voA/r/m2dyLWY3SXIBNSa0V4PJHKg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=mssola.com; spf=fail smtp.mailfrom=mssola.com; dkim=pass (2048-bit key) header.d=mssola.com header.i=@mssola.com header.b=EGNpaG/u; arc=none smtp.client-ip=80.241.56.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=mssola.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=mssola.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mssola.com header.i=@mssola.com header.b="EGNpaG/u" Received: from smtp102.mailbox.org (smtp102.mailbox.org [IPv6:2001:67c:2050:b231:465::102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4ctJqt3YW7z9sSs; Fri, 24 Oct 2025 12:22:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mssola.com; s=MBO0001; t=1761301354; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Js1n6kPDTMBIfCKrUsee5DCSrbS0DlAuQlCfrk27LTQ=; b=EGNpaG/uoS52MNNPLbxnB53UgA98RvddtykD6ad/Vjuo/dryjnHsRoMQnsk1JNu2nVoS2y JR02OQr6WVGzfokQ4cONmF9QjiKRdGsE5YpBJfcz00JmuXFaXMWuLkMcpkG7gOprvlSLFB PWmUsp+ixF7nerJZgbM8GxSupDCZHoHM5n41HP1ppuv+iAavFG5EGKnPqUY/nKcQa5fGsm fP4+kPpFXWseYF6iQR3+ntNrPxzebZWasCoq2I1kXCbvW4RmB5LQt/OtyPs5pB7xHNafWx B6PDA0eTjC2KV7Tel5Uv7jnsQGSxEahOxqd7Fsqdn+NP2noxSeQuoBJ8fy2TMQ== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=softfail (outgoing_mbo_mout: 2001:67c:2050:b231:465::102 is neither permitted nor denied by domain of mssola@mssola.com) smtp.mailfrom=mssola@mssola.com From: =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= To: linux-btrfs@vger.kernel.org Cc: clm@fb.com, dsterba@suse.com, johannes.thumshirn@wdc.com, fdmanana@suse.com, boris@bur.io, wqu@suse.com, linux-kernel@vger.kernel.org, =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= Subject: [PATCH v2 4/4] btrfs: add ASSERTs on prealloc in qgroup functions Date: Fri, 24 Oct 2025 12:21:43 +0200 Message-ID: <20251024102143.236665-5-mssola@mssola.com> In-Reply-To: <20251024102143.236665-1-mssola@mssola.com> References: <20251024102143.236665-1-mssola@mssola.com> 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 X-Rspamd-Queue-Id: 4ctJqt3YW7z9sSs The prealloc variable in these functions is always initialized to NULL. Whenever we allocate memory for it, if it fails then NULL is preserved, otherwise we delegate the ownership of the pointer to add_qgroup_rb() and set it right after to NULL Since in any case the pointer ends up being NULL at the end of its usage, we can safely remove calls to kfree() for it, while adding an ASSERT as an extra check. Signed-off-by: Miquel Sabat=C3=A9 Sol=C3=A0 --- fs/btrfs/qgroup.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c index 6919f429c6b3..1c00a5330c4d 100644 --- a/fs/btrfs/qgroup.c +++ b/fs/btrfs/qgroup.c @@ -1263,7 +1263,14 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info, btrfs_end_transaction(trans); else if (trans) ret =3D btrfs_end_transaction(trans); - kfree(prealloc); + + /* + * At this point we either failed at allocating prealloc, or we + * succeeded and passed the ownership to it to add_qgroup_rb(). In any + * case, this needs to be NULL or there is something wrong. + */ + ASSERT(prealloc =3D=3D NULL); + return ret; } =20 @@ -1693,7 +1700,12 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *t= rans, u64 qgroupid) ret =3D btrfs_sysfs_add_one_qgroup(fs_info, qgroup); out: mutex_unlock(&fs_info->qgroup_ioctl_lock); - kfree(prealloc); + /* + * At this point we either failed at allocating prealloc, or we + * succeeded and passed the ownership to it to add_qgroup_rb(). In any + * case, this needs to be NULL or there is something wrong. + */ + ASSERT(prealloc =3D=3D NULL); return ret; } =20 @@ -3301,7 +3313,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *t= rans, u64 srcid, struct btrfs_root *quota_root; struct btrfs_qgroup *srcgroup; struct btrfs_qgroup *dstgroup; - struct btrfs_qgroup *prealloc; + struct btrfs_qgroup *prealloc =3D NULL; struct btrfs_qgroup_list **qlist_prealloc =3D NULL; bool free_inherit =3D false; bool need_rescan =3D false; @@ -3542,7 +3554,14 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *= trans, u64 srcid, } if (free_inherit) kfree(inherit); - kfree(prealloc); + + /* + * At this point we either failed at allocating prealloc, or we + * succeeded and passed the ownership to it to add_qgroup_rb(). In any + * case, this needs to be NULL or there is something wrong. + */ + ASSERT(prealloc =3D=3D NULL); + return ret; } =20 --=20 2.51.1