From: Peter Zijlstra <peterz@infradead.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: keescook@chromium.org, gregkh@linuxfoundation.org,
pbonzini@redhat.com, linux-kernel@vger.kernel.org,
ojeda@kernel.org, ndesaulniers@google.com, mingo@redhat.com,
will@kernel.org, longman@redhat.com, boqun.feng@gmail.com,
juri.lelli@redhat.com, vincent.guittot@linaro.org,
dietmar.eggemann@arm.com, rostedt@goodmis.org,
bsegall@google.com, mgorman@suse.de, bristot@redhat.com,
vschneid@redhat.com, paulmck@kernel.org, frederic@kernel.org,
quic_neeraju@quicinc.com, joel@joelfernandes.org,
josh@joshtriplett.org, mathieu.desnoyers@efficios.com,
jiangshanlai@gmail.com, rcu@vger.kernel.org, tj@kernel.org,
tglx@linutronix.de
Subject: Re: [PATCH v2 1/2] locking: Introduce __cleanup__ based guards
Date: Sat, 27 May 2023 10:57:49 +0200 [thread overview]
Message-ID: <20230527085749.GA35187@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <CAHk-=wgt=cq_fU33DCv0=xD30sH8eOGHsEQRJQi=2vtrWt7oPQ@mail.gmail.com>
On Fri, May 26, 2023 at 02:54:40PM -0700, Linus Torvalds wrote:
> What's wrong with just writing it out:
>
> typedef struct fd guard_fdget_type_t;
> static inline struct fd guard_fdget_init(int fd)
> { return fdget(fd); }
> static inline void guard_fdget_exit(struct fd fd)
> { fdput(fd); }
>
(wrong guard type, ptr_guard vs lock_guard) but yeah, I had this same
realization during breakfast. Clearly the brain had already left last
night.
Specifically, I think we want ptr_guard() here (and possibly fdnull for
__to_fd(0)) for things like do_sendfile() where a struct fd is
initialized late.
The below seems to compile...
--- a/include/linux/file.h
+++ b/include/linux/file.h
@@ -10,6 +10,7 @@
#include <linux/types.h>
#include <linux/posix_types.h>
#include <linux/errno.h>
+#include <linux/guards.h>
struct file;
@@ -45,6 +46,13 @@ static inline void fdput(struct fd fd)
fput(fd.file);
}
+typedef struct fd ptr_guard_fdput_t;
+
+static inline void ptr_guard_fdput_cleanup(struct fd *fdp)
+{
+ fdput(*fdp);
+}
+
extern struct file *fget(unsigned int fd);
extern struct file *fget_raw(unsigned int fd);
extern struct file *fget_task(struct task_struct *task, unsigned int fd);
@@ -58,6 +66,8 @@ static inline struct fd __to_fd(unsigned
return (struct fd){(struct file *)(v & ~3),v & 3};
}
+#define fdnull __to_fd(0)
+
static inline struct fd fdget(unsigned int fd)
{
return __to_fd(__fdget(fd));
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -1180,7 +1180,8 @@ COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
size_t count, loff_t max)
{
- struct fd in, out;
+ ptr_guard(fdput, in) = fdnull;
+ ptr_guard(fdput, out) = fdnull;
struct inode *in_inode, *out_inode;
struct pipe_inode_info *opipe;
loff_t pos;
@@ -1191,35 +1192,35 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
/*
* Get input file, and verify that it is ok..
*/
- retval = -EBADF;
in = fdget(in_fd);
if (!in.file)
- goto out;
+ return -EBADF;
if (!(in.file->f_mode & FMODE_READ))
- goto fput_in;
- retval = -ESPIPE;
+ return -EBADF;
+
if (!ppos) {
pos = in.file->f_pos;
} else {
pos = *ppos;
if (!(in.file->f_mode & FMODE_PREAD))
- goto fput_in;
+ return -ESPIPE;
}
+
retval = rw_verify_area(READ, in.file, &pos, count);
if (retval < 0)
- goto fput_in;
+ return retval;
if (count > MAX_RW_COUNT)
count = MAX_RW_COUNT;
/*
* Get output file, and verify that it is ok..
*/
- retval = -EBADF;
out = fdget(out_fd);
if (!out.file)
- goto fput_in;
+ return -EBADF;
if (!(out.file->f_mode & FMODE_WRITE))
- goto fput_out;
+ return -EBADF;
+
in_inode = file_inode(in.file);
out_inode = file_inode(out.file);
out_pos = out.file->f_pos;
@@ -1228,9 +1229,8 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
if (unlikely(pos + count > max)) {
- retval = -EOVERFLOW;
if (pos >= max)
- goto fput_out;
+ return -EOVERFLOW;
count = max - pos;
}
@@ -1249,7 +1249,7 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
if (!opipe) {
retval = rw_verify_area(WRITE, out.file, &out_pos, count);
if (retval < 0)
- goto fput_out;
+ return retval;
file_start_write(out.file);
retval = do_splice_direct(in.file, &pos, out.file, &out_pos,
count, fl);
@@ -1278,11 +1278,6 @@ static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
if (pos > max)
retval = -EOVERFLOW;
-fput_out:
- fdput(out);
-fput_in:
- fdput(in);
-out:
return retval;
}
next prev parent reply other threads:[~2023-05-27 8:58 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-26 20:52 [PATCH v2 0/2] Lock and Pointer guards Peter Zijlstra
2023-05-26 20:52 ` [PATCH v2 1/2] locking: Introduce __cleanup__ based guards Peter Zijlstra
2023-05-26 21:24 ` Peter Zijlstra
2023-05-26 21:54 ` Linus Torvalds
2023-05-27 8:57 ` Peter Zijlstra [this message]
2023-05-26 20:52 ` [PATCH v2 2/2] sched: Use fancy new guards Peter Zijlstra
2023-05-27 17:21 ` [PATCH v2 0/2] Lock and Pointer guards Mathieu Desnoyers
2023-05-27 19:18 ` Linus Torvalds
2023-05-29 12:09 ` Paolo Bonzini
2023-05-29 19:04 ` Linus Torvalds
2023-05-29 21:27 ` Ian Lance Taylor
2023-05-30 0:06 ` Linus Torvalds
2023-05-30 9:23 ` Peter Zijlstra
2023-05-30 9:34 ` Peter Zijlstra
2023-05-30 13:58 ` Valentin Schneider
2023-06-06 9:42 ` Peter Zijlstra
2023-06-06 13:17 ` Linus Torvalds
2023-06-06 13:40 ` Peter Zijlstra
2023-06-06 14:50 ` Linus Torvalds
2023-06-06 16:06 ` Kees Cook
2023-06-06 18:08 ` Peter Zijlstra
2023-06-06 23:22 ` Linus Torvalds
2023-06-07 9:41 ` Peter Zijlstra
2023-06-08 8:52 ` Peter Zijlstra
2023-06-08 9:04 ` Greg KH
2023-06-08 15:45 ` Linus Torvalds
2023-06-08 16:47 ` Kees Cook
2023-06-08 16:59 ` Linus Torvalds
2023-06-08 17:20 ` Nick Desaulniers
2023-06-08 18:51 ` Peter Zijlstra
2023-06-08 20:14 ` Nick Desaulniers
2023-06-09 10:20 ` Paolo Bonzini
2023-06-08 20:06 ` Peter Zijlstra
2023-06-09 2:25 ` Linus Torvalds
2023-06-09 8:14 ` Peter Zijlstra
2023-06-09 21:18 ` Kees Cook
2023-06-09 8:27 ` Rasmus Villemoes
2023-06-06 15:31 ` Kees Cook
2023-06-06 15:45 ` Linus Torvalds
2023-06-06 16:08 ` Kees Cook
2023-06-08 16:25 ` David Laight
2023-05-30 9:26 ` Peter Zijlstra
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=20230527085749.GA35187@hirez.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=boqun.feng@gmail.com \
--cc=bristot@redhat.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=frederic@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=jiangshanlai@gmail.com \
--cc=joel@joelfernandes.org \
--cc=josh@joshtriplett.org \
--cc=juri.lelli@redhat.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=mathieu.desnoyers@efficios.com \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=ndesaulniers@google.com \
--cc=ojeda@kernel.org \
--cc=paulmck@kernel.org \
--cc=pbonzini@redhat.com \
--cc=quic_neeraju@quicinc.com \
--cc=rcu@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=tglx@linutronix.de \
--cc=tj@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=will@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