Linux MIPS Architecture development
 help / color / mirror / Atom feed
From: Matt Redfearn <matt.redfearn@mips.com>
To: Heiher <r@hev.cc>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: <linux-kernel@vger.kernel.org>, <stable@vger.kernel.org>,
	James Hogan <jhogan@kernel.org>,
	Ralf Baechle <ralf@linux-mips.org>, <linux-mips@linux-mips.org>
Subject: Re: [PATCH 3.18 45/52] MIPS: memset.S: Fix clobber of v1 in last_fixup
Date: Mon, 23 Apr 2018 10:36:25 +0100	[thread overview]
Message-ID: <8293aba6-81fa-6552-529e-030cc41c705f@mips.com> (raw)
In-Reply-To: <CAHirt9jOibozCFgm8-5bXukSvRBwvxz7ctZYvugQuxnfLFCeLQ@mail.gmail.com>



On 23/04/18 08:16, Heiher wrote:
> Hi,
> 
> IIRC, The v1 is a temporary register, value is not preserved across
> function calls.

v1 is conventionally used for a function return value and as such can be 
changed by called functions. However, bzero is called from inline 
assembly and v1 is not in the clobbers list
https://elixir.bootlin.com/linux/v4.17-rc1/source/arch/mips/include/asm/uaccess.h#L652
So the calling function does not expect that register to have been used 
and can legitimately expect its value to remain after the function call, 
which without this patch, it does not - as demonstrated by the test code.

Thanks,
Matt

> 
> I don't see any functions that generated by compiler to restore values
> of v1 after clobbered it.
> 
> On Sun, Apr 22, 2018 at 9:54 PM, Greg Kroah-Hartman
> <gregkh@linuxfoundation.org> wrote:
>> 3.18-stable review patch.  If anyone has any objections, please let me know.
>>
>> ------------------
>>
>> From: Matt Redfearn <matt.redfearn@mips.com>
>>
>> commit c96eebf07692e53bf4dd5987510d8b550e793598 upstream.
>>
>> The label .Llast_fixup\@ is jumped to on page fault within the final
>> byte set loop of memset (on < MIPSR6 architectures). For some reason, in
>> this fault handler, the v1 register is randomly set to a2 & STORMASK.
>> This clobbers v1 for the calling function. This can be observed with the
>> following test code:
>>
>> static int __init __attribute__((optimize("O0"))) test_clear_user(void)
>> {
>>    register int t asm("v1");
>>    char *test;
>>    int j, k;
>>
>>    pr_info("\n\n\nTesting clear_user\n");
>>    test = vmalloc(PAGE_SIZE);
>>
>>    for (j = 256; j < 512; j++) {
>>      t = 0xa5a5a5a5;
>>      if ((k = clear_user(test + PAGE_SIZE - 256, j)) != j - 256) {
>>          pr_err("clear_user (%px %d) returned %d\n", test + PAGE_SIZE - 256, j, k);
>>      }
>>      if (t != 0xa5a5a5a5) {
>>         pr_err("v1 was clobbered to 0x%x!\n", t);
>>      }
>>    }
>>
>>    return 0;
>> }
>> late_initcall(test_clear_user);
>>
>> Which demonstrates that v1 is indeed clobbered (MIPS64):
>>
>> Testing clear_user
>> v1 was clobbered to 0x1!
>> v1 was clobbered to 0x2!
>> v1 was clobbered to 0x3!
>> v1 was clobbered to 0x4!
>> v1 was clobbered to 0x5!
>> v1 was clobbered to 0x6!
>> v1 was clobbered to 0x7!
>>
>> Since the number of bytes that could not be set is already contained in
>> a2, the andi placing a value in v1 is not necessary and actively
>> harmful in clobbering v1.
>>
>> Reported-by: James Hogan <jhogan@kernel.org>
>> Signed-off-by: Matt Redfearn <matt.redfearn@mips.com>
>> Cc: Ralf Baechle <ralf@linux-mips.org>
>> Cc: linux-mips@linux-mips.org
>> Cc: stable@vger.kernel.org
>> Patchwork: https://patchwork.linux-mips.org/patch/19109/
>> Signed-off-by: James Hogan <jhogan@kernel.org>
>> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>
>> ---
>>   arch/mips/lib/memset.S |    2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> --- a/arch/mips/lib/memset.S
>> +++ b/arch/mips/lib/memset.S
>> @@ -210,7 +210,7 @@
>>
>>   .Llast_fixup\@:
>>          jr              ra
>> -       andi            v1, a2, STORMASK
>> +        nop
>>
>>   .Lsmall_fixup\@:
>>          PTR_SUBU        a2, t1, a0
>>
>>
>>
> 
> 
> 

WARNING: multiple messages have this Message-ID (diff)
From: Matt Redfearn <matt.redfearn@mips.com>
To: Heiher <r@hev.cc>, Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	James Hogan <jhogan@kernel.org>,
	Ralf Baechle <ralf@linux-mips.org>,
	linux-mips@linux-mips.org
Subject: Re: [PATCH 3.18 45/52] MIPS: memset.S: Fix clobber of v1 in last_fixup
Date: Mon, 23 Apr 2018 10:36:25 +0100	[thread overview]
Message-ID: <8293aba6-81fa-6552-529e-030cc41c705f@mips.com> (raw)
Message-ID: <20180423093625.uWW3MycLRAfsFUsmBBonOQvOS7oxsKEjnsVq5i6_Gww@z> (raw)
In-Reply-To: <CAHirt9jOibozCFgm8-5bXukSvRBwvxz7ctZYvugQuxnfLFCeLQ@mail.gmail.com>



On 23/04/18 08:16, Heiher wrote:
> Hi,
> 
> IIRC, The v1 is a temporary register, value is not preserved across
> function calls.

v1 is conventionally used for a function return value and as such can be 
changed by called functions. However, bzero is called from inline 
assembly and v1 is not in the clobbers list
https://elixir.bootlin.com/linux/v4.17-rc1/source/arch/mips/include/asm/uaccess.h#L652
So the calling function does not expect that register to have been used 
and can legitimately expect its value to remain after the function call, 
which without this patch, it does not - as demonstrated by the test code.

Thanks,
Matt

> 
> I don't see any functions that generated by compiler to restore values
> of v1 after clobbered it.
> 
> On Sun, Apr 22, 2018 at 9:54 PM, Greg Kroah-Hartman
> <gregkh@linuxfoundation.org> wrote:
>> 3.18-stable review patch.  If anyone has any objections, please let me know.
>>
>> ------------------
>>
>> From: Matt Redfearn <matt.redfearn@mips.com>
>>
>> commit c96eebf07692e53bf4dd5987510d8b550e793598 upstream.
>>
>> The label .Llast_fixup\@ is jumped to on page fault within the final
>> byte set loop of memset (on < MIPSR6 architectures). For some reason, in
>> this fault handler, the v1 register is randomly set to a2 & STORMASK.
>> This clobbers v1 for the calling function. This can be observed with the
>> following test code:
>>
>> static int __init __attribute__((optimize("O0"))) test_clear_user(void)
>> {
>>    register int t asm("v1");
>>    char *test;
>>    int j, k;
>>
>>    pr_info("\n\n\nTesting clear_user\n");
>>    test = vmalloc(PAGE_SIZE);
>>
>>    for (j = 256; j < 512; j++) {
>>      t = 0xa5a5a5a5;
>>      if ((k = clear_user(test + PAGE_SIZE - 256, j)) != j - 256) {
>>          pr_err("clear_user (%px %d) returned %d\n", test + PAGE_SIZE - 256, j, k);
>>      }
>>      if (t != 0xa5a5a5a5) {
>>         pr_err("v1 was clobbered to 0x%x!\n", t);
>>      }
>>    }
>>
>>    return 0;
>> }
>> late_initcall(test_clear_user);
>>
>> Which demonstrates that v1 is indeed clobbered (MIPS64):
>>
>> Testing clear_user
>> v1 was clobbered to 0x1!
>> v1 was clobbered to 0x2!
>> v1 was clobbered to 0x3!
>> v1 was clobbered to 0x4!
>> v1 was clobbered to 0x5!
>> v1 was clobbered to 0x6!
>> v1 was clobbered to 0x7!
>>
>> Since the number of bytes that could not be set is already contained in
>> a2, the andi placing a value in v1 is not necessary and actively
>> harmful in clobbering v1.
>>
>> Reported-by: James Hogan <jhogan@kernel.org>
>> Signed-off-by: Matt Redfearn <matt.redfearn@mips.com>
>> Cc: Ralf Baechle <ralf@linux-mips.org>
>> Cc: linux-mips@linux-mips.org
>> Cc: stable@vger.kernel.org
>> Patchwork: https://patchwork.linux-mips.org/patch/19109/
>> Signed-off-by: James Hogan <jhogan@kernel.org>
>> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>
>> ---
>>   arch/mips/lib/memset.S |    2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> --- a/arch/mips/lib/memset.S
>> +++ b/arch/mips/lib/memset.S
>> @@ -210,7 +210,7 @@
>>
>>   .Llast_fixup\@:
>>          jr              ra
>> -       andi            v1, a2, STORMASK
>> +        nop
>>
>>   .Lsmall_fixup\@:
>>          PTR_SUBU        a2, t1, a0
>>
>>
>>
> 
> 
> 

  reply	other threads:[~2018-04-23  9:36 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20180422135315.254787616@linuxfoundation.org>
2018-04-22 13:54 ` [PATCH 3.18 43/52] MIPS: memset.S: EVA & fault support for small_memset Greg Kroah-Hartman
2018-04-22 13:54 ` [PATCH 3.18 44/52] MIPS: memset.S: Fix return of __clear_user from Lpartial_fixup Greg Kroah-Hartman
2018-04-22 13:54 ` [PATCH 3.18 45/52] MIPS: memset.S: Fix clobber of v1 in last_fixup Greg Kroah-Hartman
2018-04-23  7:16   ` Heiher
2018-04-23  9:36     ` Matt Redfearn [this message]
2018-04-23  9:36       ` Matt Redfearn
2018-04-23  9:45       ` Heiher

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=8293aba6-81fa-6552-529e-030cc41c705f@mips.com \
    --to=matt.redfearn@mips.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jhogan@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@linux-mips.org \
    --cc=r@hev.cc \
    --cc=ralf@linux-mips.org \
    --cc=stable@vger.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