lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Wed, 6 May 2020 15:51:28 +0200
From:   Peter Zijlstra <peterz@...radead.org>
To:     Nick Desaulniers <ndesaulniers@...gle.com>
Cc:     Rasmus Villemoes <linux@...musvillemoes.dk>,
        "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" <x86@...nel.org>,
        LKML <linux-kernel@...r.kernel.org>,
        Steven Rostedt <rostedt@...dmis.org>,
        Masami Hiramatsu <mhiramat@...nel.org>, bristot@...hat.com,
        Jason Baron <jbaron@...mai.com>,
        Linus Torvalds <torvalds@...ux-foundation.org>,
        Thomas Gleixner <tglx@...utronix.de>,
        Ingo Molnar <mingo@...nel.org>, Nadav Amit <namit@...are.com>,
        "H. Peter Anvin" <hpa@...or.com>,
        Andy Lutomirski <luto@...nel.org>,
        Ard Biesheuvel <ard.biesheuvel@...aro.org>,
        Josh Poimboeuf <jpoimboe@...hat.com>,
        Paolo Bonzini <pbonzini@...hat.com>,
        Mathieu Desnoyers <mathieu.desnoyers@...icios.com>,
        "H.J. Lu" <hjl.tools@...il.com>,
        clang-built-linux <clang-built-linux@...glegroups.com>
Subject: Re: [PATCH v4 14/18] static_call: Add static_cond_call()

On Tue, May 05, 2020 at 11:13:53AM -0700, Nick Desaulniers wrote:
> On Tue, May 5, 2020 at 2:36 AM Peter Zijlstra <peterz@...radead.org> wrote:
> >
> >
> > HJ, Nick,
> >
> > Any chance any of you can see a way to make your respective compilers
> > not emit utter junk for this?
> >
> > On Mon, May 04, 2020 at 10:14:45PM +0200, Peter Zijlstra wrote:
> >
> > > https://godbolt.org/z/SDRG2q
> 
> Woah, a godbolt link! Now we're speaking the same language.  What were
> you expecting?

Given the output for x86-64 clang (trunk)

	bar:                                    # @bar
		movl    %edi, .L_x$local(%rip)
		retq
	ponies:                                 # @ponies
		movq    .Lfoo$local(%rip), %rax
		testq   %rax, %rax
		movl    $__static_call_nop, %ecx
		cmovneq %rax, %rcx
		jmpq    *%rcx                   # TAILCALL
	__static_call_nop:                      # @__static_call_nop
		retq
	_x:
	.L_x$local:
		.long   0                       # 0x0

	foo:
	.Lfoo$local:
		.zero   8


I was hoping for:

	bar:                                    # @bar
		movl    %edi, .L_x$local(%rip)
		retq
	ponies:                                 # @ponies
		movq    .Lfoo$local(%rip), %rax
		testq   %rax, %rax
		jz	1f
		jmpq    *%rcx                   # TAILCALL
	1:
		retq

That avoids the indirect call (possible retpoline) and does an immediate
return.

So it does 2 things different:

 - it realizes the NULL case is a constant and uses an
   immediate call and avoids the indirect call/jmp.

 - it realizes __static_call_nop() is a no-op and avoids the call
   entirely and does an immediate return.

> Us to remove the conditional check that a volatile read
> wasn't NULL?

No, obviously the load is required, and the READ_ONCE() is so that the
compiler will not emit 2 different loads (just for giggles).

That is:

	tmp1 = name.func;
	if (!tmp) {
		tmp2 = name.func;
		tmp2(args);
	}

is a valid translation of:

	if (!name.func)
		name.func(args)

and allows for a NULL dereference (as noted by Rasmus).

What I did do want, per the above, is to avoid the indirect (tail) call.
Because indirect jmp/call are evil and expensive.

> I am simultaneously impressed
> and disgusted by this btw, cool stuff.

Yes, it's nasty, esp the casting of a function pointer like that is
gruesome.

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ