linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Guo Ren <guoren@kernel.org>
To: Arnd Bergmann <arnd@arndb.de>
Cc: Guenter Roeck <linux@roeck-us.net>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-csky@vger.kernel.org,
	linux-arch <linux-arch@vger.kernel.org>,
	Guo Ren <guoren@linux.alibaba.com>,
	Christoph Hellwig <hch@infradead.org>
Subject: Re: [PATCH] csky: uaccess.h: Coding convention with asm generic
Date: Wed, 28 Apr 2021 21:03:03 +0800	[thread overview]
Message-ID: <CAJF2gTSPs20OLQfx=FUnbOfcHDMeKY_i7pjS2JkeNyPstBttFQ@mail.gmail.com> (raw)
In-Reply-To: <CAK8P3a1DvsXSEDoovLk11hzNHyJi7vqNoToU+n5aFi2viZO_Uw@mail.gmail.com>

On Wed, Apr 28, 2021 at 5:26 PM Arnd Bergmann <arnd@arndb.de> wrote:
>
> On Wed, Apr 28, 2021 at 10:30 AM Guo Ren <guoren@kernel.org> wrote:
> > On Wed, Apr 28, 2021 at 11:18 AM Guenter Roeck <linux@roeck-us.net> wrote:
> > >
> > > On Wed, Apr 21, 2021 at 08:54:15AM +0000, guoren@kernel.org wrote:
> > > > From: Guo Ren <guoren@linux.alibaba.com>
> > > >
> > > > 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@linux.alibaba.com>
> > > > Cc: Arnd Bergmann <arnd@arndb.de>
> > >
> > > 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/

      parent reply	other threads:[~2021-04-28 13:03 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-21  8:54 [PATCH] csky: uaccess.h: Coding convention with asm generic guoren
2021-04-21 14:48 ` Christoph Hellwig
2021-04-28  3:18 ` Guenter Roeck
2021-04-28  8:29   ` Guo Ren
2021-04-28  9:25     ` Arnd Bergmann
2021-04-28 12:49       ` Christoph Hellwig
2021-04-28 13:07         ` Guo Ren
2021-04-28 13:48         ` Arnd Bergmann
2021-04-28 13:03       ` Guo Ren [this message]

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='CAJF2gTSPs20OLQfx=FUnbOfcHDMeKY_i7pjS2JkeNyPstBttFQ@mail.gmail.com' \
    --to=guoren@kernel.org \
    --cc=arnd@arndb.de \
    --cc=guoren@linux.alibaba.com \
    --cc=hch@infradead.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-csky@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /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).