Provide conveniance wrappers around scoped user access similiar to
put/get_user(), which reduce the usage sites to:

       if (!get_user_scoped(val, ptr))
                return -EFAULT;

Signed-off-by: Thomas Gleixner <[email protected]>
---
V4: Rename to scoped
---
 include/linux/uaccess.h |   44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -825,6 +825,50 @@ for (bool done = false; !done; done = tr
 #define scoped_user_rw_access(uptr, elbl)                              \
        scoped_user_rw_access_size(uptr, sizeof(*(uptr)), elbl)
 
+/**
+ * get_user_scoped - Read user data with scoped access
+ * @val:       The variable to store the value read from user memory
+ * @usrc:      Pointer to the user space memory to read from
+ *
+ * Return: true if successful, false when faulted
+ */
+#define get_user_scoped(val, usrc)                             \
+({                                                             \
+       __label__ efault;                                       \
+       typeof(usrc) _tmpsrc = usrc;                            \
+       bool _ret = true;                                       \
+                                                               \
+       scoped_user_read_access(_tmpsrc, efault)                \
+               unsafe_get_user(val, _tmpsrc, efault);          \
+       if (0) {                                                \
+       efault:                                                 \
+               _ret = false;                                   \
+       }                                                       \
+       _ret;                                                   \
+})
+
+/**
+ * put_user_scoped - Write to user memory with scoped access
+ * @val:       The value to write
+ * @udst:      Pointer to the user space memory to write to
+ *
+ * Return: true if successful, false when faulted
+ */
+#define put_user_scoped(val, udst)                             \
+({                                                             \
+       __label__ efault;                                       \
+       typeof(udst) _tmpdst = udst;                            \
+       bool _ret = true;                                       \
+                                                               \
+       scoped_user_write_access(_tmpdst, efault)               \
+               unsafe_put_user(val, _tmpdst, efault);          \
+       if (0) {                                                \
+       efault:                                                 \
+               _ret = false;                                   \
+       }                                                       \
+       _ret;                                                   \
+})
+
 #ifdef CONFIG_HARDENED_USERCOPY
 void __noreturn usercopy_abort(const char *name, const char *detail,
                               bool to_user, unsigned long offset,


Reply via email to