From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta-sndfb-w03.mail.nifty.com (mta-sndfb-w03.mail.nifty.com [106.153.227.67]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8389554DAC1; Tue, 22 Sep 2026 13:23:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=106.153.227.67 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790083443; cv=none; b=sJrj8N71hLcyAhQDzG7R2aaxVUgh83k54R5HkmXcnAWwZjjzP1ZrsBMeelgznHr2Bn1IBi9QrMem3aztq8zu+8zWFA4pBeJo0HxCAnM4N7LHBUiSzH6hv+YqIjS99i45bbTjFs2CMyKpTSW5PKRVfjgFxbz/41e/jvhoi5vIJc0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790083443; c=relaxed/simple; bh=DtOTlP9qGiXEFmFlZrL4RPB+KCu1K4qt2Txq46xjPo4=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=WNBRIxodTfvgP+MbHcvruHfw3PBE9faqL30Clw/vOyXLKIcUV6D06z6nmG9eg3ImY7mN97nHE5hH35/nt++zn4dlEYGdpyiaIH/0Cgd0rrpNhi+pfJHc9zjMF37qBIrN4Du71pK/YTEGV93/IlYJOb2WZQ3gIl2FkYViA/Hyl0M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nifty.com; spf=pass smtp.mailfrom=nifty.com; dkim=pass (2048-bit key) header.d=nifty.com header.i=@nifty.com header.b=JhssVj9z; arc=none smtp.client-ip=106.153.227.67 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nifty.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nifty.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nifty.com header.i=@nifty.com header.b="JhssVj9z" Received: from sakura.ysato.name by mta-snd-w10.mail.nifty.com with ESMTP id <20260922131911542.ZWMI.44671.sakura.ysato.name@nifty.com>; Tue, 22 Sep 2026 22:19:11 +0900 Received: from SIOS1075.ysato.ml (ZM005235.ppp.dion.ne.jp [222.8.5.235]) by sakura.ysato.name (Postfix) with ESMTPSA id E202D1C0081; Tue, 22 Sep 2026 22:19:10 +0900 (JST) Date: Tue, 22 Sep 2026 22:19:09 +0900 Message-ID: <87ld8t77aq.wl-yoshinori.sato@nifty.com> From: yoshinori.sato@nifty.com To: John Paul Adrian Glaubitz Cc: Florian Fuchs , Rich Felker , linux-sh@vger.kernel.org, Geert Uytterhoeven , linux-kernel@vger.kernel.org Subject: Re: [PATCH] sh: lib: Restore r4 in shift helpers In-Reply-To: <85c4e0b40ec4b52b73c1525c2340cdaf31fd7d6a.camel@physik.fu-berlin.de> References: <20260714104147.2016549-1-fuchsfl@gmail.com> <85c4e0b40ec4b52b73c1525c2340cdaf31fd7d6a.camel@physik.fu-berlin.de> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?ISO-8859-4?Q?Goj=F2?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/30.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=default-1th84yt82rvi; t=1790083151; bh=jhrLNJmTD0W/w6263Qa0kJsZ9CDIVbRIiGhDNgpGojo=; h=Date:From:To:Cc:Subject:In-Reply-To; b=JhssVj9zMhRxTSdc9f4uGnrIvvwswsGzZvoMp6yu29vDBLQLWP+rDSND1R+GCoZAx/vP6lef pKzEoIx9vCRk3tc3LV5BdipWDeZDfZieVk3OaTCvderPl5xiD6SeWmUOHe+j88+sdO1qTXYcgF 8Zth62/Y1M9qQ79Wgd1sa4VZOK+W0ZrlrS53aMbxfCuXSJoP21gJqcTFJpal91AO1R9J9xpiPp qjrQP6MgmYqQ8kyTW+voRESWFa+QOjPB6whzkt3H3Ht1gy3uArdQJoIvjScF/szUcQbOETg2J6 9q2ujxNFTw93/YLjR3kxvQPWRXbY9RjUcyr+sOoay/PREWcQ== On Thu, 17 Sep 2026 16:17:00 +0900, John Paul Adrian Glaubitz wrote: > > Hi Florian. > > On Tue, 2026-07-14 at 12:41 +0200, Florian Fuchs wrote: > > Commit 940d4113f330 ("sh: New gcc support") added new shift helpers > > that use r4 as a scratch register while dispatching to the selected shift > > sequence. But, the helpers return without restoring r4. With GCC 17, the > > register allocator can keep a live value in r4 across the helper call. > > Clobbering it results in runtime data corruption. Restore r4 before > > jumping to the selected shift sequence. > > > > Fixes: 940d4113f330 ("sh: New gcc support") > > Signed-off-by: Florian Fuchs > > --- > > Without the patch, the early boot on e.g J2 gets a kernel BUG at > > mm/percpu.c:2604 / "can't handle more than one group." > > PCPU_SETUP_BUG_ON(pcpu_verify_alloc_info(ai) < 0); > > As the static condition in mm/percpu-km.c wasn't true: > > ai->nr_groups != 1 > > nr_groups contained 60 - the clobbered value from the shift helper. > > > > This change was tested on the J2 core on the Mimas v2 board. It can > > theoretically also target other SH2 devices, but I don't have any other > > than J2 sadly. > > > > The flow of operations matches now the state in the libgcc, see also > > in gcc: libgcc/config/sh/lib1funcs.S > > --- > > arch/sh/lib/ashlsi3.S | 3 ++- > > arch/sh/lib/ashrsi3.S | 3 ++- > > arch/sh/lib/lshrsi3.S | 3 ++- > > 3 files changed, 6 insertions(+), 3 deletions(-) > > > > diff --git a/arch/sh/lib/ashlsi3.S b/arch/sh/lib/ashlsi3.S > > index 4df4401cdf31..73a9d709b169 100644 > > --- a/arch/sh/lib/ashlsi3.S > > +++ b/arch/sh/lib/ashlsi3.S > > @@ -63,8 +63,9 @@ __ashlsi3_r0: > > mova ashlsi3_table,r0 > > mov.b @(r0,r4),r4 > > add r4,r0 > > + mov.l @r15+,r4 > > jmp @r0 > > - mov.l @r15+,r0 > > + mov r4,r0 > > > > Please correct me if I'm wrong, but after reading the code in [1], it looks > to me as your patch doesn't preserve r4 but it's actually storing it into > r0 to be used by the selected shift sequence. > > With the previous code, r4 is pushed onto the stack first but not restored > before the shift sequence is jumped to, so what your patch does not make sure > that r4 is restored across the complete call of the shift helper but rather > restore the input value in r4 from the stack before calling the jump sequence. > > What am I missing? > > Adrian > > > [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/sh/lib/ashlsi3.S > > -- > .''`. John Paul Adrian Glaubitz > : :' : Debian Developer > `. `' Physicist > `- GPG: 62FF 8A75 84E0 2956 9546 0006 7426 3B37 F5B5 F913 Here is the modified code snippet. and #31,r0 mov.l r4,@-r15 mov r0,r4 mova ashlsi3_table,r0 mov.b @(r0,r4),r4 add r4,r0 mov.l @r15+,r4 jmp @r0 mov r4,r0 I understand that r4 no longer changes as a result of this modification. While the sh function-calling convention permits the `r4` register to be clobbered, the `libgcc` included with GCC does not actually clobber `r4`, and GCC's own rules appear to rely on `r4` remaining intact during such calls. This change won't break builds with older versions of GCC, so I think it's fine to go ahead and apply it. -- Yosinori Sato