diff mbox series

[v6,05/34] iov_iter: Change the direction macros into an enum

Message ID 167391051810.2311931.8545361041888737395.stgit@warthog.procyon.org.uk (mailing list archive)
State New, archived
Headers show
Series iov_iter: Improve page extraction (ref, pin or just list) | expand

Commit Message

David Howells Jan. 16, 2023, 11:08 p.m. UTC
Change the ITER_SOURCE and ITER_DEST direction macros into an enum and
provide three new helper functions:

 iov_iter_dir() - returns the iterator direction
 iov_iter_is_dest() - returns true if it's an ITER_DEST iterator
 iov_iter_is_source() - returns true if it's an ITER_SOURCE iterator

Signed-off-by: David Howells <dhowells@redhat.com>
cc: Al Viro <viro@zeniv.linux.org.uk>

Link: https://lore.kernel.org/r/167305161763.1521586.6593798818336440133.stgit@warthog.procyon.org.uk/ # v4
Link: https://lore.kernel.org/r/167344726413.2425628.317218805692680763.stgit@warthog.procyon.org.uk/ # v5
---

 include/linux/uio.h |   30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

Comments

Al Viro Jan. 18, 2023, 11:14 p.m. UTC | #1
On Mon, Jan 16, 2023 at 11:08:38PM +0000, David Howells wrote:
> Change the ITER_SOURCE and ITER_DEST direction macros into an enum and
> provide three new helper functions:
> 
>  iov_iter_dir() - returns the iterator direction
>  iov_iter_is_dest() - returns true if it's an ITER_DEST iterator
>  iov_iter_is_source() - returns true if it's an ITER_SOURCE iterator

What for?  We have two valid values -
	1) it is a data source
	2) it is not a data source
Why do we need to store that as an enum?
David Howells Jan. 18, 2023, 11:17 p.m. UTC | #2
Al Viro <viro@zeniv.linux.org.uk> wrote:

> > Change the ITER_SOURCE and ITER_DEST direction macros into an enum and
> > provide three new helper functions:
> > 
> >  iov_iter_dir() - returns the iterator direction
> >  iov_iter_is_dest() - returns true if it's an ITER_DEST iterator
> >  iov_iter_is_source() - returns true if it's an ITER_SOURCE iterator
> 
> What for?  We have two valid values -
> 	1) it is a data source
> 	2) it is not a data source
> Why do we need to store that as an enum?

Compile time type checking.

David
Al Viro Jan. 18, 2023, 11:19 p.m. UTC | #3
On Wed, Jan 18, 2023 at 11:17:41PM +0000, David Howells wrote:
> Al Viro <viro@zeniv.linux.org.uk> wrote:
> 
> > > Change the ITER_SOURCE and ITER_DEST direction macros into an enum and
> > > provide three new helper functions:
> > > 
> > >  iov_iter_dir() - returns the iterator direction
> > >  iov_iter_is_dest() - returns true if it's an ITER_DEST iterator
> > >  iov_iter_is_source() - returns true if it's an ITER_SOURCE iterator
> > 
> > What for?  We have two valid values -
> > 	1) it is a data source
> > 	2) it is not a data source
> > Why do we need to store that as an enum?
> 
> Compile time type checking.

Huh?  int-to-enum conversion is quiet; it would catch explicit
huge constants, but that's it...
David Howells Jan. 18, 2023, 11:24 p.m. UTC | #4
Al Viro <viro@zeniv.linux.org.uk> wrote:

> > Compile time type checking.
> 
> Huh?  int-to-enum conversion is quiet; it would catch explicit
> huge constants, but that's it...

*shrug*.

But can we at least get rid of the:

	iov_iter_foo(&iter, ITER_SOURCE, ...);

	WARN_ON(direction & ~(READ | WRITE));

mismatch?  Either use ITER_SOURCE/DEST or use READ/WRITE but not mix them.

David
diff mbox series

Patch

diff --git a/include/linux/uio.h b/include/linux/uio.h
index 365e26c405f2..8d0dabfcb2fe 100644
--- a/include/linux/uio.h
+++ b/include/linux/uio.h
@@ -29,8 +29,10 @@  enum iter_type {
 	ITER_UBUF,
 };
 
-#define ITER_SOURCE	1	// == WRITE
-#define ITER_DEST	0	// == READ
+enum iter_dir {
+	ITER_DEST	= 0,	// == READ
+	ITER_SOURCE	= 1,	// == WRITE
+} __mode(byte);
 
 struct iov_iter_state {
 	size_t iov_offset;
@@ -39,9 +41,9 @@  struct iov_iter_state {
 };
 
 struct iov_iter {
-	u8 iter_type;
+	enum iter_type iter_type __mode(byte);
 	bool nofault;
-	bool data_source;
+	enum iter_dir data_source;
 	bool user_backed;
 	union {
 		size_t iov_offset;
@@ -114,6 +116,26 @@  static inline bool iov_iter_is_xarray(const struct iov_iter *i)
 	return iov_iter_type(i) == ITER_XARRAY;
 }
 
+static inline enum iter_dir iov_iter_dir(const struct iov_iter *i)
+{
+	return i->data_source;
+}
+
+static inline bool iov_iter_is_source(const struct iov_iter *i)
+{
+	return iov_iter_dir(i) == ITER_SOURCE; /* ie. WRITE */
+}
+
+static inline bool iov_iter_is_dest(const struct iov_iter *i)
+{
+	return iov_iter_dir(i) == ITER_DEST; /* ie. READ */
+}
+
+static inline bool iov_iter_dir_valid(enum iter_dir direction)
+{
+	return direction == ITER_DEST || direction == ITER_SOURCE;
+}
+
 static inline bool user_backed_iter(const struct iov_iter *i)
 {
 	return i->user_backed;