From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by sourceware.org (Postfix) with ESMTPS id 0DCE23840C1D for ; Tue, 26 Jan 2021 16:36:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0DCE23840C1D Received: by mail-qv1-xf2b.google.com with SMTP id n14so3346445qvg.5 for ; Tue, 26 Jan 2021 08:36:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:references:from:autocrypt:subject:message-id :date:user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=qGt5P15c2ULRzjDkOaFBEtgdJrB9T+RsmnejB6YyJok=; b=BsJXv4P7yQMSbMFaICNxiD36SeQpjIjlHNa8s2SXERfiIB1H9KtywpDusydltrcEUe FPWS5VF3cywNuZYTEyOvxnkWgPv21ykGodoE6yflrEDH77mkgXovu2+HIrdR/Qk5xRM5 es8oY5yqmYqsHGr/9WN8fRElNc+ZFmUu0DbUuO0h8FVWUkynLtLU2fLcvLffMjoRRFYX hL20Hp2BnJ5GWe/vlrtynlc47RSu3myhjqF4zEy0eeFTYqZhdywWniakz24fa59e5y/y zTcs9Ahnc+HxQ2aE8s/SK523LEEUdFyPBLC6W+y92OBGxkAYT1IKhzpxcs0cO+FOGjaw ACfQ== X-Gm-Message-State: AOAM530pal2wrzhuZaxqMhWBR14kueTO5BMqXRGHso2oJ39AG0N1L/F7 NmxrYyJDB+1sQ0cYwMByohxxydPIwnBjdg== X-Google-Smtp-Source: ABdhPJx0Op++fF6pUjvsZ9tFQN3xdIjGgHfUxdLYts0iGkuv+tXJr9+2nQs/AR3O+3RRKHBqP53w1A== X-Received: by 2002:a0c:a819:: with SMTP id w25mr6244014qva.6.1611678976145; Tue, 26 Jan 2021 08:36:16 -0800 (PST) Received: from [192.168.1.4] ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id c20sm13387661qtj.29.2021.01.26.08.36.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 Jan 2021 08:36:15 -0800 (PST) To: libc-alpha@sourceware.org, fweimer@redhat.com, Raoni Fassina Firmino via Libc-alpha References: <20210126130515.jivsqeoyj5nu5znk@work-tp> From: Adhemerval Zanella Autocrypt: addr=adhemerval.zanella@linaro.org; prefer-encrypt=mutual; keydata= mQINBFcVGkoBEADiQU2x/cBBmAVf5C2d1xgz6zCnlCefbqaflUBw4hB/bEME40QsrVzWZ5Nq 8kxkEczZzAOKkkvv4pRVLlLn/zDtFXhlcvQRJ3yFMGqzBjofucOrmdYkOGo0uCaoJKPT186L NWp53SACXguFJpnw4ODI64ziInzXQs/rUJqrFoVIlrPDmNv/LUv1OVPKz20ETjgfpg8MNwG6 iMizMefCl+RbtXbIEZ3TE/IaDT/jcOirjv96lBKrc/pAL0h/O71Kwbbp43fimW80GhjiaN2y WGByepnkAVP7FyNarhdDpJhoDmUk9yfwNuIuESaCQtfd3vgKKuo6grcKZ8bHy7IXX1XJj2X/ BgRVhVgMHAnDPFIkXtP+SiarkUaLjGzCz7XkUn4XAGDskBNfbizFqYUQCaL2FdbW3DeZqNIa nSzKAZK7Dm9+0VVSRZXP89w71Y7JUV56xL/PlOE+YKKFdEw+gQjQi0e+DZILAtFjJLoCrkEX w4LluMhYX/X8XP6/C3xW0yOZhvHYyn72sV4yJ1uyc/qz3OY32CRy+bwPzAMAkhdwcORA3JPb kPTlimhQqVgvca8m+MQ/JFZ6D+K7QPyvEv7bQ7M+IzFmTkOCwCJ3xqOD6GjX3aphk8Sr0dq3 4Awlf5xFDAG8dn8Uuutb7naGBd/fEv6t8dfkNyzj6yvc4jpVxwARAQABtElBZGhlbWVydmFs IFphbmVsbGEgTmV0dG8gKExpbmFybyBWUE4gS2V5KSA8YWRoZW1lcnZhbC56YW5lbGxhQGxp bmFyby5vcmc+iQI3BBM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2d4+m/qO6+ cFTAceXmIoFsN8+gFN3i8Is3u12u8xGudcBPvpoy4OoG Subject: Re: [PATCH] powerpc64: Workaround sigtramp vdso return call. Message-ID: <4bb4b2b1-17c1-98d4-6eb8-41db2b7deb88@linaro.org> Date: Tue, 26 Jan 2021 13:36:12 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20210126130515.jivsqeoyj5nu5znk@work-tp> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Jan 2021 16:36:19 -0000 On 26/01/2021 10:05, Raoni Fassina Firmino via Libc-alpha wrote: > Hi all, > > There was some initial discussions on the mailing list about the > failing misc/tst-sigcontext-get_pc[1], and I made some suggestions of > possible solutions. As the window for the release is closing I want > to sent the more simple one of them ASAP for consideration (others > would not make it in time) > > o/ > Raoni > > [1] https://sourceware.org/pipermail/libc-alpha/2021-January/121933.html As Rich has noted, this should not interfere with BZ#12683 fix (which I do hope we get upstream in next release): the resulting Program Counter from the signal handle context is set to next instruct pass the syscall: (gdb) n 39 uintptr_t pc = sigcontext_get_pc (ctx); $1 = 140737351668608 (gdb) p /x pc $2 = 0x7ffff7da5380 (gdb) x/20i 0x7ffff7da5380 0x7ffff7da5380 <__GI_raise+496>: mfcr r0 0x7ffff7da5384 <__GI_raise+500>: addi r1,r1,400 0x7ffff7da5388 <__GI_raise+504>: mr r3,r31 0x7ffff7da538c <__GI_raise+508>: ld r0,16(r1) So this fix is indeed a backtrace() one, which raise another question: do we still need to keep this powerpc optimization? It seems that only powerpc and microblaze does use an optimized version and I think by using the generic libgcc based one might allow some consolidation and code simplification (specially for powerpc64 case, where it would allow to remove some internal vDSO symbol setup). > > ---- 8< ---- > > A not so recent kernel change[1] changed how the trampoline > `__kernel_sigtramp_rt64` is used to call signal handlers. > > This was exposed on the test misc/tst-sigcontext-get_pc > > Before kernel 5.9, the kernel set LR to the trampoline address and > jumped directly to the signal handler, and at the end the signal > handler, as any other function, would `blr` to the address set. In > other words, the trampoline was executed just at the end of the signal > handler and the only thing it did was call sigreturn. But since > kernel 5.9 the kernel set CTRL to the signal handler and calls to the > trampoline code, the trampoline then `bctrl` to the address in CTRL, > setting the LR to the next instruction in the middle of the > trampoline, when the signal handler returns, the rest of the > trampoline code executes the same code as before. > > Here is the full trampoline code as of kernel 5.11.0-rc5 for > reference: > > V_FUNCTION_BEGIN(__kernel_sigtramp_rt64) > .Lsigrt_start: > bctrl▸ /* call the handler */ > addi▸ r1, r1, __SIGNAL_FRAMESIZE > li▸ r0,__NR_rt_sigreturn > sc > .Lsigrt_end: > V_FUNCTION_END(__kernel_sigtramp_rt64) > > This new behavior breaks how `backtrace()` uses to detect the > trampoline frame to correctly reconstruct the stack frame when it is > called from inside a signal handling. > > This workaround rely on the fact that the trampoline code is at very > least two (maybe 3?) instructions in size (as it is in the 32 bits > version, only on `li` and `sc`), so it is safe to check the return > address be in the range __kernel_sigtramp_rt64 .. + 4. > > [1] subject: powerpc/64/signal: Balance return predictor stack in signal trampoline > commit: 0138ba5783ae0dcc799ad401a1e8ac8333790df9 > url: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0138ba5783ae0dcc799ad401a1e8ac8333790df9 > --- > sysdeps/powerpc/powerpc64/backtrace.c | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) > > diff --git a/sysdeps/powerpc/powerpc64/backtrace.c b/sysdeps/powerpc/powerpc64/backtrace.c > index ae64c5d7a5..5540085436 100644 > --- a/sysdeps/powerpc/powerpc64/backtrace.c > +++ b/sysdeps/powerpc/powerpc64/backtrace.c > @@ -54,11 +54,20 @@ struct signal_frame_64 { > /* We don't care about the rest, since the IP value is at 'uc' field. */ > }; > > +/* Test if the address match to the inside the trampoline code. We cannot > + * count on the return address be the beginning of sigtramp, the kernel may jump Line too long. > + * to the trampoline and the trampoline jump to the signal handler and in this > + * case the address will be somewhere in the middle of the trampoline. This is > + * a workaround while we cannot know the sigtramp size. It will be at least two Ditto. > + * instructions long since it needs at least to load the syscall number for > + * sigreturn and call it. > + */ Maybe describe the kernel change itself, such as: /* Up to kernel 5.9, returning from an interrupt or syscall to a signal handler starts execution directly at the handler's entry point, with LR set to address of the sigreturn trampoline (the vDSO symbol). Newer kernels will branch to signal handler from the trampoline instead, so checking the stacktrace against the vDSO entrypoint does not work in such case. The vDSO branches with a 'bctrl' instruction, so checking either the vDSO address itself and the next instruction should cover all kernel versions. */ > static inline bool > is_sigtramp_address (void *nip) > { > #ifdef HAVE_SIGTRAMP_RT64 > - if (nip == GLRO (dl_vdso_sigtramp_rt64)) > + void *sigtramp_addr = GLRO (dl_vdso_sigtramp_rt64); > + if (nip >= sigtramp_addr && nip <= sigtramp_addr + 4) Maybe it would be simpler to just: if (nip == GLRO (dl_vdso_sigtramp_rt64) || nip == GLRO (dl_vdso_sigtramp_rt64) + 4) (dl_vdso_sigtramp_rt64 is a void* and gcc allows void pointer arithmetic as an extension). > return true; > #endif > return false; >