From: Jeff Law <law@redhat.com>
To: Wilco Dijkstra <Wilco.Dijkstra@arm.com>,
gcc-patches <gcc-patches@gcc.gnu.org>
Cc: Richard Earnshaw <Richard.Earnshaw@arm.com>,
James Greenhalgh <James.Greenhalgh@arm.com>,
Marcus Shawcroft <Marcus.Shawcroft@arm.com>, nd <nd@arm.com>
Subject: Re: [RFA][PATCH] Stack clash protection 07/08 -- V4 (aarch64 bits)
Date: Sat, 28 Oct 2017 11:23:00 -0000 [thread overview]
Message-ID: <dc1452bb-69af-4c3a-b08c-dbb0a6f219da@redhat.com> (raw)
In-Reply-To: <DB6PR0801MB205342840273A485E44D16D783480@DB6PR0801MB2053.eurprd08.prod.outlook.com>
On 10/13/2017 02:26 PM, Wilco Dijkstra wrote:
> Hi,
>
> To continue the review of the AArch64 frame code I tried a few examples
> to figure out what it does now. For initial_adjust <= 63*1024 and final_adjust <
> 1024 there are no probes inserted as expected, ie. the vast majority of
> functions are unaffected. So that works perfectly.
Right.
>
> For larger frames the first oddity is that there are now 2 separate params
> controlling how probes are generated:
>
> stack-clash-protection-guard-size (default 12, but set to 16 on AArch64)
> stack-clash-protection-probe-interval (default 12)
>
> I don't see how this makes sense. These values are closely related, so if
> one is different from the other, probing becomes ineffective/incorrect.
> For example we generate code that trivially bypasses the guard despite
> all the probing:
My hope would be that we simply don't ever use the params. They were
done as much for *you* to experiment with as anything. I'd happy just
delete them as there's essentially no guard rails to ensure their values
are sane.
>
> --param=stack-clash-protection-probe-interval=13
> --param=stack-clash-protection-guard-size=12
>
> So if there is a good reason to continue with 2 separate values, we must
> force probe interval <= guard size!
The param code really isn't designed to enforce values that are
inter-dependent. It has a min, max & default values. No more, no less.
If you set up something inconsistent with the params, it's simply not
going to work.
>
> Also on AArch64 --param=stack-clash-protection-probe-interval=16 causes
> crashes due to the offsets used in the probes - we don't need large offsets
> as we want to probe close to the bottom of the stack.
Not a surprise. While I tried to handle larger intervals, I certainly
didn't test them. Given the ISA I wouldn't expect an interval > 12 to
be useful or necessarily even work correctly.
>
> Functions with a large stack emit like alloca a lot of code, here I used
> --param=stack-clash-protection-probe-interval=15:
>
> int f1(int x)
> {
> char arr[128*1024];
> return arr[x];
> }
>
> f1:
> mov x16, 64512
> sub sp, sp, x16
> .cfi_def_cfa_offset 64512
> mov x16, -32768
> add sp, sp, x16
> .cfi_def_cfa_offset -1024
> str xzr, [sp, 32760]
> add sp, sp, x16
> .cfi_def_cfa_offset -66560
> str xzr, [sp, 32760]
> sub sp, sp, #1024
> .cfi_def_cfa_offset -65536
> str xzr, [sp, 1016]
> ldrb w0, [sp, w0, sxtw]
> .cfi_def_cfa_offset 131072
> add sp, sp, 131072
> .cfi_def_cfa_offset 0
> ret
>
> Note the cfa offsets are wrong.
Yes. They definitely look wrong. There's a clear logic error in
setting up the ADJUST_CFA note when the probing interval is larger than
2**12. That should be easily fixed. Let me poke at it.
>
> There is an odd mix of a big initial adjustment, then some probes+adjustments and
> then a final adjustment and probe for the remainder. I can't see the point of having
> both an initial and remainder adjustment. I would expect this:
>
> sub sp, sp, 65536
> str xzr, [sp, 1024]
> sub sp, sp, 65536
> str xzr, [sp, 1024]
> ldrb w0, [sp, w0, sxtw]
> add sp, sp, 131072
> ret
I'm really not able to justify spending further time optimizing the
aarch64 implementation. I've done the best I can. You can take the
work as-is or improve it, but I really can't justify further time
investment on that architecture.
>
>
> int f2(int x)
> {
> char arr[128*1024];
> return arr[x];
> }
>
> f2:
> mov x16, 64512
> sub sp, sp, x16
> mov x16, -65536
> movk x16, 0xfffd, lsl 16
> add x16, sp, x16
> .LPSRL0:
> sub sp, sp, 4096
> str xzr, [sp, 4088]
> cmp sp, x16
> b.ne .LPSRL0
> sub sp, sp, #1024
> str xzr, [sp, 1016]
> ldrb w0, [sp, w0, sxtw]
> add sp, sp, 262144
> ret
>
> The cfa entries are OK for this case. There is a mix of positive/negative offsets which
> makes things confusing. Again there are 3 kinds of adjustments when for this size we
> only need the loop.
>
> Reusing the existing gen_probe_stack_range code appears a bad idea since
> it ignores the probe interval and just defaults to 4KB. I don't see why it should be
> any more complex than this:
>
> sub x16, sp, 262144 // only need temporary if > 1MB
> .LPSRL0:
> sub sp, sp, 65536
> str xzr, [sp, 1024]
> cmp sp, x16
> b.ne .LPSRL0
> ldrb w0, [sp, w0, sxtw]
> add sp, sp, 262144
> ret
>
> Probe insertion if final adjustment >= 1024 also generates a lot of redundant
> code - although this is more a theoretical issue given this is so rare.
Again, if ARM wants this optimized, then ARM's engineers are going to
have to take the lead here. I've invested all I can reasonably invest
in terms of trying optimize the probing for this target.
jeff
next prev parent reply other threads:[~2017-10-28 4:09 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-29 15:15 Jeff Law
[not found] ` <DB6PR0801MB205340E4DACC1CF3696A150983480@DB6PR0801MB2053.eurprd08.prod.outlook.com>
2017-10-13 16:14 ` Wilco Dijkstra
2017-10-13 20:47 ` Wilco Dijkstra
2017-10-28 11:23 ` Jeff Law [this message]
2017-11-21 11:59 ` James Greenhalgh
2017-11-22 18:52 ` Jeff Law
2017-11-27 19:25 ` James Greenhalgh
2017-12-19 0:49 ` Jeff Law
2017-11-27 16:32 ` Szabolcs Nagy
2017-11-27 17:50 ` Wilco Dijkstra
2017-11-27 18:44 ` Jeff Law
2017-11-27 18:30 ` Jeff Law
2017-11-28 16:31 ` Rich Felker
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=dc1452bb-69af-4c3a-b08c-dbb0a6f219da@redhat.com \
--to=law@redhat.com \
--cc=James.Greenhalgh@arm.com \
--cc=Marcus.Shawcroft@arm.com \
--cc=Richard.Earnshaw@arm.com \
--cc=Wilco.Dijkstra@arm.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=nd@arm.com \
/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 read-only IMAP folder(s) and NNTP newsgroup(s).