public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo.btrfs@gmx.com>
To: David Sterba <dsterba@suse.com>, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 4/4] btrfs: accessors: delete token versions of set/get helpers
Date: Sun, 29 Jun 2025 10:43:26 +0930	[thread overview]
Message-ID: <8993e49a-7fc2-41a0-8dec-3dbbf84ec601@gmx.com> (raw)
In-Reply-To: <a7f51e27606c24c063007273d0189bad487b921f.1751032655.git.dsterba@suse.com>



在 2025/6/27 23:33, David Sterba 写道:
> Once upon a time there was a need to cache address of extent buffer
> pages, as it was a costly operation (map_private_extent_buffer(),
> cfed81a04eb555 ("Btrfs: add the ability to cache a pointer into the
> eb")).  This was not even due to use of HIGHMEM, this had been removed
> before that due to possible locking issues ( a65917156e3459 ("Btrfs:
> stop using highmem for extent_buffers")).
> 
> Over the time the amount of work in the set/get helpers got reduced and
> became quite straightforward bounds checking with an unaligned
> read/write, commit db3756c879773c ("btrfs: remove unused
> map_private_extent_buffer").

Thanks a lot for the history of the token accessors.

And this is also a great chance to us to sync the removal of token eb 
accessors to the progs.

If you haven't yet started that progs removal, I'm totally fine to do that.

Thanks,
Qu

> 
> The actual caching of the page_address()/folio_address() in the token
> was more work for very little gain. This depended on subsequent access
> into the same page/folio, otherwise the cached pointer had to be
> updated.
> 
> For metadata-heavy operations this showed up in the 'perf top' profile
> where the btrfs_get_token_32 calls were at the top, on my testing
> machine consuming about 2-3%. The other generic 32/64 bit helpers also
> appeared in the profile with similar fraction.
> 
> After removing use of the token helpers we can remove them completely,
> this leads to reduction of btrfs.ko by 6.7KiB on release config.
> 
>     text    data     bss     dec     hex filename
> 1463289  115665   16088 1595042  1856a2 pre/btrfs.ko
> 1456601  115665   16088 1588354  183c82 post/btrfs.ko
> 
> DELTA: -6688
> 
> Signed-off-by: David Sterba <dsterba@suse.com>
> ---
>   fs/btrfs/accessors.c | 78 --------------------------------------------
>   fs/btrfs/accessors.h | 37 ---------------------
>   2 files changed, 115 deletions(-)
> 
> diff --git a/fs/btrfs/accessors.c b/fs/btrfs/accessors.c
> index e3716516ca3876..5cfb0801700e6c 100644
> --- a/fs/btrfs/accessors.c
> +++ b/fs/btrfs/accessors.c
> @@ -25,13 +25,6 @@ static bool check_setget_bounds(const struct extent_buffer *eb,
>   	return true;
>   }
>   
> -void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb)
> -{
> -	token->eb = eb;
> -	token->kaddr = folio_address(eb->folios[0]);
> -	token->offset = 0;
> -}
> -
>   /*
>    * Macro templates that define helpers to read/write extent buffer data of a
>    * given size, that are also used via ctree.h for access to item members by
> @@ -41,11 +34,6 @@ void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *e
>    * - btrfs_set_8 (for 8/16/32/64)
>    * - btrfs_get_8 (for 8/16/32/64)
>    *
> - * Generic helpers with a token (cached address of the most recently accessed
> - * page):
> - * - btrfs_set_token_8 (for 8/16/32/64)
> - * - btrfs_get_token_8 (for 8/16/32/64)
> - *
>    * The set/get functions handle data spanning two pages transparently, in case
>    * metadata block size is larger than page.  Every pointer to metadata items is
>    * an offset into the extent buffer page array, cast to a specific type.  This
> @@ -57,37 +45,6 @@ void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *e
>    */
>   
>   #define DEFINE_BTRFS_SETGET_BITS(bits)					\
> -u##bits btrfs_get_token_##bits(struct btrfs_map_token *token,		\
> -			       const void *ptr, unsigned long off)	\
> -{									\
> -	const unsigned long member_offset = (unsigned long)ptr + off;	\
> -	const unsigned long idx = get_eb_folio_index(token->eb, member_offset); \
> -	const unsigned long oil = get_eb_offset_in_folio(token->eb,	\
> -							 member_offset);\
> -	const int unit_size = token->eb->folio_size;			\
> -	const int unit_shift = token->eb->folio_shift;			\
> -	const int size = sizeof(u##bits);				\
> -	u8 lebytes[sizeof(u##bits)];					\
> -	const int part = unit_size - oil;				\
> -									\
> -	ASSERT(token);							\
> -	ASSERT(token->kaddr);						\
> -	ASSERT(check_setget_bounds(token->eb, ptr, off, size));		\
> -	if (token->offset <= member_offset &&				\
> -	    member_offset + size <= token->offset + unit_size) {	\
> -		return get_unaligned_le##bits(token->kaddr + oil);	\
> -	}								\
> -	token->kaddr = folio_address(token->eb->folios[idx]);		\
> -	token->offset = idx << unit_shift;				\
> -	if (INLINE_EXTENT_BUFFER_PAGES == 1 || oil + size <= unit_size) \
> -		return get_unaligned_le##bits(token->kaddr + oil);	\
> -									\
> -	memcpy(lebytes, token->kaddr + oil, part);			\
> -	token->kaddr = folio_address(token->eb->folios[idx + 1]);	\
> -	token->offset = (idx + 1) << unit_shift;			\
> -	memcpy(lebytes + part, token->kaddr, size - part);		\
> -	return get_unaligned_le##bits(lebytes);				\
> -}									\
>   u##bits btrfs_get_##bits(const struct extent_buffer *eb,		\
>   			 const void *ptr, unsigned long off)		\
>   {									\
> @@ -110,41 +67,6 @@ u##bits btrfs_get_##bits(const struct extent_buffer *eb,		\
>   	memcpy(lebytes + part, kaddr, size - part);			\
>   	return get_unaligned_le##bits(lebytes);				\
>   }									\
> -void btrfs_set_token_##bits(struct btrfs_map_token *token,		\
> -			    const void *ptr, unsigned long off,		\
> -			    u##bits val)				\
> -{									\
> -	const unsigned long member_offset = (unsigned long)ptr + off;	\
> -	const unsigned long idx = get_eb_folio_index(token->eb, member_offset); \
> -	const unsigned long oil = get_eb_offset_in_folio(token->eb,	\
> -							 member_offset);\
> -	const int unit_size = token->eb->folio_size;			\
> -	const int unit_shift = token->eb->folio_shift;			\
> -	const int size = sizeof(u##bits);				\
> -	u8 lebytes[sizeof(u##bits)];					\
> -	const int part = unit_size - oil;				\
> -									\
> -	ASSERT(token);							\
> -	ASSERT(token->kaddr);						\
> -	ASSERT(check_setget_bounds(token->eb, ptr, off, size));		\
> -	if (token->offset <= member_offset &&				\
> -	    member_offset + size <= token->offset + unit_size) {	\
> -		put_unaligned_le##bits(val, token->kaddr + oil);	\
> -		return;							\
> -	}								\
> -	token->kaddr = folio_address(token->eb->folios[idx]);		\
> -	token->offset = idx << unit_shift;				\
> -	if (INLINE_EXTENT_BUFFER_PAGES == 1 ||				\
> -	    oil + size <= unit_size) {					\
> -		put_unaligned_le##bits(val, token->kaddr + oil);	\
> -		return;							\
> -	}								\
> -	put_unaligned_le##bits(val, lebytes);				\
> -	memcpy(token->kaddr + oil, lebytes, part);			\
> -	token->kaddr = folio_address(token->eb->folios[idx + 1]);	\
> -	token->offset = (idx + 1) << unit_shift;			\
> -	memcpy(token->kaddr, lebytes + part, size - part);		\
> -}									\
>   void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr,	\
>   		      unsigned long off, u##bits val)			\
>   {									\
> diff --git a/fs/btrfs/accessors.h b/fs/btrfs/accessors.h
> index 15ea6348800b08..99b3ced12805bb 100644
> --- a/fs/btrfs/accessors.h
> +++ b/fs/btrfs/accessors.h
> @@ -16,14 +16,6 @@
>   
>   struct extent_buffer;
>   
> -struct btrfs_map_token {
> -	struct extent_buffer *eb;
> -	char *kaddr;
> -	unsigned long offset;
> -};
> -
> -void btrfs_init_map_token(struct btrfs_map_token *token, struct extent_buffer *eb);
> -
>   /*
>    * Some macros to generate set/get functions for the struct fields.  This
>    * assumes there is a lefoo_to_cpu for every type, so lets make a simple one
> @@ -56,11 +48,6 @@ static inline void put_unaligned_le8(u8 val, void *p)
>   			    sizeof_field(type, member)))
>   
>   #define DECLARE_BTRFS_SETGET_BITS(bits)					\
> -u##bits btrfs_get_token_##bits(struct btrfs_map_token *token,		\
> -			       const void *ptr, unsigned long off);	\
> -void btrfs_set_token_##bits(struct btrfs_map_token *token,		\
> -			    const void *ptr, unsigned long off,		\
> -			    u##bits val);				\
>   u##bits btrfs_get_##bits(const struct extent_buffer *eb,		\
>   			 const void *ptr, unsigned long off);		\
>   void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr,	\
> @@ -83,18 +70,6 @@ static inline void btrfs_set_##name(const struct extent_buffer *eb, type *s, \
>   {									\
>   	static_assert(sizeof(u##bits) == sizeof_field(type, member));	\
>   	btrfs_set_##bits(eb, s, offsetof(type, member), val);		\
> -}									\
> -static inline u##bits btrfs_token_##name(struct btrfs_map_token *token,	\
> -					 const type *s)			\
> -{									\
> -	static_assert(sizeof(u##bits) == sizeof_field(type, member));	\
> -	return btrfs_get_token_##bits(token, s, offsetof(type, member));\
> -}									\
> -static inline void btrfs_set_token_##name(struct btrfs_map_token *token,\
> -					  type *s, u##bits val)		\
> -{									\
> -	static_assert(sizeof(u##bits) == sizeof_field(type, member));	\
> -	btrfs_set_token_##bits(token, s, offsetof(type, member), val);	\
>   }
>   
>   #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits)		\
> @@ -479,18 +454,6 @@ static inline void btrfs_set_item_##member(const struct extent_buffer *eb,	\
>   					   int slot, u32 val)			\
>   {										\
>   	btrfs_set_raw_item_##member(eb, btrfs_item_nr(eb, slot), val);		\
> -}										\
> -static inline u32 btrfs_token_item_##member(struct btrfs_map_token *token,	\
> -					    int slot)				\
> -{										\
> -	struct btrfs_item *item = btrfs_item_nr(token->eb, slot);		\
> -	return btrfs_token_raw_item_##member(token, item);			\
> -}										\
> -static inline void btrfs_set_token_item_##member(struct btrfs_map_token *token,	\
> -						 int slot, u32 val)		\
> -{										\
> -	struct btrfs_item *item = btrfs_item_nr(token->eb, slot);		\
> -	btrfs_set_token_raw_item_##member(token, item, val);			\
>   }
>   
>   BTRFS_ITEM_SETGET_FUNCS(offset)


  reply	other threads:[~2025-06-29  1:13 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-27 14:03 [PATCH 0/4] Set/get accessor cleanups David Sterba
2025-06-27 14:03 ` [PATCH 1/4] btrfs: don't use token set/get accessors for btrfs_item members David Sterba
2025-06-27 14:03 ` [PATCH 2/4] btrfs: don't use token set/get accessors in inode.c:fill_inode_item() David Sterba
2025-06-27 14:03 ` [PATCH 3/4] btrfs: tree-log: don't use token set/get accessors in fill_inode_item() David Sterba
2025-06-27 14:03 ` [PATCH 4/4] btrfs: accessors: delete token versions of set/get helpers David Sterba
2025-06-29  1:13   ` Qu Wenruo [this message]
2025-06-30 16:11     ` David Sterba
2025-06-29  1:18 ` [PATCH 0/4] Set/get accessor cleanups Qu Wenruo
2025-06-30 15:54   ` David Sterba
2025-06-30  6:25 ` Johannes Thumshirn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8993e49a-7fc2-41a0-8dec-3dbbf84ec601@gmx.com \
    --to=quwenruo.btrfs@gmx.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox