From patchwork Fri Aug 9 23:11:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Martins X-Patchwork-Id: 13759307 Received: from mail-oa1-f65.google.com (mail-oa1-f65.google.com [209.85.160.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E442B16CD16 for ; Fri, 9 Aug 2024 23:13:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723245189; cv=none; b=Y7adpUGaBIoXqK/cAjMrBue4W9nnIGWt+X7zS/V869c1AzbJdZ1WJuO2iivdC5Bww1rbLyvLbighfZE2nvlzRdb8ozfHDVKmiMNHqK/ZaULF9W74e3UjvgOhfjvbNCKRp0e2J5bXgfThGzdelosXr2FlbPHAATktUG0q5RFuERg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723245189; c=relaxed/simple; bh=b3aE4OU31/P6Alr6f8pvKJScwtC6WMvFNKDVbkb7SN0=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZkP2Liwjm0je+Z78eMqjUKaC3h3tethFIGNBAmLc+wtwqTbhQnpZZN1bC4wMy4bTwDkxyWqhixfXRNv9D4NDfoaM7QQgt4Q5mvism7NCRzMa2Sucz1XcW7NQXxR6LoVhDh8XFV7xFjXIsr3lHaDq6VHAnf50hMOUfjWDXsgrqYw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=NxefaP8i; arc=none smtp.client-ip=209.85.160.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NxefaP8i" Received: by mail-oa1-f65.google.com with SMTP id 586e51a60fabf-2642cfb2f6aso1780030fac.2 for ; Fri, 09 Aug 2024 16:13:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723245187; x=1723849987; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=qD9gIWmjwth9e80JD5TSQ9Qh3GmJGcSVMby3XxxnIGA=; b=NxefaP8iSm23MqY7FE0nOeTV4L9+Hx+xOi2ts9iftt8U+34qwx/l2VhPHW+SHlGzfq IjSJqqjWB8DAHBsaTM78cYUdB+oiQAGyLvjWUKs/qdFrMbPqbB2Zz8jZFIvwtwSJmiHb VEtv/YqgW/T+9AqEDM5Db6Bpjp3h/MfN9jCsw1DkwRY+9GRHdLuw11EDx6i9TcPkYt8h gmdR6+LE6UWIVPFWoGDmXBL4YnkL1wxbp5AxmGou3QQkCHP1z00CfXNjfdRGk7P3joxx 3OBObDbyL7WbrpoTtOtXnK4AHQyrjA8sbGVqQGDneT7mP8H8zMplcnmPtIARIrshusnT RYmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723245187; x=1723849987; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qD9gIWmjwth9e80JD5TSQ9Qh3GmJGcSVMby3XxxnIGA=; b=fmqFXwJHBl9LBcBeBAorU/T1v55Ra1/0cnMshmHWgBkklXKTyxtDsuXo2oVaLTlXfm 0e7vRiGIv0HeyEZk+ClYkEM07/YMvklukmFAjrqVjVunXp9NxCkcbW43QPkr8lEO9ccR U9GoOAaPs+fxLTSZni3G5hcLg6E6EOLYuiCa7IkA406qbgbkih2RIk5O8y6V1bbhCYSS YxdNYc/QYr3cC3uHy1OT9I18wthFq9HwLETLarBhcNx8aCnTfKzdlf8FTTqUzgRda9JN f+YIkIeV3JbDzlvDPShVdGnWlc9ZgeRIdj6pAVvdCeN5SzwwKO6uLY1gFjhLCn85ZzXm YMaA== X-Gm-Message-State: AOJu0Yw0YZkn8GkyKI8As4EgMsjgCbahhR/1YnqjnLJP/qAejtLxNC9g GuttD72RTLlYzZGY0WZv6eT9ixTFe64f9Bp0WWJw6H/JEc5AMKcA1RgtcAb5 X-Google-Smtp-Source: AGHT+IFxKCxPndPRm5oNl5TO2VzQly10cHZ/eOmaENKQMAXpbKND7ItTII3uciZC3iGdc/59tb053w== X-Received: by 2002:a05:6870:b49f:b0:260:eb3a:1bc with SMTP id 586e51a60fabf-26c630218d5mr3895833fac.41.1723245186641; Fri, 09 Aug 2024 16:13:06 -0700 (PDT) Received: from localhost (fwdproxy-eag-004.fbsv.net. [2a03:2880:3ff:4::face:b00c]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-26c720918eesm279015fac.19.2024.08.09.16.13.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 16:13:06 -0700 (PDT) From: Leo Martins To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 1/2] btrfs: use __free in linux/cleanup.h to reduce btrfs_free_path boilerplate Date: Fri, 9 Aug 2024 16:11:48 -0700 Message-ID: <7e5f9aebbfdd190cdb4f12589a779d873f3fd91e.1723245033.git.loemra.dev@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This patch lays the groundwork for future improvements to the btrfs file system code by introducing the __free(btrfs_free_path) attribute. This attribute allows the kernel to automatically call btrfs_free_path() on variables marked with it when they go out of scope, ensuring proper memory management and preventing potential memory leaks. Test Plan: Built and booted the kernel with patch applied. Ran btrfs/fstests to make sure that no regressions were introduced. Signed-off-by: Leo Martins --- fs/btrfs/ctree.c | 3 ++- fs/btrfs/ctree.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c index 451203055bbf..9938664d7dbb 100644 --- a/fs/btrfs/ctree.c +++ b/fs/btrfs/ctree.c @@ -3,6 +3,7 @@ * Copyright (C) 2007,2008 Oracle. All rights reserved. */ +#include #include #include #include @@ -196,7 +197,7 @@ struct btrfs_path *btrfs_alloc_path(void) /* this also releases the path */ void btrfs_free_path(struct btrfs_path *p) { - if (!p) + if (IS_ERR_OR_NULL(p)) return; btrfs_release_path(p); kmem_cache_free(btrfs_path_cachep, p); diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h index 75fa563e4cac..be4e14b6e39a 100644 --- a/fs/btrfs/ctree.h +++ b/fs/btrfs/ctree.h @@ -599,6 +599,7 @@ int btrfs_search_slot_for_read(struct btrfs_root *root, void btrfs_release_path(struct btrfs_path *p); struct btrfs_path *btrfs_alloc_path(void); void btrfs_free_path(struct btrfs_path *p); +DEFINE_FREE(btrfs_free_path, struct btrfs_path *, if (_T) btrfs_free_path(_T)); int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, int slot, int nr); From patchwork Fri Aug 9 23:11:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Martins X-Patchwork-Id: 13759308 Received: from mail-oi1-f194.google.com (mail-oi1-f194.google.com [209.85.167.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7DF43168490 for ; Fri, 9 Aug 2024 23:13:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723245191; cv=none; b=UEn0wE1RMh5Jx4ixaRNLa109o192DkM5uQBxSLWd2j5SLsEnkOSKwqzF2diVC7IOp4eSYR1ueQSzNUyztAKL8zqCUqG9Z8R1xsWp39OC3QFk4ti7L9eNY7R85cDqf5cDLOBUPDffAnRosY33ukGlt/31rqbiw2KzAyoGmUPv5+Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723245191; c=relaxed/simple; bh=XogM9MZf83Rh04Ww8e2u/feKdrAGX7dYiHwjV043OC8=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L0BcCcRcZjhcwdbiuTKjooC2rqI1KtoBvFZJP+KS4pEr7MdJ6VBensSyr63H4iG3pqbaeyo/xbBuptSVNds1U0xGWNH4l8sHxVHSzXV4NnnQ2am0pDMvmtUU4bSBTH+clV5e3RnhbwK+08pEiL2JfIrLi9nQdRL3wu50Mq671cM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=O35PwDBm; arc=none smtp.client-ip=209.85.167.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="O35PwDBm" Received: by mail-oi1-f194.google.com with SMTP id 5614622812f47-3db35ec5688so1553123b6e.3 for ; Fri, 09 Aug 2024 16:13:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723245188; x=1723849988; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=wvDzw+bJFK2QkWo1B/Ta98uLYB+B9NbfDHmU+mKJV7o=; b=O35PwDBmV8m+/Z+pNbKpuojfUcEB/vdYMZKwPwmwOpgYtbq74DYrP66eqqZik8XYRt FV7GMx4mYR1YikO6RrBPclRuWObNPum1F0qa3zeVFeISDgZN42v6kvr5qCMbHomvTGdZ mGaC9iEEXOCaRMA3WGXKdUmLk/3HkRWn2m+sZYxxFlVvi539JlN2PMSxlnwIVYyMmPEy 2QBySs0WSyML9DwoZZRfOeZMP0ZjzMP84YGqJrOMOwK7oOBgW9e1pmkA0UW0ZDRSF5Ho VIGPIE9PV9K4vap0dMi8E076VhsHbkbLzupM/KSR2ctmHET5m7DK3GNtKPahle2lQfBV TqIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723245188; x=1723849988; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wvDzw+bJFK2QkWo1B/Ta98uLYB+B9NbfDHmU+mKJV7o=; b=h+zuNhFfAdtn9fFz2CuE0aSreHLmpIPE4iYGfHDq/2liZ1dGe71q1Tf5BjY1858FY3 nJ5Q/L8OTv3kzgaEyXS2G/je1tNxi/PbC/2mBeIAySOvRaQ1j6Mvy6CWEEKVJ8h7zcwQ ahpHoTJ4BgfQscHkJgPVI98W4gnsimoaGy7i7RMqXgKZsoGim1FWH99+n3oTlb5XkuD9 NsXaSGUHGBqIcAEFSlEk4ff4706TkdukPHJAIPx42zUaIO7w3vRvH1wiVrp9C2BZgKyU e5qN6A2qsUtQ4QghKvE08u+jkokxm43j4MMMfGp+1Fm7afz2Z/CgH1D62C5unYwR2XgS OstQ== X-Gm-Message-State: AOJu0YyvnIJghgToyj3u6VOEWPomFjXhRYm2oaGwWti60QP40X6Ydqsu PEn7F26saRgJkip1Q1GlIaxtbxMrw5xMkMffwAycVnYf0INLFTzUTZ0181yC X-Google-Smtp-Source: AGHT+IHGDwh6Y76WQMyE/G1t8C0BUFbyVasw2HbBvJcJ4g0W0gcq1Jp/coCCPOpenXFJeIIkXdKg5g== X-Received: by 2002:a05:6808:1782:b0:3d8:4603:e7a3 with SMTP id 5614622812f47-3dc41677437mr3656109b6e.1.1723245188015; Fri, 09 Aug 2024 16:13:08 -0700 (PDT) Received: from localhost (fwdproxy-eag-112.fbsv.net. [2a03:2880:3ff:70::face:b00c]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3dd060bc058sm142655b6e.48.2024.08.09.16.13.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 16:13:07 -0700 (PDT) From: Leo Martins To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH 2/2] btrfs: use __free to automatically free btrfs_path on exit Date: Fri, 9 Aug 2024 16:11:49 -0700 Message-ID: X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduces the __free attribute to xattr.c. Marks the path variable in the btrfs_getxattr(), btrfs_setxattr(), and btrfs_listxattr() functions with the __free(btrfs_free_path) attribute. When a variable is marked with the __free attribute, the kernel will automatically call the specified function (in this case, btrfs_free_path()) on the variable when it goes out of scope. This ensures that the memory allocated for the variable is properly released, preventing potential memory leaks. By using the __free attribute, we can simplify the code and reduce the risk of memory-related bugs. Test Plan: Built and booted the kernel with patch applied Ran btrfs/fstests to make sure that no regressions were introduced Signed-off-by: Leo Martins --- fs/btrfs/xattr.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/fs/btrfs/xattr.c b/fs/btrfs/xattr.c index 738c7bb8ea7c..a8d5db02202b 100644 --- a/fs/btrfs/xattr.c +++ b/fs/btrfs/xattr.c @@ -29,9 +29,8 @@ int btrfs_getxattr(const struct inode *inode, const char *name, { struct btrfs_dir_item *di; struct btrfs_root *root = BTRFS_I(inode)->root; - struct btrfs_path *path; + struct btrfs_path *path __free(btrfs_free_path) = NULL; struct extent_buffer *leaf; - int ret = 0; unsigned long data_ptr; path = btrfs_alloc_path(); @@ -42,24 +41,20 @@ int btrfs_getxattr(const struct inode *inode, const char *name, di = btrfs_lookup_xattr(NULL, root, path, btrfs_ino(BTRFS_I(inode)), name, strlen(name), 0); if (!di) { - ret = -ENODATA; - goto out; + return -ENODATA; } else if (IS_ERR(di)) { - ret = PTR_ERR(di); - goto out; + return PTR_ERR(di); } leaf = path->nodes[0]; /* if size is 0, that means we want the size of the attr */ if (!size) { - ret = btrfs_dir_data_len(leaf, di); - goto out; + return btrfs_dir_data_len(leaf, di); } /* now get the data out of our dir_item */ if (btrfs_dir_data_len(leaf, di) > size) { - ret = -ERANGE; - goto out; + return -ERANGE; } /* @@ -73,11 +68,7 @@ int btrfs_getxattr(const struct inode *inode, const char *name, btrfs_dir_name_len(leaf, di)); read_extent_buffer(leaf, buffer, data_ptr, btrfs_dir_data_len(leaf, di)); - ret = btrfs_dir_data_len(leaf, di); - -out: - btrfs_free_path(path); - return ret; + return btrfs_dir_data_len(leaf, di); } int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode, @@ -86,7 +77,7 @@ int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode, struct btrfs_dir_item *di = NULL; struct btrfs_root *root = BTRFS_I(inode)->root; struct btrfs_fs_info *fs_info = root->fs_info; - struct btrfs_path *path; + struct btrfs_path *path __free(btrfs_free_path) = NULL; size_t name_len = strlen(name); int ret = 0; @@ -214,7 +205,6 @@ int btrfs_setxattr(struct btrfs_trans_handle *trans, struct inode *inode, */ } out: - btrfs_free_path(path); if (!ret) { set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags); @@ -280,7 +270,7 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) struct btrfs_key key; struct inode *inode = d_inode(dentry); struct btrfs_root *root = BTRFS_I(inode)->root; - struct btrfs_path *path; + struct btrfs_path *path __free(btrfs_free_path) = NULL; int iter_ret = 0; int ret = 0; size_t total_size = 0, size_left = size; @@ -356,8 +346,6 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size) else ret = total_size; - btrfs_free_path(path); - return ret; }