From: Nikolay Borisov <nborisov@suse.com>
To: David Sterba <dsterba@suse.com>, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 2/3] btrfs: add const function attribute
Date: Wed, 2 Oct 2019 14:07:50 +0300 [thread overview]
Message-ID: <625281f3-90fc-56d8-22e9-76557bb3d410@suse.com> (raw)
In-Reply-To: <543f96a0b47e4856e6adbf3761a56df96480f358.1569587835.git.dsterba@suse.com>
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,
>
next prev parent reply other threads:[~2019-10-02 11:07 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-01 17:57 [PATCH 0/3] Coldify, constify, purify (function attributes) David Sterba
2019-10-01 17:57 ` [PATCH 1/3] btrfs: add __cold attribute to more functions David Sterba
2019-10-02 10:52 ` Nikolay Borisov
2019-10-04 10:56 ` David Sterba
2019-10-01 17:57 ` [PATCH 2/3] btrfs: add const function attribute David Sterba
2019-10-02 11:07 ` Nikolay Borisov [this message]
2019-10-04 11:01 ` David Sterba
2019-10-01 17:57 ` [PATCH 3/3] btrfs: add __pure attribute to functions David Sterba
2019-10-02 11:09 ` Nikolay Borisov
2019-10-01 17:57 ` [PATCH 0/3] Coldify, constify, purify (function attributes) David Sterba
2019-10-02 12:20 ` Nikolay Borisov
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=625281f3-90fc-56d8-22e9-76557bb3d410@suse.com \
--to=nborisov@suse.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