[2/3] btrfs: add const function attribute
diff mbox series

Message ID 543f96a0b47e4856e6adbf3761a56df96480f358.1569587835.git.dsterba@suse.com
State New
Headers show
Series
  • Coldify, constify, purify (function attributes)
Related show

Commit Message

David Sterba Oct. 1, 2019, 5:57 p.m. UTC
For some reason the attribute is called __attribute_const__ and not
__const, marks functions that have no observable effects on program
state, IOW not reading pointers, just the arguments and calculating a
value. Allows the compiler to do some optimizations, based on
-Wsuggest-attribute=const . The effects are rather small, though, about
60 bytes decrese of btrfs.ko.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/ctree.h   | 2 +-
 fs/btrfs/super.c   | 2 +-
 fs/btrfs/volumes.c | 2 +-
 fs/btrfs/volumes.h | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

Comments

Nikolay Borisov Oct. 2, 2019, 11:07 a.m. UTC | #1
On 1.10.19 г. 20:57 ч., David Sterba wrote:
> For some reason the attribute is called __attribute_const__ and not
> __const, marks functions that have no observable effects on program
> state, IOW not reading pointers, just the arguments and calculating a
> value. Allows the compiler to do some optimizations, based on
> -Wsuggest-attribute=const . The effects are rather small, though, about
> 60 bytes decrese of btrfs.ko.
> 
> Signed-off-by: David Sterba <dsterba@suse.com>
> ---
>  fs/btrfs/ctree.h   | 2 +-
>  fs/btrfs/super.c   | 2 +-
>  fs/btrfs/volumes.c | 2 +-
>  fs/btrfs/volumes.h | 2 +-
>  4 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
> index 4bf0433b1179..793085770c84 100644
> --- a/fs/btrfs/ctree.h
> +++ b/fs/btrfs/ctree.h
> @@ -3146,7 +3146,7 @@ __cold
>  void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
>  		     unsigned int line, int errno, const char *fmt, ...);
>  
> -const char *btrfs_decode_error(int errno);
> +const char * __attribute_const__ btrfs_decode_error(int errno);
>  
>  __cold
>  void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
> diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
> index 3da35d8b21a3..b3e6d7aa3402 100644
> --- a/fs/btrfs/super.c
> +++ b/fs/btrfs/super.c
> @@ -66,7 +66,7 @@ static struct file_system_type btrfs_root_fs_type;
>  
>  static int btrfs_remount(struct super_block *sb, int *flags, char *data);
>  
> -const char *btrfs_decode_error(int errno)
> +const char * __attribute_const__ btrfs_decode_error(int errno)
>  {
>  	char *errstr = "unknown";
>  
> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> index 3fd89aee539d..c343b7cdfb53 100644
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -297,7 +297,7 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
>  
>  DEFINE_MUTEX(uuid_mutex);
>  static LIST_HEAD(fs_uuids);
> -struct list_head *btrfs_get_fs_uuids(void)
> +struct list_head * __attribute_const__ btrfs_get_fs_uuids(void)

I'm not entirely sure this function is cons. According to the manual:

Calls to functions whose return value is not affected by changes to the
observable state of the program and that have no observable effects on
such state other than to return a value may lend themselves to
optimizations such as common subexpression elimination.

The const attribute prohibits a function from reading objects that
affect its return value between successive invocations. However,
functions declared with the attribute can safely read objects that do
not change their return value, such as non-volatile constants.


My doubt stems from the fact this function actually references outside
memory, namely gets the ptr to fs_uuids. There is a specific remark not
to use const when the function takes a ptr argument but it doesn't say
anything when getting a ptr from a global var.

>  {
>  	return &fs_uuids;
>  }
> diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
> index a7da1f3e3627..0ae0677a8d86 100644
> --- a/fs/btrfs/volumes.h
> +++ b/fs/btrfs/volumes.h
> @@ -571,7 +571,7 @@ static inline enum btrfs_raid_types btrfs_bg_flags_to_raid_index(u64 flags)
>  
>  void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
>  
> -struct list_head *btrfs_get_fs_uuids(void);
> +struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);
>  void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info);
>  void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info);
>  bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
>
David Sterba Oct. 4, 2019, 11:01 a.m. UTC | #2
On Wed, Oct 02, 2019 at 02:07:50PM +0300, Nikolay Borisov wrote:
> > +struct list_head * __attribute_const__ btrfs_get_fs_uuids(void)
> 
> I'm not entirely sure this function is cons. According to the manual:
> 
> Calls to functions whose return value is not affected by changes to the
> observable state of the program and that have no observable effects on
> such state other than to return a value may lend themselves to
> optimizations such as common subexpression elimination.
> 
> The const attribute prohibits a function from reading objects that
> affect its return value between successive invocations. However,
> functions declared with the attribute can safely read objects that do
> not change their return value, such as non-volatile constants.
> 
> 
> My doubt stems from the fact this function actually references outside
> memory, namely gets the ptr to fs_uuids. There is a specific remark not
> to use const when the function takes a ptr argument but it doesn't say
> anything when getting a ptr from a global var.

The fs_uuids are a non-volatile constant. It does not change accross the
lifetime of the module, so all code executed will always see the same
value.

Patch
diff mbox series

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 4bf0433b1179..793085770c84 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -3146,7 +3146,7 @@  __cold
 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
 		     unsigned int line, int errno, const char *fmt, ...);
 
-const char *btrfs_decode_error(int errno);
+const char * __attribute_const__ btrfs_decode_error(int errno);
 
 __cold
 void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 3da35d8b21a3..b3e6d7aa3402 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -66,7 +66,7 @@  static struct file_system_type btrfs_root_fs_type;
 
 static int btrfs_remount(struct super_block *sb, int *flags, char *data);
 
-const char *btrfs_decode_error(int errno)
+const char * __attribute_const__ btrfs_decode_error(int errno)
 {
 	char *errstr = "unknown";
 
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 3fd89aee539d..c343b7cdfb53 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -297,7 +297,7 @@  static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
 
 DEFINE_MUTEX(uuid_mutex);
 static LIST_HEAD(fs_uuids);
-struct list_head *btrfs_get_fs_uuids(void)
+struct list_head * __attribute_const__ btrfs_get_fs_uuids(void)
 {
 	return &fs_uuids;
 }
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index a7da1f3e3627..0ae0677a8d86 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -571,7 +571,7 @@  static inline enum btrfs_raid_types btrfs_bg_flags_to_raid_index(u64 flags)
 
 void btrfs_commit_device_sizes(struct btrfs_transaction *trans);
 
-struct list_head *btrfs_get_fs_uuids(void);
+struct list_head * __attribute_const__ btrfs_get_fs_uuids(void);
 void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info);
 void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info);
 bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,