mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Linus Torvalds' <torvalds@linux-foundation.org>,
	'Peter Anvin' <hpa@zytor.com>
Cc: 'Rasmus Villemoes' <linux@rasmusvillemoes.dk>,
	'Thomas Gleixner' <tglx@linutronix.de>,
	'Ingo Molnar' <mingo@redhat.com>,
	'Borislav Petkov' <bp@alien8.de>,
	'the arch/x86 maintainers' <x86@kernel.org>,
	"'Sean Christopherson'" <sean.j.christopherson@intel.com>,
	'Naresh Kamboju' <naresh.kamboju@linaro.org>,
	'Linux Kernel Mailing List' <linux-kernel@vger.kernel.org>
Subject: RE: [PATCH] x86/uaccess: fix code generation in put_user()
Date: Sat, 24 Oct 2020 16:58:25 +0000	[thread overview]
Message-ID: <1eaa025ba7104edd815c9ac66d5790e4@AcuMS.aculab.com> (raw)
In-Reply-To: <bca28d6e33a3475193478e762214c6ea@AcuMS.aculab.com>

From: David Laight
> Sent: 23 October 2020 22:52
...
> Could do_put_user() do an initial check for 64 bit
> then expand a different #define that contains the actual
> code passing either "a" or "A" for the constriant.
> 
> Apart from another level of indirection nothing is duplicated.

This code seems to compile to something sensible.
It does need change the registers that get_user_n() must
use - the normal return value is now in %ax (and %dx for
64bit values on 32bit systems, with the error in %cx.
(I've not actually tested it.)

#define __inttype_max(x, _max) __typeof__(      \
        __typefits(x,char,                      \
          __typefits(x,short,                   \
            __typefits(x,int,                   \
              __typefits(x,long,_max)))))

#define __inttype(x) __inttype_max(x, 0ULL)

#define get_user_1(x, ptr, type, constraint)                            \
({                                                                      \
        int __ret_gu;                                                   \
        type __val_gu;                                                  \
        asm volatile("call __get_user_%P4"                              \
                     : "=c" (__ret_gu), constraint (__val_gu),          \
                        ASM_CALL_CONSTRAINT                             \
                     : "a" (ptr), "i" (sizeof(*(ptr))));                \
        (x) = (__force __typeof__(*(ptr))) __val_gu;                    \
        __builtin_expect(__ret_gu, 0);                                  \
})

#define get_user(x, ptr)                                                \
({                                                                      \
        __chk_user_ptr(ptr);                                            \
        might_fault();                                                  \
        (sizeof *(ptr) > sizeof(long))                                  \
                ? get_user_1(x, ptr, long long, "=A")                   \
                : get_user_1(x, ptr, __inttype_max(*(ptr),0ul), "=a");  \
})

The __inttype_max() is needed (I think) because clang will try (and fail)
to generate the asm for 64bit values on 32bit systems.
So the type needs limiting to 32bits.
Always using 'long' works - but generates extra casts.

The "=A" constraint (%rax or %rdx) is never used on 64bit because
the test is always false.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

  parent reply	other threads:[~2020-10-24 16:58 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-23 20:31 Rasmus Villemoes
2020-10-23 20:42 ` Andy Lutomirski
2020-10-23 20:52   ` Linus Torvalds
2020-10-23 20:54   ` hpa
2020-10-23 20:55 ` Linus Torvalds
2020-10-23 20:59   ` hpa
2020-10-23 21:11     ` Linus Torvalds
2020-10-23 21:52       ` David Laight
2020-10-23 23:39         ` hpa
2020-10-24 16:58         ` David Laight [this message]
2020-10-23 22:52       ` hpa

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=1eaa025ba7104edd815c9ac66d5790e4@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=bp@alien8.de \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=mingo@redhat.com \
    --cc=naresh.kamboju@linaro.org \
    --cc=sean.j.christopherson@intel.com \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=x86@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

all inboxes | Powered by JetHome®