[PATCH] powerpc: Fix 85xx build

2022-10-10 Thread Joel Stanley
The merge of the kbuild tree dropped the renaming of the FSL_BOOKE
kconfig option.

Fixes: 8afc66e8d43b ("Merge tag 'kbuild-v6.1' of 
git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild")
Signed-off-by: Joel Stanley 
---
 arch/powerpc/kernel/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/Makefile b/arch/powerpc/kernel/Makefile
index 68ea30fb373e..ee2d76cb3187 100644
--- a/arch/powerpc/kernel/Makefile
+++ b/arch/powerpc/kernel/Makefile
@@ -121,7 +121,7 @@ obj-$(CONFIG_PPC_BOOK3S_32) += head_book3s_32.o
 obj-$(CONFIG_40x)  += head_40x.o
 obj-$(CONFIG_44x)  += head_44x.o
 obj-$(CONFIG_PPC_8xx)  += head_8xx.o
-obj-$(CONFIG_FSL_BOOKE)+= head_85xx.o
+obj-$(CONFIG_PPC_85xx) += head_85xx.o
 extra-y+= vmlinux.lds
 
 obj-$(CONFIG_RELOCATABLE)  += reloc_$(BITS).o
-- 
2.35.1



RE: [PATCH v6 3/7] treewide: use get_random_{u8,u16}() when possible, part 1

2022-10-10 Thread Elliott, Robert (Servers)

> diff --git a/crypto/testmgr.c b/crypto/testmgr.c
...
> @@ -944,7 +944,7 @@ static void generate_random_bytes(u8 *buf, size_t count)
>   default:
>   /* Fully random bytes */
>   for (i = 0; i < count; i++)
> - buf[i] = (u8)prandom_u32();
> + buf[i] = get_random_u8();

Should that whole for loop be replaced with this?
get_random_bytes(buf, count);




Re: [PATCH v6 3/7] treewide: use get_random_{u8,u16}() when possible, part 1

2022-10-10 Thread Jason A. Donenfeld
On Tue, Oct 11, 2022 at 01:18:40AM +, Elliott, Robert (Servers) wrote:
> 
> > diff --git a/crypto/testmgr.c b/crypto/testmgr.c
> ...
> > @@ -944,7 +944,7 @@ static void generate_random_bytes(u8 *buf, size_t count)
> > default:
> > /* Fully random bytes */
> > for (i = 0; i < count; i++)
> > -   buf[i] = (u8)prandom_u32();
> > +   buf[i] = get_random_u8();
> 
> Should that whole for loop be replaced with this?
> get_random_bytes(buf, count);

Wow, that's kind of grotesque. Yea, it certainly should. But that's
beyond the scope of this patchset. I'll send a follow-up patch just for
this case to Herbert after this cleanup lands, though.

Jason


Re: [GIT PULL] Please pull powerpc/linux.git powerpc-6.1-1 tag

2022-10-10 Thread Jason A. Donenfeld
On Tue, Oct 11, 2022 at 12:53:17PM +1100, Michael Ellerman wrote:
> "Jason A. Donenfeld"  writes:
> > On Mon, Oct 10, 2022 at 01:25:25PM -0600, Jason A. Donenfeld wrote:
> >> Hi Michael,
> >> 
> >> On Sun, Oct 09, 2022 at 10:01:39PM +1100, Michael Ellerman wrote:
> >> > powerpc updates for 6.1
> >> > 
> >> >  - Remove our now never-true definitions for pgd_huge() and p4d_leaf().
> >> > 
> >> >  - Add pte_needs_flush() and huge_pmd_needs_flush() for 64-bit.
> >> > 
> >> >  - Add support for syscall wrappers.
> >> > 
> >> >  - Add support for KFENCE on 64-bit.
> >> > 
> >> >  - Update 64-bit HV KVM to use the new guest state entry/exit accounting 
> >> > API.
> >> > 
> >> >  - Support execute-only memory when using the Radix MMU (P9 or later).
> >> > 
> >> >  - Implement CONFIG_PARAVIRT_TIME_ACCOUNTING for pseries guests.
> >> > 
> >> >  - Updates to our linker script to move more data into read-only 
> >> > sections.
> >> > 
> >> >  - Allow the VDSO to be randomised on 32-bit.
> >> > 
> >> >  - Many other small features and fixes.
> >> 
> >> FYI, something in here broke the wireguard test suite, which runs the
> >> iperf3 networking utility. The full log is here [1], but the relevant part
> >> is: 
> >> 
> >> [+] NS1: iperf3 -Z -t 3 -c 192.168.241.2
> >> Connecting to host 192.168.241.2, port 5201
> >> iperf3: error - failed to read /dev/urandom: Bad address
> >> 
> >> I'll see if I can narrow it down a bit more and bisect. But just FYI, in
> >> case you have an intuition.
> >
> > Huh. From iov_iter.c:
> >
> > static int copyout(void __user *to, const void *from, size_t n)
> > {
> > size_t before = n;
> > if (should_fail_usercopy())
> > return n;
> > if (access_ok(to, n)) {
> > instrument_copy_to_user(to, from, n);
> > n = raw_copy_to_user(to, from, n);
> > if (n == before)
> > pr_err("SARU n still %zu pointer is %lx\n", n, 
> > (unsigned long)to);
> > }
> > return n;
> > }
> >
> > I added the pr_err() there to catch the failure:
> > [3.443506] SARU n still 64 pointer is b78db000
> >
> > Also I managed to extract the failing portion of iperf3 into something
> > smaller:
> >
> > int temp;
> > char *x;
> > ssize_t l;
> > FILE *f;
> > char template[] = "/blah-XX";
> >
> > temp = mkstemp(template);
> > if (temp < 0)
> > panic("mkstemp");
> > if (unlink(template) < 0)
> > panic("unlink");
> > if (ftruncate(temp, 0x2) < 0)
> > panic("ftruncate");
> > x = mmap(NULL, 0x2, PROT_READ|PROT_WRITE, MAP_PRIVATE, temp, 0);
> > if (x == MAP_FAILED)
> > panic("mmap");
> > f = fopen("/dev/urandom", "rb");
> > if (!f)
> > panic("fopen");
> > setbuf(f, NULL);
> > if (fread(x, 1, 0x2, f) != 0x2)
> > panic("fread");
> 
> Does that fail for you reliably?
> 
> It succeeds for me running under qemu ppce500, though I'm not using your
> kernel config yet.

Yes, every time without fail, across two systems and two qemu builds.

Jason


Re: [GIT PULL] Please pull powerpc/linux.git powerpc-6.1-1 tag

2022-10-10 Thread Jason A. Donenfeld
On Tue, Oct 11, 2022 at 12:44:20PM +1100, Michael Ellerman wrote:
> "Jason A. Donenfeld"  writes:
> > Hi Andrew,
> >
> > On Tue, Oct 11, 2022 at 11:00:15AM +1100, Andrew Donnellan wrote:
> >> Thanks for bisecting, this is interesting! Could you provide your
> >> .config and the environment you're running in? Your reproducer doesn't
> >> seem to trigger it on my baremetal POWER8 pseries_le_defconfig.
> >
> > Sure.
> >
> > .config: https://xn--4db.cc/NemFt2Vs (change CONFIG_INITRAMFS_SOURCE)
> > Toolchain: 
> > https://download.wireguard.com/qemu-test/toolchains/20211123/powerpc-linux-musl-cross.tgz
> >
> > You can also just run:
> >
> >   ARCH=powerpc make -C tools/testing/selftests/wireguard/qemu -j$(nproc)
> >
> > And that'll assemble the whole thing.
> 
> I tried that :)
> 
> What host OS are you running that on?
> 
> I get:
> 
>   mkdir -p 
> /scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc
>   powerpc-linux-musl-gcc -o 
> /scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/init
>  -O3 -pipe  -std=gnu11 init.c
>   
> /scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/powerpc-linux-musl-cross/bin/../lib/gcc/powerpc-linux-musl/11.2.1/../../../../powerpc-linux-musl/bin/ld:
>  cannot find Scrt1.o: No such file or directory
>   
> /scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/powerpc-linux-musl-cross/bin/../lib/gcc/powerpc-linux-musl/11.2.1/../../../../powerpc-linux-musl/bin/ld:
>  cannot find crti.o: No such file or directory
>   
> /scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/powerpc-linux-musl-cross/bin/../lib/gcc/powerpc-linux-musl/11.2.1/../../../../powerpc-linux-musl/bin/ld:
>  cannot find crtbeginS.o: No such file or directory
>   
> /scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/powerpc-linux-musl-cross/bin/../lib/gcc/powerpc-linux-musl/11.2.1/../../../../powerpc-linux-musl/bin/ld:
>  cannot find -lgcc
>   
> /scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/powerpc-linux-musl-cross/bin/../lib/gcc/powerpc-linux-musl/11.2.1/../../../../powerpc-linux-musl/bin/ld:
>  cannot find -lgcc
>   collect2: error: ld returned 1 exit status

Here's what happened:

- You started the thing and the kernel compile complained about an
  unclean tree.
- You ran mrproper.
- You tried to run the thing again.

amirite?

If so, what happened is that mrproper deleted the .o files from the
toolchain. Solution:

  ARCH=powerpc make -C tools/testing/selftests/wireguard/qemu -j$(nproc) clean
  ARCH=powerpc make -C tools/testing/selftests/wireguard/qemu -j$(nproc)

Let me know how that goes.

Jason


Re: [PATCH linux-next][RFC] powerpc: fix HOTPLUG error in rcutorture

2022-10-10 Thread Zhouyi Zhou
Thanks Michael for reviewing my patch

On Mon, Oct 10, 2022 at 7:21 PM Michael Ellerman  wrote:
>
> Zhouyi Zhou  writes:
> > I think we should avoid torture offline the cpu who do tick timer
> > when nohz full is running.
>
> Can you tell us what the bug you're fixing is?
>
> Did you see a crash/oops/hang etc? Or are you just proposing this as
> something that would be a good idea?
Sorry for the trouble and inconvenience that I bring to the community.
I haven't made myself clear in my patch.
The ins and outs are as follows:
1) cd linux-next
2) ./tools/testing/selftests/rcutorture/bin/torture.sh
after 19 hours ;-)
3) tail  
./tools/testing/selftests/rcutorture/res/2022.09.30-01.06.22-torture/results-scftorture/NOPREEMPT/console.log

[  121.449268][   T57] scftorture:  scf_invoked_count VER: 2415215
resched: 697463 single: 619512/619760 single_ofl: 255751/256554
single_rpc: 620692 single_rpc_ofl: 0 many: 155476/154658 all:
77282/76988 onoff: 3/3:5/6 18,25:9,28 63:93 (HZ=100) ste: 0 stnmie: 0
stnmoe: 0 staf: 0
[  121.454485][   T57] scftorture: --- End of test: LOCK_HOTPLUG:
verbose=1 holdoff=10 longwait=0 nthreads=4 onoff_holdoff=30
onoff_interval=1000 shutdown_secs=1 stat_interval=15 stutter=5
use_cpus_read_lock=0, weight_resched=-1, weight_single=-1,
weight_single_rpc=-1, weight_single_wait=-1, weight_many=-1,
weight_many_wait=-1, weight_all=-1, weight_all_wait=-1
[  121.469305][   T57] reboot: Power down

I see "End of test: LOCK_HOTPLUG", which means the function
torture_offline in kernel torture.c failed to bring down the cpu.
4) Then I chase the reason down to tick_nohz_cpu_down:
if (tick_nohz_full_running && tick_do_timer_cpu == cpu)
  return -EBUSY;
5) I create above patch
>
> > Tested on PPC VM of Open Source Lab of Oregon State University.
> > The test results show that after the fix, the success rate of
> > rcutorture is improved.
> > After:
> > Successes: 40 Failures: 9
> > Before:
> > Successes: 38 Failures: 11
> >
> > I examined the console.log and Make.out files one by one, no new
> > compile error or test error is introduced by above fix.
> >
> > Signed-off-by: Zhouyi Zhou 
> > ---
> > Dear PPC developers
> >
> > I found this bug when trying to do rcutorture tests in ppc VM of
> > Open Source Lab of Oregon State University:
> >
> > ubuntu@ubuntu:~/linux-next/tools/testing/selftests/rcutorture/res/2022.09.30-01.06.22-torture$
> >  find . -name "console.log.diags"|xargs grep HOTPLUG
> > ./results-scftorture/NOPREEMPT/console.log.diags:WARNING: HOTPLUG FAILURES 
> > NOPREEMPT
> > ./results-rcutorture/TASKS03/console.log.diags:WARNING: HOTPLUG FAILURES 
> > TASKS03
> > ./results-rcutorture/TREE04/console.log.diags:WARNING: HOTPLUG FAILURES 
> > TREE04
> > ./results-scftorture-kasan/NOPREEMPT/console.log.diags:WARNING: HOTPLUG 
> > FAILURES NOPREEMPT
> > ./results-rcutorture-kasan/TASKS03/console.log.diags:WARNING: HOTPLUG 
> > FAILURES TASKS03
> > ./results-rcutorture-kasan/TREE04/console.log.diags:WARNING: HOTPLUG 
> > FAILURES TREE04
> >
> > I tried to fix this bug.
> >
> > Thanks for your patience and guidance ;-)
> >
> > Thanks
> > Zhouyi
> > --
> >  arch/powerpc/kernel/sysfs.c | 8 +++-
> >  1 file changed, 7 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/powerpc/kernel/sysfs.c b/arch/powerpc/kernel/sysfs.c
> > index ef9a61718940..be9c0e45337e 100644
> > --- a/arch/powerpc/kernel/sysfs.c
> > +++ b/arch/powerpc/kernel/sysfs.c
> > @@ -4,6 +4,7 @@
> >  #include 
> >  #include 
> >  #include 
> > +#include 
> >  #include 
> >  #include 
> >  #include 
> > @@ -21,6 +22,7 @@
> >  #include 
> >  #include 
> >  #include 
> > +#include "../../../kernel/time/tick-internal.h"
>
> Needing to include this internal header is a sign that we are using the
> wrong API or otherwise using time keeping internals we shouldn't be.
Yes, when I do this, I guess there is something wrong in my patch.
>
> >  #include "cacheinfo.h"
> >  #include "setup.h"
> > @@ -1151,7 +1153,11 @@ static int __init topology_init(void)
> >* CPU.  For instance, the boot cpu might never be valid
> >* for hotplugging.
> >*/
> > - if (smp_ops && smp_ops->cpu_offline_self)
> > + if (smp_ops && smp_ops->cpu_offline_self
> > +#ifdef CONFIG_NO_HZ_FULL
> > + && !(tick_nohz_full_running && tick_do_timer_cpu == cpu)
> > +#endif
> > + )
>
> I can't see any other arches doing anything like this. I don't think
> it's the arches responsibility.
Agree!

X86 seems to disable CPU0's hotplug by default, while
tick_do_timer_cpu has a default value 0.

42 #ifdef CONFIG_BOOTPARAM_HOTPLUG_CPU0
43 static int cpu0_hotpluggable = 1;
44 #else
45 static int cpu0_hotpluggable;
46 static int __init enable_cpu0_hotplug(char *str)
47 {
48 cpu0_hotpluggable = 1;
49 return 1;
50 }
51
52 __setup("cpu0_hotplug", enable_cpu0_hotplug);
53 #endif

I need more time to make clear the relationship of X86's
cpu0_hotpluggable and 

Re: [GIT PULL] Please pull powerpc/linux.git powerpc-6.1-1 tag

2022-10-10 Thread Michael Ellerman
"Jason A. Donenfeld"  writes:
> On Mon, Oct 10, 2022 at 01:25:25PM -0600, Jason A. Donenfeld wrote:
>> Hi Michael,
>> 
>> On Sun, Oct 09, 2022 at 10:01:39PM +1100, Michael Ellerman wrote:
>> > powerpc updates for 6.1
>> > 
>> >  - Remove our now never-true definitions for pgd_huge() and p4d_leaf().
>> > 
>> >  - Add pte_needs_flush() and huge_pmd_needs_flush() for 64-bit.
>> > 
>> >  - Add support for syscall wrappers.
>> > 
>> >  - Add support for KFENCE on 64-bit.
>> > 
>> >  - Update 64-bit HV KVM to use the new guest state entry/exit accounting 
>> > API.
>> > 
>> >  - Support execute-only memory when using the Radix MMU (P9 or later).
>> > 
>> >  - Implement CONFIG_PARAVIRT_TIME_ACCOUNTING for pseries guests.
>> > 
>> >  - Updates to our linker script to move more data into read-only sections.
>> > 
>> >  - Allow the VDSO to be randomised on 32-bit.
>> > 
>> >  - Many other small features and fixes.
>> 
>> FYI, something in here broke the wireguard test suite, which runs the
>> iperf3 networking utility. The full log is here [1], but the relevant part
>> is: 
>> 
>> [+] NS1: iperf3 -Z -t 3 -c 192.168.241.2
>> Connecting to host 192.168.241.2, port 5201
>> iperf3: error - failed to read /dev/urandom: Bad address
>> 
>> I'll see if I can narrow it down a bit more and bisect. But just FYI, in
>> case you have an intuition.
>
> Huh. From iov_iter.c:
>
> static int copyout(void __user *to, const void *from, size_t n)
> {
> size_t before = n;
> if (should_fail_usercopy())
> return n;
> if (access_ok(to, n)) {
> instrument_copy_to_user(to, from, n);
> n = raw_copy_to_user(to, from, n);
> if (n == before)
> pr_err("SARU n still %zu pointer is %lx\n", n, 
> (unsigned long)to);
> }
> return n;
> }
>
> I added the pr_err() there to catch the failure:
> [3.443506] SARU n still 64 pointer is b78db000
>
> Also I managed to extract the failing portion of iperf3 into something
> smaller:
>
> int temp;
> char *x;
> ssize_t l;
> FILE *f;
> char template[] = "/blah-XX";
>
> temp = mkstemp(template);
> if (temp < 0)
> panic("mkstemp");
> if (unlink(template) < 0)
> panic("unlink");
> if (ftruncate(temp, 0x2) < 0)
> panic("ftruncate");
> x = mmap(NULL, 0x2, PROT_READ|PROT_WRITE, MAP_PRIVATE, temp, 0);
> if (x == MAP_FAILED)
> panic("mmap");
> f = fopen("/dev/urandom", "rb");
> if (!f)
> panic("fopen");
> setbuf(f, NULL);
> if (fread(x, 1, 0x2, f) != 0x2)
> panic("fread");

Does that fail for you reliably?

It succeeds for me running under qemu ppce500, though I'm not using your
kernel config yet.

cheers


Re: [GIT PULL] Please pull powerpc/linux.git powerpc-6.1-1 tag

2022-10-10 Thread Michael Ellerman
"Jason A. Donenfeld"  writes:
> Hi Andrew,
>
> On Tue, Oct 11, 2022 at 11:00:15AM +1100, Andrew Donnellan wrote:
>> Thanks for bisecting, this is interesting! Could you provide your
>> .config and the environment you're running in? Your reproducer doesn't
>> seem to trigger it on my baremetal POWER8 pseries_le_defconfig.
>
> Sure.
>
> .config: https://xn--4db.cc/NemFt2Vs (change CONFIG_INITRAMFS_SOURCE)
> Toolchain: 
> https://download.wireguard.com/qemu-test/toolchains/20211123/powerpc-linux-musl-cross.tgz
>
> You can also just run:
>
>   ARCH=powerpc make -C tools/testing/selftests/wireguard/qemu -j$(nproc)
>
> And that'll assemble the whole thing.

I tried that :)

What host OS are you running that on?

I get:

  mkdir -p 
/scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc
  powerpc-linux-musl-gcc -o 
/scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/init
 -O3 -pipe  -std=gnu11 init.c
  
/scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/powerpc-linux-musl-cross/bin/../lib/gcc/powerpc-linux-musl/11.2.1/../../../../powerpc-linux-musl/bin/ld:
 cannot find Scrt1.o: No such file or directory
  
/scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/powerpc-linux-musl-cross/bin/../lib/gcc/powerpc-linux-musl/11.2.1/../../../../powerpc-linux-musl/bin/ld:
 cannot find crti.o: No such file or directory
  
/scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/powerpc-linux-musl-cross/bin/../lib/gcc/powerpc-linux-musl/11.2.1/../../../../powerpc-linux-musl/bin/ld:
 cannot find crtbeginS.o: No such file or directory
  
/scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/powerpc-linux-musl-cross/bin/../lib/gcc/powerpc-linux-musl/11.2.1/../../../../powerpc-linux-musl/bin/ld:
 cannot find -lgcc
  
/scratch/michael/linus/tools/testing/selftests/wireguard/qemu/build/powerpc/powerpc-linux-musl-cross/bin/../lib/gcc/powerpc-linux-musl/11.2.1/../../../../powerpc-linux-musl/bin/ld:
 cannot find -lgcc
  collect2: error: ld returned 1 exit status

cheers


[PATCH v6 7/7] prandom: remove unused functions

2022-10-10 Thread Jason A. Donenfeld
With no callers left of prandom_u32() and prandom_bytes(), as well as
get_random_int(), remove these deprecated wrappers, in favor of
get_random_u32() and get_random_bytes().

Reviewed-by: Greg Kroah-Hartman 
Reviewed-by: Kees Cook 
Reviewed-by: Yury Norov 
Signed-off-by: Jason A. Donenfeld 
---
 drivers/char/random.c   | 11 +--
 include/linux/prandom.h | 12 
 include/linux/random.h  |  5 -
 3 files changed, 5 insertions(+), 23 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 01acf235f263..2fe28eeb2f38 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -97,7 +97,7 @@ MODULE_PARM_DESC(ratelimit_disable, "Disable random ratelimit 
suppression");
  * Returns whether or not the input pool has been seeded and thus guaranteed
  * to supply cryptographically secure random numbers. This applies to: the
  * /dev/urandom device, the get_random_bytes function, and the get_random_{u8,
- * u16,u32,u64,int,long} family of functions.
+ * u16,u32,u64,long} family of functions.
  *
  * Returns: true if the input pool has been seeded.
  *  false if the input pool has not been seeded.
@@ -161,15 +161,14 @@ EXPORT_SYMBOL(wait_for_random_bytes);
  * u16 get_random_u16()
  * u32 get_random_u32()
  * u64 get_random_u64()
- * unsigned int get_random_int()
  * unsigned long get_random_long()
  *
  * These interfaces will return the requested number of random bytes
  * into the given buffer or as a return value. This is equivalent to
- * a read from /dev/urandom. The u8, u16, u32, u64, int, and long
- * family of functions may be higher performance for one-off random
- * integers, because they do a bit of buffering and do not invoke
- * reseeding until the buffer is emptied.
+ * a read from /dev/urandom. The u8, u16, u32, u64, long family of
+ * functions may be higher performance for one-off random integers,
+ * because they do a bit of buffering and do not invoke reseeding
+ * until the buffer is emptied.
  *
  */
 
diff --git a/include/linux/prandom.h b/include/linux/prandom.h
index 78db003bc290..e0a0759dd09c 100644
--- a/include/linux/prandom.h
+++ b/include/linux/prandom.h
@@ -12,18 +12,6 @@
 #include 
 #include 
 
-/* Deprecated: use get_random_u32 instead. */
-static inline u32 prandom_u32(void)
-{
-   return get_random_u32();
-}
-
-/* Deprecated: use get_random_bytes instead. */
-static inline void prandom_bytes(void *buf, size_t nbytes)
-{
-   return get_random_bytes(buf, nbytes);
-}
-
 struct rnd_state {
__u32 s1, s2, s3, s4;
 };
diff --git a/include/linux/random.h b/include/linux/random.h
index 08322f700cdc..147a5e0d0b8e 100644
--- a/include/linux/random.h
+++ b/include/linux/random.h
@@ -42,10 +42,6 @@ u8 get_random_u8(void);
 u16 get_random_u16(void);
 u32 get_random_u32(void);
 u64 get_random_u64(void);
-static inline unsigned int get_random_int(void)
-{
-   return get_random_u32();
-}
 static inline unsigned long get_random_long(void)
 {
 #if BITS_PER_LONG == 64
@@ -100,7 +96,6 @@ declare_get_random_var_wait(u8, u8)
 declare_get_random_var_wait(u16, u16)
 declare_get_random_var_wait(u32, u32)
 declare_get_random_var_wait(u64, u32)
-declare_get_random_var_wait(int, unsigned int)
 declare_get_random_var_wait(long, unsigned long)
 #undef declare_get_random_var
 
-- 
2.37.3



[PATCH v6 6/7] treewide: use get_random_bytes() when possible

2022-10-10 Thread Jason A. Donenfeld
The prandom_bytes() function has been a deprecated inline wrapper around
get_random_bytes() for several releases now, and compiles down to the
exact same code. Replace the deprecated wrapper with a direct call to
the real function. This was done as a basic find and replace.

Reviewed-by: Greg Kroah-Hartman 
Reviewed-by: Kees Cook 
Reviewed-by: Yury Norov 
Reviewed-by: Christophe Leroy  # powerpc
Signed-off-by: Jason A. Donenfeld 
---
 arch/powerpc/crypto/crc-vpmsum_test.c   |  2 +-
 block/blk-crypto-fallback.c |  2 +-
 crypto/async_tx/raid6test.c |  2 +-
 drivers/dma/dmatest.c   |  2 +-
 drivers/mtd/nand/raw/nandsim.c  |  2 +-
 drivers/mtd/tests/mtd_nandecctest.c |  2 +-
 drivers/mtd/tests/speedtest.c   |  2 +-
 drivers/mtd/tests/stresstest.c  |  2 +-
 drivers/net/ethernet/broadcom/bnxt/bnxt.c   |  2 +-
 drivers/net/ethernet/rocker/rocker_main.c   |  2 +-
 drivers/net/wireguard/selftest/allowedips.c | 12 ++--
 fs/ubifs/debug.c|  2 +-
 kernel/kcsan/selftest.c |  2 +-
 lib/random32.c  |  2 +-
 lib/test_objagg.c   |  2 +-
 lib/uuid.c  |  2 +-
 net/ipv4/route.c|  2 +-
 net/mac80211/rc80211_minstrel_ht.c  |  2 +-
 net/sched/sch_pie.c |  2 +-
 19 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/arch/powerpc/crypto/crc-vpmsum_test.c 
b/arch/powerpc/crypto/crc-vpmsum_test.c
index c1c1ef9457fb..273c527868db 100644
--- a/arch/powerpc/crypto/crc-vpmsum_test.c
+++ b/arch/powerpc/crypto/crc-vpmsum_test.c
@@ -82,7 +82,7 @@ static int __init crc_test_init(void)
 
if (len <= offset)
continue;
-   prandom_bytes(data, len);
+   get_random_bytes(data, len);
len -= offset;
 
crypto_shash_update(crct10dif_shash, data+offset, len);
diff --git a/block/blk-crypto-fallback.c b/block/blk-crypto-fallback.c
index 621abd1b0e4d..ad9844c5b40c 100644
--- a/block/blk-crypto-fallback.c
+++ b/block/blk-crypto-fallback.c
@@ -539,7 +539,7 @@ static int blk_crypto_fallback_init(void)
if (blk_crypto_fallback_inited)
return 0;
 
-   prandom_bytes(blank_key, BLK_CRYPTO_MAX_KEY_SIZE);
+   get_random_bytes(blank_key, BLK_CRYPTO_MAX_KEY_SIZE);
 
err = bioset_init(_bio_split, 64, 0, 0);
if (err)
diff --git a/crypto/async_tx/raid6test.c b/crypto/async_tx/raid6test.c
index c9d218e53bcb..f74505f2baf0 100644
--- a/crypto/async_tx/raid6test.c
+++ b/crypto/async_tx/raid6test.c
@@ -37,7 +37,7 @@ static void makedata(int disks)
int i;
 
for (i = 0; i < disks; i++) {
-   prandom_bytes(page_address(data[i]), PAGE_SIZE);
+   get_random_bytes(page_address(data[i]), PAGE_SIZE);
dataptrs[i] = data[i];
dataoffs[i] = 0;
}
diff --git a/drivers/dma/dmatest.c b/drivers/dma/dmatest.c
index 9fe2ae794316..ffe621695e47 100644
--- a/drivers/dma/dmatest.c
+++ b/drivers/dma/dmatest.c
@@ -312,7 +312,7 @@ static unsigned long dmatest_random(void)
 {
unsigned long buf;
 
-   prandom_bytes(, sizeof(buf));
+   get_random_bytes(, sizeof(buf));
return buf;
 }
 
diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index d211939c8bdd..672719023241 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -1393,7 +1393,7 @@ static int ns_do_read_error(struct nandsim *ns, int num)
unsigned int page_no = ns->regs.row;
 
if (ns_read_error(page_no)) {
-   prandom_bytes(ns->buf.byte, num);
+   get_random_bytes(ns->buf.byte, num);
NS_WARN("simulating read error in page %u\n", page_no);
return 1;
}
diff --git a/drivers/mtd/tests/mtd_nandecctest.c 
b/drivers/mtd/tests/mtd_nandecctest.c
index 1c7201b0f372..440988562cfd 100644
--- a/drivers/mtd/tests/mtd_nandecctest.c
+++ b/drivers/mtd/tests/mtd_nandecctest.c
@@ -266,7 +266,7 @@ static int nand_ecc_test_run(const size_t size)
goto error;
}
 
-   prandom_bytes(correct_data, size);
+   get_random_bytes(correct_data, size);
ecc_sw_hamming_calculate(correct_data, size, correct_ecc, sm_order);
for (i = 0; i < ARRAY_SIZE(nand_ecc_test); i++) {
nand_ecc_test[i].prepare(error_data, error_ecc,
diff --git a/drivers/mtd/tests/speedtest.c b/drivers/mtd/tests/speedtest.c
index c9ec7086bfa1..075bce32caa5 100644
--- a/drivers/mtd/tests/speedtest.c
+++ b/drivers/mtd/tests/speedtest.c
@@ -223,7 +223,7 @@ static int __init mtd_speedtest_init(void)
if (!iobuf)
goto out;
 
-   prandom_bytes(iobuf, mtd->erasesize);
+   get_random_bytes(iobuf, 

[PATCH v6 5/7] treewide: use get_random_u32() when possible

2022-10-10 Thread Jason A. Donenfeld
The prandom_u32() function has been a deprecated inline wrapper around
get_random_u32() for several releases now, and compiles down to the
exact same code. Replace the deprecated wrapper with a direct call to
the real function. The same also applies to get_random_int(), which is
just a wrapper around get_random_u32(). This was done as a basic find
and replace.

Reviewed-by: Greg Kroah-Hartman 
Reviewed-by: Kees Cook 
Reviewed-by: Yury Norov 
Acked-by: Toke Høiland-Jørgensen  # for sch_cake
Acked-by: Chuck Lever  # for nfsd
Reviewed-by: Jan Kara  # for ext4
Acked-by: Mika Westerberg  # for thunderbolt
Acked-by: Darrick J. Wong  # for xfs
Signed-off-by: Jason A. Donenfeld 
---
 Documentation/networking/filter.rst|  2 +-
 arch/parisc/kernel/process.c   |  2 +-
 arch/parisc/kernel/sys_parisc.c|  4 ++--
 arch/s390/mm/mmap.c|  2 +-
 arch/x86/kernel/cpu/amd.c  |  2 +-
 drivers/gpu/drm/i915/i915_gem_gtt.c|  6 +++---
 drivers/gpu/drm/i915/selftests/i915_selftest.c |  2 +-
 drivers/gpu/drm/tests/drm_buddy_test.c |  2 +-
 drivers/gpu/drm/tests/drm_mm_test.c|  2 +-
 drivers/infiniband/hw/cxgb4/cm.c   |  4 ++--
 drivers/infiniband/hw/hfi1/tid_rdma.c  |  2 +-
 drivers/infiniband/hw/mlx4/mad.c   |  2 +-
 drivers/infiniband/ulp/ipoib/ipoib_cm.c|  2 +-
 drivers/md/raid5-cache.c   |  2 +-
 .../media/test-drivers/vivid/vivid-touch-cap.c |  4 ++--
 drivers/misc/habanalabs/gaudi2/gaudi2.c|  2 +-
 drivers/net/bonding/bond_main.c|  2 +-
 drivers/net/ethernet/broadcom/cnic.c   |  2 +-
 .../chelsio/inline_crypto/chtls/chtls_cm.c |  2 +-
 drivers/net/ethernet/rocker/rocker_main.c  |  6 +++---
 .../wireless/broadcom/brcm80211/brcmfmac/pno.c |  2 +-
 .../net/wireless/marvell/mwifiex/cfg80211.c|  4 ++--
 .../net/wireless/microchip/wilc1000/cfg80211.c |  2 +-
 .../net/wireless/quantenna/qtnfmac/cfg80211.c  |  2 +-
 drivers/net/wireless/ti/wlcore/main.c  |  2 +-
 drivers/nvme/common/auth.c |  2 +-
 drivers/scsi/cxgbi/cxgb4i/cxgb4i.c |  4 ++--
 drivers/target/iscsi/cxgbit/cxgbit_cm.c|  2 +-
 drivers/thunderbolt/xdomain.c  |  2 +-
 drivers/video/fbdev/uvesafb.c  |  2 +-
 fs/exfat/inode.c   |  2 +-
 fs/ext4/ialloc.c   |  2 +-
 fs/ext4/ioctl.c|  4 ++--
 fs/ext4/mmp.c  |  2 +-
 fs/f2fs/namei.c|  2 +-
 fs/fat/inode.c |  2 +-
 fs/nfsd/nfs4state.c|  4 ++--
 fs/ntfs3/fslog.c   |  6 +++---
 fs/ubifs/journal.c |  2 +-
 fs/xfs/libxfs/xfs_ialloc.c |  2 +-
 fs/xfs/xfs_icache.c|  2 +-
 fs/xfs/xfs_log.c   |  2 +-
 include/net/netfilter/nf_queue.h   |  2 +-
 include/net/red.h  |  2 +-
 include/net/sock.h |  2 +-
 kernel/bpf/bloom_filter.c  |  2 +-
 kernel/bpf/core.c  |  2 +-
 kernel/bpf/hashtab.c   |  2 +-
 kernel/bpf/verifier.c  |  2 +-
 kernel/kcsan/selftest.c|  2 +-
 lib/random32.c |  2 +-
 lib/reed_solomon/test_rslib.c  |  6 +++---
 lib/test_fprobe.c  |  2 +-
 lib/test_kprobes.c |  2 +-
 lib/test_min_heap.c|  6 +++---
 lib/test_rhashtable.c  |  6 +++---
 mm/shmem.c |  2 +-
 mm/slab.c  |  2 +-
 net/core/pktgen.c  |  4 ++--
 net/ipv4/route.c   |  2 +-
 net/ipv4/tcp_cdg.c |  2 +-
 net/ipv4/udp.c |  2 +-
 net/ipv6/ip6_flowlabel.c   |  2 +-
 net/ipv6/output_core.c |  2 +-
 net/netfilter/ipvs/ip_vs_conn.c|  2 +-
 net/netfilter/xt_statistic.c   |  2 +-
 net/openvswitch/actions.c  |  2 +-
 net/sched/sch_cake.c   |  2 +-
 net/sched/sch_netem.c  | 18 +-
 net/sunrpc/auth_gss/gss_krb5_wrap.c|  4 ++--
 net/sunrpc/xprt.c  |  2 +-
 net/unix/af_unix.c |  2 +-
 72 files changed, 101 insertions(+), 101 deletions(-)

diff --git a/Documentation/networking/filter.rst 
b/Documentation/networking/filter.rst
index 43cdc4d34745..f69da5074860 100644
--- 

[PATCH v6 4/7] treewide: use get_random_{u8,u16}() when possible, part 2

2022-10-10 Thread Jason A. Donenfeld
Rather than truncate a 32-bit value to a 16-bit value or an 8-bit value,
simply use the get_random_{u8,u16}() functions, which are faster than
wasting the additional bytes from a 32-bit value. This was done by hand,
identifying all of the places where one of the random integer functions
was used in a non-32-bit context.

Reviewed-by: Greg Kroah-Hartman 
Reviewed-by: Kees Cook 
Reviewed-by: Yury Norov 
Signed-off-by: Jason A. Donenfeld 
---
 arch/s390/kernel/process.c | 2 +-
 drivers/mtd/nand/raw/nandsim.c | 2 +-
 lib/test_vmalloc.c | 2 +-
 net/rds/bind.c | 2 +-
 net/sched/sch_sfb.c| 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/arch/s390/kernel/process.c b/arch/s390/kernel/process.c
index 5ec78555dd2e..42af4b3aa02b 100644
--- a/arch/s390/kernel/process.c
+++ b/arch/s390/kernel/process.c
@@ -230,7 +230,7 @@ unsigned long arch_align_stack(unsigned long sp)
 
 static inline unsigned long brk_rnd(void)
 {
-   return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
+   return (get_random_u16() & BRK_RND_MASK) << PAGE_SHIFT;
 }
 
 unsigned long arch_randomize_brk(struct mm_struct *mm)
diff --git a/drivers/mtd/nand/raw/nandsim.c b/drivers/mtd/nand/raw/nandsim.c
index 50bcf745e816..d211939c8bdd 100644
--- a/drivers/mtd/nand/raw/nandsim.c
+++ b/drivers/mtd/nand/raw/nandsim.c
@@ -1402,7 +1402,7 @@ static int ns_do_read_error(struct nandsim *ns, int num)
 
 static void ns_do_bit_flips(struct nandsim *ns, int num)
 {
-   if (bitflips && prandom_u32() < (1 << 22)) {
+   if (bitflips && get_random_u16() < (1 << 6)) {
int flips = 1;
if (bitflips > 1)
flips = prandom_u32_max(bitflips) + 1;
diff --git a/lib/test_vmalloc.c b/lib/test_vmalloc.c
index a26bbbf20e62..cf7780572f5b 100644
--- a/lib/test_vmalloc.c
+++ b/lib/test_vmalloc.c
@@ -80,7 +80,7 @@ static int random_size_align_alloc_test(void)
int i;
 
for (i = 0; i < test_loop_count; i++) {
-   rnd = prandom_u32();
+   rnd = get_random_u8();
 
/*
 * Maximum 1024 pages, if PAGE_SIZE is 4096.
diff --git a/net/rds/bind.c b/net/rds/bind.c
index 5b5fb4ca8d3e..97a29172a8ee 100644
--- a/net/rds/bind.c
+++ b/net/rds/bind.c
@@ -104,7 +104,7 @@ static int rds_add_bound(struct rds_sock *rs, const struct 
in6_addr *addr,
return -EINVAL;
last = rover;
} else {
-   rover = max_t(u16, prandom_u32(), 2);
+   rover = max_t(u16, get_random_u16(), 2);
last = rover - 1;
}
 
diff --git a/net/sched/sch_sfb.c b/net/sched/sch_sfb.c
index e2389fa3cff8..0366a1a029a9 100644
--- a/net/sched/sch_sfb.c
+++ b/net/sched/sch_sfb.c
@@ -379,7 +379,7 @@ static int sfb_enqueue(struct sk_buff *skb, struct Qdisc 
*sch,
goto enqueue;
}
 
-   r = prandom_u32() & SFB_MAX_PROB;
+   r = get_random_u16() & SFB_MAX_PROB;
 
if (unlikely(r < p_min)) {
if (unlikely(p_min > SFB_MAX_PROB / 2)) {
-- 
2.37.3



[PATCH v6 3/7] treewide: use get_random_{u8,u16}() when possible, part 1

2022-10-10 Thread Jason A. Donenfeld
Rather than truncate a 32-bit value to a 16-bit value or an 8-bit value,
simply use the get_random_{u8,u16}() functions, which are faster than
wasting the additional bytes from a 32-bit value. This was done
mechanically with this coccinelle script:

@@
expression E;
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
typedef u16;
typedef __be16;
typedef __le16;
typedef u8;
@@
(
- (get_random_u32() & 0x)
+ get_random_u16()
|
- (get_random_u32() & 0xff)
+ get_random_u8()
|
- (get_random_u32() % 65536)
+ get_random_u16()
|
- (get_random_u32() % 256)
+ get_random_u8()
|
- (get_random_u32() >> 16)
+ get_random_u16()
|
- (get_random_u32() >> 24)
+ get_random_u8()
|
- (u16)get_random_u32()
+ get_random_u16()
|
- (u8)get_random_u32()
+ get_random_u8()
|
- (__be16)get_random_u32()
+ (__be16)get_random_u16()
|
- (__le16)get_random_u32()
+ (__le16)get_random_u16()
|
- prandom_u32_max(65536)
+ get_random_u16()
|
- prandom_u32_max(256)
+ get_random_u8()
|
- E->inet_id = get_random_u32()
+ E->inet_id = get_random_u16()
)

@@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
typedef u16;
identifier v;
@@
- u16 v = get_random_u32();
+ u16 v = get_random_u16();

@@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
typedef u8;
identifier v;
@@
- u8 v = get_random_u32();
+ u8 v = get_random_u8();

@@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
typedef u16;
u16 v;
@@
-  v = get_random_u32();
+  v = get_random_u16();

@@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
typedef u8;
u8 v;
@@
-  v = get_random_u32();
+  v = get_random_u8();

// Find a potential literal
@literal_mask@
expression LITERAL;
type T;
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
position p;
@@

((T)get_random_u32()@p & (LITERAL))

// Examine limits
@script:python add_one@
literal << literal_mask.LITERAL;
RESULT;
@@

value = None
if literal.startswith('0x'):
value = int(literal, 16)
elif literal[0] in '123456789':
value = int(literal, 10)
if value is None:
print("I don't know how to handle %s" % (literal))
cocci.include_match(False)
elif value < 256:
coccinelle.RESULT = cocci.make_ident("get_random_u8")
elif value < 65536:
coccinelle.RESULT = cocci.make_ident("get_random_u16")
else:
print("Skipping large mask of %s" % (literal))
cocci.include_match(False)

// Replace the literal mask with the calculated result.
@plus_one@
expression literal_mask.LITERAL;
position literal_mask.p;
identifier add_one.RESULT;
identifier FUNC;
@@

-   (FUNC()@p & (LITERAL))
+   (RESULT() & LITERAL)

Reviewed-by: Greg Kroah-Hartman 
Reviewed-by: Kees Cook 
Reviewed-by: Yury Norov 
Acked-by: Toke Høiland-Jørgensen  # for sch_cake
Signed-off-by: Jason A. Donenfeld 
---
 arch/arm/kernel/signal.c  | 2 +-
 arch/arm64/kernel/syscall.c   | 2 +-
 crypto/testmgr.c  | 8 
 drivers/media/common/v4l2-tpg/v4l2-tpg-core.c | 2 +-
 drivers/media/test-drivers/vivid/vivid-radio-rx.c | 4 ++--
 .../net/ethernet/chelsio/inline_crypto/chtls/chtls_cm.c   | 2 +-
 drivers/net/hamradio/baycom_epp.c | 2 +-
 drivers/net/hamradio/hdlcdrv.c| 2 +-
 drivers/net/hamradio/yam.c| 2 +-
 drivers/net/wireguard/selftest/allowedips.c   | 4 ++--
 drivers/net/wireless/st/cw1200/wsm.c  | 2 +-
 drivers/scsi/lpfc/lpfc_hbadisc.c  | 6 +++---
 lib/cmdline_kunit.c   | 4 ++--
 net/dccp/ipv4.c   | 4 ++--
 net/ipv4/datagram.c   | 2 +-
 net/ipv4/ip_output.c  | 2 +-
 net/ipv4/tcp_ipv4.c   | 4 ++--
 net/mac80211/scan.c   | 2 +-
 net/netfilter/nf_nat_core.c   | 4 ++--
 net/sched/sch_cake.c  | 6 +++---
 net/sctp/socket.c | 2 +-
 21 files changed, 34 insertions(+), 34 deletions(-)

diff --git a/arch/arm/kernel/signal.c b/arch/arm/kernel/signal.c
index ea128e32e8ca..e07f359254c3 100644
--- a/arch/arm/kernel/signal.c
+++ b/arch/arm/kernel/signal.c
@@ -655,7 +655,7 @@ struct page *get_signal_page(void)
 PAGE_SIZE / sizeof(u32));
 
/* Give the signal return code some randomness */
-   offset = 0x200 + (get_random_int() & 0x7fc);
+   offset = 0x200 + (get_random_u16() & 0x7fc);
signal_return_offset = offset;
 
/* Copy signal return handlers into the page */
diff --git a/arch/arm64/kernel/syscall.c b/arch/arm64/kernel/syscall.c
index 733451fe7e41..d72e8f23422d 100644
--- 

[PATCH v6 2/7] treewide: use prandom_u32_max() when possible, part 2

2022-10-10 Thread Jason A. Donenfeld
Rather than incurring a division or requesting too many random bytes for
the given range, use the prandom_u32_max() function, which only takes
the minimum required bytes from the RNG and avoids divisions. This was
done by hand, covering things that coccinelle could not do on its own.

Reviewed-by: Greg Kroah-Hartman 
Reviewed-by: Kees Cook 
Reviewed-by: Yury Norov 
Reviewed-by: Jan Kara  # for ext2, ext4, and sbitmap
Signed-off-by: Jason A. Donenfeld 
---
 fs/ext2/ialloc.c   |  3 +--
 fs/ext4/ialloc.c   |  5 ++---
 lib/sbitmap.c  |  2 +-
 lib/test_vmalloc.c | 17 -
 4 files changed, 8 insertions(+), 19 deletions(-)

diff --git a/fs/ext2/ialloc.c b/fs/ext2/ialloc.c
index 998dd2ac8008..f4944c4dee60 100644
--- a/fs/ext2/ialloc.c
+++ b/fs/ext2/ialloc.c
@@ -277,8 +277,7 @@ static int find_group_orlov(struct super_block *sb, struct 
inode *parent)
int best_ndir = inodes_per_group;
int best_group = -1;
 
-   group = prandom_u32();
-   parent_group = (unsigned)group % ngroups;
+   parent_group = prandom_u32_max(ngroups);
for (i = 0; i < ngroups; i++) {
group = (parent_group + i) % ngroups;
desc = ext2_get_group_desc (sb, group, NULL);
diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 208b87ce8858..7575aa359675 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -463,10 +463,9 @@ static int find_group_orlov(struct super_block *sb, struct 
inode *parent,
hinfo.hash_version = DX_HASH_HALF_MD4;
hinfo.seed = sbi->s_hash_seed;
ext4fs_dirhash(parent, qstr->name, qstr->len, );
-   grp = hinfo.hash;
+   parent_group = hinfo.hash % ngroups;
} else
-   grp = prandom_u32();
-   parent_group = (unsigned)grp % ngroups;
+   parent_group = prandom_u32_max(ngroups);
for (i = 0; i < ngroups; i++) {
g = (parent_group + i) % ngroups;
get_orlov_stats(sb, g, flex_size, );
diff --git a/lib/sbitmap.c b/lib/sbitmap.c
index 055dac069afb..7280ae8ca88c 100644
--- a/lib/sbitmap.c
+++ b/lib/sbitmap.c
@@ -21,7 +21,7 @@ static int init_alloc_hint(struct sbitmap *sb, gfp_t flags)
int i;
 
for_each_possible_cpu(i)
-   *per_cpu_ptr(sb->alloc_hint, i) = prandom_u32() % depth;
+   *per_cpu_ptr(sb->alloc_hint, i) = 
prandom_u32_max(depth);
}
return 0;
 }
diff --git a/lib/test_vmalloc.c b/lib/test_vmalloc.c
index 4f2f2d1bac56..a26bbbf20e62 100644
--- a/lib/test_vmalloc.c
+++ b/lib/test_vmalloc.c
@@ -151,9 +151,7 @@ static int random_size_alloc_test(void)
int i;
 
for (i = 0; i < test_loop_count; i++) {
-   n = prandom_u32();
-   n = (n % 100) + 1;
-
+   n = prandom_u32_max(100) + 1;
p = vmalloc(n * PAGE_SIZE);
 
if (!p)
@@ -293,16 +291,12 @@ pcpu_alloc_test(void)
return -1;
 
for (i = 0; i < 35000; i++) {
-   unsigned int r;
-
-   r = prandom_u32();
-   size = (r % (PAGE_SIZE / 4)) + 1;
+   size = prandom_u32_max(PAGE_SIZE / 4) + 1;
 
/*
 * Maximum PAGE_SIZE
 */
-   r = prandom_u32();
-   align = 1 << ((r % 11) + 1);
+   align = 1 << (prandom_u32_max(11) + 1);
 
pcpu[i] = __alloc_percpu(size, align);
if (!pcpu[i])
@@ -393,14 +387,11 @@ static struct test_driver {
 
 static void shuffle_array(int *arr, int n)
 {
-   unsigned int rnd;
int i, j;
 
for (i = n - 1; i > 0; i--)  {
-   rnd = prandom_u32();
-
/* Cut the range. */
-   j = rnd % i;
+   j = prandom_u32_max(i);
 
/* Swap indexes. */
swap(arr[i], arr[j]);
-- 
2.37.3



[PATCH v6 1/7] treewide: use prandom_u32_max() when possible, part 1

2022-10-10 Thread Jason A. Donenfeld
Rather than incurring a division or requesting too many random bytes for
the given range, use the prandom_u32_max() function, which only takes
the minimum required bytes from the RNG and avoids divisions. This was
done mechanically with this coccinelle script:

@basic@
expression E;
type T;
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
typedef u64;
@@
(
- ((T)get_random_u32() % (E))
+ prandom_u32_max(E)
|
- ((T)get_random_u32() & ((E) - 1))
+ prandom_u32_max(E * XXX_MAKE_SURE_E_IS_POW2)
|
- ((u64)(E) * get_random_u32() >> 32)
+ prandom_u32_max(E)
|
- ((T)get_random_u32() & ~PAGE_MASK)
+ prandom_u32_max(PAGE_SIZE)
)

@multi_line@
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
identifier RAND;
expression E;
@@

-   RAND = get_random_u32();
... when != RAND
-   RAND %= (E);
+   RAND = prandom_u32_max(E);

// Find a potential literal
@literal_mask@
expression LITERAL;
type T;
identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
position p;
@@

((T)get_random_u32()@p & (LITERAL))

// Add one to the literal.
@script:python add_one@
literal << literal_mask.LITERAL;
RESULT;
@@

value = None
if literal.startswith('0x'):
value = int(literal, 16)
elif literal[0] in '123456789':
value = int(literal, 10)
if value is None:
print("I don't know how to handle %s" % (literal))
cocci.include_match(False)
elif value == 2**32 - 1 or value == 2**31 - 1 or value == 2**24 - 1 or value == 
2**16 - 1 or value == 2**8 - 1:
print("Skipping 0x%x for cleanup elsewhere" % (value))
cocci.include_match(False)
elif value & (value + 1) != 0:
print("Skipping 0x%x because it's not a power of two minus one" % 
(value))
cocci.include_match(False)
elif literal.startswith('0x'):
coccinelle.RESULT = cocci.make_expr("0x%x" % (value + 1))
else:
coccinelle.RESULT = cocci.make_expr("%d" % (value + 1))

// Replace the literal mask with the calculated result.
@plus_one@
expression literal_mask.LITERAL;
position literal_mask.p;
expression add_one.RESULT;
identifier FUNC;
@@

-   (FUNC()@p & (LITERAL))
+   prandom_u32_max(RESULT)

@collapse_ret@
type T;
identifier VAR;
expression E;
@@

 {
-   T VAR;
-   VAR = (E);
-   return VAR;
+   return E;
 }

@drop_var@
type T;
identifier VAR;
@@

 {
-   T VAR;
... when != VAR
 }

Reviewed-by: Greg Kroah-Hartman 
Reviewed-by: Kees Cook 
Reviewed-by: Yury Norov 
Reviewed-by: KP Singh 
Reviewed-by: Jan Kara  # for ext4 and sbitmap
Reviewed-by: Christoph Böhmwalder  # for drbd
Acked-by: Ulf Hansson  # for mmc
Acked-by: Darrick J. Wong  # for xfs
Signed-off-by: Jason A. Donenfeld 
---
 arch/arm/kernel/process.c |  2 +-
 arch/arm64/kernel/process.c   |  2 +-
 arch/loongarch/kernel/process.c   |  2 +-
 arch/loongarch/kernel/vdso.c  |  2 +-
 arch/mips/kernel/process.c|  2 +-
 arch/mips/kernel/vdso.c   |  2 +-
 arch/parisc/kernel/vdso.c |  2 +-
 arch/powerpc/kernel/process.c |  2 +-
 arch/s390/kernel/process.c|  2 +-
 arch/s390/kernel/vdso.c   |  2 +-
 arch/sparc/vdso/vma.c |  2 +-
 arch/um/kernel/process.c  |  2 +-
 arch/x86/entry/vdso/vma.c |  2 +-
 arch/x86/kernel/module.c  |  2 +-
 arch/x86/kernel/process.c |  2 +-
 arch/x86/mm/pat/cpa-test.c|  4 +-
 crypto/testmgr.c  | 86 +--
 drivers/block/drbd/drbd_receiver.c|  4 +-
 .../gpu/drm/i915/gem/i915_gem_execbuffer.c|  2 +-
 drivers/infiniband/core/cma.c |  2 +-
 drivers/infiniband/hw/cxgb4/id_table.c|  4 +-
 drivers/infiniband/hw/hns/hns_roce_ah.c   |  5 +-
 drivers/infiniband/ulp/rtrs/rtrs-clt.c|  3 +-
 drivers/md/bcache/request.c   |  2 +-
 .../test-drivers/vivid/vivid-touch-cap.c  |  2 +-
 drivers/mmc/core/core.c   |  4 +-
 drivers/mmc/host/dw_mmc.c |  2 +-
 drivers/mtd/nand/raw/nandsim.c|  4 +-
 drivers/mtd/tests/mtd_nandecctest.c   | 10 +--
 drivers/mtd/tests/stresstest.c| 17 +---
 drivers/mtd/ubi/debug.c   |  2 +-
 drivers/mtd/ubi/debug.h   |  6 +-
 drivers/net/ethernet/broadcom/cnic.c  |  3 +-
 .../chelsio/inline_crypto/chtls/chtls_io.c|  4 +-
 drivers/net/hamradio/baycom_epp.c |  2 +-
 drivers/net/hamradio/hdlcdrv.c|  2 +-
 drivers/net/hamradio/yam.c|  2 +-
 drivers/net/phy/at803x.c  |  2 +-
 .../broadcom/brcm80211/brcmfmac/p2p.c |  2 +-
 .../net/wireless/intel/iwlwifi/mvm/mac-ctxt.c |  2 +-
 drivers/scsi/fcoe/fcoe_ctlr.c 

[PATCH v6 0/7] treewide cleanup of random integer usage

2022-10-10 Thread Jason A. Donenfeld
Changes v5->v6:
- Added a few missing conversions that weren't in my older tree, so now
  this should be ready to go, as well as a couple nits people had from
  v5. Barring something large and unforeseen, this is the "final
  version", as this is ready to ship. Thanks to everyone who reviewed
  this.

Hi folks,

This is a five part treewide cleanup of random integer handling. The
rules for random integers are:

- If you want a secure or an insecure random u64, use get_random_u64().
- If you want a secure or an insecure random u32, use get_random_u32().
  * The old function prandom_u32() has been deprecated for a while now
and is just a wrapper around get_random_u32(). Same for
get_random_int().
- If you want a secure or an insecure random u16, use get_random_u16().
- If you want a secure or an insecure random u8, use get_random_u8().
- If you want secure or insecure random bytes, use get_random_bytes().
  * The old function prandom_bytes() has been deprecated for a while now
and has long been a wrapper around get_random_bytes().
- If you want a non-uniform random u32, u16, or u8 bounded by a certain
  open interval maximum, use prandom_u32_max().
  * I say "non-uniform", because it doesn't do any rejection sampling or
divisions. Hence, it stays within the prandom_* namespace.

These rules ought to be applied uniformly, so that we can clean up the
deprecated functions, and earn the benefits of using the modern
functions. In particular, in addition to the boring substitutions, this
patchset accomplishes a few nice effects:

- By using prandom_u32_max() with an upper-bound that the compiler can
  prove at compile-time is ≤65536 or ≤256, internally get_random_u16()
  or get_random_u8() is used, which wastes fewer batched random bytes,
  and hence has higher throughput.

- By using prandom_u32_max() instead of %, when the upper-bound is not a
  constant, division is still avoided, because prandom_u32_max() uses
  a faster multiplication-based trick instead.

- By using get_random_u16() or get_random_u8() in cases where the return
  value is intended to indeed be a u16 or a u8, we waste fewer batched
  random bytes, and hence have higher throughput.

So, based on those rules and benefits from following them, this patchset
breaks down into the following five steps:

1) Replace `prandom_u32() % max` and variants thereof with
   prandom_u32_max(max).

   * Part 1 is done with Coccinelle. Part 2 is done by hand.

2) Replace `(type)get_random_u32()` and variants thereof with
   get_random_u16() or get_random_u8(). I took the pains to actually
   look and see what every lvalue type was across the entire tree.

   * Part 1 is done with Coccinelle. Part 2 is done by hand.

3) Replace remaining deprecated uses of prandom_u32() and
   get_random_int() with get_random_u32(). 

   * A boring search and replace operation.

4) Replace remaining deprecated uses of prandom_bytes() with
   get_random_bytes().

   * A boring search and replace operation.

5) Remove the deprecated and now-unused prandom_u32() and
   prandom_bytes() inline wrapper functions.

   * Just deleting code and updating comments.

I'll be sending this toward the end of the 6.1 merge window via the
random.git tree.

Please take a look! The number of lines touched is quite small, so this
should be reviewable, and as much as is possible has been pushed into
Coccinelle scripts.

Thanks,
Jason

Cc: Andreas Noever 
Cc: Andrew Morton 
Cc: Andy Shevchenko 
Cc: Borislav Petkov 
Cc: Catalin Marinas 
Cc: Christoph Böhmwalder 
Cc: Christoph Hellwig 
Cc: Christophe Leroy 
Cc: Daniel Borkmann 
Cc: Dave Airlie 
Cc: Dave Hansen 
Cc: David S. Miller 
Cc: Eric Dumazet 
Cc: Florian Westphal 
Cc: Greg Kroah-Hartman ,
Cc: H. Peter Anvin 
Cc: Heiko Carstens 
Cc: Helge Deller 
Cc: Herbert Xu 
Cc: Huacai Chen 
Cc: Hugh Dickins 
Cc: Jakub Kicinski 
Cc: James E.J. Bottomley 
Cc: Jan Kara 
Cc: Jason Gunthorpe 
Cc: Jens Axboe 
Cc: Johannes Berg 
Cc: Jonathan Corbet 
Cc: Jozsef Kadlecsik 
Cc: KP Singh 
Cc: Kees Cook 
Cc: Marco Elver 
Cc: Mauro Carvalho Chehab 
Cc: Michael Ellerman 
Cc: Pablo Neira Ayuso 
Cc: Paolo Abeni 
Cc: Peter Zijlstra 
Cc: Richard Weinberger 
Cc: Russell King 
Cc: Theodore Ts'o 
Cc: Thomas Bogendoerfer 
Cc: Thomas Gleixner 
Cc: Thomas Graf 
Cc: Ulf Hansson 
Cc: Vignesh Raghavendra 
Cc: WANG Xuerui 
Cc: Will Deacon 
Cc: Yury Norov 
Cc: dri-de...@lists.freedesktop.org
Cc: kasan-...@googlegroups.com
Cc: kernel-janit...@vger.kernel.org
Cc: linux-arm-ker...@lists.infradead.org
Cc: linux-bl...@vger.kernel.org
Cc: linux-cry...@vger.kernel.org
Cc: linux-...@vger.kernel.org
Cc: linux-fsde...@vger.kernel.org
Cc: linux-me...@vger.kernel.org
Cc: linux-m...@vger.kernel.org
Cc: linux...@kvack.org
Cc: linux-...@vger.kernel.org
Cc: linux-...@lists.infradead.org
Cc: linux-n...@lists.infradead.org
Cc: linux-par...@vger.kernel.org
Cc: linux-r...@vger.kernel.org
Cc: linux-s...@vger.kernel.org
Cc: linux...@lists.infradead.org
Cc: linux-...@vger.kernel.org
Cc: 

Re: [GIT PULL] Please pull powerpc/linux.git powerpc-6.1-1 tag

2022-10-10 Thread Jason A. Donenfeld
Hi Andrew,

On Tue, Oct 11, 2022 at 11:00:15AM +1100, Andrew Donnellan wrote:
> Thanks for bisecting, this is interesting! Could you provide your
> .config and the environment you're running in? Your reproducer doesn't
> seem to trigger it on my baremetal POWER8 pseries_le_defconfig.

Sure.

.config: https://xn--4db.cc/NemFt2Vs (change CONFIG_INITRAMFS_SOURCE)
Toolchain: 
https://download.wireguard.com/qemu-test/toolchains/20211123/powerpc-linux-musl-cross.tgz

You can also just run:

  ARCH=powerpc make -C tools/testing/selftests/wireguard/qemu -j$(nproc)

And that'll assemble the whole thing.

Jason


Re: [GIT PULL] Please pull powerpc/linux.git powerpc-6.1-1 tag

2022-10-10 Thread Andrew Donnellan
On Mon, 2022-10-10 at 16:26 -0600, Jason A. Donenfeld wrote:
> 
> Bisected:
> 
> 7e92e01b724526b98cbc7f03dd4afa0295780d56 is the first bad commit
> commit 7e92e01b724526b98cbc7f03dd4afa0295780d56
> Author: Rohan McLure 
> Date:   Wed Sep 21 16:56:01 2022 +1000
> 
>     powerpc: Provide syscall wrapper
> 
>     Implement syscall wrapper as per s390, x86, arm64. When enabled
>     cause handlers to accept parameters from a stack frame rather
> than
>     from user scratch register state. This allows for user registers
> to be
>     safely cleared in order to reduce caller influence on speculation
>     within syscall routine. The wrapper is a macro that emits syscall
>     handler symbols that call into the target handler, obtaining its
>     parameters from a struct pt_regs on the stack.
> 
>     As registers are already saved to the stack prior to calling
>     system_call_exception, it appears that this function is executed
> more
>     efficiently with the new stack-pointer convention than with
> parameters
>     passed by registers, avoiding the allocation of a stack frame for
> this
>     method. On a 32-bit system, we see >20% performance increases on
> the
>     null_syscall microbenchmark, and on a Power 8 the performance
> gains
>     amortise the cost of clearing and restoring registers which is
>     implemented at the end of this series, seeing final result of
> ~5.6%
>     performance improvement on null_syscall.
> 
>     Syscalls are wrapped in this fashion on all platforms except for
> the
>     Cell processor as this commit does not provide SPU support. This
> can be
>     quickly fixed in a successive patch, but requires
> spu_sys_callback to
>     allocate a pt_regs structure to satisfy the wrapped calling
> convention.
> 
>     Co-developed-by: Andrew Donnellan 
>     Signed-off-by: Andrew Donnellan 
>     Signed-off-by: Rohan McLure 
>     Reviewed-by: Nicholas Piggin 
>     [mpe: Make incompatible with COMPAT to retain clearing of high
> bits of args]
>     Signed-off-by: Michael Ellerman 
>     Link:
> https://lore.kernel.org/r/20220921065605.1051927-22-rmcl...@linux.ibm.com

Thanks for bisecting, this is interesting! Could you provide your
.config and the environment you're running in? Your reproducer doesn't
seem to trigger it on my baremetal POWER8 pseries_le_defconfig.

-- 
Andrew DonnellanOzLabs, ADL Canberra
a...@linux.ibm.com   IBM Australia Limited



Re: [GIT PULL] Please pull powerpc/linux.git powerpc-6.1-1 tag

2022-10-10 Thread Jason A. Donenfeld
On Mon, Oct 10, 2022 at 02:03:09PM -0600, Jason A. Donenfeld wrote:
> On Mon, Oct 10, 2022 at 01:25:25PM -0600, Jason A. Donenfeld wrote:
> > Hi Michael,
> > 
> > On Sun, Oct 09, 2022 at 10:01:39PM +1100, Michael Ellerman wrote:
> > > powerpc updates for 6.1
> > > 
> > >  - Remove our now never-true definitions for pgd_huge() and p4d_leaf().
> > > 
> > >  - Add pte_needs_flush() and huge_pmd_needs_flush() for 64-bit.
> > > 
> > >  - Add support for syscall wrappers.
> > > 
> > >  - Add support for KFENCE on 64-bit.
> > > 
> > >  - Update 64-bit HV KVM to use the new guest state entry/exit accounting 
> > > API.
> > > 
> > >  - Support execute-only memory when using the Radix MMU (P9 or later).
> > > 
> > >  - Implement CONFIG_PARAVIRT_TIME_ACCOUNTING for pseries guests.
> > > 
> > >  - Updates to our linker script to move more data into read-only sections.
> > > 
> > >  - Allow the VDSO to be randomised on 32-bit.
> > > 
> > >  - Many other small features and fixes.
> > 
> > FYI, something in here broke the wireguard test suite, which runs the
> > iperf3 networking utility. The full log is here [1], but the relevant part
> > is: 
> > 
> > [+] NS1: iperf3 -Z -t 3 -c 192.168.241.2
> > Connecting to host 192.168.241.2, port 5201
> > iperf3: error - failed to read /dev/urandom: Bad address
> > 
> > I'll see if I can narrow it down a bit more and bisect. But just FYI, in
> > case you have an intuition.
> 
> Huh. From iov_iter.c:
> 
> static int copyout(void __user *to, const void *from, size_t n)
> {
> size_t before = n;
> if (should_fail_usercopy())
> return n;
> if (access_ok(to, n)) {
> instrument_copy_to_user(to, from, n);
> n = raw_copy_to_user(to, from, n);
> if (n == before)
> pr_err("SARU n still %zu pointer is %lx\n", n, 
> (unsigned long)to);
> }
> return n;
> }
> 
> I added the pr_err() there to catch the failure:
> [3.443506] SARU n still 64 pointer is b78db000
> 
> Also I managed to extract the failing portion of iperf3 into something
> smaller:
> 
> int temp;
> char *x;
> ssize_t l;
> FILE *f;
> char template[] = "/blah-XX";
> 
> temp = mkstemp(template);
> if (temp < 0)
> panic("mkstemp");
> if (unlink(template) < 0)
> panic("unlink");
> if (ftruncate(temp, 0x2) < 0)
> panic("ftruncate");
> x = mmap(NULL, 0x2, PROT_READ|PROT_WRITE, MAP_PRIVATE, temp, 0);
> if (x == MAP_FAILED)
> panic("mmap");
> f = fopen("/dev/urandom", "rb");
> if (!f)
> panic("fopen");
> setbuf(f, NULL);
> if (fread(x, 1, 0x2, f) != 0x2)
> panic("fread");
> 
> Jason

Bisected:

7e92e01b724526b98cbc7f03dd4afa0295780d56 is the first bad commit
commit 7e92e01b724526b98cbc7f03dd4afa0295780d56
Author: Rohan McLure 
Date:   Wed Sep 21 16:56:01 2022 +1000

powerpc: Provide syscall wrapper

Implement syscall wrapper as per s390, x86, arm64. When enabled
cause handlers to accept parameters from a stack frame rather than
from user scratch register state. This allows for user registers to be
safely cleared in order to reduce caller influence on speculation
within syscall routine. The wrapper is a macro that emits syscall
handler symbols that call into the target handler, obtaining its
parameters from a struct pt_regs on the stack.

As registers are already saved to the stack prior to calling
system_call_exception, it appears that this function is executed more
efficiently with the new stack-pointer convention than with parameters
passed by registers, avoiding the allocation of a stack frame for this
method. On a 32-bit system, we see >20% performance increases on the
null_syscall microbenchmark, and on a Power 8 the performance gains
amortise the cost of clearing and restoring registers which is
implemented at the end of this series, seeing final result of ~5.6%
performance improvement on null_syscall.

Syscalls are wrapped in this fashion on all platforms except for the
Cell processor as this commit does not provide SPU support. This can be
quickly fixed in a successive patch, but requires spu_sys_callback to
allocate a pt_regs structure to satisfy the wrapped calling convention.

Co-developed-by: Andrew Donnellan 
Signed-off-by: Andrew Donnellan 
Signed-off-by: Rohan McLure 
Reviewed-by: Nicholas Piggin 
[mpe: Make incompatible with COMPAT to retain clearing of high bits of args]
Signed-off-by: Michael Ellerman 
Link: 
https://lore.kernel.org/r/20220921065605.1051927-22-rmcl...@linux.ibm.com

 arch/powerpc/Kconfig   |  1 +
 arch/powerpc/include/asm/syscall.h |  4 +++
 arch/powerpc/include/asm/syscall_wrapper.h | 51 

Re: [PATCH v2 4/4] powerpc/pseries: Move dtl scanning and steal time accounting to pseries platform

2022-10-10 Thread Guenter Roeck
On Fri, Sep 02, 2022 at 06:53:16PM +1000, Nicholas Piggin wrote:
> dtl is the PAPR Dispatch Trace Log, which is entirely a pseries feature.
> The pseries platform alrady has a file dealing with the dtl, so move
> scanning for stolen time accounting there from kernel/time.c.
> 
> Signed-off-by: Nicholas Piggin 

This patch ties DTL to PPC_SPLPAR without updating configuration
dependencies. As result, the following build error may now be seen
if CONFIG_PPC_SPLPAR=y and CONFIG_DTL=n.

arch/powerpc/kernel/irq.o: in function `.do_IRQ':
irq.c:(.text+0x2798): undefined reference to `.pseries_accumulate_stolen_time'

I updated my own configurations to avoid the problem, but you might see
randconfig failures with this error in the future.

Guenter


Re: [GIT PULL] Please pull powerpc/linux.git powerpc-6.1-1 tag

2022-10-10 Thread Jason A. Donenfeld
On Mon, Oct 10, 2022 at 01:25:25PM -0600, Jason A. Donenfeld wrote:
> Hi Michael,
> 
> On Sun, Oct 09, 2022 at 10:01:39PM +1100, Michael Ellerman wrote:
> > powerpc updates for 6.1
> > 
> >  - Remove our now never-true definitions for pgd_huge() and p4d_leaf().
> > 
> >  - Add pte_needs_flush() and huge_pmd_needs_flush() for 64-bit.
> > 
> >  - Add support for syscall wrappers.
> > 
> >  - Add support for KFENCE on 64-bit.
> > 
> >  - Update 64-bit HV KVM to use the new guest state entry/exit accounting 
> > API.
> > 
> >  - Support execute-only memory when using the Radix MMU (P9 or later).
> > 
> >  - Implement CONFIG_PARAVIRT_TIME_ACCOUNTING for pseries guests.
> > 
> >  - Updates to our linker script to move more data into read-only sections.
> > 
> >  - Allow the VDSO to be randomised on 32-bit.
> > 
> >  - Many other small features and fixes.
> 
> FYI, something in here broke the wireguard test suite, which runs the
> iperf3 networking utility. The full log is here [1], but the relevant part
> is: 
> 
> [+] NS1: iperf3 -Z -t 3 -c 192.168.241.2
> Connecting to host 192.168.241.2, port 5201
> iperf3: error - failed to read /dev/urandom: Bad address
> 
> I'll see if I can narrow it down a bit more and bisect. But just FYI, in
> case you have an intuition.

Huh. From iov_iter.c:

static int copyout(void __user *to, const void *from, size_t n)
{
size_t before = n;
if (should_fail_usercopy())
return n;
if (access_ok(to, n)) {
instrument_copy_to_user(to, from, n);
n = raw_copy_to_user(to, from, n);
if (n == before)
pr_err("SARU n still %zu pointer is %lx\n", n, 
(unsigned long)to);
}
return n;
}

I added the pr_err() there to catch the failure:
[3.443506] SARU n still 64 pointer is b78db000

Also I managed to extract the failing portion of iperf3 into something
smaller:

int temp;
char *x;
ssize_t l;
FILE *f;
char template[] = "/blah-XX";

temp = mkstemp(template);
if (temp < 0)
panic("mkstemp");
if (unlink(template) < 0)
panic("unlink");
if (ftruncate(temp, 0x2) < 0)
panic("ftruncate");
x = mmap(NULL, 0x2, PROT_READ|PROT_WRITE, MAP_PRIVATE, temp, 0);
if (x == MAP_FAILED)
panic("mmap");
f = fopen("/dev/urandom", "rb");
if (!f)
panic("fopen");
setbuf(f, NULL);
if (fread(x, 1, 0x2, f) != 0x2)
panic("fread");

Jason


Re: [GIT PULL] Please pull powerpc/linux.git powerpc-6.1-1 tag

2022-10-10 Thread Jason A. Donenfeld
Hi Michael,

On Sun, Oct 09, 2022 at 10:01:39PM +1100, Michael Ellerman wrote:
> powerpc updates for 6.1
> 
>  - Remove our now never-true definitions for pgd_huge() and p4d_leaf().
> 
>  - Add pte_needs_flush() and huge_pmd_needs_flush() for 64-bit.
> 
>  - Add support for syscall wrappers.
> 
>  - Add support for KFENCE on 64-bit.
> 
>  - Update 64-bit HV KVM to use the new guest state entry/exit accounting API.
> 
>  - Support execute-only memory when using the Radix MMU (P9 or later).
> 
>  - Implement CONFIG_PARAVIRT_TIME_ACCOUNTING for pseries guests.
> 
>  - Updates to our linker script to move more data into read-only sections.
> 
>  - Allow the VDSO to be randomised on 32-bit.
> 
>  - Many other small features and fixes.

FYI, something in here broke the wireguard test suite, which runs the
iperf3 networking utility. The full log is here [1], but the relevant part
is: 

[+] NS1: iperf3 -Z -t 3 -c 192.168.241.2
Connecting to host 192.168.241.2, port 5201
iperf3: error - failed to read /dev/urandom: Bad address

I'll see if I can narrow it down a bit more and bisect. But just FYI, in
case you have an intuition.

Jason


[1] 
https://build.wireguard.com/linux/4de65c5830233e7a4adf2e679510089ec4e210c7/powerpc.log


[linux-next:master] BUILD REGRESSION cd9fd78f5c11b5e165d9317ef11e613f4aef4dd1

2022-10-10 Thread kernel test robot
tree/branch: 
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
branch HEAD: cd9fd78f5c11b5e165d9317ef11e613f4aef4dd1  Add linux-next specific 
files for 20221010

Error/Warning reports:

https://lore.kernel.org/linux-doc/202209201326.sy9kholm-...@intel.com
https://lore.kernel.org/linux-doc/202209231933.vcyettul-...@intel.com
https://lore.kernel.org/linux-doc/202210070057.npbamyxb-...@intel.com
https://lore.kernel.org/linux-mm/202210090954.ptr6m6rj-...@intel.com
https://lore.kernel.org/llvm/202209220019.yr2vuxhg-...@intel.com

Error/Warning: (recently discovered and may have been fixed)

Warning: Documentation/translations/zh_CN/devicetree/kernel-api.rst references 
a file that doesn't exist: Documentation/Devicetree/kernel-api.rst
Warning: MAINTAINERS references a file that doesn't exist: 
Documentation/devicetree/bindings/clock/microchip,mpfs.yaml
Warning: MAINTAINERS references a file that doesn't exist: 
Documentation/devicetree/bindings/mtd/amlogic,meson-nand.txt
arch/arm64/kernel/alternative.c:199:6: warning: no previous prototype for 
'apply_alternatives_vdso' [-Wmissing-prototypes]
arch/arm64/kernel/alternative.c:295:14: warning: no previous prototype for 
'alt_cb_patch_nops' [-Wmissing-prototypes]
arch/loongarch/mm/init.c:166:24: warning: variable 'new' set but not used 
[-Wunused-but-set-variable]
arch/parisc/kernel/setup.c:78 setup_cmdline() warn: curly braces intended?
drivers/gpu/drm/amd/amdgpu/../display/dc/virtual/virtual_link_hwss.c:40:6: 
warning: no previous prototype for 'virtual_disable_link_output' 
[-Wmissing-prototypes]
drivers/iio/adc/mcp3911.c:252 mcp3911_write_raw() error: buffer overflow 
'mcp3911_osr_table' 8 <= 31
drivers/iio/adc/mcp3911.c:499 mcp3911_probe() warn: passing zero to 'PTR_ERR'
drivers/nvme/target/loop.c:578 nvme_loop_create_ctrl() warn: 'opts->queue_size 
- 1' 18446744073709551615 can't fit into 65535 'ctrl->ctrl.sqsize'
drivers/nvme/target/loop.c:578 nvme_loop_create_ctrl() warn: 'opts->queue_size 
- 1' 4294967295 can't fit into 65535 'ctrl->ctrl.sqsize'
fs/ext4/super.c:1744:19: warning: 'deprecated_msg' defined but not used 
[-Wunused-const-variable=]
include/linux/compiler_types.h:357:45: error: call to 
'__compiletime_assert_422' declared with attribute error: FIELD_GET: mask is 
not constant
include/linux/thread_info.h:217: undefined reference to `riscv_cbom_block_size'
riscv64-linux-ld: include/linux/thread_info.h:217: undefined reference to 
`riscv_cbom_block_size'
security/apparmor/policy_unpack.c:1089 unpack_profile() warn: passing zero to 
'ERR_PTR'
security/apparmor/policy_unpack.c:548 unpack_trans_table() error: uninitialized 
symbol 'table'.

Error/Warning ids grouped by kconfigs:

gcc_recent_errors
|-- alpha-randconfig-r002-20221010
|   `-- 
drivers-gpu-drm-amd-amdgpu-..-display-dc-virtual-virtual_link_hwss.c:warning:no-previous-prototype-for-virtual_disable_link_output
|-- alpha-randconfig-r006-20221010
|   `-- 
drivers-gpu-drm-amd-amdgpu-..-display-dc-virtual-virtual_link_hwss.c:warning:no-previous-prototype-for-virtual_disable_link_output
|-- arm-allyesconfig
|   `-- 
drivers-gpu-drm-amd-amdgpu-..-display-dc-virtual-virtual_link_hwss.c:warning:no-previous-prototype-for-virtual_disable_link_output
|-- arm64-allyesconfig
|   |-- 
arch-arm64-kernel-alternative.c:warning:no-previous-prototype-for-alt_cb_patch_nops
|   |-- 
arch-arm64-kernel-alternative.c:warning:no-previous-prototype-for-apply_alternatives_vdso
|   `-- 
drivers-gpu-drm-amd-amdgpu-..-display-dc-virtual-virtual_link_hwss.c:warning:no-previous-prototype-for-virtual_disable_link_output
|-- arm64-randconfig-c043-20221010
|   |-- 
arch-arm64-kernel-alternative.c:warning:no-previous-prototype-for-alt_cb_patch_nops
|   `-- 
arch-arm64-kernel-alternative.c:warning:no-previous-prototype-for-apply_alternatives_vdso
|-- i386-allyesconfig
|   |-- 
drivers-gpu-drm-amd-amdgpu-..-display-dc-virtual-virtual_link_hwss.c:warning:no-previous-prototype-for-virtual_disable_link_output
|   `-- fs-ext4-super.c:warning:deprecated_msg-defined-but-not-used
|-- i386-defconfig
|   `-- fs-ext4-super.c:warning:deprecated_msg-defined-but-not-used
|-- i386-randconfig-a011-20221010
|   `-- fs-ext4-super.c:warning:deprecated_msg-defined-but-not-used
|-- i386-randconfig-a012-20221010
|   `-- fs-ext4-super.c:warning:deprecated_msg-defined-but-not-used
|-- i386-randconfig-a014-20221010
|   `-- fs-ext4-super.c:warning:deprecated_msg-defined-but-not-used
|-- i386-randconfig-a016-20221010
|   `-- fs-ext4-super.c:warning:deprecated_msg-defined-but-not-used
|-- i386-randconfig-c001-20221010
|   `-- 
include-linux-compiler_types.h:error:call-to-__compiletime_assert_NNN-declared-with-attribute-error:FIELD_GET:mask-is-not-constant
|-- i386-randconfig-c021-20221010
|   `-- fs-ext4-super.c:warning:deprecated_msg-defined-but-not-used
|-- i386-randconfig-m021-20221010
|   |-- 
arch-x86-kernel-apic-apic.c-generic_processor_info()-warn:always-true-condition-(num_processors-()-)-(-u32max-)
|   

Re: [PATCH] powerpc/kasan/book3s_64: warn when running with hash MMU

2022-10-10 Thread Christophe Leroy


Le 10/10/2022 à 16:10, Nathan Lynch a écrit :
> Michael Ellerman  writes:
>> Christophe Leroy  writes:
>>> + KASAN list
>>>
>>> Le 06/10/2022 à 06:10, Michael Ellerman a écrit :
 Nathan Lynch  writes:
> kasan is known to crash at boot on book3s_64 with non-radix MMU. As
> noted in commit 41b7a347bf14 ("powerpc: Book3S 64-bit outline-only
> KASAN support"):
>
> A kernel with CONFIG_KASAN=y will crash during boot on a machine
> using HPT translation because not all the entry points to the
> generic KASAN code are protected with a call to kasan_arch_is_ready().

 I guess I thought there was some plan to fix that.
>>>
>>> I was thinking the same.
>>>
>>> Do we have a list of the said entry points to the generic code that are
>>> lacking a call to kasan_arch_is_ready() ?
>>>
>>> Typically, the BUG dump below shows that kasan_byte_accessible() is
>>> lacking the check. It should be straight forward to add
>>> kasan_arch_is_ready() check to kasan_byte_accessible(), shouldn't it ?
>>
>> Yes :)
>>
>> And one other spot, but the patch below boots OK for me. I'll leave it
>> running for a while just in case there's a path I've missed.
> 
> It works for me too, thanks (p8 pseries qemu).
> 
> This avoids the boot-time oops, but kasan remains unimplemented for hash
> mmu. Raising the question: with the trivial crashes addressed, is the
> current message ('KASAN not enabled as it requires radix!') sufficient
> to notify developers (such as me, a week ago) who mean to use kasan on a
> book3s platform, unaware that it's radix-only? Would a WARN or something
> more prominent still be justified?
> 
> I guess people will figure it out as soon as they think to search the
> kernel log for 'KASAN'...

I don't think the big hammer WARN would be justified.
WARN is supposed to be used only with unexpected conditions.

KASAN not working with hash-MMU is expected. A pr_warn() should be enough.

Someone who has a kernel with KASAN built in but who is not interested 
by KASAN and who is booting it one a HASH-MMU will be terrified by a WARN.

[PATCH] powerpc/kernel: fix repeated words in comments

2022-10-10 Thread wangjianli
Delete the redundant word 'the'.

Signed-off-by: wangjianli 
---
 arch/powerpc/kernel/process.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/process.c b/arch/powerpc/kernel/process.c
index 406d7ee9e322..7a6e54e4f1d8 100644
--- a/arch/powerpc/kernel/process.c
+++ b/arch/powerpc/kernel/process.c
@@ -176,7 +176,7 @@ void giveup_fpu(struct task_struct *tsk)
 EXPORT_SYMBOL(giveup_fpu);
 
 /*
- * Make sure the floating-point register state in the
+ * Make sure the floating-point register state in
  * the thread_struct is up to date for task tsk.
  */
 void flush_fp_to_thread(struct task_struct *tsk)
-- 
2.36.1



Re: [PATCH] powerpc/kasan/book3s_64: warn when running with hash MMU

2022-10-10 Thread Nathan Lynch
Michael Ellerman  writes:
> Christophe Leroy  writes:
>> + KASAN list
>>
>> Le 06/10/2022 à 06:10, Michael Ellerman a écrit :
>>> Nathan Lynch  writes:
 kasan is known to crash at boot on book3s_64 with non-radix MMU. As
 noted in commit 41b7a347bf14 ("powerpc: Book3S 64-bit outline-only
 KASAN support"):

A kernel with CONFIG_KASAN=y will crash during boot on a machine
using HPT translation because not all the entry points to the
generic KASAN code are protected with a call to kasan_arch_is_ready().
>>> 
>>> I guess I thought there was some plan to fix that.
>>
>> I was thinking the same.
>>
>> Do we have a list of the said entry points to the generic code that are 
>> lacking a call to kasan_arch_is_ready() ?
>>
>> Typically, the BUG dump below shows that kasan_byte_accessible() is 
>> lacking the check. It should be straight forward to add 
>> kasan_arch_is_ready() check to kasan_byte_accessible(), shouldn't it ?
>
> Yes :)
>
> And one other spot, but the patch below boots OK for me. I'll leave it
> running for a while just in case there's a path I've missed.

It works for me too, thanks (p8 pseries qemu).

This avoids the boot-time oops, but kasan remains unimplemented for hash
mmu. Raising the question: with the trivial crashes addressed, is the
current message ('KASAN not enabled as it requires radix!') sufficient
to notify developers (such as me, a week ago) who mean to use kasan on a
book3s platform, unaware that it's radix-only? Would a WARN or something
more prominent still be justified?

I guess people will figure it out as soon as they think to search the
kernel log for 'KASAN'...


Re: [PATCH v4 00/16] objtool: Enable and implement --mcount option on powerpc

2022-10-10 Thread Naveen N. Rao

Sathvika Vasireddy wrote:

This patchset enables and implements objtool --mcount
option on powerpc. This applies atop powerpc/merge branch.

Changelog:


v4:

* Patch 11/16 - Introduce a new config option
CONFIG_HAVE_OBJTOOL_NOP_MCOUNT as a means for
architectures to enable nop'ing ftrace locations.

  - Remove Acked-by tag from Peter Zijlstra (Intel),
		and Reviewed-by tag from Christophe Leroy. 
		[This is done because I reworked the patch to add

a new config option to objtool. Please let me know
if you want me to retain the tags. Thanks!]

* Patch 16/16 - Rework the patch to handle only 'bl' instruction
		decoding. 


With changes to the two patches in this series that I have described, 
and with my patch to have ftrace ignore weak symbols (*) applied, I 
built a ppc64le config.


(*) 
http://lkml.kernel.org/r/20220809105425.424045-1-naveen.n@linux.vnet.ibm.com

I then used the below diff to disable objtool and to build with 
recordmcount:


diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 7e28a8fd4c24fa..81c9f895d69012 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -238,8 +238,6 @@ config PPC
   select HAVE_MOD_ARCH_SPECIFIC
   select HAVE_NMI if PERF_EVENTS || (PPC64 && 
PPC_BOOK3S)
   select HAVE_OPTPROBES
-   select HAVE_OBJTOOL if PPC32 || MPROFILE_KERNEL
-   select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
   select HAVE_PERF_EVENTS
   select HAVE_PERF_EVENTS_NMI if PPC64
   select HAVE_PERF_REGS

Comparing available_filter_functions across the two builds showed the 
below differences:
 $ diff ftrace_funcs_recordmcount_20221010.sort.out ftrace_funcs_objtool_20221010.sort.out 
 6799d6798

 < __dev_alloc_name
 6800a6800
 > dev_alloc_name_ns
 11680d11679
 < find_zone.isra.0
 22396a22396
 > ns_capable_common.part.0

All the above changes are down to compiler optimizations and shuffling 
due to CONFIG_OBJTOOL being enabled and changing annotate_unreachable().


As such, for this series:
Reviewed-by: Naveen N. Rao 
Tested-by: Naveen N. Rao 


Josh,
Are you ok if this series is taken in through the powerpc tree?


- Naveen


Re: [PATCH v4 11/16] objtool: Add --mnop as an option to --mcount

2022-10-10 Thread Naveen N. Rao

Sathvika Vasireddy wrote:

Some architectures (powerpc) may not support ftrace locations being nop'ed
out at build time. Introduce CONFIG_HAVE_OBJTOOL_NOP_MCOUNT for objtool, as
a means for architectures to enable nop'ing of ftrace locations. Add --mnop
as an option to objtool --mcount, to indicate support for the same.

Also, make sure that --mnop can be passed as an option to objtool only when
--mcount is passed.

Signed-off-by: Sathvika Vasireddy 
---
 Makefile|  4 +++-
 arch/x86/Kconfig|  1 +
 kernel/trace/Kconfig|  7 +++
 scripts/Makefile.lib|  1 +
 tools/objtool/builtin-check.c   | 14 ++
 tools/objtool/check.c   | 19 ++-
 tools/objtool/include/objtool/builtin.h |  1 +
 7 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index a5e9d9388649..b2230ad14748 100644
--- a/Makefile
+++ b/Makefile
@@ -857,7 +857,9 @@ ifdef CONFIG_FTRACE_MCOUNT_USE_CC
   endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
-  CC_FLAGS_USING   += -DCC_USING_NOP_MCOUNT
+  ifdef CONFIG_HAVE_OBJTOOL_NOP_MCOUNT
+CC_FLAGS_USING += -DCC_USING_NOP_MCOUNT
+  endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
   ifdef CONFIG_HAVE_C_RECORDMCOUNT
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index f9920f1341c8..2a79a05c4402 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -189,6 +189,7 @@ config X86
select HAVE_CONTEXT_TRACKING_USER_OFFSTACK  if 
HAVE_CONTEXT_TRACKING_USER
select HAVE_C_RECORDMCOUNT
select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
+   select HAVE_OBJTOOL_NOP_MCOUNT  if HAVE_OBJTOOL_MCOUNT
select HAVE_BUILDTIME_MCOUNT_SORT
select HAVE_DEBUG_KMEMLEAK
select HAVE_DMA_CONTIGUOUS
diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
index 1052126bdca2..9c696cb24756 100644
--- a/kernel/trace/Kconfig
+++ b/kernel/trace/Kconfig
@@ -76,6 +76,13 @@ config HAVE_OBJTOOL_MCOUNT
help
  Arch supports objtool --mcount

+config HAVE_OBJTOOL_NOP_MCOUNT
+   bool
+   help
+ Arch supports the objtool options --mcount with --mnop.
+ An architecture can select this if it wants to enable nop'ing
+ of ftrace locations.
+
 config HAVE_C_RECORDMCOUNT
bool
help
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 3fb6a99e78c4..ce14e3b8577f 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -234,6 +234,7 @@ objtool_args =  
\
$(if $(CONFIG_HAVE_NOINSTR_HACK), --hacks=noinstr)  \
$(if $(CONFIG_X86_KERNEL_IBT), --ibt)   \
$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount) \
+   $(if $(CONFIG_HAVE_OBJTOOL_NOP_MCOUNT), --mnop) \


This still won't help: for instance, if CONFIG_FTRACE itself is 
disabled. I think we should make this depend on 
CONFIG_FTRACE_MCOUNT_USE_OBJTOOL. The below change works for me:


diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 54d2d6451bdacc..fd3f55a1fdb7bb 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -245,8 +245,8 @@ objtool_args =  
\
   $(if $(CONFIG_HAVE_JUMP_LABEL_HACK), --hacks=jump_label)\
   $(if $(CONFIG_HAVE_NOINSTR_HACK), --hacks=noinstr)  \
   $(if $(CONFIG_X86_KERNEL_IBT), --ibt)   \
-   $(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount) \
-   $(if $(CONFIG_HAVE_OBJTOOL_NOP_MCOUNT), --mnop) \
+$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL),   \
+ $(if $(CONFIG_HAVE_OBJTOOL_NOP_MCOUNT), --mcount --mnop, 
--mcount)) \
   $(if $(CONFIG_UNWINDER_ORC), --orc) \
   $(if $(CONFIG_RETPOLINE), --retpoline)  \
   $(if $(CONFIG_RETHUNK), --rethunk)  \


- Naveen


Re: [PATCH v4 03/16] powerpc: Fix objtool unannotated intra-function call warnings

2022-10-10 Thread Naveen N. Rao

Sathvika Vasireddy wrote:

Objtool throws unannotated intra-function call warnings in the following
assembly files:

arch/powerpc/kernel/vector.o: warning: objtool: .text+0x53c: unannotated 
intra-function call

arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x60: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x124: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x5d4: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x5dc: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xcb8: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xd0c: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x1030: 
unannotated intra-function call

arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x358: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x728: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x4d94: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x4ec4: unannotated 
intra-function call

arch/powerpc/kvm/book3s_hv_interrupts.o: warning: objtool: .text+0x6c: 
unannotated intra-function call
arch/powerpc/kernel/misc_64.o: warning: objtool: .text+0x64: unannotated 
intra-function call

Objtool does not add STT_NOTYPE symbols with size 0 to the rbtree, which
is why find_call_destination() function is not able to find the
destination symbol for 'bl' instruction. For such symbols, objtool is
throwing unannotated intra-function call warnings in assembly files. Fix
these warnings by annotating those symbols with SYM_FUNC_START_LOCAL and
SYM_FUNC_END macros, inorder to set symbol type to STT_FUNC and symbol
size accordingly.

Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/exceptions-64s.S|  7 +--
 arch/powerpc/kernel/head_64.S   |  7 +--
 arch/powerpc/kernel/misc_64.S   |  4 +++-
 arch/powerpc/kernel/vector.S|  4 +++-
 arch/powerpc/kvm/book3s_hv_interrupts.S |  4 +++-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 22 +++---
 6 files changed, 34 insertions(+), 14 deletions(-)


Applying this to powerpc/merge today shows two small conflicts:



diff --git a/arch/powerpc/kernel/exceptions-64s.S 
b/arch/powerpc/kernel/exceptions-64s.S
index 3d0dc133a9ae..56a31424c8b0 100644
--- a/arch/powerpc/kernel/exceptions-64s.S
+++ b/arch/powerpc/kernel/exceptions-64s.S
@@ -13,6 +13,7 @@
  *
  */

+#include 
 #include 
 #include 
 #include 
@@ -3075,7 +3076,7 @@ CLOSE_FIXED_SECTION(virt_trampolines);
 USE_TEXT_SECTION()

 /* MSR[RI] should be clear because this uses SRR[01] */
-enable_machine_check:
+SYM_FUNC_START_LOCAL(enable_machine_check)


This is now a _GLOBAL(), so this hunk and the below 
SYM_FUNC_END(enable_machine_check) change can be dropped.



mflrr0
bcl 20,31,$+4
 0: mflrr3
@@ -3087,9 +3088,10 @@ enable_machine_check:
RFI_TO_KERNEL
 1: mtlrr0
blr
+SYM_FUNC_END(enable_machine_check)

 /* MSR[RI] should be clear because this uses SRR[01] */
-disable_machine_check:
+SYM_FUNC_START_LOCAL(disable_machine_check)
mflrr0
bcl 20,31,$+4
 0: mflrr3
@@ -3102,3 +3104,4 @@ disable_machine_check:
RFI_TO_KERNEL
 1: mtlrr0
blr
+SYM_FUNC_END(disable_machine_check)
diff --git a/arch/powerpc/kernel/head_64.S b/arch/powerpc/kernel/head_64.S
index cf2c08902c05..10e2d43420d0 100644
--- a/arch/powerpc/kernel/head_64.S
+++ b/arch/powerpc/kernel/head_64.S
@@ -18,6 +18,7 @@
  *  variants.
  */

+#include 
 #include 
 #include 
 #include 
@@ -465,7 +466,7 @@ generic_secondary_common_init:
  * Assumes we're mapped EA == RA if the MMU is on.
  */
 #ifdef CONFIG_PPC_BOOK3S
-__mmu_off:
+SYM_FUNC_START_LOCAL(__mmu_off)
mfmsr   r3
andi.   r0,r3,MSR_IR|MSR_DR
beqlr
@@ -476,6 +477,7 @@ __mmu_off:
sync
rfid
b   .   /* prevent speculative execution */
+SYM_FUNC_END(__mmu_off)
 #endif


@@ -869,7 +871,7 @@ _GLOBAL(start_secondary_resume)
 /*
  * This subroutine clobbers r11 and r12
  */
-enable_64b_mode:
+SYM_FUNC_START_LOCAL(enable_64b_mode)
mfmsr   r11 /* grab the current MSR */
 #ifdef CONFIG_PPC_BOOK3E
orisr11,r11,0x8000  /* CM bit set, we'll set ICM later */
@@ -881,6 +883,7 @@ enable_64b_mode:
isync
 #endif
blr
+SYM_FUNC_END(enable_64b_mode)

 /*
  * This puts the TOC pointer into r2, offset by 0x8000 (as expected
diff --git a/arch/powerpc/kernel/misc_64.S b/arch/powerpc/kernel/misc_64.S
index fd6d8d3a548e..b36fb89ff718 100644
--- a/arch/powerpc/kernel/misc_64.S
+++ b/arch/powerpc/kernel/misc_64.S
@@ 

Re: [PATCH linux-next][RFC] powerpc: fix HOTPLUG error in rcutorture

2022-10-10 Thread Michael Ellerman
Zhouyi Zhou  writes:
> I think we should avoid torture offline the cpu who do tick timer
> when nohz full is running.

Can you tell us what the bug you're fixing is?

Did you see a crash/oops/hang etc? Or are you just proposing this as
something that would be a good idea?

> Tested on PPC VM of Open Source Lab of Oregon State University.
> The test results show that after the fix, the success rate of
> rcutorture is improved.
> After:
> Successes: 40 Failures: 9
> Before:
> Successes: 38 Failures: 11
>
> I examined the console.log and Make.out files one by one, no new
> compile error or test error is introduced by above fix.
>
> Signed-off-by: Zhouyi Zhou 
> ---
> Dear PPC developers
>
> I found this bug when trying to do rcutorture tests in ppc VM of
> Open Source Lab of Oregon State University:
>
> ubuntu@ubuntu:~/linux-next/tools/testing/selftests/rcutorture/res/2022.09.30-01.06.22-torture$
>  find . -name "console.log.diags"|xargs grep HOTPLUG
> ./results-scftorture/NOPREEMPT/console.log.diags:WARNING: HOTPLUG FAILURES 
> NOPREEMPT
> ./results-rcutorture/TASKS03/console.log.diags:WARNING: HOTPLUG FAILURES 
> TASKS03
> ./results-rcutorture/TREE04/console.log.diags:WARNING: HOTPLUG FAILURES TREE04
> ./results-scftorture-kasan/NOPREEMPT/console.log.diags:WARNING: HOTPLUG 
> FAILURES NOPREEMPT
> ./results-rcutorture-kasan/TASKS03/console.log.diags:WARNING: HOTPLUG 
> FAILURES TASKS03
> ./results-rcutorture-kasan/TREE04/console.log.diags:WARNING: HOTPLUG FAILURES 
> TREE04
>
> I tried to fix this bug.
>
> Thanks for your patience and guidance ;-)
>
> Thanks 
> Zhouyi
> --
>  arch/powerpc/kernel/sysfs.c | 8 +++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/arch/powerpc/kernel/sysfs.c b/arch/powerpc/kernel/sysfs.c
> index ef9a61718940..be9c0e45337e 100644
> --- a/arch/powerpc/kernel/sysfs.c
> +++ b/arch/powerpc/kernel/sysfs.c
> @@ -4,6 +4,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -21,6 +22,7 @@
>  #include 
>  #include 
>  #include 
> +#include "../../../kernel/time/tick-internal.h"
  
Needing to include this internal header is a sign that we are using the
wrong API or otherwise using time keeping internals we shouldn't be.

>  #include "cacheinfo.h"
>  #include "setup.h"
> @@ -1151,7 +1153,11 @@ static int __init topology_init(void)
>* CPU.  For instance, the boot cpu might never be valid
>* for hotplugging.
>*/
> - if (smp_ops && smp_ops->cpu_offline_self)
> + if (smp_ops && smp_ops->cpu_offline_self
> +#ifdef CONFIG_NO_HZ_FULL
> + && !(tick_nohz_full_running && tick_do_timer_cpu == cpu)
> +#endif
> + )

I can't see any other arches doing anything like this. I don't think
it's the arches responsibility.

If the time keeping core needs a CPU to stay online to run the timer
then it needs to organise that itself IMHO :)

cheers

>   c->hotpluggable = 1;
>  #endif
>  
> -- 
> 2.25.1


Re: [PATCH] perf: Rewrite core context handling

2022-10-10 Thread Ravi Bangoria
On 10-Oct-22 3:53 PM, Peter Zijlstra wrote:
> On Tue, Sep 06, 2022 at 11:20:53AM +0530, Ravi Bangoria wrote:
> 
>> This one was simple enough so I prepared a patch for this. Let
>> me know if you see any issues with below diff.
> 
> I've extraed this as a separate patch since it's not strictly required
> for correctness and the patch is a quite large enough.

Sure. I'll keep it separate.

Thanks,
Ravi


Re: [PATCH] perf: Rewrite core context handling

2022-10-10 Thread Ravi Bangoria
On 10-Oct-22 3:44 PM, Peter Zijlstra wrote:
> On Wed, Sep 07, 2022 at 04:58:49PM +0530, Ravi Bangoria wrote:
>>> -static void
>>> -ctx_flexible_sched_in(struct perf_event_context *ctx,
>>> - struct perf_cpu_context *cpuctx)
>>> +/* XXX .busy thingy from Peter's patch */
>>> +static void ctx_flexible_sched_in(struct perf_event_context *ctx, struct 
>>> pmu *pmu)
>>
>> This one turned out to be very easy. Given that, we iterate over each
>> pmu, we can just return error if we fail to schedule any flexible event.
>> (It wouldn't be straight forward like this if we needed to implement
>> pmu=NULL optimization.)
>>
>> ---
>> diff --git a/kernel/events/core.c b/kernel/events/core.c
>> index e0232e0bb74e..923656af73fe 100644
>> --- a/kernel/events/core.c
>> +++ b/kernel/events/core.c
>> @@ -3751,6 +3751,7 @@ static int merge_sched_in(struct perf_event *event, 
>> void *data)
>>  cpc = 
>> this_cpu_ptr(event->pmu_ctx->pmu->cpu_pmu_context);
>>  perf_mux_hrtimer_restart(cpc);
>>  group_update_userpage(event);
>> +return -EBUSY;
>>  }
>>  }
>>  
> 
> I'm afraid this breaks things; consider:
> 
>   f79256532682 ("perf/core: fix userpage->time_enabled of inactive events")
> 
> I totally hate this -- because it means we *HAVE* to iterate the
> inactive events, but alas.

Sure. Will drop this.

Thanks,
Ravi


Re: [PATCH] perf: Rewrite core context handling

2022-10-10 Thread Peter Zijlstra
On Tue, Sep 06, 2022 at 11:20:53AM +0530, Ravi Bangoria wrote:

> This one was simple enough so I prepared a patch for this. Let
> me know if you see any issues with below diff.

I've extraed this as a separate patch since it's not strictly required
for correctness and the patch is a quite large enough.

> ---
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 820c56c66b26..e0232e0bb74e 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -9807,6 +9807,44 @@ static struct pmu perf_swevent = {
>  
>  #ifdef CONFIG_EVENT_TRACING
>  
> +static void tp_perf_event_destroy(struct perf_event *event)
> +{
> + perf_trace_destroy(event);
> +}
> +
> +static int perf_tp_event_init(struct perf_event *event)
> +{
> + int err;
> +
> + if (event->attr.type != PERF_TYPE_TRACEPOINT)
> + return -ENOENT;
> +
> + /*
> +  * no branch sampling for tracepoint events
> +  */
> + if (has_branch_stack(event))
> + return -EOPNOTSUPP;
> +
> + err = perf_trace_init(event);
> + if (err)
> + return err;
> +
> + event->destroy = tp_perf_event_destroy;
> +
> + return 0;
> +}
> +
> +static struct pmu perf_tracepoint = {
> + .task_ctx_nr= perf_sw_context,
> +
> + .event_init = perf_tp_event_init,
> + .add= perf_trace_add,
> + .del= perf_trace_del,
> + .start  = perf_swevent_start,
> + .stop   = perf_swevent_stop,
> + .read   = perf_swevent_read,
> +};
> +
>  static int perf_tp_filter_match(struct perf_event *event,
>   struct perf_sample_data *data)
>  {
> @@ -9856,6 +9894,49 @@ void perf_trace_run_bpf_submit(void *raw_data, int 
> size, int rctx,
>  }
>  EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
>  
> +
> +static void __perf_tp_event_target_task(u64 count, void *record,
> + struct pt_regs *regs,
> + struct perf_sample_data *data,
> + struct perf_event *event)
> +{
> + struct trace_entry *entry = record;
> +
> + if (event->attr.config != entry->type)
> + return;
> + /* Cannot deliver synchronous signal to other task. */
> + if (event->attr.sigtrap)
> + return;
> + if (perf_tp_event_match(event, data, regs))
> + perf_swevent_event(event, count, data, regs);
> +}
> +
> +static void perf_tp_event_target_task(u64 count, void *record,
> +   struct pt_regs *regs,
> +   struct perf_sample_data *data,
> +   struct perf_event_context *ctx)
> +{
> + struct perf_event *event, *sibling;
> +
> + event = perf_event_groups_first(>pinned_groups, smp_processor_id(),
> + _tracepoint, NULL);
> + for (; event; event = perf_event_groups_next(event, _tracepoint)) {
> + __perf_tp_event_target_task(count, record, regs, data, event);
> + for_each_sibling_event(sibling, event) {
> + __perf_tp_event_target_task(count, record, regs, data, 
> sibling);
> + }
> + }
> +
> + event = perf_event_groups_first(>flexible_groups, 
> smp_processor_id(),
> + _tracepoint, NULL);
> + for (; event; event = perf_event_groups_next(event, _tracepoint)) {
> + __perf_tp_event_target_task(count, record, regs, data, event);
> + for_each_sibling_event(sibling, event) {
> + __perf_tp_event_target_task(count, record, regs, data, 
> sibling);
> + }
> + }
> +}
> +
>  void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
>  struct pt_regs *regs, struct hlist_head *head, int rctx,
>  struct task_struct *task)
> @@ -9886,29 +9967,15 @@ void perf_tp_event(u16 event_type, u64 count, void 
> *record, int entry_size,
>*/
>   if (task && task != current) {
>   struct perf_event_context *ctx;
> - struct trace_entry *entry = record;
>  
>   rcu_read_lock();
>   ctx = rcu_dereference(task->perf_event_ctxp);
>   if (!ctx)
>   goto unlock;
>  
> - // XXX iterate groups instead, we should be able to
> - // find the subtree for the perf_tracepoint pmu and CPU.
> -
> - list_for_each_entry_rcu(event, >event_list, event_entry) {
> - if (event->cpu != smp_processor_id())
> - continue;
> - if (event->attr.type != PERF_TYPE_TRACEPOINT)
> - continue;
> - if (event->attr.config != entry->type)
> - continue;
> - /* Cannot deliver synchronous signal to other task. */
> - 

Re: [PATCH] perf: Rewrite core context handling

2022-10-10 Thread Peter Zijlstra
On Wed, Sep 07, 2022 at 04:58:49PM +0530, Ravi Bangoria wrote:
> > -static void
> > -ctx_flexible_sched_in(struct perf_event_context *ctx,
> > - struct perf_cpu_context *cpuctx)
> > +/* XXX .busy thingy from Peter's patch */
> > +static void ctx_flexible_sched_in(struct perf_event_context *ctx, struct 
> > pmu *pmu)
> 
> This one turned out to be very easy. Given that, we iterate over each
> pmu, we can just return error if we fail to schedule any flexible event.
> (It wouldn't be straight forward like this if we needed to implement
> pmu=NULL optimization.)
> 
> ---
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index e0232e0bb74e..923656af73fe 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -3751,6 +3751,7 @@ static int merge_sched_in(struct perf_event *event, 
> void *data)
>   cpc = 
> this_cpu_ptr(event->pmu_ctx->pmu->cpu_pmu_context);
>   perf_mux_hrtimer_restart(cpc);
>   group_update_userpage(event);
> + return -EBUSY;
>   }
>   }
>  

I'm afraid this breaks things; consider:

  f79256532682 ("perf/core: fix userpage->time_enabled of inactive events")

I totally hate this -- because it means we *HAVE* to iterate the
inactive events, but alas.


Re: [PATCH v4 1/6] powerpc/code-patching: Implement generic text patching function

2022-10-10 Thread Andrew Donnellan
On Mon, 2022-10-10 at 15:45 +1100, Andrew Donnellan wrote:
> On Mon, 2022-10-10 at 11:29 +1100, Benjamin Gray wrote:
> > Adds a generic memory patching mechanism for patches of size int or
> > long
> > bytes.
> > 
> > The patch_instruction function is reimplemented in terms of this
> > more generic function. This generic implementation allows patching
> > of
> > arbitrary long data, such as pointers on 64-bit. As a performance
> > optimisation the patch will unconditionally flush the icache, as
> > patch_instruction is used significantly more often and in more
> > performance sensitive contexts (e.g., ftrace activation).
> > 
> > On 32-bit patch_int is marked noinline to prevent a mis-
> > optimisation.
> > Without noinline, inside patch_branch the compiler may inline all
> > the
> > way to do_patch_memory, preventing the compiler from inlining
> > do_patch_memory into patch_int. This would needlessly force
> > patch_int
> > to be a branch to do_patch_memory.
> > 
> > The 'IS_ENABLED(CONFIG_PPC64) && ' part of the patch size condition
> > helps GCC inline __patch_memory properly. Only 64-bit requires
> > doubleword writes anyway, as ints and longs are the same size on
> > 32-
> > bit.
> > 
> > Signed-off-by: Benjamin Gray 
> 
> It looks like this needs rebasing.

Alternatively, I just need to learn to read cover letters properly and
notice that this is based off another series...

> 
> Otherwise I gave this a read over and didn't spot anything, but I'll
> hold off on a reviewed-by tag until it's rebased.
> 

Reviewed-by: Andrew Donnellan 

> > ---
> >  arch/powerpc/include/asm/code-patching.h | 29 ++
> >  arch/powerpc/lib/code-patching.c | 73 ++--
> > --
> > --
> >  2 files changed, 85 insertions(+), 17 deletions(-)
> > 
> > diff --git a/arch/powerpc/include/asm/code-patching.h
> > b/arch/powerpc/include/asm/code-patching.h
> > index 3f881548fb61..170bfa848c7c 100644
> > --- a/arch/powerpc/include/asm/code-patching.h
> > +++ b/arch/powerpc/include/asm/code-patching.h
> > @@ -72,7 +72,36 @@ static inline int create_branch(ppc_inst_t
> > *instr,
> > const u32 *addr,
> >  int create_cond_branch(ppc_inst_t *instr, const u32 *addr,
> >    unsigned long target, int flags);
> >  int patch_branch(u32 *addr, unsigned long target, int flags);
> > +
> > +/* patch_uint and patch_ulong must only be called on addresses
> > where
> > the patch
> > + * does not cross a cacheline, otherwise it may not be flushed
> > properly and
> > + * mixes of new and stale data may be observed.
> > + *
> > + * patch_instruction and other instruction patchers automatically
> > satisfy this
> > + * requirement due to instruction alignment requirements.
> > + */
> > +
> > +int patch_uint(void *addr, unsigned int val);
> > +
> > +#ifdef CONFIG_PPC64
> > +
> > +int patch_ulong(void *addr, unsigned long val);
> >  int patch_instruction(u32 *addr, ppc_inst_t instr);
> > +
> > +#else
> > +
> > +static inline int patch_ulong(void *addr, unsigned long val)
> > +{
> > +   return patch_uint(addr, val);
> > +}
> > +
> > +static inline int patch_instruction(u32 *addr, ppc_inst_t instr)
> > +{
> > +   return patch_uint(addr, ppc_inst_val(instr));
> > +}
> > +
> > +#endif
> > +
> >  int raw_patch_instruction(u32 *addr, ppc_inst_t instr);
> >  
> >  static inline unsigned long patch_site_addr(s32 *site)
> > diff --git a/arch/powerpc/lib/code-patching.c
> > b/arch/powerpc/lib/code-patching.c
> > index 125c55e3e148..e41c0ccec79f 100644
> > --- a/arch/powerpc/lib/code-patching.c
> > +++ b/arch/powerpc/lib/code-patching.c
> > @@ -15,20 +15,24 @@
> >  #include 
> >  #include 
> >  
> > -static int __patch_instruction(u32 *exec_addr, ppc_inst_t instr,
> > u32
> > *patch_addr)
> > +static int __patch_memory(void *patch_addr, unsigned long val,
> > void
> > *exec_addr,
> > + bool is_dword)
> >  {
> > -   if (!ppc_inst_prefixed(instr)) {
> > -   u32 val = ppc_inst_val(instr);
> > -
> > -   __put_kernel_nofault(patch_addr, , u32,
> > failed);
> > -   } else {
> > -   u64 val = ppc_inst_as_ulong(instr);
> > +   /* Prefixed instruction may cross cacheline if cacheline
> > smaller than 64 bytes */
> > +   BUILD_BUG_ON(IS_ENABLED(CONFIG_PPC64) && L1_CACHE_BYTES <
> > 64);
> >  
> > +   if (IS_ENABLED(CONFIG_PPC64) && unlikely(is_dword))
> > __put_kernel_nofault(patch_addr, , u64,
> > failed);
> > -   }
> > +   else
> > +   __put_kernel_nofault(patch_addr, , u32,
> > failed);
> >  
> > -   asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r"
> > (patch_addr),
> > -   "r"
> > (exec_addr));
> > +   /* Assume data is inside a single cacheline */
> > +   dcbst(patch_addr);
> > +   mb(); /* sync */
> > +   /* Flush on the EA that may be executed in case of a non-
> > coherent icache */
> > +   icbi(exec_addr);
> > +   

Re: [PATCH v3 4/4] arc: Use generic dump_stack_print_cmdline() implementation

2022-10-10 Thread Helge Deller

On 10/10/22 07:18, Vineet Gupta wrote:

On 8/8/22 06:09, Helge Deller wrote:

The process program name and command line is now shown in generic code
in dump_stack_print_info(), so drop the arc-specific implementation.

Signed-off-by: Helge Deller 


But that info printing was added back in 2018 by e36df28f532f882.
I don't think arc is using show_regs_print_info -> dump_stack_print_info yet.
Or is there a different code path now which calls here ?


Right.
See patches #1 and #2 of this series which added this
info to dump_stack_print_info().



---
  arch/arc/kernel/troubleshoot.c | 24 
  1 file changed, 24 deletions(-)

diff --git a/arch/arc/kernel/troubleshoot.c b/arch/arc/kernel/troubleshoot.c
index 7654c2e42dc0..9807e590ee55 100644
--- a/arch/arc/kernel/troubleshoot.c
+++ b/arch/arc/kernel/troubleshoot.c
@@ -51,29 +51,6 @@ static void print_regs_callee(struct callee_regs *regs)
  regs->r24, regs->r25);
  }

-static void print_task_path_n_nm(struct task_struct *tsk)
-{
-    char *path_nm = NULL;
-    struct mm_struct *mm;
-    struct file *exe_file;
-    char buf[ARC_PATH_MAX];
-
-    mm = get_task_mm(tsk);
-    if (!mm)
-    goto done;
-
-    exe_file = get_mm_exe_file(mm);
-    mmput(mm);
-
-    if (exe_file) {
-    path_nm = file_path(exe_file, buf, ARC_PATH_MAX-1);
-    fput(exe_file);
-    }
-
-done:
-    pr_info("Path: %s\n", !IS_ERR(path_nm) ? path_nm : "?");
-}
-
  static void show_faulting_vma(unsigned long address)
  {
  struct vm_area_struct *vma;
@@ -176,7 +153,6 @@ void show_regs(struct pt_regs *regs)
   */
  preempt_enable();


Maybe we remove preempt* as well now (perhaps as a follow up patch)
since that was added by f731a8e89f8c78 "ARC: show_regs: lockdep:
re-enable preemption" where show_regs -> print_task_path_n_nm ->
mmput was triggering lockdep splat which is supposedly removed.


The patch series was dropped from Andrew's queue, because the kernel
test robot showed some issues:
https://lore.kernel.org/lkml/Yu59QdVpPgnXUnQC@xsang-OptiPlex-9020/
Maybe adding preempt_enable() in my patches would fix that -
sadly I haven't had time to follow up on this yet ...

Helge





-    print_task_path_n_nm(tsk);
  show_regs_print_info(KERN_INFO);

  show_ecr_verbose(regs);