From nobody Sun Feb 8 18:19:20 2026 Received: from mout-y-209.mailbox.org (mout-y-209.mailbox.org [91.198.250.237]) (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 1A6CB1EDA1E; Tue, 21 Oct 2025 14:28:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.198.250.237 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761056907; cv=none; b=g74if+gi3QwjrJWTFXvxxPhKrrPjj9kIqZVlqcxnW6tCfduNP4ESluIlZ/D3XU6Ok8xw5h/RClxwnfkdOweI2deUEkcBn9708PI++dUnWcxRmBHGPpeWYwYyYxfMSJHVFT+zLSHp2otQv80RpHdO8oW9ciu7CMKhIn8Fi+uoVP8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761056907; c=relaxed/simple; bh=TVUUCnjng9lxzyy0qq+GzVmU/aLF6wLcZy0qlWhCrbM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=F0qtob5HSmX3Xvu8THTyVsAoyfmuulGsKIoUQB0lDnMMk9z2qcTGApvzJqoaNCgc1Sb/3mCL9ZyZ17Jv/fe71063LfBWJ/WwENs8OeZSKcDmQHlSPcV6ezQ/2/5zSTT0uYAzaTZBtHz9W2uBR/RDqvvYXeHsYVcfrerlw6uaI5E= 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=wjOMsl4l; arc=none smtp.client-ip=91.198.250.237 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="wjOMsl4l" 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-y-209.mailbox.org (Postfix) with ESMTPS id 4crZQp1ls0z9ttm; Tue, 21 Oct 2025 16:28:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mssola.com; s=MBO0001; t=1761056898; 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=uilBc+Gi0c4GQOo+svzYnBLdiIGJkGSGTPDGvTOpCSM=; b=wjOMsl4ly+IxaO7XsArq5z/P+9ALd6VFPof1tX0GTT82Q41UMTVLKTkcntS0JUa3CzNf4A bwj0crvRFFPxaMtjzJ8RpXf13ogZ3kR+IRdWcz1ea05vVYGPkYjtpA9iSqtS17NLyuHRsR 3JYNDIhELaWFA8cunpSwJ0XWC3sr8PEXaFX7nMnU+tT+wR2QmMC20IrxnJyAubOX4SLP6D 5MSDI0vxAbgvMxjcDrvPhjQg5F8NILYY6ptLfM0qJFrtQ+D6hvYDDtnaTACVjWbwyb2QuV lmCpLIcLlvE5j+okbI8ZdA0MBuFeVvf48P7ud5YzImFLxUH0BlSt+gPYnFHF3A== 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, neal@gompa.dev, linux-kernel@vger.kernel.org, =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= Subject: [PATCH 1/4] btrfs: declare free_ipath() via DEFINE_FREE() instead Date: Tue, 21 Oct 2025 16:27:46 +0200 Message-ID: <20251021142749.642956-2-mssola@mssola.com> In-Reply-To: <20251021142749.642956-1-mssola@mssola.com> References: <20251021142749.642956-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 This transforms the signature to __free_ipath() instead of the original free_ipath(), but this function was only being used as a cleanup function anyways. Hence, define it as a helper and use it via the __free() attribute on all uses. This change also means that __free_ipath() will be inlined whereas that wasn't the case for the original one, but this shouldn't be a problem. A follow up macro like we do with BTRFS_PATH_AUTO_FREE() has been discarded as the usage of this struct is not as widespread as that. 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..a1456402752a 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_ipath() 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..d3b1ad281793 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(ipath, 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..4d154209d70b 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(ipath) =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..453832ded917 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(ipath) =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..74d8af1ff02d 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(ipath) =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 18:19:20 2026 Received: from mout-y-209.mailbox.org (mout-y-209.mailbox.org [91.198.250.237]) (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 92A6825EF9C; Tue, 21 Oct 2025 14:28:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.198.250.237 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761056908; cv=none; b=hGm/9j4RvXYYJgDGlz5AoP94SrNKvkDIeWpfsxK1pcVIKFKXc/q1zRk7NyevGSCNPHwf62MJtwc0hcs2f6F0j2N8ArjbN25lWVD9CY/6I+lRm9281M3WwgmmJhxDpdm8VtyE1ZaBLUMPzlGjtD2ins2zwg4aeLjgd1dG0j//Efw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761056908; c=relaxed/simple; bh=zMyKREAOYWVFCApr0xhtd/UF5pT71OuhTStzFWNDAmw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ThOv1G8C05fxRpncTKWyeIqOPvR7iCLsEKw9yMUU3fyFNVfAtTcNrSv/tE0kc95dtPZaFOtFx3RYJnHZKLVt6LGGsReVv38+fckTBTot6tVLbR9Ya725/QMGoVOsqsqH6/nOEmz8SFT1zMA4Ad6hUxYUeibZ2mEUezZ8b4rMAQ4= 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=leh0EJf0; arc=none smtp.client-ip=91.198.250.237 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="leh0EJf0" 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-y-209.mailbox.org (Postfix) with ESMTPS id 4crZQs4qGpzB0gr; Tue, 21 Oct 2025 16:28:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mssola.com; s=MBO0001; t=1761056901; 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=4Zc8N7D+mRZxcFLHziOmGNcp6TISGWgy+Cb4QBRBUK4=; b=leh0EJf0P5HWxVFPIWGAjUFrnLlSYqmD/AF/h3zyQiXHmO/Jjwnal7/tu5Q7PHUi6m+XEF ao3SjedTtA6uAQQp26kZZlGnCuPH5HSXTBXJkTinwgKgXuPYo0w7txx3RTmNzffZMOMMHl edgXoW9BPhJspi1PLcdqswMlW2jiqtPTeeABGXhGsW/eGWaozRyeXQMAhrPGz6eRRRRqy4 CwKmhE2JFDf/g6xejtkqBx1nHp/YKl5+D+Pnvg1KUksWDOj5GdvkYjH1TmvfgHRPIX/AqL bgr+qyLj7ysUYmKWUN2Nd3s7pP4Fg1CI5pHVovDCUlbRo/HgQYo/i0B5Mv9oNA== 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, neal@gompa.dev, linux-kernel@vger.kernel.org, =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= , David Sterba Subject: [PATCH 2/4] btrfs: define the AUTO_K(V)FREE_PTR helper macros Date: Tue, 21 Oct 2025 16:27:47 +0200 Message-ID: <20251021142749.642956-3-mssola@mssola.com> In-Reply-To: <20251021142749.642956-1-mssola@mssola.com> References: <20251021142749.642956-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: 4crZQs4qGpzB0gr 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..0e33327e70d9 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_PTR(name) *name __free(kfree) =3D NULL +#define AUTO_KVFREE_PTR(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 18:19:20 2026 Received: from mout-y-209.mailbox.org (mout-y-209.mailbox.org [91.198.250.237]) (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 9F917338F38; Tue, 21 Oct 2025 14:28:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.198.250.237 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761056912; cv=none; b=AUHnQfZElTXI57dU+X0PIIZE68nbMn768QMzrl21Qq48p5WMJvqQnr8ysm6FTvEU+RGv4NNzh3fVb+kr/5/R2T5ir2yo6zW6MECT4BvRGUsFdDe+pbCRh14EhAYaoLETKaW1hOQBmAUDHKGmZWbnhTCFRxrhoHwKw48MYElr3go= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761056912; c=relaxed/simple; bh=/8bVVTr/f5koIplbbifjRrkpftNfOUSsSZx1Pb5YROo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=H+46HUecCEez1CEfGQrypp5GXH3bvY/3TelkUk/O7nNljyuuZrdNcPgl1YHedhZgTm8Ieb5q2ETy044OmSWzBu3hyL481Z2CzSy58TJKbTIhOVDuy1j9CYV9uydmdJuh20KiQTQSSCTf7Ly0aQyQByksTf3+M04ZA8j+uLO7NXQ= 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=EA07soDy; arc=none smtp.client-ip=91.198.250.237 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="EA07soDy" 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-y-209.mailbox.org (Postfix) with ESMTPS id 4crZQx1CLXzB0hg; Tue, 21 Oct 2025 16:28:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mssola.com; s=MBO0001; t=1761056905; 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=5/f9P1v0BBQh+CTO8kyD9MdWqixiz0cFKU1SeKBP+QM=; b=EA07soDyELttqBG7bbp+6gDRKI3Z8OLA/18NJb1o/qfk5yALON8UsAJz8po/bFOOqOl/zX m2u7euQTrMmi6R5AF3HVb2v0sflg7DloJf7wiEdAr9hb+FqofAcuLZ+u9ir8Hjqi/nbuPo JHAlIhVbbCO0CrAjNUS/kF9AHF7J0bcA+Qw2rVtAzgtPepRzEC5SLyUp5ZC+UjiOaYg1ZR D7o+XS4c62zVfiTDi9r+huG4AF4SQpjIsSG7TFJXAMVK4IfVGg+0hKMBRNkwhCNbc4Rro7 0WBJkcLMXxVJumYwbUV8vnfhPCsqOkHqvtbOfG0BNS+iwGgVutXcT3A2OKSOPA== 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, neal@gompa.dev, linux-kernel@vger.kernel.org, =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= Subject: [PATCH 3/4] btrfs: apply the AUTO_K(V)FREE_PTR macros throughout the tree Date: Tue, 21 Oct 2025 16:27:48 +0200 Message-ID: <20251021142749.642956-4-mssola@mssola.com> In-Reply-To: <20251021142749.642956-1-mssola@mssola.com> References: <20251021142749.642956-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_PTR and AUTO_KVFREE_PTR 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..36731beb8f37 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_PTR(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_PTR(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..7ec94f6bc1e0 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_PTR(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..3e4c30a54e7a 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_PTR(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_PTR(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 453832ded917..97c62c2803b1 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_PTR(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_PTR(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_PTR(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_PTR(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..6adb57d5c958 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_PTR(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..8a7277329df9 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_PTR(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_PTR(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..2155850431b1 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_PTR(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..9d591a466c9b 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_PTR(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_PTR(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_PTR(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..3d00a9390b45 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_PTR(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_PTR(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_PTR(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_PTR(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..aa7416ee1556 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_PTR(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..2c5d10e558e4 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_PTR(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..2a2c61cb99d1 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_PTR(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..2144adcff05c 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_PTR(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_PTR(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_PTR(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..dcc6c400bb5f 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_PTR(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_PTR(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_PTR(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..0f026dc3dfe8 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_PTR(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 18:19:20 2026 Received: from mout-y-209.mailbox.org (mout-y-209.mailbox.org [91.198.250.237]) (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 CABBE33CEB1; Tue, 21 Oct 2025 14:28:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.198.250.237 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761056913; cv=none; b=KhRbdb6sJmWiDv+TVIrvbPsxBQ/0pAq10G+P0rlrrsL6G4D+EZJ3haSp4BfCHYk8EI5K/bZjLCMTh2ivIICcjIu0bZe/kRfeJ24yMhL6ypmzA7JnhepYKtk69vNJ3b7A3Egd1ykchSO0v/GWiqWWPShGMjwBZYRbdC9vnCVCAVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761056913; c=relaxed/simple; bh=kIjr2ON/PurIYOXE+LNsjEc2BbF67/ys+PB2KDggH2M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XG4HtzRGDRvA71lhbdCyJnLz/nxL70ibueySqhqBQyqrRgf2Ke+Q+7OhCs/Jt3uciuQiwqLGK3aLml+96EurVG5fhpDHk0b7jLCqod/R2DHY34D1MCjp5n1kYR2RwJe46c9I3RnYGSKdFea1oX43WUjGkZsXh2ze6LCVPFbkBZg= 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=0tYh6Nke; arc=none smtp.client-ip=91.198.250.237 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="0tYh6Nke" 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-y-209.mailbox.org (Postfix) with ESMTPS id 4crZR00gqLz9ttm; Tue, 21 Oct 2025 16:28:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mssola.com; s=MBO0001; t=1761056908; 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=5x3oERtMwu9v6VSR766wyFue1jS0wUt8HyF2GQPQzSU=; b=0tYh6Nke7uPFz4pSjzTyBWQIujeuYe1lOoJ1S34z3MEvEPb7Pq8wPBCw2+ohmACoU2NoJv AHceXXjHrP4aQ9pfQDgD6Jx+b/CZPUSo9LyHGYe4rMwrRhkM5gC9QdezN+0jV8/iIFUKNM TSfTPlh7pr7BCOEmgJouEAxFsAmUAdqS3YGIKmtLWP9cvSkWTwEDKAc+kd5KKe1d53cXLe UpB3t0OU0X597GqQ1/nZery7UGM4GYMLFSw3H1dtrByD3cRzT5WHJblfR/0jXYUkcBTS1E hmwYe4kZ+IjhOlN+gh+ik75GiVVX7yXXvUcmxG5TXdWBRruC4jfgXmOYkBY7Lg== 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, neal@gompa.dev, linux-kernel@vger.kernel.org, =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= Subject: [PATCH 4/4] btrfs: add ASSERTs on prealloc in qgroup functions Date: Tue, 21 Oct 2025 16:27:49 +0200 Message-ID: <20251021142749.642956-5-mssola@mssola.com> In-Reply-To: <20251021142749.642956-1-mssola@mssola.com> References: <20251021142749.642956-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 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 6adb57d5c958..664135240803 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