linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vasily Averin <vvs@openvz.org>
To: Christoph Hellwig <hch@infradead.org>, Jan Kara <jack@suse.cz>,
	Christian Brauner <brauner@kernel.org>
Cc: kernel@openvz.org, linux-kernel@vger.kernel.org,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Amir Goldstein <amir73il@gmail.com>,
	Matthew Bobrowski <repnop@google.com>,
	linux-fsdevel@vger.kernel.org
Subject: [PATCH v2] sparse: fix incorrect fmode_t casts
Date: Wed, 18 May 2022 13:49:07 +0300	[thread overview]
Message-ID: <86e82d40-0952-e76f-aac5-53abe48ec770@openvz.org> (raw)
In-Reply-To: <YoNDA0SOFjyoFlJS@infradead.org>

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 <vvs@openvz.org>
---
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


  reply	other threads:[~2022-05-18 10:49 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-14 10:22 [PATCH] sparse: use force attribute for fmode_t casts Vasily Averin
2022-05-16 11:18 ` Jan Kara
2022-05-16 11:47 ` Christian Brauner
2022-05-17  6:38 ` Christoph Hellwig
2022-05-18 10:49   ` Vasily Averin [this message]
2022-05-18 11:12     ` [PATCH v2] sparse: fix incorrect " Jan Kara
2022-05-18 13:22     ` Christoph Hellwig
2022-05-18 14:44     ` Christian Brauner

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=86e82d40-0952-e76f-aac5-53abe48ec770@openvz.org \
    --to=vvs@openvz.org \
    --cc=amir73il@gmail.com \
    --cc=brauner@kernel.org \
    --cc=hch@infradead.org \
    --cc=jack@suse.cz \
    --cc=kernel@openvz.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=repnop@google.com \
    --cc=viro@zeniv.linux.org.uk \
    /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;
as well as URLs for NNTP newsgroup(s).