Re: [PATCH] csky: uaccess.h: Coding convention with asm generic

From: Guo Ren
Date: Wed Apr 28 2021 - 09:03:24 EST


On Wed, Apr 28, 2021 at 5:26 PM Arnd Bergmann <arnd@xxxxxxxx> wrote:
>
> On Wed, Apr 28, 2021 at 10:30 AM Guo Ren <guoren@xxxxxxxxxx> wrote:
> > On Wed, Apr 28, 2021 at 11:18 AM Guenter Roeck <linux@xxxxxxxxxxxx> wrote:
> > >
> > > On Wed, Apr 21, 2021 at 08:54:15AM +0000, guoren@xxxxxxxxxx wrote:
> > > > From: Guo Ren <guoren@xxxxxxxxxxxxxxxxx>
> > > >
> > > > Using asm-generic/uaccess.h to prevent duplicated code:
> > > > - Add user_addr_max which mentioned in generic uaccess.h
> > > > - Remove custom definitions of KERNEL/USER_DS, get/set_fs,
> > > > uaccess_kerenl
> > > > - Using generic extable.h instead of custom definitions in
> > > > uaccess.h
> > > >
> > > > Signed-off-by: Guo Ren <guoren@xxxxxxxxxxxxxxxxx>
> > > > Cc: Arnd Bergmann <arnd@xxxxxxxx>
> > >
> > > Building csky:tinyconfig ... failed
> > > --------------
> > > Error log:
> > > csky-linux-ld: fs/readdir.o: in function `__put_user_fn':
> > > readdir.c:(.text+0x7c): undefined reference to `__put_user_bad'
> > > csky-linux-ld: fs/readdir.o: in function `$d':
> > > readdir.c:(.text+0x1bc): undefined reference to `__put_user_bad'
> > > make[1]: *** [Makefile:1277: vmlinux] Error 1
> > > make: *** [Makefile:222: __sub-make] Error 2
> > It's a bug, I can't put __put_user_bad in __put_user_fn, and
> > __put_user has done that:
> >
> > /*
> > * These are the main single-value transfer routines. They automatically
> > * use the right size if we just have the right pointer type.
> > * This version just falls back to copy_{from,to}_user, which should
> > * provide a fast-path for small values.
> > */
> > #define __put_user(x, ptr) \
> > ({ \
> > __typeof__(*(ptr)) __x = (x); \
> > int __pu_err = -EFAULT; \
> > __chk_user_ptr(ptr); \
> > switch (sizeof (*(ptr))) { \
> > case 1: \
> > case 2: \
> > case 4: \
> > case 8: \
> > __pu_err = __put_user_fn(sizeof (*(ptr)), \
> > ptr, &__x); \
> > break; \
> > default: \
> > __put_user_bad(); \
> > break; \
> > } \
> > __pu_err; \
> > })
>
> Actually, please don't use the asm-generic __put_user version based
> on copy_to_user, we probably have killed it off long ago.
>
> We might want to come up with a new version of asm-generic/uaccess.h
> that actually makes it easier to have a sane per-architecture
> implementation of the low-level accessors without set_fs().
>
> I've added Christoph to Cc here, he probably has some ideas
> on where we should be heading.
>
> One noteworthy aspect is that almost nothing users the low-level
> __get_user()/__put_user() helpers any more outside of architecture
> specific code, so we may not need to have separate versions
> for much longer.

Thx Arnd, here is my implementation:

#define __put_user_asm_64(x, ptr, err) \
do { \
int tmp; \
int errcode; \
\
__asm__ __volatile__( \
" ldw %3, (%1, 0) \n" \
"1: stw %3, (%2, 0) \n" \
" ldw %3, (%1, 4) \n" \
"2: stw %3, (%2, 4) \n" \
" br 4f \n" \
"3: mov %0, %4 \n" \
" br 4f \n" \
".section __ex_table, \"a\" \n" \
".align 2 \n" \
".long 1b, 3b \n" \
".long 2b, 3b \n" \
".previous \n" \
"4: \n" \
: "=r"(err), "=r"(x), "=r"(ptr), \
"=r"(tmp), "=r"(errcode) \
: "0"(err), "1"(x), "2"(ptr), "3"(0), \
"4"(-EFAULT) \
: "memory"); \
} while (0)

static inline int __put_user_fn(size_t size, void __user *ptr, void *x)
{
int retval = 0;
u32 tmp;
...
case 8:
__put_user_asm_64(x, (u64 *)ptr, retval);
break;
}

return retval;
}
#define __put_user_fn __put_user_fn

#define __get_user_asm_64(x, ptr, err) \
do { \
int tmp; \
int errcode; \
\
__asm__ __volatile__( \
"1: ldw %3, (%2, 0) \n" \
" stw %3, (%1, 0) \n" \
"2: ldw %3, (%2, 4) \n" \
" stw %3, (%1, 4) \n" \
" br 4f \n" \
"3: mov %0, %4 \n" \
" br 4f \n" \
".section __ex_table, \"a\" \n" \
".align 2 \n" \
".long 1b, 3b \n" \
".long 2b, 3b \n" \
".previous \n" \
"4: \n" \
: "=r"(err), "=r"(x), "=r"(ptr), \
"=r"(tmp), "=r"(errcode) \
: "0"(err), "1"(x), "2"(ptr), "3"(0), \
"4"(-EFAULT) \
: "memory"); \
} while (0)

static inline int __get_user_fn(size_t size, const void __user *ptr, void *x)
{
int retval;
u32 tmp;

...
case 8:
__get_user_asm_64(x, ptr, retval);
break;
}

return retval;
}
#define __get_user_fn __get_user_fn


--
Best Regards
Guo Ren

ML: https://lore.kernel.org/linux-csky/