2022-05-14 14:28:03

by Vasily Averin

[permalink] [raw]
Subject: [PATCH] sparse: use force attribute for fmode_t casts

Fixes sparce warnings:
fs/notify/fanotify/fanotify_user.c:267:63: sparse:
warning: restricted fmode_t degrades to integer
fs/notify/fanotify/fanotify_user.c:1351:28: sparse:
warning: restricted fmode_t degrades to integer
fs/proc/base.c:2240:25: sparse: warning: cast to restricted fmode_t
fs/proc/base.c:2297:42: sparse: warning: cast from restricted fmode_t
fs/proc/base.c:2394:48: sparse: warning: cast from restricted fmode_t
fs/open.c:1024:21: sparse: warning: restricted fmode_t degrades to integer

Signed-off-by: Vasily Averin <[email protected]>
---
fs/notify/fanotify/fanotify_user.c | 4 ++--
fs/open.c | 2 +-
fs/proc/base.c | 6 +++---
3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 9b32b76a9c30..6b058828e412 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -264,7 +264,7 @@ static int create_fd(struct fsnotify_group *group, struct path *path,
* originally opened O_WRONLY.
*/
new_file = dentry_open(path,
- group->fanotify_data.f_flags | FMODE_NONOTIFY,
+ group->fanotify_data.f_flags | (__force int)FMODE_NONOTIFY,
current_cred());
if (IS_ERR(new_file)) {
/*
@@ -1348,7 +1348,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
(!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))
return -EINVAL;

- f_flags = O_RDWR | FMODE_NONOTIFY;
+ f_flags = O_RDWR | (__force int)FMODE_NONOTIFY;
if (flags & FAN_CLOEXEC)
f_flags |= O_CLOEXEC;
if (flags & FAN_NONBLOCK)
diff --git a/fs/open.c b/fs/open.c
index 1315253e0247..b5ff39ccebfd 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -1021,7 +1021,7 @@ inline struct open_how build_open_how(int flags, umode_t mode)
inline int build_open_flags(const struct open_how *how, struct open_flags *op)
{
u64 flags = how->flags;
- u64 strip = FMODE_NONOTIFY | O_CLOEXEC;
+ u64 strip = (__force u64)FMODE_NONOTIFY | O_CLOEXEC;
int lookup_flags = 0;
int acc_mode = ACC_MODE(flags);

diff --git a/fs/proc/base.c b/fs/proc/base.c
index c1031843cc6a..194b5ac069e7 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -2237,7 +2237,7 @@ static struct dentry *
proc_map_files_instantiate(struct dentry *dentry,
struct task_struct *task, const void *ptr)
{
- fmode_t mode = (fmode_t)(unsigned long)ptr;
+ fmode_t mode = (__force fmode_t)(unsigned long)ptr;
struct proc_inode *ei;
struct inode *inode;

@@ -2294,7 +2294,7 @@ static struct dentry *proc_map_files_lookup(struct inode *dir,

if (vma->vm_file)
result = proc_map_files_instantiate(dentry, task,
- (void *)(unsigned long)vma->vm_file->f_mode);
+ (void *)(__force unsigned long)vma->vm_file->f_mode);

out_no_vma:
mmap_read_unlock(mm);
@@ -2391,7 +2391,7 @@ proc_map_files_readdir(struct file *file, struct dir_context *ctx)
buf, len,
proc_map_files_instantiate,
task,
- (void *)(unsigned long)p->mode))
+ (void *)(__force unsigned long)p->mode))
break;
ctx->pos++;
}
--
2.31.1



2022-05-16 19:38:56

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH] sparse: use force attribute for fmode_t casts

On Sat 14-05-22 13:22:28, Vasily Averin wrote:
> Fixes sparce warnings:
> fs/notify/fanotify/fanotify_user.c:267:63: sparse:
> warning: restricted fmode_t degrades to integer
> fs/notify/fanotify/fanotify_user.c:1351:28: sparse:
> warning: restricted fmode_t degrades to integer
> fs/proc/base.c:2240:25: sparse: warning: cast to restricted fmode_t
> fs/proc/base.c:2297:42: sparse: warning: cast from restricted fmode_t
> fs/proc/base.c:2394:48: sparse: warning: cast from restricted fmode_t
> fs/open.c:1024:21: sparse: warning: restricted fmode_t degrades to integer
>
> Signed-off-by: Vasily Averin <[email protected]>

Looks good. Feel free to add:

Acked-by: Jan Kara <[email protected]>

for the fsnotify related bits.

Honza

> ---
> fs/notify/fanotify/fanotify_user.c | 4 ++--
> fs/open.c | 2 +-
> fs/proc/base.c | 6 +++---
> 3 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
> index 9b32b76a9c30..6b058828e412 100644
> --- a/fs/notify/fanotify/fanotify_user.c
> +++ b/fs/notify/fanotify/fanotify_user.c
> @@ -264,7 +264,7 @@ static int create_fd(struct fsnotify_group *group, struct path *path,
> * originally opened O_WRONLY.
> */
> new_file = dentry_open(path,
> - group->fanotify_data.f_flags | FMODE_NONOTIFY,
> + group->fanotify_data.f_flags | (__force int)FMODE_NONOTIFY,
> current_cred());
> if (IS_ERR(new_file)) {
> /*
> @@ -1348,7 +1348,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
> (!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))
> return -EINVAL;
>
> - f_flags = O_RDWR | FMODE_NONOTIFY;
> + f_flags = O_RDWR | (__force int)FMODE_NONOTIFY;
> if (flags & FAN_CLOEXEC)
> f_flags |= O_CLOEXEC;
> if (flags & FAN_NONBLOCK)
> diff --git a/fs/open.c b/fs/open.c
> index 1315253e0247..b5ff39ccebfd 100644
> --- a/fs/open.c
> +++ b/fs/open.c
> @@ -1021,7 +1021,7 @@ inline struct open_how build_open_how(int flags, umode_t mode)
> inline int build_open_flags(const struct open_how *how, struct open_flags *op)
> {
> u64 flags = how->flags;
> - u64 strip = FMODE_NONOTIFY | O_CLOEXEC;
> + u64 strip = (__force u64)FMODE_NONOTIFY | O_CLOEXEC;
> int lookup_flags = 0;
> int acc_mode = ACC_MODE(flags);
>
> diff --git a/fs/proc/base.c b/fs/proc/base.c
> index c1031843cc6a..194b5ac069e7 100644
> --- a/fs/proc/base.c
> +++ b/fs/proc/base.c
> @@ -2237,7 +2237,7 @@ static struct dentry *
> proc_map_files_instantiate(struct dentry *dentry,
> struct task_struct *task, const void *ptr)
> {
> - fmode_t mode = (fmode_t)(unsigned long)ptr;
> + fmode_t mode = (__force fmode_t)(unsigned long)ptr;
> struct proc_inode *ei;
> struct inode *inode;
>
> @@ -2294,7 +2294,7 @@ static struct dentry *proc_map_files_lookup(struct inode *dir,
>
> if (vma->vm_file)
> result = proc_map_files_instantiate(dentry, task,
> - (void *)(unsigned long)vma->vm_file->f_mode);
> + (void *)(__force unsigned long)vma->vm_file->f_mode);
>
> out_no_vma:
> mmap_read_unlock(mm);
> @@ -2391,7 +2391,7 @@ proc_map_files_readdir(struct file *file, struct dir_context *ctx)
> buf, len,
> proc_map_files_instantiate,
> task,
> - (void *)(unsigned long)p->mode))
> + (void *)(__force unsigned long)p->mode))
> break;
> ctx->pos++;
> }
> --
> 2.31.1
>
--
Jan Kara <[email protected]>
SUSE Labs, CR

2022-05-17 01:33:37

by Christian Brauner

[permalink] [raw]
Subject: Re: [PATCH] sparse: use force attribute for fmode_t casts

On Sat, May 14, 2022 at 01:22:28PM +0300, Vasily Averin wrote:
> Fixes sparce warnings:
> fs/notify/fanotify/fanotify_user.c:267:63: sparse:
> warning: restricted fmode_t degrades to integer
> fs/notify/fanotify/fanotify_user.c:1351:28: sparse:
> warning: restricted fmode_t degrades to integer
> fs/proc/base.c:2240:25: sparse: warning: cast to restricted fmode_t
> fs/proc/base.c:2297:42: sparse: warning: cast from restricted fmode_t
> fs/proc/base.c:2394:48: sparse: warning: cast from restricted fmode_t
> fs/open.c:1024:21: sparse: warning: restricted fmode_t degrades to integer
>
> Signed-off-by: Vasily Averin <[email protected]>
> ---
> fs/notify/fanotify/fanotify_user.c | 4 ++--
> fs/open.c | 2 +-

Looks good to me,
Reviewed-by: Christian Brauner (Microsoft) <[email protected]>

2022-05-18 04:45:41

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH] sparse: use force attribute for fmode_t casts

Please just use __FMODE_NONOTIFY instead, which might be a bit misnamed,
but was added exactly for this kind of use.

And remember once more: __force casts are a massive code smell. Never
add them before thinking hard about them and adding a comment explaining
why they can't be avoided, preferably in a dedicated helper.

2022-05-18 10:57:40

by Vasily Averin

[permalink] [raw]
Subject: [PATCH v2] sparse: fix incorrect fmode_t casts

Fixes sparce warnings:
fs/notify/fanotify/fanotify_user.c:267:63: sparse:
warning: restricted fmode_t degrades to integer
fs/notify/fanotify/fanotify_user.c:1351:28: sparse:
warning: restricted fmode_t degrades to integer
fs/proc/base.c:2240:25: sparse: warning: cast to restricted fmode_t
fs/proc/base.c:2297:42: sparse: warning: cast from restricted fmode_t
fs/proc/base.c:2394:48: sparse: warning: cast from restricted fmode_t
fs/open.c:1024:21: sparse: warning: restricted fmode_t degrades to integer

Signed-off-by: Vasily Averin <[email protected]>
---
v2:
1) use __FMODE_NONOTIFY instead of FMODE_NONOTIFY
2) introduced fmode_instantiate_de/encode helpers
thanks Christoph Hellwig for the hints
---
fs/notify/fanotify/fanotify_user.c | 4 ++--
fs/open.c | 2 +-
fs/proc/base.c | 21 ++++++++++++++++-----
3 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 9b32b76a9c30..2bec3b612618 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -264,7 +264,7 @@ static int create_fd(struct fsnotify_group *group, struct path *path,
* originally opened O_WRONLY.
*/
new_file = dentry_open(path,
- group->fanotify_data.f_flags | FMODE_NONOTIFY,
+ group->fanotify_data.f_flags | __FMODE_NONOTIFY,
current_cred());
if (IS_ERR(new_file)) {
/*
@@ -1348,7 +1348,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
(!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))
return -EINVAL;

- f_flags = O_RDWR | FMODE_NONOTIFY;
+ f_flags = O_RDWR | __FMODE_NONOTIFY;
if (flags & FAN_CLOEXEC)
f_flags |= O_CLOEXEC;
if (flags & FAN_NONBLOCK)
diff --git a/fs/open.c b/fs/open.c
index 1315253e0247..386c52e4c3b1 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -1021,7 +1021,7 @@ inline struct open_how build_open_how(int flags, umode_t mode)
inline int build_open_flags(const struct open_how *how, struct open_flags *op)
{
u64 flags = how->flags;
- u64 strip = FMODE_NONOTIFY | O_CLOEXEC;
+ u64 strip = __FMODE_NONOTIFY | O_CLOEXEC;
int lookup_flags = 0;
int acc_mode = ACC_MODE(flags);

diff --git a/fs/proc/base.c b/fs/proc/base.c
index c1031843cc6a..b8ed41eb5784 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -2233,11 +2233,21 @@ static const struct inode_operations proc_map_files_link_inode_operations = {
.setattr = proc_setattr,
};

+static fmode_t fmode_instantiate_decode(const void *ptr)
+{
+ return (__force fmode_t)(unsigned long)ptr;
+}
+
+static void *fmode_instantiate_encode(fmode_t fmode)
+{
+ return (void *)(__force unsigned long)fmode;
+}
+
static struct dentry *
proc_map_files_instantiate(struct dentry *dentry,
struct task_struct *task, const void *ptr)
{
- fmode_t mode = (fmode_t)(unsigned long)ptr;
+ fmode_t mode = fmode_instantiate_decode(ptr);
struct proc_inode *ei;
struct inode *inode;

@@ -2292,10 +2302,11 @@ static struct dentry *proc_map_files_lookup(struct inode *dir,
if (!vma)
goto out_no_vma;

- if (vma->vm_file)
- result = proc_map_files_instantiate(dentry, task,
- (void *)(unsigned long)vma->vm_file->f_mode);
+ if (vma->vm_file) {
+ void *ptr = fmode_instantiate_encode(vma->vm_file->f_mode);

+ result = proc_map_files_instantiate(dentry, task, ptr);
+ }
out_no_vma:
mmap_read_unlock(mm);
out_put_mm:
@@ -2391,7 +2402,7 @@ proc_map_files_readdir(struct file *file, struct dir_context *ctx)
buf, len,
proc_map_files_instantiate,
task,
- (void *)(unsigned long)p->mode))
+ (void *)(__force unsigned long)p->mode))
break;
ctx->pos++;
}
--
2.31.1


2022-05-18 11:12:21

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH v2] sparse: fix incorrect fmode_t casts

On Wed 18-05-22 13:49:07, Vasily Averin wrote:
> Fixes sparce warnings:
> fs/notify/fanotify/fanotify_user.c:267:63: sparse:
> warning: restricted fmode_t degrades to integer
> fs/notify/fanotify/fanotify_user.c:1351:28: sparse:
> warning: restricted fmode_t degrades to integer
> fs/proc/base.c:2240:25: sparse: warning: cast to restricted fmode_t
> fs/proc/base.c:2297:42: sparse: warning: cast from restricted fmode_t
> fs/proc/base.c:2394:48: sparse: warning: cast from restricted fmode_t
> fs/open.c:1024:21: sparse: warning: restricted fmode_t degrades to integer
>
> Signed-off-by: Vasily Averin <[email protected]>

Feel free to add:

Acked-by: Jan Kara <[email protected]>

for the fanotify bits.

Honza

> ---
> v2:
> 1) use __FMODE_NONOTIFY instead of FMODE_NONOTIFY
> 2) introduced fmode_instantiate_de/encode helpers
> thanks Christoph Hellwig for the hints
> ---
> fs/notify/fanotify/fanotify_user.c | 4 ++--
> fs/open.c | 2 +-
> fs/proc/base.c | 21 ++++++++++++++++-----
> 3 files changed, 19 insertions(+), 8 deletions(-)
>
> diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
> index 9b32b76a9c30..2bec3b612618 100644
> --- a/fs/notify/fanotify/fanotify_user.c
> +++ b/fs/notify/fanotify/fanotify_user.c
> @@ -264,7 +264,7 @@ static int create_fd(struct fsnotify_group *group, struct path *path,
> * originally opened O_WRONLY.
> */
> new_file = dentry_open(path,
> - group->fanotify_data.f_flags | FMODE_NONOTIFY,
> + group->fanotify_data.f_flags | __FMODE_NONOTIFY,
> current_cred());
> if (IS_ERR(new_file)) {
> /*
> @@ -1348,7 +1348,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
> (!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))
> return -EINVAL;
>
> - f_flags = O_RDWR | FMODE_NONOTIFY;
> + f_flags = O_RDWR | __FMODE_NONOTIFY;
> if (flags & FAN_CLOEXEC)
> f_flags |= O_CLOEXEC;
> if (flags & FAN_NONBLOCK)
> diff --git a/fs/open.c b/fs/open.c
> index 1315253e0247..386c52e4c3b1 100644
> --- a/fs/open.c
> +++ b/fs/open.c
> @@ -1021,7 +1021,7 @@ inline struct open_how build_open_how(int flags, umode_t mode)
> inline int build_open_flags(const struct open_how *how, struct open_flags *op)
> {
> u64 flags = how->flags;
> - u64 strip = FMODE_NONOTIFY | O_CLOEXEC;
> + u64 strip = __FMODE_NONOTIFY | O_CLOEXEC;
> int lookup_flags = 0;
> int acc_mode = ACC_MODE(flags);
>
> diff --git a/fs/proc/base.c b/fs/proc/base.c
> index c1031843cc6a..b8ed41eb5784 100644
> --- a/fs/proc/base.c
> +++ b/fs/proc/base.c
> @@ -2233,11 +2233,21 @@ static const struct inode_operations proc_map_files_link_inode_operations = {
> .setattr = proc_setattr,
> };
>
> +static fmode_t fmode_instantiate_decode(const void *ptr)
> +{
> + return (__force fmode_t)(unsigned long)ptr;
> +}
> +
> +static void *fmode_instantiate_encode(fmode_t fmode)
> +{
> + return (void *)(__force unsigned long)fmode;
> +}
> +
> static struct dentry *
> proc_map_files_instantiate(struct dentry *dentry,
> struct task_struct *task, const void *ptr)
> {
> - fmode_t mode = (fmode_t)(unsigned long)ptr;
> + fmode_t mode = fmode_instantiate_decode(ptr);
> struct proc_inode *ei;
> struct inode *inode;
>
> @@ -2292,10 +2302,11 @@ static struct dentry *proc_map_files_lookup(struct inode *dir,
> if (!vma)
> goto out_no_vma;
>
> - if (vma->vm_file)
> - result = proc_map_files_instantiate(dentry, task,
> - (void *)(unsigned long)vma->vm_file->f_mode);
> + if (vma->vm_file) {
> + void *ptr = fmode_instantiate_encode(vma->vm_file->f_mode);
>
> + result = proc_map_files_instantiate(dentry, task, ptr);
> + }
> out_no_vma:
> mmap_read_unlock(mm);
> out_put_mm:
> @@ -2391,7 +2402,7 @@ proc_map_files_readdir(struct file *file, struct dir_context *ctx)
> buf, len,
> proc_map_files_instantiate,
> task,
> - (void *)(unsigned long)p->mode))
> + (void *)(__force unsigned long)p->mode))
> break;
> ctx->pos++;
> }
> --
> 2.31.1
>
--
Jan Kara <[email protected]>
SUSE Labs, CR

2022-05-18 13:25:43

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH v2] sparse: fix incorrect fmode_t casts

The fanotify and open.c changes looks fine, but should be split
into one each with a proper subject and description.

For procfs something simpler like the patch below might be a better
option:

diff --git a/fs/proc/base.c b/fs/proc/base.c
index c1031843cc6aa..c07c6b51f0cb9 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -2237,13 +2237,13 @@ static struct dentry *
proc_map_files_instantiate(struct dentry *dentry,
struct task_struct *task, const void *ptr)
{
- fmode_t mode = (fmode_t)(unsigned long)ptr;
+ const fmode_t *mode = ptr;
struct proc_inode *ei;
struct inode *inode;

inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
- ((mode & FMODE_READ ) ? S_IRUSR : 0) |
- ((mode & FMODE_WRITE) ? S_IWUSR : 0));
+ ((*mode & FMODE_READ ) ? S_IRUSR : 0) |
+ ((*mode & FMODE_WRITE) ? S_IWUSR : 0));
if (!inode)
return ERR_PTR(-ENOENT);

@@ -2294,7 +2294,7 @@ static struct dentry *proc_map_files_lookup(struct inode *dir,

if (vma->vm_file)
result = proc_map_files_instantiate(dentry, task,
- (void *)(unsigned long)vma->vm_file->f_mode);
+ &vma->vm_file->f_mode);

out_no_vma:
mmap_read_unlock(mm);
@@ -2391,7 +2391,7 @@ proc_map_files_readdir(struct file *file, struct dir_context *ctx)
buf, len,
proc_map_files_instantiate,
task,
- (void *)(unsigned long)p->mode))
+ &p->mode))
break;
ctx->pos++;
}

2022-05-18 14:45:11

by Christian Brauner

[permalink] [raw]
Subject: Re: [PATCH v2] sparse: fix incorrect fmode_t casts

On Wed, May 18, 2022 at 01:49:07PM +0300, Vasily Averin wrote:
> Fixes sparce warnings:
> fs/notify/fanotify/fanotify_user.c:267:63: sparse:
> warning: restricted fmode_t degrades to integer
> fs/notify/fanotify/fanotify_user.c:1351:28: sparse:
> warning: restricted fmode_t degrades to integer
> fs/proc/base.c:2240:25: sparse: warning: cast to restricted fmode_t
> fs/proc/base.c:2297:42: sparse: warning: cast from restricted fmode_t
> fs/proc/base.c:2394:48: sparse: warning: cast from restricted fmode_t
> fs/open.c:1024:21: sparse: warning: restricted fmode_t degrades to integer
>
> Signed-off-by: Vasily Averin <[email protected]>
> ---

Looks good to me,
Reviewed-by: Christian Brauner (Microsoft) <[email protected]>