[PULL 0/2] xtensa fixes for 5.12

2021-03-29 Thread Max Filippov
Hi Linus,

please pull the following fixes for the xtensa architecture for v5.12.

The following changes since commit a38fd8748464831584a19438cbb3082b5a2dab15:

  Linux 5.12-rc2 (2021-03-05 17:33:41 -0800)

are available in the Git repository at:

  git://github.com/jcmvbkbc/linux-xtensa.git tags/xtensa-20210329

for you to fetch changes up to 7b9acbb6aad4f54623dcd4bd4b1a60fe0c727b09:

  xtensa: fix uaccess-related livelock in do_page_fault (2021-03-29 11:25:11 
-0700)


Xtensa fixes for v5.12:

- fix build with separate exception vectors when they are placed too far
  from the rest of the kernel;
- fix uaccess-related livelock in do_page_fault.


Max Filippov (2):
  xtensa: move coprocessor_flush to the .text section
  xtensa: fix uaccess-related livelock in do_page_fault

 arch/xtensa/kernel/coprocessor.S | 64 +---
 arch/xtensa/mm/fault.c   |  5 +++-
 2 files changed, 37 insertions(+), 32 deletions(-)

-- 
Thanks.
-- Max


Re: [PATCH 01/23] atomctl.rst: A typo fix

2021-03-28 Thread Max Filippov
On Sun, Mar 28, 2021 at 10:18 PM Bhaskar Chowdhury
 wrote:
>
> s/controlers/controllers/
>
> Signed-off-by: Bhaskar Chowdhury 
> ---
>  Documentation/xtensa/atomctl.rst | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/Documentation/xtensa/atomctl.rst 
> b/Documentation/xtensa/atomctl.rst
> index 1ecbd0ba9a2e..a0efab2abe8f 100644
> --- a/Documentation/xtensa/atomctl.rst
> +++ b/Documentation/xtensa/atomctl.rst
> @@ -23,7 +23,7 @@ doing a Cached (WB) transaction and use the Memory RCW for 
> un-cached
>  operations.
>
>  For systems without an coherent cache controller, non-MX, we always
> -use the memory controllers RCW, thought non-MX controlers likely
> +use the memory controllers RCW, thought non-MX controllers likely

In this line you could also do s/thought/though/.

-- 
Thanks.
-- Max


Re: [PATCH 01/23] atomctl.rst: A typo fix

2021-03-28 Thread Max Filippov
On Sun, Mar 28, 2021 at 10:37 PM Max Filippov  wrote:
>
> On Sun, Mar 28, 2021 at 10:18 PM Bhaskar Chowdhury
>  wrote:
> >
> > s/controlers/controllers/
> >
> > Signed-off-by: Bhaskar Chowdhury 
> > ---
> >  Documentation/xtensa/atomctl.rst | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/Documentation/xtensa/atomctl.rst 
> > b/Documentation/xtensa/atomctl.rst
> > index 1ecbd0ba9a2e..a0efab2abe8f 100644
> > --- a/Documentation/xtensa/atomctl.rst
> > +++ b/Documentation/xtensa/atomctl.rst
> > @@ -23,7 +23,7 @@ doing a Cached (WB) transaction and use the Memory RCW 
> > for un-cached
> >  operations.
> >
> >  For systems without an coherent cache controller, non-MX, we always
> > -use the memory controllers RCW, thought non-MX controlers likely
> > +use the memory controllers RCW, thought non-MX controllers likely
>
> In this line you could also do s/thought/though/.

...and s/memory controllers/memory controller's/

-- 
Thanks.
-- Max


[PATCH] xtensa: fix uaccess-related livelock in do_page_fault

2021-03-27 Thread Max Filippov
If a uaccess (e.g. get_user()) triggers a fault and there's a
fault signal pending, the handler will return to the uaccess without
having performed a uaccess fault fixup, and so the CPU will immediately
execute the uaccess instruction again, whereupon it will livelock
bouncing between that instruction and the fault handler.

https://lore.kernel.org/lkml/20210121123140.GD48431@C02TD0UTHF1T.local/

Cc: sta...@vger.kernel.org
Reported-by: Mark Rutland 
Signed-off-by: Max Filippov 
---
 arch/xtensa/mm/fault.c | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/xtensa/mm/fault.c b/arch/xtensa/mm/fault.c
index 7666408ce12a..95a74890c7e9 100644
--- a/arch/xtensa/mm/fault.c
+++ b/arch/xtensa/mm/fault.c
@@ -112,8 +112,11 @@ void do_page_fault(struct pt_regs *regs)
 */
fault = handle_mm_fault(vma, address, flags, regs);
 
-   if (fault_signal_pending(fault, regs))
+   if (fault_signal_pending(fault, regs)) {
+   if (!user_mode(regs))
+   goto bad_page_fault;
return;
+   }
 
if (unlikely(fault & VM_FAULT_ERROR)) {
if (fault & VM_FAULT_OOM)
-- 
2.20.1



[PATCH 2/2] xtensa: ISS: add GDBIO implementation to semihosting interface

2021-03-26 Thread Max Filippov
Add GDBIO implementation for the xtensa semihosting interface. It offers
less functions than the simcall interface, so make some semihosting
functions optional and return error when implementation is not
available.
Add Kconfig menu to select semihosting implementation and add simcall and
GDBIO choices there.

Signed-off-by: Max Filippov 
---
 arch/xtensa/Kconfig   | 22 
 .../iss/include/platform/simcall-gdbio.h  | 34 ++
 .../platforms/iss/include/platform/simcall.h  | 36 +++
 3 files changed, 92 insertions(+)
 create mode 100644 arch/xtensa/platforms/iss/include/platform/simcall-gdbio.h

diff --git a/arch/xtensa/Kconfig b/arch/xtensa/Kconfig
index d637b396f81c..6ad4c1161518 100644
--- a/arch/xtensa/Kconfig
+++ b/arch/xtensa/Kconfig
@@ -393,6 +393,28 @@ config PARSE_BOOTPARAM
 
  If unsure, say Y.
 
+choice
+   prompt "Semihosting interface"
+   default XTENSA_SIMCALL_ISS
+   depends on XTENSA_PLATFORM_ISS
+   help
+ Choose semihosting interface that will be used for serial port,
+ block device and networking.
+
+config XTENSA_SIMCALL_ISS
+   bool "simcall"
+   help
+ Use simcall instruction. simcall is only available on simulators,
+ it does nothing on hardware.
+
+config XTENSA_SIMCALL_GDBIO
+   bool "GDBIO"
+   help
+ Use break instruction. It is available on real hardware when GDB
+ is attached to it via JTAG.
+
+endchoice
+
 config BLK_DEV_SIMDISK
tristate "Host file-based simulated block device support"
default n
diff --git a/arch/xtensa/platforms/iss/include/platform/simcall-gdbio.h 
b/arch/xtensa/platforms/iss/include/platform/simcall-gdbio.h
new file mode 100644
index ..e642860e25a8
--- /dev/null
+++ b/arch/xtensa/platforms/iss/include/platform/simcall-gdbio.h
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021 Cadence Design Systems Inc. */
+
+#ifndef _XTENSA_PLATFORM_ISS_SIMCALL_GDBIO_H
+#define _XTENSA_PLATFORM_ISS_SIMCALL_GDBIO_H
+
+/*
+ *  System call like services offered by the GDBIO host.
+ */
+
+#define SYS_open   -2
+#define SYS_close  -3
+#define SYS_read   -4
+#define SYS_write  -5
+#define SYS_lseek  -6
+
+static int errno;
+
+static inline int __simc(int a, int b, int c, int d)
+{
+   register int a1 asm("a2") = a;
+   register int b1 asm("a6") = b;
+   register int c1 asm("a3") = c;
+   register int d1 asm("a4") = d;
+   __asm__ __volatile__ (
+   "break 1, 14\n"
+   : "+r"(a1), "+r"(c1)
+   : "r"(b1), "r"(d1)
+   : "memory");
+   errno = c1;
+   return a1;
+}
+
+#endif /* _XTENSA_PLATFORM_ISS_SIMCALL_GDBIO_H */
diff --git a/arch/xtensa/platforms/iss/include/platform/simcall.h 
b/arch/xtensa/platforms/iss/include/platform/simcall.h
index 612080bf0b3d..e1ec50ce39ee 100644
--- a/arch/xtensa/platforms/iss/include/platform/simcall.h
+++ b/arch/xtensa/platforms/iss/include/platform/simcall.h
@@ -12,11 +12,23 @@
 #ifndef _XTENSA_PLATFORM_ISS_SIMCALL_H
 #define _XTENSA_PLATFORM_ISS_SIMCALL_H
 
+#include 
+
+#ifdef CONFIG_XTENSA_SIMCALL_ISS
 #include 
+#endif
+#ifdef CONFIG_XTENSA_SIMCALL_GDBIO
+#include 
+#endif
 
 static inline int simc_exit(int exit_code)
 {
+#ifdef SYS_exit
return __simc(SYS_exit, exit_code, 0, 0);
+#else
+   WARN_ONCE(1, "%s: not implemented\n", __func__);
+   return -1;
+#endif
 }
 
 static inline int simc_open(const char *file, int flags, int mode)
@@ -31,7 +43,12 @@ static inline int simc_close(int fd)
 
 static inline int simc_ioctl(int fd, int request, void *arg)
 {
+#ifdef SYS_ioctl
return __simc(SYS_ioctl, fd, request, (int) arg);
+#else
+   WARN_ONCE(1, "%s: not implemented\n", __func__);
+   return -1;
+#endif
 }
 
 static inline int simc_read(int fd, void *buf, size_t count)
@@ -46,9 +63,14 @@ static inline int simc_write(int fd, const void *buf, size_t 
count)
 
 static inline int simc_poll(int fd)
 {
+#ifdef SYS_select_one
long timeval[2] = { 0, 0 };
 
return __simc(SYS_select_one, fd, XTISS_SELECT_ONE_READ, (int));
+#else
+   WARN_ONCE(1, "%s: not implemented\n", __func__);
+   return -1;
+#endif
 }
 
 static inline int simc_lseek(int fd, uint32_t off, int whence)
@@ -58,17 +80,31 @@ static inline int simc_lseek(int fd, uint32_t off, int 
whence)
 
 static inline int simc_argc(void)
 {
+#ifdef SYS_iss_argc
return __simc(SYS_iss_argc, 0, 0, 0);
+#else
+   WARN_ONCE(1, "%s: not implemented\n", __func__);
+   return 0;
+#endif
 }
 
 static inline int simc_argv_size(void)
 {
+#ifdef SYS_iss_argv_size
return __simc(SYS_iss_argv_size, 0, 0, 0);
+#else
+   WARN_ONCE(

[PATCH 1/2] xtensa: ISS: split simcall implementation from semihosting interface

2021-03-26 Thread Max Filippov
Disconnect existing ISS simcall implementation from the semihosting
interface to allow for alternative implementations selectable at
configure time.

Signed-off-by: Max Filippov 
---
 .../iss/include/platform/simcall-iss.h| 73 +++
 .../platforms/iss/include/platform/simcall.h  | 70 +-
 2 files changed, 75 insertions(+), 68 deletions(-)
 create mode 100644 arch/xtensa/platforms/iss/include/platform/simcall-iss.h

diff --git a/arch/xtensa/platforms/iss/include/platform/simcall-iss.h 
b/arch/xtensa/platforms/iss/include/platform/simcall-iss.h
new file mode 100644
index ..5a1e7a1f182e
--- /dev/null
+++ b/arch/xtensa/platforms/iss/include/platform/simcall-iss.h
@@ -0,0 +1,73 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2021 Cadence Design Systems Inc. */
+
+#ifndef _XTENSA_PLATFORM_ISS_SIMCALL_ISS_H
+#define _XTENSA_PLATFORM_ISS_SIMCALL_ISS_H
+
+/*
+ *  System call like services offered by the simulator host.
+ */
+
+#define SYS_nop0   /* unused */
+#define SYS_exit   1   /*x*/
+#define SYS_fork   2
+#define SYS_read   3   /*x*/
+#define SYS_write  4   /*x*/
+#define SYS_open   5   /*x*/
+#define SYS_close  6   /*x*/
+#define SYS_rename 7   /*x 38 - waitpid */
+#define SYS_creat  8   /*x*/
+#define SYS_link   9   /*x (not implemented on WIN32) */
+#define SYS_unlink 10  /*x*/
+#define SYS_execv  11  /* n/a - execve */
+#define SYS_execve 12  /* 11 - chdir */
+#define SYS_pipe   13  /* 42 - time */
+#define SYS_stat   14  /* 106 - mknod */
+#define SYS_chmod  15
+#define SYS_chown  16  /* 202 - lchown */
+#define SYS_utime  17  /* 30 - break */
+#define SYS_wait   18  /* n/a - oldstat */
+#define SYS_lseek  19  /*x*/
+#define SYS_getpid 20
+#define SYS_isatty 21  /* n/a - mount */
+#define SYS_fstat  22  /* 108 - oldumount */
+#define SYS_time   23  /* 13 - setuid */
+#define SYS_gettimeofday 24/*x 78 - getuid (not implemented on WIN32) */
+#define SYS_times  25  /*X 43 - stime (Xtensa-specific implementation) 
*/
+#define SYS_socket  26
+#define SYS_sendto  27
+#define SYS_recvfrom28
+#define SYS_select_one  29  /* not compatible select, one file descriptor 
at the time */
+#define SYS_bind30
+#define SYS_ioctl  31
+
+#define SYS_iss_argc   1000/* returns value of argc */
+#define SYS_iss_argv_size 1001 /* bytes needed for argv & arg strings */
+#define SYS_iss_set_argv 1002  /* saves argv & arg strings at given addr */
+
+/*
+ * SYS_select_one specifiers
+ */
+
+#define  XTISS_SELECT_ONE_READ1
+#define  XTISS_SELECT_ONE_WRITE   2
+#define  XTISS_SELECT_ONE_EXCEPT  3
+
+static int errno;
+
+static inline int __simc(int a, int b, int c, int d)
+{
+   register int a1 asm("a2") = a;
+   register int b1 asm("a3") = b;
+   register int c1 asm("a4") = c;
+   register int d1 asm("a5") = d;
+   __asm__ __volatile__ (
+   "simcall\n"
+   : "+r"(a1), "+r"(b1)
+   : "r"(c1), "r"(d1)
+   : "memory");
+   errno = b1;
+   return a1;
+}
+
+#endif /* _XTENSA_PLATFORM_ISS_SIMCALL_ISS_H */
diff --git a/arch/xtensa/platforms/iss/include/platform/simcall.h 
b/arch/xtensa/platforms/iss/include/platform/simcall.h
index 5ba2454e6c37..612080bf0b3d 100644
--- a/arch/xtensa/platforms/iss/include/platform/simcall.h
+++ b/arch/xtensa/platforms/iss/include/platform/simcall.h
@@ -6,78 +6,13 @@
  * for more details.
  *
  * Copyright (C) 2001 Tensilica Inc.
- * Copyright (C) 2017 Cadence Design Systems Inc.
+ * Copyright (C) 2017 - 2021 Cadence Design Systems Inc.
  */
 
 #ifndef _XTENSA_PLATFORM_ISS_SIMCALL_H
 #define _XTENSA_PLATFORM_ISS_SIMCALL_H
 
-
-/*
- *  System call like services offered by the simulator host.
- */
-
-#define SYS_nop0   /* unused */
-#define SYS_exit   1   /*x*/
-#define SYS_fork   2
-#define SYS_read   3   /*x*/
-#define SYS_write  4   /*x*/
-#define SYS_open   5   /*x*/
-#define SYS_close  6   /*x*/
-#define SYS_rename 7   /*x 38 - waitpid */
-#define SYS_creat  8   /*x*/
-#define SYS_link   9   /*x (not implemented on WIN32) */
-#define SYS_unlink 10  /*x*/
-#define SYS_execv  11  /* n/a - execve */
-#define SYS_execve 12  /* 11 - chdir */
-#define SYS_pipe   13  /* 42 - time */
-#define SYS_stat   14  /* 106 - mknod */
-#define SYS_chmod  15
-#define SYS_chown  16  /* 202 - lchown */
-#define SYS_utime  17  /* 30 - break */
-#define SYS_wait   18  /* n/a - oldstat */
-#define SYS_lseek  19  /*x*/
-#define SYS_getpid 20
-#define SYS_

[PATCH 0/2] xtensa: add GDBIO implementation to semihosting interface

2021-03-26 Thread Max Filippov
Hello,

this series adds GDBIO implementation to the semihosting interface and
makes it a configuration time choice. GDBIO interface is useful for
development hardware platforms that don't offer any peripherals and only
accessible via JTAG. xtensa kernel built for ISS may be run on such
platforms using GDBIO implementation of the semihosting interface.

Max Filippov (2):
  xtensa: ISS: split simcall implementation from semihosting interface
  xtensa: ISS: add GDBIO implementation to semihosting interface

 arch/xtensa/Kconfig   |  22 
 .../iss/include/platform/simcall-gdbio.h  |  34 ++
 .../iss/include/platform/simcall-iss.h|  73 
 .../platforms/iss/include/platform/simcall.h  | 104 +++---
 4 files changed, 166 insertions(+), 67 deletions(-)
 create mode 100644 arch/xtensa/platforms/iss/include/platform/simcall-gdbio.h
 create mode 100644 arch/xtensa/platforms/iss/include/platform/simcall-iss.h

-- 
2.20.1



Re: [PATCH] platforms/iss/simcall.h: Change compitible to compatible

2021-03-26 Thread Max Filippov
On Thu, Mar 25, 2021 at 6:29 PM Bhaskar Chowdhury  wrote:
>
>
> s/compitible/compatible/
>
> Signed-off-by: Bhaskar Chowdhury 
> ---
>  arch/xtensa/platforms/iss/include/platform/simcall.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Thanks, applied to my xtensa tree with minor adjustment of the subject line.

-- 
Thanks.
-- Max


Re: [PATCH] xtensa: Couple of typo fixes

2021-03-26 Thread Max Filippov
On Wed, Mar 24, 2021 at 9:09 PM Bhaskar Chowdhury  wrote:
>
> s/contans/contains/
> s/desination/destination/
>
> Signed-off-by: Bhaskar Chowdhury 
> ---
>  arch/xtensa/kernel/head.S | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)

Thanks, applied to my xtensa tree.


--
Thanks.
-- Max


[PATCH] xtensa: drop extraneous register load from initialize_mmu

2021-03-14 Thread Max Filippov
Commit a9f2fc628e3a ("xtensa: cleanup MMU setup and kernel layout macros")
removed the use of a2 in the beginning of the initialize_mmu macro, but
left the register load that is no longer used. Remove it as well.

Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/initialize_mmu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/xtensa/include/asm/initialize_mmu.h 
b/arch/xtensa/include/asm/initialize_mmu.h
index 05cb13dfe6f4..9793b49fc641 100644
--- a/arch/xtensa/include/asm/initialize_mmu.h
+++ b/arch/xtensa/include/asm/initialize_mmu.h
@@ -73,7 +73,7 @@
_j  2f
 
.align  4
-1: movia2, 0x1000
+1:
 
 #if CONFIG_KERNEL_LOAD_ADDRESS < 0x4000ul
 #define TEMP_MAPPING_VADDR 0x4000
-- 
2.20.1



[PATCH] xtensa: fix pgprot_noncached assumptions

2021-03-14 Thread Max Filippov
pgprot_noncached assumes that cache bypass attribute is represented as
zero. This may not always be true. Fix pgprot_noncached definition by
adding _PAGE_CA_BYPASS to the result.

Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/pgtable.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/arch/xtensa/include/asm/pgtable.h 
b/arch/xtensa/include/asm/pgtable.h
index 4dc04e6c01d7..d7fc45c920c2 100644
--- a/arch/xtensa/include/asm/pgtable.h
+++ b/arch/xtensa/include/asm/pgtable.h
@@ -280,7 +280,9 @@ static inline pte_t pte_mkyoung(pte_t pte)
 static inline pte_t pte_mkwrite(pte_t pte)
{ pte_val(pte) |= _PAGE_WRITABLE; return pte; }
 
-#define pgprot_noncached(prot) (__pgprot(pgprot_val(prot) & ~_PAGE_CA_MASK))
+#define pgprot_noncached(prot) \
+   ((__pgprot((pgprot_val(prot) & ~_PAGE_CA_MASK) | \
+  _PAGE_CA_BYPASS)))
 
 /*
  * Conversion functions: convert a page and protection to a page entry,
-- 
2.20.1



[PATCH] xtensa: simplify coherent_kvaddr logic

2021-03-14 Thread Max Filippov
Functions coherent_kvaddr, clear_page_alias and copy_page_alias use
physical address 0 as a special value that means 'this page is in the
KSEG mapping and its existing virtual address has the same color as the
virtual address of its future mapping, so don't map it to the
TLBTEMP_BASE area'.

Simplify this logic and drop special handling of low memory pages/pages
with coherent mapping and always use TLBTEMP_BASE area.

Signed-off-by: Max Filippov 
---
 arch/xtensa/mm/cache.c |  9 ++---
 arch/xtensa/mm/misc.S  | 36 +---
 2 files changed, 7 insertions(+), 38 deletions(-)

diff --git a/arch/xtensa/mm/cache.c b/arch/xtensa/mm/cache.c
index 085b8c77b9d9..19e5a478a7e8 100644
--- a/arch/xtensa/mm/cache.c
+++ b/arch/xtensa/mm/cache.c
@@ -81,13 +81,8 @@ static inline void kmap_invalidate_coherent(struct page 
*page,
 static inline void *coherent_kvaddr(struct page *page, unsigned long base,
unsigned long vaddr, unsigned long *paddr)
 {
-   if (PageHighMem(page) || !DCACHE_ALIAS_EQ(page_to_phys(page), vaddr)) {
-   *paddr = page_to_phys(page);
-   return (void *)(base + (vaddr & DCACHE_ALIAS_MASK));
-   } else {
-   *paddr = 0;
-   return page_to_virt(page);
-   }
+   *paddr = page_to_phys(page);
+   return (void *)(base + (vaddr & DCACHE_ALIAS_MASK));
 }
 
 void clear_user_highpage(struct page *page, unsigned long vaddr)
diff --git a/arch/xtensa/mm/misc.S b/arch/xtensa/mm/misc.S
index 25cd67debee6..0527bf6e3211 100644
--- a/arch/xtensa/mm/misc.S
+++ b/arch/xtensa/mm/misc.S
@@ -118,20 +118,13 @@ ENTRY(clear_page_alias)
 
abi_entry_default
 
-   /* Skip setting up a temporary DTLB if not aliased low page. */
-
movia5, PAGE_OFFSET
-   movia6, 0
-   beqza3, 1f
-
-   /* Setup a temporary DTLB for the addr. */
-
addia6, a3, (PAGE_KERNEL | _PAGE_HW_WRITE)
mov a4, a2
wdtlb   a6, a2
dsync
 
-1: movia3, 0
+   movia3, 0
__loopi a2, a7, PAGE_SIZE, 32
s32ia3, a2, 0
s32ia3, a2, 4
@@ -143,12 +136,9 @@ ENTRY(clear_page_alias)
s32ia3, a2, 28
__endla a2, a7, 32
 
-   bneza6, 1f
-   abi_ret_default
-
-   /* We need to invalidate the temporary idtlb entry, if any. */
+   /* We need to invalidate the temporary dtlb entry. */
 
-1: idtlb   a4
+   idtlb   a4
dsync
 
abi_ret_default
@@ -166,22 +156,12 @@ ENTRY(copy_page_alias)
 
abi_entry_default
 
-   /* Skip setting up a temporary DTLB for destination if not aliased. */
-
-   movia6, 0
-   movia7, 0
-   beqza4, 1f
-
/* Setup a temporary DTLB for destination. */
 
addia6, a4, (PAGE_KERNEL | _PAGE_HW_WRITE)
wdtlb   a6, a2
dsync
 
-   /* Skip setting up a temporary DTLB for source if not aliased. */
-
-1: beqza5, 1f
-
/* Setup a temporary DTLB for source. */
 
addia7, a5, PAGE_KERNEL
@@ -219,17 +199,11 @@ ENTRY(copy_page_alias)
 
/* We need to invalidate any temporary mapping! */
 
-   bneza6, 1f
-   bneza7, 2f
-   abi_ret_default
-
-1: addia2, a2, -PAGE_SIZE
+   addia2, a2, -PAGE_SIZE
idtlb   a2
dsync
-   bneza7, 2f
-   abi_ret_default
 
-2: addia3, a3, -PAGE_SIZE+1
+   addia3, a3, -PAGE_SIZE+1
idtlb   a3
dsync
 
-- 
2.20.1



Re: [PATCH 1/3] xtensa: stop filling syscall array with sys_ni_syscall

2021-03-14 Thread Max Filippov
On Mon, Mar 1, 2021 at 7:37 AM Masahiro Yamada  wrote:
>
> arch/xtensa/kernel/syscalls/syscalltbl.sh fills missing syscall numbers
> with sys_ni_syscall.
>
> So, the generated arch/xtensa/include/generated/asm/syscall_table.h
> has no hole.
>
> Hence, the line:
>
>   [0 ... __NR_syscalls - 1] = (syscall_t)_ni_syscall,
>
> is meaningless.
>
> The number of generated __SYSCALL() macros is the same as __NR_syscalls
> (this is 442 as of v5.11).
>
> Hence, the array size, [__NR_syscalls] is unneeded.
>
> The designated initializer, '[nr] =', is also unneeded.
>
> This file does not need to know __NR_syscalls. Drop the unneeded
>  include directive.
>
> Signed-off-by: Masahiro Yamada 
> ---
>
>  arch/xtensa/kernel/syscall.c | 7 ++-
>  1 file changed, 2 insertions(+), 5 deletions(-)

Thanks. I've applied the whole series to my xtensa tree.

-- 
Thanks.
-- Max


Re: [PATCH v2 1/2] xtensa: move CONFIG_CPU_*_ENDIAN defines to Kconfig

2021-03-14 Thread Max Filippov
On Sat, Mar 13, 2021 at 4:24 AM Masahiro Yamada  wrote:
>
> Move the definition of CONFIG_CPU_*_ENDIAN to Kconfig, the best place
> for CONFIG options.
>
> I slightly simplified the test code. You can use the -P option to suppress
> linemarker generation. The grep command is unneeded.
>
>   $ echo __XTENSA_EB__ | xtensa-linux-gcc -E -
>   # 1 ""
>   # 1 ""
>   # 1 ""
>   # 1 ""
>   1
>
>   $ echo __XTENSA_EB__ | xtensa-linux-gcc -E -P -
>   1
>
> Signed-off-by: Masahiro Yamada 
> ---
>
> Changes in v2:
>  - more cleanups
>
>  arch/xtensa/Kconfig| 6 ++
>  arch/xtensa/Makefile   | 9 +
>  arch/xtensa/boot/Makefile  | 4 
>  arch/xtensa/boot/boot-elf/Makefile | 6 +-
>  arch/xtensa/boot/boot-redboot/Makefile | 6 +-
>  5 files changed, 9 insertions(+), 22 deletions(-)

Thanks. I've made one change and applied both patches to my xtensa tree.

[...]

> diff --git a/arch/xtensa/Makefile b/arch/xtensa/Makefile
> index cf0940708702..f43c731dec89 100644
> --- a/arch/xtensa/Makefile
> +++ b/arch/xtensa/Makefile
> @@ -52,14 +52,7 @@ ifneq ($(CONFIG_LD_NO_RELAX),)
>  KBUILD_LDFLAGS := --no-relax
>  endif
>
> -ifeq ($(shell echo __XTENSA_EB__ | $(CC) -E - | grep -v "\#"),1)
> -CHECKFLAGS += -D__XTENSA_EB__
> -KBUILD_CPPFLAGS += -DCONFIG_CPU_BIG_ENDIAN
> -endif
> -ifeq ($(shell echo __XTENSA_EL__ | $(CC) -E - | grep -v "\#"),1)
> -CHECKFLAGS += -D__XTENSA_EL__
> -KBUILD_CPPFLAGS += -DCONFIG_CPU_LITTLE_ENDIAN
> -endif
> +CHECKFLAGS += -D $(if $(CONFIG_CPU_BIG_ENDIAN),__XTENSA_EB__,__XTENSA_EL__)

I've removed the space between -D and the $(if ...) to make the result
look like the rest of -D options.

-- 
Thanks.
-- Max


Re: [PATCH] xtensa: move CONFIG_CPU_*_ENDIAN defines to Kconfig

2021-03-13 Thread Max Filippov
On Fri, Mar 12, 2021 at 7:14 AM Masahiro Yamada  wrote:
>
> Move the definition of CONFIG_CPU_*_ENDIAN to Kconfig, the best place
> for CONFIG options.
>
> I slightly simplified the test code. You can use the -P option to suppress
> linemarker generation. The grep command is unneeded.
>
>   $ echo __XTENSA_EB__ | xtensa-linux-gcc -E -
>   # 1 ""
>   # 1 ""
>   # 1 ""
>   # 1 ""
>   1
>
>   $ echo __XTENSA_EB__ | xtensa-linux-gcc -E -P -
>   1
>
> Signed-off-by: Masahiro Yamada 
> ---

> If I understood correctly, xtensa compilers can support either
> big-endian or little-endian. We cannot change it via a command option.
> Actually, it does not understand -mbig-endian or -mlittle-endian.
> (Is this correct?)

This is correct. The idea is that specific xtensa core has fixed endianness,
but it can be either little or big. The toolchain is configured for the specific
xtensa core at build time, so there's no need for a runtime endian switch.

>   $ xtensa-linux-gcc   -mbig-endian  /dev/null  -c -o /dev/null
>   xtensa-linux-gcc: error: unrecognized command-line option '-mbig-endian'
>
> I see -mbig-endian / -mlittle-endian in old GCC manual.
>   https://gcc.gnu.org/onlinedocs/gcc-3.3.5/gcc/Xtensa-Options.html
> But, I cannot see them in recent gcc manuals.
> So, I have no idea better than checking __XTENSA_EB__ or __XTENSA_EL__.
> I just moved the logic to Kconfig from Makefile.
>
>
>  arch/xtensa/Kconfig  | 6 ++
>  arch/xtensa/Makefile | 9 +
>  2 files changed, 7 insertions(+), 8 deletions(-)

Acked-by: Max Filippov 
Please let me know if I should take it into the xtensa tree or you will
carry it in the kbuild tree.

-- 
Thanks.
-- Max


Re: [PATCH] xtensa: fix warning comparing pointer to 0

2021-03-09 Thread Max Filippov
On Tue, Mar 9, 2021 at 11:10 PM Jiapeng Chong
 wrote:
>
> Fix the following coccicheck warning:
>
> ./arch/xtensa/kernel/pci.c:79:17-18: WARNING comparing pointer to 0.
>
> Reported-by: Abaci Robot 
> Signed-off-by: Jiapeng Chong 
> ---
>  arch/xtensa/kernel/pci.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Thanks, applied to my xtensa tree.

-- 
Thanks.
-- Max


Re: [PATCH 30/44] tty: xtensa/iss, don't reassign to tty->port

2021-03-02 Thread Max Filippov
On Mon, Mar 1, 2021 at 10:22 PM Jiri Slaby  wrote:
>
> We already do tty_port_link_device in rs_init, so we don't need to
> reassign a port to tty->port. It would be too late in tty::ops::open
> anyway.
>
> Signed-off-by: Jiri Slaby 
> Cc: Chris Zankel 
> Cc: Max Filippov 
> Cc: linux-xte...@linux-xtensa.org
> ---
>  arch/xtensa/platforms/iss/console.c | 1 -
>  1 file changed, 1 deletion(-)

Acked-by: Max Filippov 

-- 
Thanks.
-- Max


Re: [PATCH 34/44] tty: do not check tty_unregister_driver's return value

2021-03-02 Thread Max Filippov
On Mon, Mar 1, 2021 at 10:22 PM Jiri Slaby  wrote:
>
> These drivers check tty_unregister_driver return value. But they don't
> handle a failure correctly (they free the driver in any case). So stop
> checking tty_unregister_driver return value and remove also the prints.
>
> In the next patch, tty_unregister_driver's return type will be switched
> to void.
>
> Signed-off-by: Jiri Slaby 
> Cc: Chris Zankel 
> Cc: Max Filippov 
> Cc: linux-xte...@linux-xtensa.org
> Cc: Jiri Kosina 
> Cc: David Sterba 
> ---
>  arch/xtensa/platforms/iss/console.c | 6 +-
>  drivers/tty/amiserial.c | 8 ++--
>  drivers/tty/ipwireless/tty.c| 7 +--
>  drivers/tty/moxa.c  | 4 +---
>  drivers/tty/serial/kgdb_nmi.c   | 4 +---
>  drivers/tty/synclink_gt.c   | 5 +
>  6 files changed, 7 insertions(+), 27 deletions(-)

Reviewed-by: Max Filippov 

-- 
Thanks.
-- Max


Re: [PATCH 33/44] tty: xtensa/iss, make rs_init static

2021-03-02 Thread Max Filippov
On Mon, Mar 1, 2021 at 10:22 PM Jiri Slaby  wrote:
>
> To fix the warning:
> warning: no previous prototype for 'rs_init'
>
> Signed-off-by: Jiri Slaby 
> Cc: Chris Zankel 
> Cc: Max Filippov 
> Cc: linux-xte...@linux-xtensa.org
> ---
>  arch/xtensa/platforms/iss/console.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Acked-by: Max Filippov 

-- 
Thanks.
-- Max


Re: [PATCH 32/44] tty: xtensa/iss, setup the timer statically

2021-03-02 Thread Max Filippov
On Mon, Mar 1, 2021 at 10:22 PM Jiri Slaby  wrote:
>
> Use DEFINE_TIMER and avoid runtime initialization of the serial_timer.
>
> Signed-off-by: Jiri Slaby 
> Cc: Chris Zankel 
> Cc: Max Filippov 
> Cc: linux-xte...@linux-xtensa.org
> ---
>  arch/xtensa/platforms/iss/console.c | 11 ---
>  1 file changed, 4 insertions(+), 7 deletions(-)

Acked-by: Max Filippov 

-- 
Thanks.
-- Max


Re: [PATCH 31/44] tty: xtensa/iss, remove stale comments

2021-03-02 Thread Max Filippov
On Mon, Mar 1, 2021 at 10:22 PM Jiri Slaby  wrote:
>
> These are likely taken over from amiserial. iss doesn't do anything of
> that.
>
> Signed-off-by: Jiri Slaby 
> Cc: Chris Zankel 
> Cc: Max Filippov 
> Cc: linux-xte...@linux-xtensa.org
> ---
>  arch/xtensa/platforms/iss/console.c | 18 --
>  1 file changed, 18 deletions(-)

Acked-by: Max Filippov 

-- 
Thanks.
-- Max


Re: [PATCH 29/44] tty: xtensa/iss, drop serial_version & serial_name

2021-03-02 Thread Max Filippov
On Mon, Mar 1, 2021 at 10:22 PM Jiri Slaby  wrote:
>
> There is no need to print the information during module load. Neither to
> print some artificial version. So drop these strings and a print.
>
> Signed-off-by: Jiri Slaby 
> Cc: Chris Zankel 
> Cc: Max Filippov 
> Cc: linux-xte...@linux-xtensa.org
> ---
>  arch/xtensa/platforms/iss/console.c | 7 +--
>  1 file changed, 1 insertion(+), 6 deletions(-)

Acked-by: Max Filippov 

-- 
Thanks.
-- Max


[PATCH] xtensa: move coprocessor_flush to the .text section

2021-02-26 Thread Max Filippov
coprocessor_flush is not a part of fast exception handlers, but it uses
parts of fast coprocessor handling code that's why it's in the same
source file. It uses call0 opcode to invoke those parts so there are no
limitations on their relative location, but the rest of the code calls
coprocessor_flush with call8 and that doesn't work when vectors are
placed in a different gigabyte-aligned area than the rest of the kernel.

Move coprocessor_flush from the .exception.text section to the .text so
that it's reachable from the rest of the kernel with call8.

Cc: sta...@vger.kernel.org
Signed-off-by: Max Filippov 
---
 arch/xtensa/kernel/coprocessor.S | 64 
 1 file changed, 33 insertions(+), 31 deletions(-)

diff --git a/arch/xtensa/kernel/coprocessor.S b/arch/xtensa/kernel/coprocessor.S
index c426b846beef..45cc0ae0af6f 100644
--- a/arch/xtensa/kernel/coprocessor.S
+++ b/arch/xtensa/kernel/coprocessor.S
@@ -99,37 +99,6 @@
LOAD_CP_REGS_TAB(6)
LOAD_CP_REGS_TAB(7)
 
-/*
- * coprocessor_flush(struct thread_info*, index)
- * a2a3
- *
- * Save coprocessor registers for coprocessor 'index'.
- * The register values are saved to or loaded from the coprocessor area 
- * inside the task_info structure.
- *
- * Note that this function doesn't update the coprocessor_owner information!
- *
- */
-
-ENTRY(coprocessor_flush)
-
-   /* reserve 4 bytes on stack to save a0 */
-   abi_entry(4)
-
-   s32ia0, a1, 0
-   movia0, .Lsave_cp_regs_jump_table
-   addx8   a3, a3, a0
-   l32ia4, a3, 4
-   l32ia3, a3, 0
-   add a2, a2, a4
-   beqza3, 1f
-   callx0  a3
-1: l32ia0, a1, 0
-
-   abi_ret(4)
-
-ENDPROC(coprocessor_flush)
-
 /*
  * Entry condition:
  *
@@ -245,6 +214,39 @@ ENTRY(fast_coprocessor)
 
 ENDPROC(fast_coprocessor)
 
+   .text
+
+/*
+ * coprocessor_flush(struct thread_info*, index)
+ * a2a3
+ *
+ * Save coprocessor registers for coprocessor 'index'.
+ * The register values are saved to or loaded from the coprocessor area
+ * inside the task_info structure.
+ *
+ * Note that this function doesn't update the coprocessor_owner information!
+ *
+ */
+
+ENTRY(coprocessor_flush)
+
+   /* reserve 4 bytes on stack to save a0 */
+   abi_entry(4)
+
+   s32ia0, a1, 0
+   movia0, .Lsave_cp_regs_jump_table
+   addx8   a3, a3, a0
+   l32ia4, a3, 4
+   l32ia3, a3, 0
+   add a2, a2, a4
+   beqza3, 1f
+   callx0  a3
+1: l32ia0, a1, 0
+
+   abi_ret(4)
+
+ENDPROC(coprocessor_flush)
+
.data
 
 ENTRY(coprocessor_owner)
-- 
2.20.1



Re: [PATCH] mm/memtest: Add ARCH_USE_MEMTEST

2021-02-04 Thread Max Filippov
On Thu, Feb 4, 2021 at 8:10 PM Anshuman Khandual
 wrote:
>
> early_memtest() does not get called from all architectures. Hence enabling
> CONFIG_MEMTEST and providing a valid memtest=[1..N] kernel command line
> option might not trigger the memory pattern tests as would be expected in
> normal circumstances. This situation is misleading.
>
> The change here prevents the above mentioned problem after introducing a
> new config option ARCH_USE_MEMTEST that should be subscribed on platforms
> that call early_memtest(), in order to enable the config CONFIG_MEMTEST.
> Conversely CONFIG_MEMTEST cannot be enabled on platforms where it would
> not be tested anyway.
>
> Cc: Russell King 
> Cc: Catalin Marinas 
> Cc: Will Deacon 
> Cc: Thomas Bogendoerfer 
> Cc: Michael Ellerman 
> Cc: Benjamin Herrenschmidt 
> Cc: Paul Mackerras 
> Cc: Thomas Gleixner 
> Cc: Ingo Molnar 
> Cc: Chris Zankel 
> Cc: Max Filippov 
> Cc: linux-arm-ker...@lists.infradead.org
> Cc: linux-m...@vger.kernel.org
> Cc: linuxppc-...@lists.ozlabs.org
> Cc: linux-xte...@linux-xtensa.org
> Cc: linux...@kvack.org
> Cc: linux-kernel@vger.kernel.org
> Signed-off-by: Anshuman Khandual 
> ---
> This patch applies on v5.11-rc6 and has been tested on arm64 platform. But
> it has been just build tested on all other platforms.
>
>  arch/arm/Kconfig | 1 +
>  arch/arm64/Kconfig   | 1 +
>  arch/mips/Kconfig| 1 +
>  arch/powerpc/Kconfig | 1 +
>  arch/x86/Kconfig | 1 +
>  arch/xtensa/Kconfig  | 1 +
>  lib/Kconfig.debug| 9 -
>  7 files changed, 14 insertions(+), 1 deletion(-)

Anshuman, entries in arch/*/Konfig files are sorted in alphabetical order,
please keep them that way.

Reviewed-by: Max Filippov 

-- 
Thanks.
-- Max


Re: Old platforms: bring out your dead

2021-01-14 Thread Max Filippov
Hi Arnd,

On Thu, Jan 14, 2021 at 1:25 PM Arnd Bergmann  wrote:
> | arch/mips/Kconfig:config HIGHMEM
> | arch/xtensa/Kconfig:config HIGHMEM
>
> AFAICT On MIPS (prior to MIPS32r3) and xtensa, you have at
> most 512MB in the linear map, so the VMSPLIT_2G or VMSPLIT_4G_4G
> tricks won't work.

Regarding xtensa this was done to minimize difference between
MMUv2 and MMUv3 virtual memory layouts. MMUv2 has been
obsoleted more than 10 years ago, and MMUv3 is much more
flexible and can do e.g. 4GB linear map. The only piece of xtensa
MMUv2 hardware that I have has 96MB of DRAM which fits into
its linear mapping. So maybe it's time to do a cleanup and
rearrange virtual memory layout to eliminate the need of highmem.

> I have no idea who uses xtensa systems with lots of memory on
> modern kernels.

We definitely use it for development internally at Cadence/Tensilica,
mainly on simulators, but also on FPGA boards (e.g. on KC705 we
can use all of the 1GB onboard DRAM).
In the last few years we've had a few support requests for linux on
xtensa cores with MMU, but AFAICT none of them had to deal with
more than 512MB of onboard memory.

-- 
Thanks.
-- Max


Re: [PATCH 16/18] arch: xtensa: Remove CONFIG_OPROFILE support

2021-01-14 Thread Max Filippov
On Thu, Jan 14, 2021 at 3:36 AM Viresh Kumar  wrote:
>
> The "oprofile" user-space tools don't use the kernel OPROFILE support
> any more, and haven't in a long time. User-space has been converted to
> the perf interfaces.
>
> Remove the old oprofile's architecture specific support.
>
> Suggested-by: Christoph Hellwig 
> Suggested-by: Linus Torvalds 
> Signed-off-by: Viresh Kumar 
> ---
>  arch/xtensa/Kconfig |  1 -
>  arch/xtensa/Makefile|  1 -
>  arch/xtensa/configs/audio_kc705_defconfig   |  1 -
>  arch/xtensa/configs/generic_kc705_defconfig |  1 -
>  arch/xtensa/configs/smp_lx200_defconfig |  1 -
>  arch/xtensa/configs/xip_kc705_defconfig |  1 -
>  arch/xtensa/kernel/stacktrace.c |  2 +-
>  arch/xtensa/oprofile/Makefile   | 10 
>  arch/xtensa/oprofile/backtrace.c| 27 -
>  arch/xtensa/oprofile/init.c | 26 
>  10 files changed, 1 insertion(+), 70 deletions(-)
>  delete mode 100644 arch/xtensa/oprofile/Makefile
>  delete mode 100644 arch/xtensa/oprofile/backtrace.c
>  delete mode 100644 arch/xtensa/oprofile/init.c

Although I still keep userspace oprofile tools that use this interface,
I haven't run them for ages.

Acked-by: Max Filippov 

-- 
Thanks.
-- Max


Re: [PATCH] xtensa: fix spelling mistake in Kconfig "wont" -> "won't"

2020-12-17 Thread Max Filippov
On Thu, Dec 17, 2020 at 9:24 AM Colin King  wrote:
>
> From: Colin Ian King 
>
> There is a spelling mistake in the Kconfig help text. Fix it.
>
> Signed-off-by: Colin Ian King 
> ---
>  arch/xtensa/Kconfig | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Thanks. Applied to my xtensa tree.

-- Max


[PULL 0/3] xtensa fixes for v5.10

2020-11-19 Thread Max Filippov
Hi Linus,

please pull the following fixes for the Xtensa architecture for v5.10.

The following changes since commit 09162bc32c880a791c6c0668ce0745cf7958f576:

  Linux 5.10-rc4 (2020-11-15 16:44:31 -0800)

are available in the Git repository at:

  git://github.com/jcmvbkbc/linux-xtensa.git tags/xtensa-20201119

for you to fetch changes up to dc293f2106903ab9c24e9cea18c276e32c394c33:

  xtensa: uaccess: Add missing __user to strncpy_from_user() prototype 
(2020-11-17 05:09:28 -0800)


Xtensa fixes for v5.10:

- fix placement of cache alias remapping area
- disable preemption around cache alias management calls
- add missing __user annotation to strncpy_from_user argument


Laurent Pinchart (1):
  xtensa: uaccess: Add missing __user to strncpy_from_user() prototype

Max Filippov (2):
  xtensa: fix TLBTEMP area placement
  xtensa: disable preemption around cache alias management calls

 Documentation/xtensa/mmu.rst  |  9 ++---
 arch/xtensa/include/asm/pgtable.h |  2 +-
 arch/xtensa/include/asm/uaccess.h |  2 +-
 arch/xtensa/mm/cache.c| 14 ++
 4 files changed, 22 insertions(+), 5 deletions(-)

-- 
Thanks.
-- Max


[PATCH v2] xtensa/mm/highmem: Make generic kmap_atomic() work correctly

2020-11-16 Thread Max Filippov
From: Thomas Gleixner 

The conversion to the generic kmap_atomic() implementation missed the fact
that xtensa's fixmap works bottom up while all other implementations work
top down. There is no real reason why xtensa needs to work that way.

Cure it by:

  - Using the generic fix_to_virt()/virt_to_fix() functions which work top
down
  - Adjusting the mapping defines
  - Using the generic index calculation for the non cache aliasing case
  - Making the cache colour offset reverse so the effective index is correct

While at it, remove the outdated and misleading comment above the fixmap
enum which originates from the initial copy of this code from i386.

Reported-by: Max Filippov 
Fixes: 629ed3f7dad2 ("xtensa/mm/highmem: Switch to generic kmap atomic")
Signed-off-by: Thomas Gleixner 
Signed-off-by: Max Filippov 
Tested-by: Max Filippov 
---
Changes v1->v2:
- fix off-by-one error in kmap_idx calculation

 arch/xtensa/include/asm/fixmap.h  | 55 ---
 arch/xtensa/include/asm/highmem.h | 15 +
 arch/xtensa/mm/highmem.c  | 18 ++
 arch/xtensa/mm/init.c |  4 +--
 arch/xtensa/mm/mmu.c  |  3 +-
 5 files changed, 31 insertions(+), 64 deletions(-)

diff --git a/arch/xtensa/include/asm/fixmap.h b/arch/xtensa/include/asm/fixmap.h
index 92049b61c351..1c65dc1d3397 100644
--- a/arch/xtensa/include/asm/fixmap.h
+++ b/arch/xtensa/include/asm/fixmap.h
@@ -17,63 +17,22 @@
 #include 
 #include 
 #include 
-#endif
 
-/*
- * Here we define all the compile-time 'special' virtual
- * addresses. The point is to have a constant address at
- * compile time, but to set the physical address only
- * in the boot process. We allocate these special addresses
- * from the start of the consistent memory region upwards.
- * Also this lets us do fail-safe vmalloc(), we
- * can guarantee that these special addresses and
- * vmalloc()-ed addresses never overlap.
- *
- * these 'compile-time allocated' memory buffers are
- * fixed-size 4k pages. (or larger if used with an increment
- * higher than 1) use fixmap_set(idx,phys) to associate
- * physical memory with fixmap indices.
- */
+/* The map slots for temporary mappings via kmap_atomic/local(). */
 enum fixed_addresses {
-#ifdef CONFIG_HIGHMEM
-   /* reserved pte's for temporary kernel mappings */
FIX_KMAP_BEGIN,
FIX_KMAP_END = FIX_KMAP_BEGIN +
(KM_MAX_IDX * NR_CPUS * DCACHE_N_COLORS) - 1,
-#endif
__end_of_fixed_addresses
 };
 
-#define FIXADDR_TOP (XCHAL_KSEG_CACHED_VADDR - PAGE_SIZE)
+#define FIXADDR_END (XCHAL_KSEG_CACHED_VADDR - PAGE_SIZE)
 #define FIXADDR_SIZE   (__end_of_fixed_addresses << PAGE_SHIFT)
-#define FIXADDR_START  ((FIXADDR_TOP - FIXADDR_SIZE) & PMD_MASK)
+/* Enforce that FIXADDR_START is PMD aligned to handle cache aliasing */
+#define FIXADDR_START  ((FIXADDR_END - FIXADDR_SIZE) & PMD_MASK)
+#define FIXADDR_TOP(FIXADDR_START + FIXADDR_SIZE - PAGE_SIZE)
 
-#define __fix_to_virt(x)   (FIXADDR_START + ((x) << PAGE_SHIFT))
-#define __virt_to_fix(x)   (((x) - FIXADDR_START) >> PAGE_SHIFT)
-
-#ifndef __ASSEMBLY__
-/*
- * 'index to address' translation. If anyone tries to use the idx
- * directly without translation, we catch the bug with a NULL-deference
- * kernel oops. Illegal ranges of incoming indices are caught too.
- */
-static __always_inline unsigned long fix_to_virt(const unsigned int idx)
-{
-   /* Check if this memory layout is broken because fixmap overlaps page
-* table.
-*/
-   BUILD_BUG_ON(FIXADDR_START <
-TLBTEMP_BASE_1 + TLBTEMP_SIZE);
-   BUILD_BUG_ON(idx >= __end_of_fixed_addresses);
-   return __fix_to_virt(idx);
-}
-
-static inline unsigned long virt_to_fix(const unsigned long vaddr)
-{
-   BUG_ON(vaddr >= FIXADDR_TOP || vaddr < FIXADDR_START);
-   return __virt_to_fix(vaddr);
-}
-
-#endif
+#include 
 
+#endif /* CONFIG_HIGHMEM */
 #endif
diff --git a/arch/xtensa/include/asm/highmem.h 
b/arch/xtensa/include/asm/highmem.h
index 0fc3b1cebc56..34b8b620e7f1 100644
--- a/arch/xtensa/include/asm/highmem.h
+++ b/arch/xtensa/include/asm/highmem.h
@@ -12,6 +12,7 @@
 #ifndef _XTENSA_HIGHMEM_H
 #define _XTENSA_HIGHMEM_H
 
+#ifdef CONFIG_HIGHMEM
 #include 
 #include 
 #include 
@@ -58,6 +59,13 @@ static inline wait_queue_head_t 
*get_pkmap_wait_queue_head(unsigned int color)
 {
return pkmap_map_wait_arr + color;
 }
+
+enum fixed_addresses kmap_local_map_idx(int type, unsigned long pfn);
+#define arch_kmap_local_map_idxkmap_local_map_idx
+
+enum fixed_addresses kmap_local_unmap_idx(int type, unsigned long addr);
+#define arch_kmap_local_unmap_idx  kmap_local_unmap_idx
+
 #endif
 
 extern pte_t *pkmap_page_table;
@@ -67,15 +75,10 @@ static inline void flush_cache_kmaps(void)
flush_cache_all();
 }
 
-enum fixed_addresses kmap_local_map_idx(int type, unsigned long pfn);
-#define arch_k

Re: [PATCH] highmem: fix highmem for xtensa

2020-11-16 Thread Max Filippov
Hi Thomas,

On Fri, Nov 13, 2020 at 2:34 PM Thomas Gleixner  wrote:
> I agree that the top down mechanics is not really intuitive, but that
> does not justify the ifdeffery in the generic code.

But then maybe xtensa did the right thing where everyone else just
copied the not really intuitive implementation? If nobody else cares
then maybe generic fix_to_virt/virt_to_fix can be changed for positive
indexing?

> xtensa can just use the generic fix_to_virt/virt_to_fix mechanics. All
> it needs is to adjust the mapping defines and to adjust the color offset
> to
>
> NR_COLORS - color
>
> which is not an unreasonable ask. As a side effect all highmem inflicted
> systems which do not have the cache aliasing problem can just use the
> generic code as is. See untested patch below.

Thanks. I'll test this patch and post the result.
But still this change doesn't look like a step in the right direction to me:
I can't find the reason why fixmap must be indexed backwards.

> It builds for some configs, but the smp_lx200_defconfig (which has the
> aliasing) it fails to build even without this patch (highmem.o at least
> builds).
>
> Toolchain is the one from https://mirrors.edge.kernel.org/pub/tools/crosstool/

xtensa toolchain must match the selected CPU core. For smp_lx200_defconfig
the toolchain is available here:
https://github.com/foss-xtensa/toolchain/releases/download/2020.07/x86_64-2020.07-xtensa-test_mmuhifi_c3-elf.tar.gz

-- 
Thanks.
-- Max


[PATCH 2/2] xtensa: disable preemption around cache alias management calls

2020-11-16 Thread Max Filippov
Although cache alias management calls set up and tear down TLB entries
and fast_second_level_miss is able to restore TLB entry should it be
evicted they absolutely cannot preempt each other because they use the
same TLBTEMP area for different purposes.
Disable preemption around all cache alias management calls to enforce
that.

Cc: sta...@vger.kernel.org
Signed-off-by: Max Filippov 
---
 arch/xtensa/mm/cache.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/arch/xtensa/mm/cache.c b/arch/xtensa/mm/cache.c
index 5835406b3cec..085b8c77b9d9 100644
--- a/arch/xtensa/mm/cache.c
+++ b/arch/xtensa/mm/cache.c
@@ -70,8 +70,10 @@ static inline void kmap_invalidate_coherent(struct page 
*page,
kvaddr = TLBTEMP_BASE_1 +
(page_to_phys(page) & DCACHE_ALIAS_MASK);
 
+   preempt_disable();
__invalidate_dcache_page_alias(kvaddr,
   page_to_phys(page));
+   preempt_enable();
}
}
 }
@@ -156,6 +158,7 @@ void flush_dcache_page(struct page *page)
if (!alias && !mapping)
return;
 
+   preempt_disable();
virt = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
__flush_invalidate_dcache_page_alias(virt, phys);
 
@@ -166,6 +169,7 @@ void flush_dcache_page(struct page *page)
 
if (mapping)
__invalidate_icache_page_alias(virt, phys);
+   preempt_enable();
}
 
/* There shouldn't be an entry in the cache for this page anymore. */
@@ -199,8 +203,10 @@ void local_flush_cache_page(struct vm_area_struct *vma, 
unsigned long address,
unsigned long phys = page_to_phys(pfn_to_page(pfn));
unsigned long virt = TLBTEMP_BASE_1 + (address & DCACHE_ALIAS_MASK);
 
+   preempt_disable();
__flush_invalidate_dcache_page_alias(virt, phys);
__invalidate_icache_page_alias(virt, phys);
+   preempt_enable();
 }
 EXPORT_SYMBOL(local_flush_cache_page);
 
@@ -227,11 +233,13 @@ update_mmu_cache(struct vm_area_struct * vma, unsigned 
long addr, pte_t *ptep)
unsigned long phys = page_to_phys(page);
unsigned long tmp;
 
+   preempt_disable();
tmp = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
__flush_invalidate_dcache_page_alias(tmp, phys);
tmp = TLBTEMP_BASE_1 + (addr & DCACHE_ALIAS_MASK);
__flush_invalidate_dcache_page_alias(tmp, phys);
__invalidate_icache_page_alias(tmp, phys);
+   preempt_enable();
 
clear_bit(PG_arch_1, >flags);
}
@@ -265,7 +273,9 @@ void copy_to_user_page(struct vm_area_struct *vma, struct 
page *page,
 
if (alias) {
unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
+   preempt_disable();
__flush_invalidate_dcache_page_alias(t, phys);
+   preempt_enable();
}
 
/* Copy data */
@@ -280,9 +290,11 @@ void copy_to_user_page(struct vm_area_struct *vma, struct 
page *page,
if (alias) {
unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
 
+   preempt_disable();
__flush_invalidate_dcache_range((unsigned long) dst, len);
if ((vma->vm_flags & VM_EXEC) != 0)
__invalidate_icache_page_alias(t, phys);
+   preempt_enable();
 
} else if ((vma->vm_flags & VM_EXEC) != 0) {
__flush_dcache_range((unsigned long)dst,len);
@@ -304,7 +316,9 @@ extern void copy_from_user_page(struct vm_area_struct *vma, 
struct page *page,
 
if (alias) {
unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
+   preempt_disable();
__flush_invalidate_dcache_page_alias(t, phys);
+   preempt_enable();
}
 
memcpy(dst, src, len);
-- 
2.20.1



[PATCH 0/2] xtensa: cache aliasing fixes

2020-11-16 Thread Max Filippov
Hello,

this series changes TLBTEMP region placement and disables preemption
around all cache alias management calls. This fixes TLB miss and
multihit issues in the TLBTEMP area seen on cores with aliasing cache.

Max Filippov (2):
  xtensa: fix TLBTEMP area placement
  xtensa: disable preemption around cache alias management calls

 Documentation/xtensa/mmu.rst  |  9 ++---
 arch/xtensa/include/asm/pgtable.h |  2 +-
 arch/xtensa/mm/cache.c| 14 ++
 3 files changed, 21 insertions(+), 4 deletions(-)

-- 
2.20.1



[PATCH 1/2] xtensa: fix TLBTEMP area placement

2020-11-16 Thread Max Filippov
fast_second_level_miss handler for the TLBTEMP area has an assumption
that page table directory entry for the TLBTEMP address range is 0. For
it to be true the TLBTEMP area must be aligned to 4MB boundary and not
share its 4MB region with anything that may use a page table. This is
not true currently: TLBTEMP shares space with vmalloc space which
results in the following kinds of runtime errors when
fast_second_level_miss loads page table directory entry for the vmalloc
space instead of fixing up the TLBTEMP area:

 Unable to handle kernel paging request at virtual address c7ff0e00
  pc = d0009275, ra = 90009478
 Oops: sig: 9 [#1] PREEMPT
 CPU: 1 PID: 61 Comm: kworker/u9:2 Not tainted 
5.10.0-rc3-next-20201110-7-g1fe4962fa983-dirty #58
 Workqueue: xprtiod xs_stream_data_receive_workfn
 a00: 90009478 d11e1dc0 c7ff0e00 0020 c7ff 0001 7f8b8107 
 a08: 900c5992 d11e1d90 d0cc88b8 5506e97c  5506e97c d06c8074 d11e1d90
 pc: d0009275, ps: 00060310, depc: 0014, excvaddr: c7ff0e00
 lbeg: d0009275, lend: d0009287 lcount: 0003, sar: 0010
 Call Trace:
   xs_stream_data_receive_workfn+0x43c/0x770
   process_one_work+0x1a1/0x324
   worker_thread+0x1cc/0x3c0
   kthread+0x10d/0x124
   ret_from_kernel_thread+0xc/0x18

Cc: sta...@vger.kernel.org
Signed-off-by: Max Filippov 
---
 Documentation/xtensa/mmu.rst  | 9 ++---
 arch/xtensa/include/asm/pgtable.h | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/Documentation/xtensa/mmu.rst b/Documentation/xtensa/mmu.rst
index e52a12960fdc..450573afa31a 100644
--- a/Documentation/xtensa/mmu.rst
+++ b/Documentation/xtensa/mmu.rst
@@ -82,7 +82,8 @@ Default MMUv2-compatible layout::
   +--+
   | VMALLOC area |  VMALLOC_START0xc000  128MB - 64KB
   +--+  VMALLOC_END
-  | Cache aliasing   |  TLBTEMP_BASE_1   0xc7ff  DCACHE_WAY_SIZE
+  +--+
+  | Cache aliasing   |  TLBTEMP_BASE_1   0xc800  DCACHE_WAY_SIZE
   | remap area 1 |
   +--+
   | Cache aliasing   |  TLBTEMP_BASE_2   DCACHE_WAY_SIZE
@@ -124,7 +125,8 @@ Default MMUv2-compatible layout::
   +--+
   | VMALLOC area |  VMALLOC_START0xa000  128MB - 64KB
   +--+  VMALLOC_END
-  | Cache aliasing   |  TLBTEMP_BASE_1   0xa7ff  DCACHE_WAY_SIZE
+  +--+
+  | Cache aliasing   |  TLBTEMP_BASE_1   0xa800  DCACHE_WAY_SIZE
   | remap area 1 |
   +--+
   | Cache aliasing   |  TLBTEMP_BASE_2   DCACHE_WAY_SIZE
@@ -167,7 +169,8 @@ Default MMUv2-compatible layout::
   +--+
   | VMALLOC area |  VMALLOC_START0x9000  128MB - 64KB
   +--+  VMALLOC_END
-  | Cache aliasing   |  TLBTEMP_BASE_1   0x97ff  DCACHE_WAY_SIZE
+  +--+
+  | Cache aliasing   |  TLBTEMP_BASE_1   0x9800  DCACHE_WAY_SIZE
   | remap area 1 |
   +--+
   | Cache aliasing   |  TLBTEMP_BASE_2   DCACHE_WAY_SIZE
diff --git a/arch/xtensa/include/asm/pgtable.h 
b/arch/xtensa/include/asm/pgtable.h
index fa054a1772e1..4dc04e6c01d7 100644
--- a/arch/xtensa/include/asm/pgtable.h
+++ b/arch/xtensa/include/asm/pgtable.h
@@ -69,7 +69,7 @@
  */
 #define VMALLOC_START  (XCHAL_KSEG_CACHED_VADDR - 0x1000)
 #define VMALLOC_END(VMALLOC_START + 0x07FE)
-#define TLBTEMP_BASE_1 (VMALLOC_END + 1)
+#define TLBTEMP_BASE_1 (VMALLOC_START + 0x0800)
 #define TLBTEMP_BASE_2 (TLBTEMP_BASE_1 + DCACHE_WAY_SIZE)
 #if 2 * DCACHE_WAY_SIZE > ICACHE_WAY_SIZE
 #define TLBTEMP_SIZE   (2 * DCACHE_WAY_SIZE)
-- 
2.20.1



Re: [PATCH] highmem: fix highmem for xtensa

2020-11-13 Thread Max Filippov
On Fri, Nov 13, 2020 at 6:36 AM Thomas Gleixner  wrote:
> On Fri, Nov 13 2020 at 05:50, Max Filippov wrote:
> > On Fri, Nov 13, 2020 at 5:40 AM Thomas Gleixner  wrote:
> >> What's wrong with just doing the obvious and making the fixmap defines
> >> the other way round?
> >
> > It becomes really awkward when we get to support high memory with
> > aliasing data cache: we must think about the actual virtual addresses
> > assigned to pages and it feels much simpler when it's done this way.
>
> Feeling are not really a technical argument. Is there any functional
> difference which matters?

arch_kmap_local_map_idx must produce index based on type and
pfn that will be translated to virtual address with the same color this
page would've had if it was in the low memory. With positive fixmap
the formula is: (type * (number of cache colors)) + (color of the pfn).
With negative fixmap there must be additional +1 and -1 in it.

-- 
Thanks.
-- Max


Re: [PATCH] highmem: fix highmem for xtensa

2020-11-13 Thread Max Filippov
Hi Thomas,

On Fri, Nov 13, 2020 at 5:40 AM Thomas Gleixner  wrote:
> On Fri, Nov 13 2020 at 04:23, Max Filippov wrote:
> > Fixmap on xtensa grows upwards, i.e. bigger fixmap entry index
> > corresponds to a higher virtual address. This was lost in highmem
> > generalization resulting in the following runtime warnings:
>
> Sorry for not noticing.
>
> > Fix it by adding __ARCH_HAS_POSITIVE_FIXMAP macro and implementing
> > vaddr_in_fixmap and fixmap_pte primitives differently depending on
> > whether it is defined or not.
>
> What's wrong with just doing the obvious and making the fixmap defines
> the other way round?

It becomes really awkward when we get to support high memory with
aliasing data cache: we must think about the actual virtual addresses
assigned to pages and it feels much simpler when it's done this way.

-- 
Thanks.
-- Max


[PATCH] highmem: fix highmem for xtensa

2020-11-13 Thread Max Filippov
Fixmap on xtensa grows upwards, i.e. bigger fixmap entry index
corresponds to a higher virtual address. This was lost in highmem
generalization resulting in the following runtime warnings:

 WARNING: CPU: 0 PID: 18 at mm/highmem.c:494 kunmap_local_indexed+0x45/0x54
 Modules linked in:
 CPU: 0 PID: 18 Comm: kworker/u2:0 Not tainted 5.10.0-rc3-next-20201113 #1
 Call Trace:
   __warn+0x8f/0xc8
   warn_slowpath_fmt+0x35/0x70
   kunmap_local_indexed+0x45/0x54
   handle_mm_fault+0x325/0xbe0
   __get_user_pages.part.61+0x131/0x22c
   __get_user_pages+0x44/0x60
   __get_user_pages_remote+0xe8/0x290
   get_user_pages_remote+0x24/0x40
   get_arg_page+0x50/0x78
   copy_string_kernel+0x5c/0x120
   kernel_execve+0x76/0xc8
   call_usermodehelper_exec_async+0xc8/0x10c
   ret_from_kernel_thread+0xc/0x18

Fix it by adding __ARCH_HAS_POSITIVE_FIXMAP macro and implementing
vaddr_in_fixmap and fixmap_pte primitives differently depending on
whether it is defined or not.

Cc: Thomas Gleixner 
Fixes: 629ed3f7dad2 ("xtensa/mm/highmem: Switch to generic kmap atomic")
Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/fixmap.h |  2 ++
 mm/highmem.c | 29 -
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/arch/xtensa/include/asm/fixmap.h b/arch/xtensa/include/asm/fixmap.h
index 92049b61c351..66787b5f13d6 100644
--- a/arch/xtensa/include/asm/fixmap.h
+++ b/arch/xtensa/include/asm/fixmap.h
@@ -51,6 +51,8 @@ enum fixed_addresses {
 #define __fix_to_virt(x)   (FIXADDR_START + ((x) << PAGE_SHIFT))
 #define __virt_to_fix(x)   (((x) - FIXADDR_START) >> PAGE_SHIFT)
 
+#define __ARCH_HAS_POSITIVE_FIXMAP
+
 #ifndef __ASSEMBLY__
 /*
  * 'index to address' translation. If anyone tries to use the idx
diff --git a/mm/highmem.c b/mm/highmem.c
index 54bd233846c9..af27ed8d6a97 100644
--- a/mm/highmem.c
+++ b/mm/highmem.c
@@ -434,6 +434,26 @@ static inline void kmap_high_unmap_local(unsigned long 
vaddr)
 #endif
 }
 
+static inline bool vaddr_in_fixmap(unsigned long addr)
+{
+#ifdef __ARCH_HAS_POSITIVE_FIXMAP
+   return addr <= __fix_to_virt(FIX_KMAP_END) &&
+   addr >= __fix_to_virt(FIX_KMAP_BEGIN);
+#else
+   return addr >= __fix_to_virt(FIX_KMAP_END) &&
+   addr <= __fix_to_virt(FIX_KMAP_BEGIN);
+#endif
+}
+
+static pte_t *fixmap_pte(pte_t *kmap_pte, int idx)
+{
+#ifdef __ARCH_HAS_POSITIVE_FIXMAP
+   return kmap_pte + idx;
+#else
+   return kmap_pte - idx;
+#endif
+}
+
 static inline int kmap_local_calc_idx(int idx)
 {
return idx + KM_MAX_IDX * smp_processor_id();
@@ -457,9 +477,9 @@ void *__kmap_local_pfn_prot(unsigned long pfn, pgprot_t 
prot)
preempt_disable();
idx = arch_kmap_local_map_idx(kmap_local_idx_push(), pfn);
vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
-   BUG_ON(!pte_none(*(kmap_pte - idx)));
+   BUG_ON(!pte_none(*(fixmap_pte(kmap_pte, idx;
pteval = pfn_pte(pfn, prot);
-   set_pte_at(_mm, vaddr, kmap_pte - idx, pteval);
+   set_pte_at(_mm, vaddr, fixmap_pte(kmap_pte, idx), pteval);
arch_kmap_local_post_map(vaddr, pteval);
preempt_enable();
 
@@ -489,8 +509,7 @@ void kunmap_local_indexed(void *vaddr)
pte_t *kmap_pte = kmap_get_pte();
int idx;
 
-   if (addr < __fix_to_virt(FIX_KMAP_END) ||
-   addr > __fix_to_virt(FIX_KMAP_BEGIN)) {
+   if (!vaddr_in_fixmap(addr)) {
WARN_ON_ONCE(addr < PAGE_OFFSET);
 
/* Handle mappings which were obtained by kmap_high_get() */
@@ -503,7 +522,7 @@ void kunmap_local_indexed(void *vaddr)
WARN_ON_ONCE(addr != __fix_to_virt(FIX_KMAP_BEGIN + idx));
 
arch_kmap_local_pre_unmap(addr);
-   pte_clear(_mm, addr, kmap_pte - idx);
+   pte_clear(_mm, addr, fixmap_pte(kmap_pte, idx));
arch_kmap_local_post_unmap(addr);
kmap_local_idx_pop();
preempt_enable();
-- 
2.20.1



Re: [PATCH v2] xtensa: fix broken TIF_NOTIFY_SIGNAL assembly

2020-11-12 Thread Max Filippov
On Wed, Nov 11, 2020 at 1:05 PM Jens Axboe  wrote:
>
> On 11/11/20 1:53 PM, Max Filippov wrote:
> > TIF_NOTIFY_SIGNAL handling in xtensa assembly is implemented
> > incorrectly: there should be a call to do_notify_resume when either
> > TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_NOTIFY_SIGNAL bit is set in the
> > thread_info::flags. The straightforward way to do it would be
> >
> > _bbsi.l a4, TIF_NEED_RESCHED, 3f
> > _bbsi.l a4, TIF_NOTIFY_RESUME, 2f
> > _bbsi.l a4, TIF_NOTIFY_SIGNAL, 2f
> > _bbci.l a4, TIF_SIGPENDING, 5f
> >
> > Optimize it a little bit and use bit mask and bnone opcode to skip
> > do_notify_resume invocation. Shuffle _TIF_* flags a bit so that used bit
> > mask fits into the immediate field of movi opcode.
>
> Thanks - do you mind if I fold this in with a reference to your
> changes? Seems like that'd be a better option than leaving it
> broken for a bit.

Sure Jens, by all means!

-- 
Thanks.
-- Max


[PATCH v2] xtensa: fix broken TIF_NOTIFY_SIGNAL assembly

2020-11-11 Thread Max Filippov
TIF_NOTIFY_SIGNAL handling in xtensa assembly is implemented
incorrectly: there should be a call to do_notify_resume when either
TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_NOTIFY_SIGNAL bit is set in the
thread_info::flags. The straightforward way to do it would be

_bbsi.l a4, TIF_NEED_RESCHED, 3f
_bbsi.l a4, TIF_NOTIFY_RESUME, 2f
_bbsi.l a4, TIF_NOTIFY_SIGNAL, 2f
_bbci.l a4, TIF_SIGPENDING, 5f

Optimize it a little bit and use bit mask and bnone opcode to skip
do_notify_resume invocation. Shuffle _TIF_* flags a bit so that used bit
mask fits into the immediate field of movi opcode.

Fixes: 4c6a9dcd4d13 ("xtensa: add support for TIF_NOTIFY_SIGNAL")
Cc: Jens Axboe 
Signed-off-by: Max Filippov 
---
Changes v1->v2:
- use more canonical order of bnone arguments: tested value first,
  bit mask second.

 arch/xtensa/include/asm/thread_info.h | 7 ---
 arch/xtensa/kernel/entry.S| 5 ++---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/arch/xtensa/include/asm/thread_info.h 
b/arch/xtensa/include/asm/thread_info.h
index 6ea521b8e2ec..a312333a9add 100644
--- a/arch/xtensa/include/asm/thread_info.h
+++ b/arch/xtensa/include/asm/thread_info.h
@@ -111,22 +111,23 @@ static inline struct thread_info 
*current_thread_info(void)
 #define TIF_NEED_RESCHED   2   /* rescheduling necessary */
 #define TIF_SINGLESTEP 3   /* restore singlestep on return to user 
mode */
 #define TIF_SYSCALL_TRACEPOINT 4   /* syscall tracepoint instrumentation */
-#define TIF_MEMDIE 5   /* is terminating due to OOM killer */
+#define TIF_NOTIFY_SIGNAL  5   /* signal notifications exist */
 #define TIF_RESTORE_SIGMASK6   /* restore signal mask in do_signal() */
 #define TIF_NOTIFY_RESUME  7   /* callback before returning to user */
 #define TIF_DB_DISABLED8   /* debug trap disabled for 
syscall */
 #define TIF_SYSCALL_AUDIT  9   /* syscall auditing active */
 #define TIF_SECCOMP10  /* secure computing */
-#define TIF_NOTIFY_SIGNAL  11  /* signal notifications exist */
+#define TIF_MEMDIE 11  /* is terminating due to OOM killer */
 
 #define _TIF_SYSCALL_TRACE (1<

[PATCH] xtensa: fix broken TIF_NOTIFY_SIGNAL assembly

2020-11-11 Thread Max Filippov
TIF_NOTIFY_SIGNAL handling in xtensa assembly is implemented
incorrectly: there should be a call to do_notify_resume when either
TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_NOTIFY_SIGNAL bit is set in the
thread_info::flags. The straightforward way to do it would be

_bbsi.l a4, TIF_NEED_RESCHED, 3f
_bbsi.l a4, TIF_NOTIFY_RESUME, 2f
_bbsi.l a4, TIF_NOTIFY_SIGNAL, 2f
_bbci.l a4, TIF_SIGPENDING, 5f

Optimize it a little bit and use bit mask and bnone opcode to skip
do_notify_resume invocation. Shuffle _TIF_* flags a bit so that used bit
mask fits into the immediate field of movi opcode.

Fixes: 4c6a9dcd4d13 ("xtensa: add support for TIF_NOTIFY_SIGNAL")
Cc: Jens Axboe 
Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/thread_info.h | 7 ---
 arch/xtensa/kernel/entry.S| 5 ++---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/arch/xtensa/include/asm/thread_info.h 
b/arch/xtensa/include/asm/thread_info.h
index 6ea521b8e2ec..a312333a9add 100644
--- a/arch/xtensa/include/asm/thread_info.h
+++ b/arch/xtensa/include/asm/thread_info.h
@@ -111,22 +111,23 @@ static inline struct thread_info 
*current_thread_info(void)
 #define TIF_NEED_RESCHED   2   /* rescheduling necessary */
 #define TIF_SINGLESTEP 3   /* restore singlestep on return to user 
mode */
 #define TIF_SYSCALL_TRACEPOINT 4   /* syscall tracepoint instrumentation */
-#define TIF_MEMDIE 5   /* is terminating due to OOM killer */
+#define TIF_NOTIFY_SIGNAL  5   /* signal notifications exist */
 #define TIF_RESTORE_SIGMASK6   /* restore signal mask in do_signal() */
 #define TIF_NOTIFY_RESUME  7   /* callback before returning to user */
 #define TIF_DB_DISABLED8   /* debug trap disabled for 
syscall */
 #define TIF_SYSCALL_AUDIT  9   /* syscall auditing active */
 #define TIF_SECCOMP10  /* secure computing */
-#define TIF_NOTIFY_SIGNAL  11  /* signal notifications exist */
+#define TIF_MEMDIE 11  /* is terminating due to OOM killer */
 
 #define _TIF_SYSCALL_TRACE (1<

Re: [PATCH] ARM, xtensa: highmem: avoid clobbering non-page aligned memory reservations

2020-10-31 Thread Max Filippov
On Sat, Oct 31, 2020 at 10:16 AM Mike Rapoport  wrote:
>
> On Sat, Oct 31, 2020 at 09:37:09AM -0700, Max Filippov wrote:
> > On Sat, Oct 31, 2020 at 2:43 AM Mike Rapoport  wrote:
> > > Please let me know how do you prefer to take it upstream.
> > > If needed this can go via memblock tree.
> >
> > Going through the memblock tree sounds right to me.
>
> Can I treat this as Ack?

Sure, for the xtensa part:
Acked-by: Max Filippov 

-- 
Thanks.
-- Max


Re: [PATCH] ARM, xtensa: highmem: avoid clobbering non-page aligned memory reservations

2020-10-31 Thread Max Filippov
On Sat, Oct 31, 2020 at 2:43 AM Mike Rapoport  wrote:
> Please let me know how do you prefer to take it upstream.
> If needed this can go via memblock tree.

Going through the memblock tree sounds right to me.

-- 
Thanks.
-- Max


Re: [PATCH] selftests/harness: Flush stdout before forking

2020-09-16 Thread Max Filippov
On Wed, Sep 16, 2020 at 9:16 PM Michael Ellerman  wrote:
>
> The test harness forks() a child to run each test. Both the parent and
> the child print to stdout using libc functions. That can lead to
> duplicated (or more) output if the libc buffers are not flushed before
> forking.
>
> It's generally not seen when running programs directly, because stdout
> will usually be line buffered when it's pointing to a terminal.
>
> This was noticed when running the seccomp_bpf test, eg:
>
>   $ ./seccomp_bpf | tee test.log
>   $ grep -c "TAP version 13" test.log
>   2
>
> But we only expect the TAP header to appear once.
>
> It can be exacerbated using stdbuf to increase the buffer size:
>
>   $ stdbuf -o 1MB ./seccomp_bpf > test.log
>   $ grep -c "TAP version 13" test.log
>   13
>
> The fix is simple, we just flush stdout & stderr before fork. Usually
> stderr is unbuffered, but that can be changed, so flush it as well
> just to be safe.
>
> Signed-off-by: Michael Ellerman 
> ---
>  tools/testing/selftests/kselftest_harness.h | 5 +
>  1 file changed, 5 insertions(+)

Tested-by: Max Filippov 

-- 
Thanks.
-- Max


Re: [PATCH 00/15] selftests/seccomp: Refactor change_syscall()

2020-09-15 Thread Max Filippov
On Mon, Sep 14, 2020 at 1:32 PM Kees Cook  wrote:
> On Mon, Sep 14, 2020 at 10:15:18PM +1000, Michael Ellerman wrote:
> > Kees Cook  writes:
> However...
>
> >
> > cheers
> >
> >
> > ./seccomp_bpf
> > TAP version 13
> > 1..86
> > # Starting 86 tests from 7 test cases.
> > #  RUN   global.kcmp ...
> > #OK  global.kcmp
> > ok 1 global.kcmp
> > [...]
> > #  RUN   global.KILL_thread ...
> > TAP version 13
> > 1..86
> > # Starting 86 tests from 7 test cases.
>
> Was this a mis-paste, or has something very very bad happened here in
> global.KILL_one_arg_six finishes?

I observe similar output corruption on xtensa when I redirect test output
into a file or pipe it to 'cat'. When it goes to the terminal it looks normal.

-- 
Thanks.
-- Max


Re: [PATCH 00/15] selftests/seccomp: Refactor change_syscall()

2020-09-15 Thread Max Filippov
Hello,

On Sat, Sep 12, 2020 at 4:08 AM Kees Cook  wrote:
> This refactors the seccomp selftest macros used in change_syscall(),
> in an effort to remove special cases for mips, arm, arm64, and xtensa,
> which paves the way for powerpc fixes.
>
> I'm not entirely done testing, but all-arch build tests and x86_64
> selftests pass. I'll be doing arm, arm64, and i386 selftests shortly,
> but I currently don't have an easy way to check xtensa, mips, nor
> powerpc. Any help there would be appreciated!

I've built and tested this series on xtensa. I had to disable two tests:
user_notification_addfd and user_notification_addfd_rlimit because
they use memfd_create and prlimit which are not available in uClibc.
With this change I've got all 86 tests passing with the following log:

./seccomp_bpf
TAP version 13
1..86
# Starting 86 tests from 7 test cases.
#  RUN   TRAP.dfl ...
#OK  TRAP.dfl
ok 1 TRAP.dfl
#  RUN   TRAP.ign ...
#OK  TRAP.ign
ok 2 TRAP.ign
#  RUN   TRAP.handler ...
#OK  TRAP.handler
ok 3 TRAP.handler
#  RUN   precedence.allow_ok ...
#OK  precedence.allow_ok
ok 4 precedence.allow_ok
#  RUN   precedence.kill_is_highest ...
#OK  precedence.kill_is_highest
ok 5 precedence.kill_is_highest
#  RUN   precedence.kill_is_highest_in_any_order ...
#OK  precedence.kill_is_highest_in_any_order
ok 6 precedence.kill_is_highest_in_any_order
#  RUN   precedence.trap_is_second ...
#OK  precedence.trap_is_second
ok 7 precedence.trap_is_second
#  RUN   precedence.trap_is_second_in_any_order ...
#OK  precedence.trap_is_second_in_any_order
ok 8 precedence.trap_is_second_in_any_order
#  RUN   precedence.errno_is_third ...
#OK  precedence.errno_is_third
ok 9 precedence.errno_is_third
#  RUN   precedence.errno_is_third_in_any_order ...
#OK  precedence.errno_is_third_in_any_order
ok 10 precedence.errno_is_third_in_any_order
#  RUN   precedence.trace_is_fourth ...
#OK  precedence.trace_is_fourth
ok 11 precedence.trace_is_fourth
#  RUN   precedence.trace_is_fourth_in_any_order ...
#OK  precedence.trace_is_fourth_in_any_order
ok 12 precedence.trace_is_fourth_in_any_order
#  RUN   precedence.log_is_fifth ...
#OK  precedence.log_is_fifth
ok 13 precedence.log_is_fifth
#  RUN   precedence.log_is_fifth_in_any_order ...
#OK  precedence.log_is_fifth_in_any_order
ok 14 precedence.log_is_fifth_in_any_order
#  RUN   TRACE_poke.read_has_side_effects ...
#OK  TRACE_poke.read_has_side_effects
ok 15 TRACE_poke.read_has_side_effects
#  RUN   TRACE_poke.getpid_runs_normally ...
#OK  TRACE_poke.getpid_runs_normally
ok 16 TRACE_poke.getpid_runs_normally
#  RUN   TRACE_syscall.ptrace.negative_ENOSYS ...
#OK  TRACE_syscall.ptrace.negative_ENOSYS
ok 17 TRACE_syscall.ptrace.negative_ENOSYS
#  RUN   TRACE_syscall.ptrace.syscall_allowed ...
#OK  TRACE_syscall.ptrace.syscall_allowed
ok 18 TRACE_syscall.ptrace.syscall_allowed
#  RUN   TRACE_syscall.ptrace.syscall_redirected ...
#OK  TRACE_syscall.ptrace.syscall_redirected
ok 19 TRACE_syscall.ptrace.syscall_redirected
#  RUN   TRACE_syscall.ptrace.syscall_errno ...
#OK  TRACE_syscall.ptrace.syscall_errno
ok 20 TRACE_syscall.ptrace.syscall_errno
#  RUN   TRACE_syscall.ptrace.syscall_faked ...
#OK  TRACE_syscall.ptrace.syscall_faked
ok 21 TRACE_syscall.ptrace.syscall_faked
#  RUN   TRACE_syscall.ptrace.skip_after ...
#OK  TRACE_syscall.ptrace.skip_after
ok 22 TRACE_syscall.ptrace.skip_after
#  RUN   TRACE_syscall.ptrace.kill_after ...
#OK  TRACE_syscall.ptrace.kill_after
ok 23 TRACE_syscall.ptrace.kill_after
#  RUN   TRACE_syscall.seccomp.negative_ENOSYS ...
#OK  TRACE_syscall.seccomp.negative_ENOSYS
ok 24 TRACE_syscall.seccomp.negative_ENOSYS
#  RUN   TRACE_syscall.seccomp.syscall_allowed ...
#OK  TRACE_syscall.seccomp.syscall_allowed
ok 25 TRACE_syscall.seccomp.syscall_allowed
#  RUN   TRACE_syscall.seccomp.syscall_redirected ...
#OK  TRACE_syscall.seccomp.syscall_redirected
ok 26 TRACE_syscall.seccomp.syscall_redirected
#  RUN   TRACE_syscall.seccomp.syscall_errno ...
#OK  TRACE_syscall.seccomp.syscall_errno
ok 27 TRACE_syscall.seccomp.syscall_errno
#  RUN   TRACE_syscall.seccomp.syscall_faked ...
#OK  TRACE_syscall.seccomp.syscall_faked
ok 28 TRACE_syscall.seccomp.syscall_faked
#  RUN   TRACE_syscall.seccomp.skip_after ...
#OK  TRACE_syscall.seccomp.skip_after
ok 29 TRACE_syscall.seccomp.skip_after
#  RUN   TRACE_syscall.seccomp.kill_after ...
#OK  TRACE_syscall.seccomp.kill_after
ok 30 

Re: [PATCH 0/3] xtensa: add seccomp support

2020-09-11 Thread Max Filippov
On Fri, Sep 11, 2020 at 12:38 PM Kees Cook  wrote:
> On Sat, Jul 18, 2020 at 07:16:51PM -0700, Max Filippov wrote:
> > Hello,
> >
> > this series adds support for seccomp filter on xtensa and updates
> > selftests/seccomp.
>
> Hi!
>
> Firstly, thanks for adding seccomp support! :) I would, however, ask
> that you CC maintainers on these kinds of changes for feedback. I was
> surprised to find the changes in the seccomp selftests today in Linus's
> tree. I didn't seem to get CCed on this series, even though
> get_maintainers shows this:
>
> $ ./scripts/get_maintainer.pl 0001-selftests-seccomp-add-xtensa-support.mbox
> Kees Cook  (supporter:SECURE COMPUTING)
> Andy Lutomirski  (reviewer:SECURE COMPUTING)
> Will Drewry  (reviewer:SECURE COMPUTING)
> Shuah Khan  (maintainer:KERNEL SELFTEST FRAMEWORK)
> ...

Sorry about that. Looks like I've filtered out too much of the cc: list.
I'll fix my workflow.

> Regardless, I'm still glad to have more arch support! :) I'll send a
> follow-up patch to refactor a bit of the selftest.

-- 
Thanks.
-- Max


Re: [PATCH v4 3/4] mm/pageblock: work around multiple arch's cmpxchg support issue

2020-09-03 Thread Max Filippov
On Thu, Sep 3, 2020 at 12:01 AM Alex Shi  wrote:
>
> Armv6, sh2, sparc32 and xtensa can not do cmpxchg1, so we have to use
> cmpxchg4 on it.

[...]

> diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig> index 
> e00d94b16658..03a6c7fd999d 100644
> --- a/arch/arm/Kconfig
> +++ b/arch/arm/Kconfig
> @@ -48,6 +48,7 @@ config ARM
> select GENERIC_ALLOCATOR
> select GENERIC_ARCH_TOPOLOGY if ARM_CPU_TOPOLOGY
> select GENERIC_ATOMIC64 if CPU_V7M || CPU_V6 || !CPU_32v6K || !AEABI
> +   select NO_CMPXCHG_BYTE if CPU_V6
> select GENERIC_CLOCKEVENTS_BROADCAST if SMP
> select GENERIC_CPU_AUTOPROBE
> select GENERIC_EARLY_IOREMAP
> diff --git a/arch/sh/Kconfig b/arch/sh/Kconfig
> index d20927128fce..4c7f0ad5b93f 100644
> --- a/arch/sh/Kconfig
> +++ b/arch/sh/Kconfig
> @@ -155,6 +155,7 @@ menu "System type"
>  config CPU_SH2
> bool
> select SH_INTC
> +   select NO_CMPXCHG_BYTE
>
>  config CPU_SH2A
> bool
> diff --git a/arch/sparc/Kconfig b/arch/sparc/Kconfig
> index efeff2c896a5..51ae5c8ede87 100644
> --- a/arch/sparc/Kconfig
> +++ b/arch/sparc/Kconfig
> @@ -58,6 +58,7 @@ config SPARC32
> select CLZ_TAB
> select HAVE_UID16
> select OLD_SIGACTION
> +   select NO_CMPXCHG_BYTE
>
>  config SPARC64
> def_bool 64BIT
> diff --git a/arch/xtensa/Kconfig b/arch/xtensa/Kconfig
> index e997e0119c02..862b008ab09e 100644
> --- a/arch/xtensa/Kconfig
> +++ b/arch/xtensa/Kconfig
> @@ -42,6 +42,7 @@ config XTENSA
> select MODULES_USE_ELF_RELA
> select PERF_USE_VMALLOC
> select VIRT_TO_BUS
> +   select NO_CMPXCHG_BYTE

Please keep the lists of select statements in Kconfig files above
alphabetically sorted.

-- 
Thanks.
-- Max


Re: [PATCH v3 23/23] xtensa: use asm-generic/mmu_context.h for no-op implementations

2020-09-01 Thread Max Filippov
On Tue, Sep 1, 2020 at 7:17 AM Nicholas Piggin  wrote:
>
> Cc: Chris Zankel 
> Cc: Max Filippov 
> Cc: linux-xte...@linux-xtensa.org
> Signed-off-by: Nicholas Piggin 
> ---
>
> Please ack or nack if you object to this being mered via
> Arnd's tree.
>
>  arch/xtensa/include/asm/mmu_context.h   | 11 +++
>  arch/xtensa/include/asm/nommu_context.h | 26 +
>  2 files changed, 4 insertions(+), 33 deletions(-)

Acked-by: Max Filippov 

-- 
Thanks.
-- Max


Re: [PATCH trivial] xtensa: fix Kconfig typo

2020-08-30 Thread Max Filippov
On Sat, Aug 29, 2020 at 10:57 PM Randy Dunlap  wrote:
>
> From: Randy Dunlap 
>
> Correct trivial typo (ful -> full).
>
> Fixes: 76743c0e0915 ("xtensa: move kernel memory layout to platform options")
> Signed-off-by: Randy Dunlap 
> Cc: Chris Zankel 
> Cc: Max Filippov 
> Cc: linux-xte...@linux-xtensa.org
> Cc: Jiri Kosina 
> ---
>  arch/xtensa/Kconfig |2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Acked-by: Max Filippov 

-- 
Thanks.
-- Max


[PATCH] binfmt_flat: revert "binfmt_flat: don't offset the data start"

2020-08-08 Thread Max Filippov
binfmt_flat loader uses the gap between text and data to store data
segment pointers for the libraries. Even in the absence of shared
libraries it stores at least one pointer to the executable's own data
segment. Text and data can go back to back in the flat binary image and
without offsetting data segment last few instructions in the text
segment may get corrupted by the data segment pointer.

Fix it by reverting commit a2357223c50a ("binfmt_flat: don't offset the
data start").

Cc: sta...@vger.kernel.org
Fixes: a2357223c50a ("binfmt_flat: don't offset the data start")
Signed-off-by: Max Filippov 
---
 fs/binfmt_flat.c | 20 
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
index f2f9086ebe98..b9c658e0548e 100644
--- a/fs/binfmt_flat.c
+++ b/fs/binfmt_flat.c
@@ -576,7 +576,7 @@ static int load_flat_file(struct linux_binprm *bprm,
goto err;
}
 
-   len = data_len + extra;
+   len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned 
long);
len = PAGE_ALIGN(len);
realdatastart = vm_mmap(NULL, 0, len,
PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
@@ -590,7 +590,9 @@ static int load_flat_file(struct linux_binprm *bprm,
vm_munmap(textpos, text_len);
goto err;
}
-   datapos = ALIGN(realdatastart, FLAT_DATA_ALIGN);
+   datapos = ALIGN(realdatastart +
+   MAX_SHARED_LIBS * sizeof(unsigned long),
+   FLAT_DATA_ALIGN);
 
pr_debug("Allocated data+bss+stack (%u bytes): %lx\n",
 data_len + bss_len + stack_len, datapos);
@@ -620,7 +622,7 @@ static int load_flat_file(struct linux_binprm *bprm,
memp_size = len;
} else {
 
-   len = text_len + data_len + extra;
+   len = text_len + data_len + extra + MAX_SHARED_LIBS * 
sizeof(u32);
len = PAGE_ALIGN(len);
textpos = vm_mmap(NULL, 0, len,
PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
@@ -635,7 +637,9 @@ static int load_flat_file(struct linux_binprm *bprm,
}
 
realdatastart = textpos + ntohl(hdr->data_start);
-   datapos = ALIGN(realdatastart, FLAT_DATA_ALIGN);
+   datapos = ALIGN(realdatastart +
+   MAX_SHARED_LIBS * sizeof(u32),
+   FLAT_DATA_ALIGN);
 
reloc = (__be32 __user *)
(datapos + (ntohl(hdr->reloc_start) - text_len));
@@ -652,9 +656,8 @@ static int load_flat_file(struct linux_binprm *bprm,
 (text_len + full_data
  - sizeof(struct flat_hdr)),
 0);
-   if (datapos != realdatastart)
-   memmove((void *)datapos, (void *)realdatastart,
-   full_data);
+   memmove((void *) datapos, (void *) realdatastart,
+   full_data);
 #else
/*
 * This is used on MMU systems mainly for testing.
@@ -710,7 +713,8 @@ static int load_flat_file(struct linux_binprm *bprm,
if (IS_ERR_VALUE(result)) {
ret = result;
pr_err("Unable to read code+data+bss, errno %d\n", ret);
-   vm_munmap(textpos, text_len + data_len + extra);
+   vm_munmap(textpos, text_len + data_len + extra +
+   MAX_SHARED_LIBS * sizeof(u32));
goto err;
}
}
-- 
2.20.1



[PULL 00/10] xtensa updates for v5.9

2020-08-05 Thread Max Filippov
Hi Linus,

please pull the following Xtensa architecture updates for v5.9.
There are trivial conflicts in the seccomp selftests with the csky
and sh trees.

The following changes since commit 92ed301919932f13b9172e525674157e983d:

  Linux 5.8-rc7 (2020-07-26 14:14:06 -0700)

are available in the Git repository at:

  git://github.com/jcmvbkbc/linux-xtensa.git tags/xtensa-20200805

for you to fetch changes up to a0fc1436f1f4f84e93144480bf30e0c958d135b6:

  xtensa: add missing exclusive access state management (2020-07-31 14:15:57 
-0700)


Xtensa updates for v5.9:

- add syscall audit support
- add seccomp filter support
- clean up make rules under arch/xtensa/boot
- fix state management for exclusive access opcodes
- fix build with PMU enabled


Masahiro Yamada (1):
  xtensa: add boot subdirectories build artifacts to 'targets'

Max Filippov (8):
  xtensa: add audit support
  xtensa: expose syscall through user_pt_regs
  xtensa: add seccomp support
  selftests/seccomp: add xtensa support
  xtensa: move vmlinux.bin[.gz] to boot subdirectory
  xtensa: add uImage and xipImage to targets
  xtensa: fix xtensa_pmu_setup prototype
  xtensa: add missing exclusive access state management

Randy Dunlap (1):
  xtensa: initialize_mmu.h: fix a duplicated word

 .../features/seccomp/seccomp-filter/arch-support.txt |  2 +-
 arch/xtensa/Kconfig  | 16 
 arch/xtensa/boot/Makefile| 12 +++-
 arch/xtensa/boot/boot-elf/Makefile   |  5 +++--
 arch/xtensa/boot/boot-redboot/Makefile   |  5 +++--
 arch/xtensa/include/asm/Kbuild   |  1 +
 arch/xtensa/include/asm/initialize_mmu.h |  2 +-
 arch/xtensa/include/asm/thread_info.h| 11 ++-
 arch/xtensa/include/uapi/asm/ptrace.h|  3 ++-
 arch/xtensa/kernel/asm-offsets.c |  3 +++
 arch/xtensa/kernel/entry.S   | 11 +++
 arch/xtensa/kernel/perf_event.c  |  2 +-
 arch/xtensa/kernel/ptrace.c  | 14 +-
 tools/testing/selftests/seccomp/seccomp_bpf.c| 16 +++-
 14 files changed, 87 insertions(+), 16 deletions(-)

-- 
Thanks.
-- Max


Re: [PATCH v1 1/2] Missing instruction_pointer_set() instances

2020-08-04 Thread Max Filippov
On Thu, Jul 30, 2020 at 7:59 PM Jin Yao  wrote:
>
> There is a potential security issue that perf kernel samples
> may be leaked even though kernel sampling is disabled. For fixing
> the potential leakage, the idea is to use instruction_pointer_set
> to set invalid ip address in leaked perf samples in some cases.
>
> But instruction_pointer_set is missing on some architectures.
> Define instruction_pointer_set for these architectures.
>
> Signed-off-by: Jin Yao 
> ---
>  arch/alpha/include/asm/ptrace.h  | 6 ++
>  arch/arc/include/asm/ptrace.h| 6 ++
>  arch/nds32/include/asm/ptrace.h  | 7 +++
>  arch/xtensa/include/asm/ptrace.h | 6 ++
>  4 files changed, 25 insertions(+)

For xtensa:
Acked-by: Max Filippov 

-- 
Thanks.
-- Max


[PATCH] xtensa: add missing exclusive access state management

2020-07-31 Thread Max Filippov
The result of the s32ex opcode is recorded in the ATOMCTL special
register and must be retrieved with the getex opcode. Context switch
between s32ex and getex may trash the ATOMCTL register and result in
duplicate update or missing update of the atomic variable.
Add atomctl8 field to the struct thread_info and use getex to swap
ATOMCTL bit 8 as a part of context switch.
Clear exclusive access monitor on kernel entry.

Cc: sta...@vger.kernel.org
Fixes: f7c34874f04a ("xtensa: add exclusive atomics support")
Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/thread_info.h |  4 
 arch/xtensa/kernel/asm-offsets.c  |  3 +++
 arch/xtensa/kernel/entry.S| 11 +++
 3 files changed, 18 insertions(+)

diff --git a/arch/xtensa/include/asm/thread_info.h 
b/arch/xtensa/include/asm/thread_info.h
index 8918f0f20c53..6acbbe0d87d3 100644
--- a/arch/xtensa/include/asm/thread_info.h
+++ b/arch/xtensa/include/asm/thread_info.h
@@ -55,6 +55,10 @@ struct thread_info {
mm_segment_taddr_limit; /* thread address space */
 
unsigned long   cpenable;
+#if XCHAL_HAVE_EXCLUSIVE
+   /* result of the most recent exclusive store */
+   unsigned long   atomctl8;
+#endif
 
/* Allocate storage for extra user states and coprocessor states. */
 #if XTENSA_HAVE_COPROCESSORS
diff --git a/arch/xtensa/kernel/asm-offsets.c b/arch/xtensa/kernel/asm-offsets.c
index 33a257b33723..dc5c83cad9be 100644
--- a/arch/xtensa/kernel/asm-offsets.c
+++ b/arch/xtensa/kernel/asm-offsets.c
@@ -93,6 +93,9 @@ int main(void)
DEFINE(THREAD_RA, offsetof (struct task_struct, thread.ra));
DEFINE(THREAD_SP, offsetof (struct task_struct, thread.sp));
DEFINE(THREAD_CPENABLE, offsetof (struct thread_info, cpenable));
+#if XCHAL_HAVE_EXCLUSIVE
+   DEFINE(THREAD_ATOMCTL8, offsetof (struct thread_info, atomctl8));
+#endif
 #if XTENSA_HAVE_COPROCESSORS
DEFINE(THREAD_XTREGS_CP0, offsetof(struct thread_info, xtregs_cp.cp0));
DEFINE(THREAD_XTREGS_CP1, offsetof(struct thread_info, xtregs_cp.cp1));
diff --git a/arch/xtensa/kernel/entry.S b/arch/xtensa/kernel/entry.S
index 98515c24d9b2..703cf6205efe 100644
--- a/arch/xtensa/kernel/entry.S
+++ b/arch/xtensa/kernel/entry.S
@@ -374,6 +374,11 @@ common_exception:
s32ia2, a1, PT_LCOUNT
 #endif
 
+#if XCHAL_HAVE_EXCLUSIVE
+   /* Clear exclusive access monitor set by interrupted code */
+   clrex
+#endif
+
/* It is now save to restore the EXC_TABLE_FIXUP variable. */
 
rsr a2, exccause
@@ -2020,6 +2025,12 @@ ENTRY(_switch_to)
s32ia3, a4, THREAD_CPENABLE
 #endif
 
+#if XCHAL_HAVE_EXCLUSIVE
+   l32ia3, a5, THREAD_ATOMCTL8
+   getex   a3
+   s32ia3, a4, THREAD_ATOMCTL8
+#endif
+
/* Flush register file. */
 
spill_registers_kernel
-- 
2.20.1



[PATCH] xtensa: fix xtensa_pmu_setup prototype

2020-07-31 Thread Max Filippov
Fix the following build error in configurations with
CONFIG_XTENSA_VARIANT_HAVE_PERF_EVENTS=y:

  arch/xtensa/kernel/perf_event.c:420:29: error: passing argument 3 of
  ‘cpuhp_setup_state’ from incompatible pointer type

Cc: sta...@vger.kernel.org
Fixes: 25a77b55e74c ("xtensa/perf: Convert the hotplug notifier to state 
machine callbacks")
Signed-off-by: Max Filippov 
---
 arch/xtensa/kernel/perf_event.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/xtensa/kernel/perf_event.c b/arch/xtensa/kernel/perf_event.c
index 99fcd63ce597..a0d05c8598d0 100644
--- a/arch/xtensa/kernel/perf_event.c
+++ b/arch/xtensa/kernel/perf_event.c
@@ -399,7 +399,7 @@ static struct pmu xtensa_pmu = {
.read = xtensa_pmu_read,
 };
 
-static int xtensa_pmu_setup(int cpu)
+static int xtensa_pmu_setup(unsigned int cpu)
 {
unsigned i;
 
-- 
2.20.1



Re: [PATCH 03/15] arm, xtensa: simplify initialization of high memory pages

2020-07-28 Thread Max Filippov
On Mon, Jul 27, 2020 at 10:12 PM Mike Rapoport  wrote:
>
> From: Mike Rapoport 
>
> The function free_highpages() in both arm and xtensa essentially open-code
> for_each_free_mem_range() loop to detect high memory pages that were not
> reserved and that should be initialized and passed to the buddy allocator.
>
> Replace open-coded implementation of for_each_free_mem_range() with usage
> of memblock API to simplify the code.
>
> Signed-off-by: Mike Rapoport 
> ---
>  arch/arm/mm/init.c| 48 +++--
>  arch/xtensa/mm/init.c | 55 ---
>  2 files changed, 18 insertions(+), 85 deletions(-)

For the xtensa part:
Reviewed-by: Max Filippov 
Tested-by: Max Filippov 

-- 
Thanks.
-- Max


[PATCH] xtensa: fix closing endif comment

2020-07-27 Thread Max Filippov
Commit 8f74afa22d9b ("xtensa: switch to generic version of pte
allocation") introduced the following build warning for xtensa
  arch/xtensa/include/asm/pgalloc.h:67:8: warning: extra tokens at end of
  #endif directive [-Wendif-labels]
Fix #endif comment.

Cc: Mike Rapoport 
Reviewed-by: Mike Rapoport 
Fixes: 8f74afa22d9b ("xtensa: switch to generic version of pte allocation")
Signed-off-by: Max Filippov 
---
Andrew, could you please take this patch as the change that it fixes is
currently in the mm tree?

 arch/xtensa/include/asm/pgalloc.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/xtensa/include/asm/pgalloc.h 
b/arch/xtensa/include/asm/pgalloc.h
index 699a8fdf9005..d3a22da4d2c9 100644
--- a/arch/xtensa/include/asm/pgalloc.h
+++ b/arch/xtensa/include/asm/pgalloc.h
@@ -64,6 +64,6 @@ static inline pgtable_t pte_alloc_one(struct mm_struct *mm)
 }
 
 #define pmd_pgtable(pmd) pmd_page(pmd)
-#endif CONFIG_MMU
+#endif /* CONFIG_MMU */
 
 #endif /* _XTENSA_PGALLOC_H */
-- 
2.20.1



Re: [PATCH 15/18] xtensa: propagate the calling conventions change down into csum_partial_copy_generic()

2020-07-22 Thread Max Filippov
On Tue, Jul 21, 2020 at 1:27 PM Al Viro  wrote:
>
> From: Al Viro 
>
> turn the exception handlers into returning 0.
>
> Signed-off-by: Al Viro 
> ---
>  arch/xtensa/include/asm/checksum.h | 20 +++-
>  arch/xtensa/lib/checksum.S | 67 
> +-
>  2 files changed, 19 insertions(+), 68 deletions(-)

Reviewed-by: Max Filippov 
Tested-by: Max Filippov 

-- 
Thanks.
-- Max


[PATCH 1/2] xtensa: move vmlinux.bin[.gz] to boot subdirectory

2020-07-21 Thread Max Filippov
vmlinux.bin and vmlinux.bin.gz are always rebuilt in the kernel build
process. Add them to 'targets' and move them to the boot subdirectory
where their rules are. Update make rules that refer to them.

Signed-off-by: Max Filippov 
---
 arch/xtensa/boot/Makefile  | 11 ++-
 arch/xtensa/boot/boot-elf/Makefile |  4 ++--
 arch/xtensa/boot/boot-redboot/Makefile |  4 ++--
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/arch/xtensa/boot/Makefile b/arch/xtensa/boot/Makefile
index 1a14d38d9b33..801fe30b4dfe 100644
--- a/arch/xtensa/boot/Makefile
+++ b/arch/xtensa/boot/Makefile
@@ -17,6 +17,7 @@ BIG_ENDIAN:= $(shell echo __XTENSA_EB__ | $(CC) -E - | 
grep -v "\#")
 export BIG_ENDIAN
 
 subdir-y   := lib
+targets+= vmlinux.bin vmlinux.bin.gz
 
 # Subdirs for the boot loader(s)
 
@@ -35,19 +36,19 @@ boot-elf boot-redboot: $(addprefix $(obj)/,$(subdir-y))
 
 OBJCOPYFLAGS = --strip-all -R .comment -R .notes -O binary
 
-vmlinux.bin: vmlinux FORCE
+$(obj)/vmlinux.bin: vmlinux FORCE
$(call if_changed,objcopy)
 
-vmlinux.bin.gz: vmlinux.bin FORCE
+$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
$(call if_changed,gzip)
 
-boot-elf: vmlinux.bin
-boot-redboot: vmlinux.bin.gz
+boot-elf: $(obj)/vmlinux.bin
+boot-redboot: $(obj)/vmlinux.bin.gz
 
 UIMAGE_LOADADDR = $(CONFIG_KERNEL_LOAD_ADDRESS)
 UIMAGE_COMPRESSION = gzip
 
-$(obj)/uImage: vmlinux.bin.gz FORCE
+$(obj)/uImage: $(obj)/vmlinux.bin.gz FORCE
$(call if_changed,uimage)
$(Q)$(kecho) '  Kernel: $@ is ready'
 
diff --git a/arch/xtensa/boot/boot-elf/Makefile 
b/arch/xtensa/boot/boot-elf/Makefile
index badee63dae27..0ebc9827f7e5 100644
--- a/arch/xtensa/boot/boot-elf/Makefile
+++ b/arch/xtensa/boot/boot-elf/Makefile
@@ -19,9 +19,9 @@ targets   += $(boot-y) boot.lds
 
 OBJS   := $(addprefix $(obj)/,$(boot-y))
 
-$(obj)/Image.o: vmlinux.bin $(OBJS)
+$(obj)/Image.o: $(obj)/../vmlinux.bin $(OBJS)
$(Q)$(OBJCOPY) $(OBJCOPY_ARGS) -R .comment \
-   --add-section image=vmlinux.bin \
+   --add-section image=$< \
--set-section-flags image=contents,alloc,load,load,data \
$(OBJS) $@
 
diff --git a/arch/xtensa/boot/boot-redboot/Makefile 
b/arch/xtensa/boot/boot-redboot/Makefile
index 1a277dd57b2a..07cb24afedc2 100644
--- a/arch/xtensa/boot/boot-redboot/Makefile
+++ b/arch/xtensa/boot/boot-redboot/Makefile
@@ -20,9 +20,9 @@ LIBS  := arch/xtensa/boot/lib/lib.a arch/xtensa/lib/lib.a
 
 LIBGCC := $(shell $(CC) $(KBUILD_CFLAGS) -print-libgcc-file-name)
 
-$(obj)/zImage.o: vmlinux.bin.gz $(OBJS)
+$(obj)/zImage.o: $(obj)/../vmlinux.bin.gz $(OBJS)
$(Q)$(OBJCOPY) $(OBJCOPY_ARGS) -R .comment \
-   --add-section image=vmlinux.bin.gz \
+   --add-section image=$< \
--set-section-flags image=contents,alloc,load,load,data \
$(OBJS) $@
 
-- 
2.20.1



[PATCH 0/2] xtensa: boot targets cleanup

2020-07-21 Thread Max Filippov
Hello,

this small clean up in the xtensa boot subdirectory adds more targets to
the 'targets' variable to avoid unnecessary rebuils.

Max Filippov (2):
  xtensa: move vmlinux.bin[.gz] to boot subdirectory
  xtensa: add uImage and xipImage to targets

 arch/xtensa/boot/Makefile  | 12 +++-
 arch/xtensa/boot/boot-elf/Makefile |  4 ++--
 arch/xtensa/boot/boot-redboot/Makefile |  4 ++--
 3 files changed, 11 insertions(+), 9 deletions(-)

-- 
2.20.1



[PATCH 2/2] xtensa: add uImage and xipImage to targets

2020-07-21 Thread Max Filippov
uImage and xipImage are always rebuilt in the xtensa kernel build
process. Add them to 'targets' to avoid that.

Signed-off-by: Max Filippov 
---
 arch/xtensa/boot/Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/xtensa/boot/Makefile b/arch/xtensa/boot/Makefile
index 801fe30b4dfe..f6bb352f94b4 100644
--- a/arch/xtensa/boot/Makefile
+++ b/arch/xtensa/boot/Makefile
@@ -18,6 +18,7 @@ export BIG_ENDIAN
 
 subdir-y   := lib
 targets+= vmlinux.bin vmlinux.bin.gz
+targets+= uImage xipImage
 
 # Subdirs for the boot loader(s)
 
-- 
2.20.1



Re: [PATCH v2] xtensa: add boot subdirectories targets to extra-y

2020-07-21 Thread Max Filippov
On Tue, Jul 21, 2020 at 5:53 PM Masahiro Yamada  wrote:
> On Tue, Jul 21, 2020 at 6:37 PM Max Filippov  wrote:
> > The commit 8fe87a92f262 ("kbuild: always create directories of targets")
> > exposed an issue in the xtensa makefiles that results in the following
> > build error in a clean directory:
>
> But, we need to fix this in the kbuild tree
> to retain the bisectability.
>
> I will insert the following before the offending commit.
> https://patchwork.kernel.org/patch/11676883/
>
> I used 'targets' instead of 'extra-y'
> because they are built on demand
> while building the final boot image.

Sure, please go ahead with your version.
Thank you for taking care of it.

-- Max


Re: [PATCH] xtensa: add boot subdirectories build artifacts to 'targets'

2020-07-21 Thread Max Filippov
On Tue, Jul 21, 2020 at 5:47 PM Masahiro Yamada  wrote:
>
> Xtensa always rebuilds the following even if nothing in the source code
> has been changed. Passing V=2 shows the reason.
>
>   AS  arch/xtensa/boot/boot-elf/bootstrap.o - due to bootstrap.o not in 
> $(targets)
>   LDS arch/xtensa/boot/boot-elf/boot.lds - due to boot.lds not in 
> $(targets)
>
> They are built by if_changed(_dep). Add them to 'targets' so .*.cmd files
> are included.
>
> Signed-off-by: Masahiro Yamada 
> ---
>
>  arch/xtensa/boot/boot-elf/Makefile | 1 +
>  arch/xtensa/boot/boot-redboot/Makefile | 1 +
>  2 files changed, 2 insertions(+)

Acked-by: Max Filippov 

-- 
Thanks.
-- Max


Re: [PATCH] xtensa: fix access check in csum_and_copy_from_user

2020-07-21 Thread Max Filippov
On Tue, Jul 21, 2020 at 4:04 PM Al Viro  wrote:
>
> On Tue, Jul 21, 2020 at 03:00:35PM -0700, Max Filippov wrote:
> > Commit d341659f470b ("xtensa: switch to providing
> > csum_and_copy_from_user()") introduced access check, but incorrectly
> > tested dst instead of src.
> > Fix access_ok argument in csum_and_copy_from_user.
>
> Applied, with apologies...  Which tree do you want it to go through?
> I'm dropping it into vfs.git#fixes, will send to Linus unless you
> prefer it to go some other way...

NP. Anything that will go into 5.8 is good.

-- 
Thanks.
-- Max


[PATCH] xtensa: fix access check in csum_and_copy_from_user

2020-07-21 Thread Max Filippov
Commit d341659f470b ("xtensa: switch to providing
csum_and_copy_from_user()") introduced access check, but incorrectly
tested dst instead of src.
Fix access_ok argument in csum_and_copy_from_user.

Cc: Al Viro 
Fixes: d341659f470b ("xtensa: switch to providing csum_and_copy_from_user()")
Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/checksum.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/xtensa/include/asm/checksum.h 
b/arch/xtensa/include/asm/checksum.h
index d8292cc9ebdf..243a5fe79d3c 100644
--- a/arch/xtensa/include/asm/checksum.h
+++ b/arch/xtensa/include/asm/checksum.h
@@ -57,7 +57,7 @@ static inline
 __wsum csum_and_copy_from_user(const void __user *src, void *dst,
   int len, __wsum sum, int *err_ptr)
 {
-   if (access_ok(dst, len))
+   if (access_ok(src, len))
return csum_partial_copy_generic((__force const void *)src, dst,
len, sum, err_ptr, NULL);
if (len)
-- 
2.20.1



Re: [PATCH] xtensa: initialize_mmu.h: fix a duplicated word

2020-07-21 Thread Max Filippov
On Tue, Jul 21, 2020 at 2:00 PM Randy Dunlap  wrote:
>
> Change "The the" to "For the".
>
> Signed-off-by: Randy Dunlap 
> Cc: Chris Zankel 
> Cc: Max Filippov 
> Cc: linux-xte...@linux-xtensa.org
> ---
>  arch/xtensa/include/asm/initialize_mmu.h |2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Thank you! Applied to my xtensa tree.

-- Max


[PATCH v2] xtensa: add boot subdirectories targets to extra-y

2020-07-21 Thread Max Filippov
The commit 8fe87a92f262 ("kbuild: always create directories of targets")
exposed an issue in the xtensa makefiles that results in the following
build error in a clean directory:
  scripts/Makefile.build:374: arch/xtensa/boot/boot-elf/boot.lds] Error 1
arch/xtensa/boot/boot-elf/bootstrap.S:21: fatal error:
opening dependency file arch/xtensa/boot/boot-elf/.bootstrap.o.d:
No such file or directory

Intermediate targets in arch/xtensa/boot/boot-elf don't get into
'targets' and build directory is not created for them.
Add boot.lds and bootstrap.o to extra-y in subdirectories of
arch/xtensa/boot.

Cc: Masahiro Yamada 
Signed-off-by: Max Filippov 
---
Changes v1->v2:
- update boot-redboot/Makefile in the same manner as boot-elf/Makefile.

 arch/xtensa/boot/boot-elf/Makefile | 1 +
 arch/xtensa/boot/boot-redboot/Makefile | 1 +
 2 files changed, 2 insertions(+)

diff --git a/arch/xtensa/boot/boot-elf/Makefile 
b/arch/xtensa/boot/boot-elf/Makefile
index 12ae1e91cb75..ad341c0fff15 100644
--- a/arch/xtensa/boot/boot-elf/Makefile
+++ b/arch/xtensa/boot/boot-elf/Makefile
@@ -15,6 +15,7 @@ export CPPFLAGS_boot.lds += -P -C
 export KBUILD_AFLAGS += -mtext-section-literals
 
 boot-y := bootstrap.o
+extra-y:= boot.lds $(boot-y)
 
 OBJS   := $(addprefix $(obj)/,$(boot-y))
 
diff --git a/arch/xtensa/boot/boot-redboot/Makefile 
b/arch/xtensa/boot/boot-redboot/Makefile
index 8632473ad319..022a76a2282a 100644
--- a/arch/xtensa/boot/boot-redboot/Makefile
+++ b/arch/xtensa/boot/boot-redboot/Makefile
@@ -13,6 +13,7 @@ endif
 LD_ARGS= -T $(srctree)/$(obj)/boot.ld
 
 boot-y := bootstrap.o
+extra-y:= $(boot-y)
 
 OBJS   := $(addprefix $(obj)/,$(boot-y))
 LIBS   := arch/xtensa/boot/lib/lib.a arch/xtensa/lib/lib.a
-- 
2.20.1



[PATCH] xtensa: add boot-elf targets to extra-y

2020-07-20 Thread Max Filippov
The commit 8fe87a92f262 ("kbuild: always create directories of targets")
exposed an issue in the xtensa makefiles that results in the following
build error in a clean directory:
  scripts/Makefile.build:374: arch/xtensa/boot/boot-elf/boot.lds] Error 1
arch/xtensa/boot/boot-elf/bootstrap.S:21: fatal error:
opening dependency file arch/xtensa/boot/boot-elf/.bootstrap.o.d:
No such file or directory

Intermediate targets in arch/xtensa/boot/boot-elf don't get into
'targets' and build directory is not created for them.
Add boot.lds and bootstrap.o to extra-y.

Cc: Masahiro Yamada 
Signed-off-by: Max Filippov 
---
 arch/xtensa/boot/boot-elf/Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/xtensa/boot/boot-elf/Makefile 
b/arch/xtensa/boot/boot-elf/Makefile
index a62a25506536..eb952d809d81 100644
--- a/arch/xtensa/boot/boot-elf/Makefile
+++ b/arch/xtensa/boot/boot-elf/Makefile
@@ -15,6 +15,7 @@ export CPPFLAGS_boot.lds += -P -C
 export KBUILD_AFLAGS += -mtext-section-literals
 
 boot-y := bootstrap.o
+extra-y:= boot.lds $(boot-y)
 
 OBJS   := $(addprefix $(obj)/,$(boot-y))
 
-- 
2.20.1



[PATCH] xtensa: fix closing endif comment

2020-07-20 Thread Max Filippov
Commit 8f74afa22d9b ("xtensa: switch to generic version of pte
allocation") introduced the following build warning for xtensa
  arch/xtensa/include/asm/pgalloc.h:67:8: warning: extra tokens at end of
  #endif directive [-Wendif-labels]
Fix #endif comment.

Cc: Mike Rapoport 
Fixes: 8f74afa22d9b ("xtensa: switch to generic version of pte allocation")
Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/pgalloc.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/xtensa/include/asm/pgalloc.h 
b/arch/xtensa/include/asm/pgalloc.h
index 699a8fdf9005..d3a22da4d2c9 100644
--- a/arch/xtensa/include/asm/pgalloc.h
+++ b/arch/xtensa/include/asm/pgalloc.h
@@ -64,6 +64,6 @@ static inline pgtable_t pte_alloc_one(struct mm_struct *mm)
 }
 
 #define pmd_pgtable(pmd) pmd_page(pmd)
-#endif CONFIG_MMU
+#endif /* CONFIG_MMU */
 
 #endif /* _XTENSA_PGALLOC_H */
-- 
2.20.1



[PATCH 0/3] xtensa: add seccomp support

2020-07-18 Thread Max Filippov
Hello,

this series adds support for seccomp filter on xtensa and updates
selftests/seccomp.

Max Filippov (3):
  xtensa: expose syscall through user_pt_regs
  xtensa: add seccomp support
  selftests/seccomp: add xtensa support

 .../seccomp/seccomp-filter/arch-support.txt  |  2 +-
 arch/xtensa/Kconfig  | 15 +++
 arch/xtensa/include/asm/Kbuild   |  1 +
 arch/xtensa/include/asm/thread_info.h|  5 -
 arch/xtensa/include/uapi/asm/ptrace.h|  3 ++-
 arch/xtensa/kernel/ptrace.c  |  8 +++-
 tools/testing/selftests/seccomp/seccomp_bpf.c| 16 +++-
 7 files changed, 45 insertions(+), 5 deletions(-)

-- 
2.20.1



[PATCH 3/3] selftests/seccomp: add xtensa support

2020-07-18 Thread Max Filippov
Xtensa returns syscall number can be obtained and changed through the
struct user_pt_regs. Syscall return value register is fixed relatively
to the current register window in the user_pt_regs, so it needs a bit of
special treatment.

Signed-off-by: Max Filippov 
---
 tools/testing/selftests/seccomp/seccomp_bpf.c | 16 +++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c 
b/tools/testing/selftests/seccomp/seccomp_bpf.c
index 252140a52553..1b445c2e7fbe 100644
--- a/tools/testing/selftests/seccomp/seccomp_bpf.c
+++ b/tools/testing/selftests/seccomp/seccomp_bpf.c
@@ -122,6 +122,8 @@ struct seccomp_data {
 #  define __NR_seccomp 358
 # elif defined(__s390__)
 #  define __NR_seccomp 348
+# elif defined(__xtensa__)
+#  define __NR_seccomp 337
 # else
 #  warning "seccomp syscall number unknown for this architecture"
 #  define __NR_seccomp 0x
@@ -1622,6 +1624,14 @@ TEST_F(TRACE_poke, getpid_runs_normally)
 # define SYSCALL_SYSCALL_NUM regs[4]
 # define SYSCALL_RET   regs[2]
 # define SYSCALL_NUM_RET_SHARE_REG
+#elif defined(__xtensa__)
+# define ARCH_REGS struct user_pt_regs
+# define SYSCALL_NUM   syscall
+/*
+ * On xtensa syscall return value is in the register
+ * a2 of the current window which is not fixed.
+ */
+#define SYSCALL_RET(reg) a[(reg).windowbase * 4 + 2]
 #else
 # error "Do not know how to find your architecture's registers and syscalls"
 #endif
@@ -1693,7 +1703,8 @@ void change_syscall(struct __test_metadata *_metadata,
EXPECT_EQ(0, ret) {}
 
 #if defined(__x86_64__) || defined(__i386__) || defined(__powerpc__) || \
-   defined(__s390__) || defined(__hppa__) || defined(__riscv)
+   defined(__s390__) || defined(__hppa__) || defined(__riscv) || \
+   defined(__xtensa__)
{
regs.SYSCALL_NUM = syscall;
}
@@ -1736,6 +1747,9 @@ void change_syscall(struct __test_metadata *_metadata,
if (syscall == -1)
 #ifdef SYSCALL_NUM_RET_SHARE_REG
TH_LOG("Can't modify syscall return on this architecture");
+
+#elif defined(__xtensa__)
+   regs.SYSCALL_RET(regs) = result;
 #else
regs.SYSCALL_RET = result;
 #endif
-- 
2.20.1



[PATCH 2/3] xtensa: add seccomp support

2020-07-18 Thread Max Filippov
Add SECCOMP to xtensa Kconfig, select HAVE_ARCH_SECCOMP_FILTER, add
TIF_SECCOMP and call secure_computing from do_syscall_trace_enter.

Signed-off-by: Max Filippov 
---
 .../seccomp/seccomp-filter/arch-support.txt   |  2 +-
 arch/xtensa/Kconfig   | 15 +++
 arch/xtensa/include/asm/Kbuild|  1 +
 arch/xtensa/include/asm/thread_info.h |  5 -
 arch/xtensa/kernel/ptrace.c   |  4 +++-
 5 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/Documentation/features/seccomp/seccomp-filter/arch-support.txt 
b/Documentation/features/seccomp/seccomp-filter/arch-support.txt
index c7b837f735b1..7b3ec8ea174a 100644
--- a/Documentation/features/seccomp/seccomp-filter/arch-support.txt
+++ b/Documentation/features/seccomp/seccomp-filter/arch-support.txt
@@ -30,5 +30,5 @@
 |  um: |  ok  |
 |   unicore32: | TODO |
 | x86: |  ok  |
-|  xtensa: | TODO |
+|  xtensa: |  ok  |
 ---
diff --git a/arch/xtensa/Kconfig b/arch/xtensa/Kconfig
index a7def0991a01..a461ee051e73 100644
--- a/arch/xtensa/Kconfig
+++ b/arch/xtensa/Kconfig
@@ -24,6 +24,7 @@ config XTENSA
select HAVE_ARCH_AUDITSYSCALL
select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL
select HAVE_ARCH_KASAN if MMU && !XIP_KERNEL
+   select HAVE_ARCH_SECCOMP_FILTER
select HAVE_ARCH_TRACEHOOK
select HAVE_COPY_THREAD_TLS
select HAVE_DEBUG_KMEMLEAK
@@ -217,6 +218,20 @@ config HOTPLUG_CPU
 
  Say N if you want to disable CPU hotplug.
 
+config SECCOMP
+   bool
+   prompt "Enable seccomp to safely compute untrusted bytecode"
+   help
+ This kernel feature is useful for number crunching applications
+ that may need to compute untrusted bytecode during their
+ execution. By using pipes or other transports made available to
+ the process as file descriptors supporting the read/write
+ syscalls, it's possible to isolate those applications in
+ their own address space using seccomp. Once seccomp is
+ enabled via prctl(PR_SET_SECCOMP), it cannot be disabled
+ and the task is only allowed to execute a few safe syscalls
+ defined by each seccomp mode.
+
 config FAST_SYSCALL_XTENSA
bool "Enable fast atomic syscalls"
default n
diff --git a/arch/xtensa/include/asm/Kbuild b/arch/xtensa/include/asm/Kbuild
index 9718e9593564..c59c42a1221a 100644
--- a/arch/xtensa/include/asm/Kbuild
+++ b/arch/xtensa/include/asm/Kbuild
@@ -7,4 +7,5 @@ generic-y += mcs_spinlock.h
 generic-y += param.h
 generic-y += qrwlock.h
 generic-y += qspinlock.h
+generic-y += seccomp.h
 generic-y += user.h
diff --git a/arch/xtensa/include/asm/thread_info.h 
b/arch/xtensa/include/asm/thread_info.h
index c49cc4a1f39a..8918f0f20c53 100644
--- a/arch/xtensa/include/asm/thread_info.h
+++ b/arch/xtensa/include/asm/thread_info.h
@@ -112,6 +112,7 @@ static inline struct thread_info *current_thread_info(void)
 #define TIF_NOTIFY_RESUME  7   /* callback before returning to user */
 #define TIF_DB_DISABLED8   /* debug trap disabled for 
syscall */
 #define TIF_SYSCALL_AUDIT  9   /* syscall auditing active */
+#define TIF_SECCOMP10  /* secure computing */
 
 #define _TIF_SYSCALL_TRACE (1<
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -559,7 +560,8 @@ int do_syscall_trace_enter(struct pt_regs *regs)
return 0;
}
 
-   if (regs->syscall == NO_SYSCALL) {
+   if (regs->syscall == NO_SYSCALL ||
+   secure_computing() == -1) {
do_syscall_trace_leave(regs);
return 0;
}
-- 
2.20.1



[PATCH 1/3] xtensa: expose syscall through user_pt_regs

2020-07-18 Thread Max Filippov
Use one of the reserved slots in struct user_pt_regs to return syscall
number in the GPR regset. Update syscall number from the GPR regset only
when it's non-zero.

Signed-off-by: Max Filippov 
---
 arch/xtensa/include/uapi/asm/ptrace.h | 3 ++-
 arch/xtensa/kernel/ptrace.c   | 4 
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/xtensa/include/uapi/asm/ptrace.h 
b/arch/xtensa/include/uapi/asm/ptrace.h
index 2ec0f9100a06..50db3e0a6341 100644
--- a/arch/xtensa/include/uapi/asm/ptrace.h
+++ b/arch/xtensa/include/uapi/asm/ptrace.h
@@ -50,7 +50,8 @@ struct user_pt_regs {
__u32 windowstart;
__u32 windowbase;
__u32 threadptr;
-   __u32 reserved[7 + 48];
+   __u32 syscall;
+   __u32 reserved[6 + 48];
__u32 a[64];
 };
 
diff --git a/arch/xtensa/kernel/ptrace.c b/arch/xtensa/kernel/ptrace.c
index 0278d7dfb4d6..437b4297948d 100644
--- a/arch/xtensa/kernel/ptrace.c
+++ b/arch/xtensa/kernel/ptrace.c
@@ -52,6 +52,7 @@ static int gpr_get(struct task_struct *target,
.threadptr = regs->threadptr,
.windowbase = regs->windowbase,
.windowstart = regs->windowstart,
+   .syscall = regs->syscall,
};
 
memcpy(newregs.a,
@@ -91,6 +92,9 @@ static int gpr_set(struct task_struct *target,
regs->sar = newregs.sar;
regs->threadptr = newregs.threadptr;
 
+   if (newregs.syscall)
+   regs->syscall = newregs.syscall;
+
if (newregs.windowbase != regs->windowbase ||
newregs.windowstart != regs->windowstart) {
u32 rotws, wmask;
-- 
2.20.1



[PATCH] xtensa: add audit support

2020-07-18 Thread Max Filippov
All bits needed for syscall audit are present on xtensa. Add
audit_syscall_entry and audit_syscall_exit calls and select
HAVE_ARCH_AUDITSYSCALL in Kconfig.

Signed-off-by: Max Filippov 
---
 arch/xtensa/Kconfig   | 1 +
 arch/xtensa/include/asm/thread_info.h | 4 +++-
 arch/xtensa/kernel/ptrace.c   | 6 ++
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/arch/xtensa/Kconfig b/arch/xtensa/Kconfig
index 3a9f1e80394a..a7def0991a01 100644
--- a/arch/xtensa/Kconfig
+++ b/arch/xtensa/Kconfig
@@ -21,6 +21,7 @@ config XTENSA
select GENERIC_PCI_IOMAP
select GENERIC_SCHED_CLOCK
select GENERIC_STRNCPY_FROM_USER if KASAN
+   select HAVE_ARCH_AUDITSYSCALL
select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL
select HAVE_ARCH_KASAN if MMU && !XIP_KERNEL
select HAVE_ARCH_TRACEHOOK
diff --git a/arch/xtensa/include/asm/thread_info.h 
b/arch/xtensa/include/asm/thread_info.h
index f092cc3f4e66..c49cc4a1f39a 100644
--- a/arch/xtensa/include/asm/thread_info.h
+++ b/arch/xtensa/include/asm/thread_info.h
@@ -111,15 +111,17 @@ static inline struct thread_info 
*current_thread_info(void)
 #define TIF_RESTORE_SIGMASK6   /* restore signal mask in do_signal() */
 #define TIF_NOTIFY_RESUME  7   /* callback before returning to user */
 #define TIF_DB_DISABLED8   /* debug trap disabled for 
syscall */
+#define TIF_SYSCALL_AUDIT  9   /* syscall auditing active */
 
 #define _TIF_SYSCALL_TRACE (1< 
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -562,6 +563,9 @@ int do_syscall_trace_enter(struct pt_regs *regs)
if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
trace_sys_enter(regs, syscall_get_nr(current, regs));
 
+   audit_syscall_entry(regs->syscall, regs->areg[6],
+   regs->areg[3], regs->areg[4],
+   regs->areg[5]);
return 1;
 }
 
@@ -569,6 +573,8 @@ void do_syscall_trace_leave(struct pt_regs *regs)
 {
int step;
 
+   audit_syscall_exit(regs);
+
if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
trace_sys_exit(regs, regs_return_value(regs));
 
-- 
2.20.1



[PULL 0/3] xtensa fixes for v5.8

2020-07-12 Thread Max Filippov
Hi Linus,

please pull the following Xtensa architecture fixes for v5.8:

The following changes since commit 9ebcfadb0610322ac537dd7aa5d9cbc2b2894c68:

  Linux 5.8-rc3 (2020-06-28 15:00:24 -0700)

are available in the Git repository at:

  git://github.com/jcmvbkbc/linux-xtensa.git tags/xtensa-20200712

for you to fetch changes up to ee769ebbe9e5fc7219e979fb7c5ed5bb5722649e:

  xtensa: simplify xtensa_pmu_irq_handler (2020-07-08 00:18:15 -0700)


Xtensa fixes for v5.8:

- fix __sync_fetch_and_{and,or}_4 declarations to avoid build warning
- update *pos in cpuinfo_op.next to avoid runtime warning
- use for_each_set_bit in xtensa_pmu_irq_handler instead of open-coding
  it


Max Filippov (2):
  xtensa: fix __sync_fetch_and_{and,or}_4 declarations
  xtensa: update *pos in cpuinfo_op.next

Xu Wang (1):
  xtensa: simplify xtensa_pmu_irq_handler

 arch/xtensa/kernel/perf_event.c   | 4 +---
 arch/xtensa/kernel/setup.c| 3 ++-
 arch/xtensa/kernel/xtensa_ksyms.c | 4 ++--
 3 files changed, 5 insertions(+), 6 deletions(-)

-- 
Thanks.
-- Max


Re: xtensa-linux-ld: section .start VMA overlaps section .image VMA

2020-07-09 Thread Max Filippov
On Thu, Jul 9, 2020 at 12:37 PM kernel test robot  wrote:
>
> tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 
> master
> head:   0bddd227f3dc55975e2b8dfa7fc6f959b062a2c7
> commit: 5e4417f921238b5acf101bfcd59a7b3463fb2dbd xtensa: reorganize vectors 
> placement
> date:   5 months ago
> config: xtensa-randconfig-r014-20200709 (attached as .config)
> compiler: xtensa-linux-gcc (GCC) 9.3.0
> reproduce (this is a W=1 build):
> wget 
> https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O 
> ~/bin/make.cross
> chmod +x ~/bin/make.cross
> git checkout 5e4417f921238b5acf101bfcd59a7b3463fb2dbd
> # save the attached .config to linux build tree
> COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross 
> ARCH=xtensa
>
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot 
>
> All errors (new ones prefixed by >>):
>
> >> xtensa-linux-ld: section .start VMA [d100,d107] 
> >> overlaps section .image VMA [d0003000,d1646087]

The config has CONFIG_XIP_KERNEL=y, so make xipImage
should be used, and it works correctly.

-- 
Thanks.
-- Max


Re: [PATCH] kernel: perf_event: use for_each_set_bit() to simplify the code

2020-07-08 Thread Max Filippov
On Tue, Jul 7, 2020 at 11:59 PM Xu Wang  wrote:
>
> Using for_each_set_bit() to simplify the code.
>
> Signed-off-by: Xu Wang 
> ---
>  arch/xtensa/kernel/perf_event.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)

Thanks! Applied to my xtensa tree with a slightly edited commit message.

-- 
Thanks.
-- Max


Re: [PATCH] kbuild: do not export LDFLAGS_vmlinux

2020-07-01 Thread Max Filippov
On Wed, Jul 1, 2020 at 12:30 PM Masahiro Yamada  wrote:

[...]

> diff --git a/arch/xtensa/boot/boot-elf/Makefile 
> b/arch/xtensa/boot/boot-elf/Makefile
> index 12ae1e91cb75..c6538d3321b9 100644
> --- a/arch/xtensa/boot/boot-elf/Makefile
> +++ b/arch/xtensa/boot/boot-elf/Makefile
> @@ -25,7 +25,7 @@ $(obj)/Image.o: vmlinux.bin $(OBJS)
> $(OBJS) $@
>
>  $(obj)/../Image.elf: $(obj)/Image.o $(obj)/boot.lds
> -   $(Q)$(LD) $(KBUILD_LDFLAGS) $(LDFLAGS_vmlinux) \
> +   $(LD) $(KBUILD_LDFLAGS) \

Can that $(Q) be retained, please?
The rest LGTM.

> -T $(obj)/boot.lds \
> --build-id=none \
> -o $@ $(obj)/Image.o

-- 
Thanks.
-- Max


Re: [PATCH 25/25] mm/xtensa: Use mm_fault_accounting()

2020-06-15 Thread Max Filippov
On Mon, Jun 15, 2020 at 3:23 PM Peter Xu  wrote:
>
> Use the new mm_fault_accounting() helper for page fault accounting.
>
> Avoid doing page fault accounting multiple times if the page fault is retried.
>
> CC: Chris Zankel 
> CC: Max Filippov 
> CC: linux-xte...@linux-xtensa.org
> Signed-off-by: Peter Xu 
> ---
>  arch/xtensa/mm/fault.c | 14 +++---
>  1 file changed, 3 insertions(+), 11 deletions(-)

Acked-by: Max Filippov 

-- 
Thanks.
-- Max


[PATCH] kernel/modules: fix build without ARCH_HAS_STRICT_MODULE_RWX

2020-06-04 Thread Max Filippov
On configurations with CONFIG_ARCH_HAS_STRICT_MODULE_RWX disabled kernel
build fails with the following message:

  kernel/module.c:3593:2: error: implicit declaration of function
  ‘module_enable_ro’;

Add empty module_enable_ro definition to fix the build.

Fixes: e6eff4376e28 ("module: Make module_enable_ro() static again")
Signed-off-by: Max Filippov 
---
 kernel/module.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/kernel/module.c b/kernel/module.c
index bca993c5f1bc..a74a29001814 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2038,6 +2038,7 @@ static void module_enable_x(const struct module *mod)
 #else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
 static void module_enable_nx(const struct module *mod) { }
 static void module_enable_x(const struct module *mod) { }
+static void module_enable_ro(const struct module *mod, bool after_init) {}
 #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */
 
 
-- 
2.20.1



[PULL 0/5] xtensa updates for v5.8

2020-06-03 Thread Max Filippov
Hi Linus,

please pull the following Xtensa architecture updates for v5.8:

The following changes since commit b9bbe6ed63b2b9f2c9ee5cbd0f2c946a2723f4ce:

  Linux 5.7-rc6 (2020-05-17 16:48:37 -0700)

are available in the Git repository at:

  git://github.com/jcmvbkbc/linux-xtensa.git tags/xtensa-20200603

for you to fetch changes up to 3ead2f97bd44a9a106572d306cb04a878c569cb2:

  xtensa: Fix spelling/grammar in comment (2020-05-25 16:11:43 -0700)


Xtensa updates for v5.8:

- fix __user annotations in asm/uaccess.h
- fix comments in entry.S


Chris Packham (1):
  xtensa: Fix spelling/grammar in comment

Max Filippov (4):
  xtensa: add missing __user annotations to __{get,put}_user_check
  xtensa: fix type conversion in __get_user_size
  xtensa: fix error paths in __get_user_{check,size}
  xtensa: add missing __user annotations to asm/uaccess.h

 arch/xtensa/include/asm/uaccess.h | 32 
 arch/xtensa/kernel/entry.S|  4 ++--
 2 files changed, 18 insertions(+), 18 deletions(-)

-- 
Thanks.
-- Max


Re: [PATCH] xtensa: Fix spelling/grammar in comment

2020-05-25 Thread Max Filippov
On Mon, May 25, 2020 at 4:04 PM Chris Packham
 wrote:
>
> Change 'excpetion' to 'exception', 'handeled' to 'handled' and 'the the'
> to 'the'.
>
> Signed-off-by: Chris Packham 
> ---
>  arch/xtensa/kernel/entry.S | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)

Thanks, applied to my xtensa tree with an additional fix:

> diff --git a/arch/xtensa/kernel/entry.S b/arch/xtensa/kernel/entry.S
> index 06fbb0a171f1..696b1e4b1a67 100644
> --- a/arch/xtensa/kernel/entry.S
> +++ b/arch/xtensa/kernel/entry.S
> @@ -959,14 +959,14 @@ ENDPROC(unrecoverable_exception)
>   * of the proper size instead.
>   *
>   * This algorithm simply backs out the register changes started by the user
> - * excpetion handler, makes it appear that we have started a window underflow
> + * exception handler, makes it appear that we have started a window underflow
>   * by rotating the window back and then setting the old window base (OWB) in
>   * the 'ps' register with the rolled back window base. The 'movsp' 
> instruction
>   * will be re-executed and this time since the next window frames is in the
>   * active AR registers it won't cause an exception.
>   *
>   * If the WindowUnderflow code gets a TLB miss the page will get mapped
> - * the the partial windeowUnderflow will be handeled in the double exception
> + * the partial windeowUnderflow will be handled in the double exception

I've changed windeowUnderflow here to WindowUnderflow.

-- 
Thanks.
-- Max


[PATCH] xtensa: fix flush_icache_user_range in noMMU configs

2020-05-25 Thread Max Filippov
flush_icache_user_range definition for xtensa is placed under
preprocessor condition that is not processed in noMMU configurations,
resulting in the following build errors:

  fs/exec.c:1065:3: error: implicit declaration of function
  ‘flush_icache_user_range’
  fs/binfmt_flat.c:857:2: error: implicit declaration of function
  ‘flush_icache_user_range’

Move definition outside conditional compilation block.

Fixes: 134d96d05d30 ("xtensa: implement flush_icache_user_range")
Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/cacheflush.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/xtensa/include/asm/cacheflush.h 
b/arch/xtensa/include/asm/cacheflush.h
index 460e666ad076..cf907e5bf2f2 100644
--- a/arch/xtensa/include/asm/cacheflush.h
+++ b/arch/xtensa/include/asm/cacheflush.h
@@ -107,8 +107,6 @@ void flush_cache_page(struct vm_area_struct*,
 #define flush_cache_page  local_flush_cache_page
 #endif
 
-#define flush_icache_user_range flush_icache_range
-
 #define local_flush_cache_all()
\
do {\
__flush_invalidate_dcache_all();\
@@ -147,6 +145,8 @@ void local_flush_cache_page(struct vm_area_struct *vma,
 
 #endif
 
+#define flush_icache_user_range flush_icache_range
+
 /* Ensure consistency between data and instruction cache. */
 #define local_flush_icache_range(start, end)   \
do {\
-- 
2.20.1



[PATCH] xtensa: fix error paths in __get_user_{check,size}

2020-05-22 Thread Max Filippov
Error paths in __get_user_check and __get_user_size directly assing 0 to
the result. It causes the following sparse warnings:

  sparse: warning: Using plain integer as NULL pointer

Convert 0 to the type pointed to by the user pointer before assigning it.

Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/uaccess.h | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/arch/xtensa/include/asm/uaccess.h 
b/arch/xtensa/include/asm/uaccess.h
index 445bb4cf3c28..0fd9b4086ae2 100644
--- a/arch/xtensa/include/asm/uaccess.h
+++ b/arch/xtensa/include/asm/uaccess.h
@@ -184,7 +184,7 @@ __asm__ __volatile__(   
\
if (access_ok(__gu_addr, size)) \
__get_user_size((x), __gu_addr, (size), __gu_err);  \
else\
-   (x) = 0;\
+   (x) = (__typeof__(*(ptr)))0;\
__gu_err;   \
 })
 
@@ -202,13 +202,15 @@ do {  
\
u64 __x;\
if (unlikely(__copy_from_user(&__x, ptr, 8))) { \
retval = -EFAULT;   \
-   (x) = 0;\
+   (x) = (__typeof__(*(ptr)))0;\
} else {\
(x) = *(__force __typeof__(*(ptr)) *)&__x;  \
}   \
break;  \
}   \
-   default: (x) = 0; __get_user_bad(); \
+   default:\
+   (x) = (__typeof__(*(ptr)))0;\
+   __get_user_bad();   \
}   \
 } while (0)
 
-- 
2.20.1



Re: [PATCH 0/3] xtensa: clean up __user annotations in asm/uaccess.h

2020-05-22 Thread Max Filippov
On Fri, May 22, 2020 at 2:58 PM Al Viro  wrote:
>
> On Fri, May 22, 2020 at 02:41:50PM -0700, Max Filippov wrote:
> Useful test:
>
> void __user *f(void __user * __user *p)
> {
> void __user *q;
> (void)get_user(q, p);
> return q;
> }

I think this change passes this test, i.e. originally reported warning
does not show up.
There's other kind of warning that it triggers:  'Using plain integer
as NULL pointer',
I'll post an updated version that fixes that as well.

-- 
Thanks.
-- Max


[PATCH 0/3] xtensa: clean up __user annotations in asm/uaccess.h

2020-05-22 Thread Max Filippov
Hello,

this series adds missing __user annotations to functions in
asm/uaccess.h. It fixes a bunch of sparse warnings for otherwise correct
code.

Max Filippov (3):
  xtensa: add missing __user annotations to __{get,put}_user_check
  xtensa: fix type conversion in __get_user_size
  xtensa: add missing __user annotations to asm/uaccess.h

 arch/xtensa/include/asm/uaccess.h | 24 +++-
 1 file changed, 11 insertions(+), 13 deletions(-)

-- 
2.20.1



[PATCH 1/3] xtensa: add missing __user annotations to __{get,put}_user_check

2020-05-22 Thread Max Filippov
__get_user_check and __put_user_check use temporary pointer but don't
mark it as __user, resulting in sparse warnings:

  sparse: warning: incorrect type in initializer (different address spaces)
  sparse:expected long *__pu_addr
  sparse:got long [noderef]  *ret

  sparse: warning: incorrect type in argument 1 (different address spaces)
  sparse:expected void [noderef]  *to
  sparse:got long *__pu_addr

Add __user annotation to temporary pointer in __get_user_check and
__put_user_check.

Reported-by: kbuild test robot 
Reported-by: Arnd Bergmann 
Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/uaccess.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/xtensa/include/asm/uaccess.h 
b/arch/xtensa/include/asm/uaccess.h
index 47b7702aaa40..754a7c96b9da 100644
--- a/arch/xtensa/include/asm/uaccess.h
+++ b/arch/xtensa/include/asm/uaccess.h
@@ -84,7 +84,7 @@ extern long __put_user_bad(void);
 #define __put_user_check(x, ptr, size) \
 ({ \
long __pu_err = -EFAULT;\
-   __typeof__(*(ptr)) *__pu_addr = (ptr);  \
+   __typeof__(*(ptr)) __user *__pu_addr = (ptr);   \
if (access_ok(__pu_addr, size)) \
__put_user_size((x), __pu_addr, (size), __pu_err);  \
__pu_err;   \
@@ -180,7 +180,7 @@ __asm__ __volatile__(   
\
 #define __get_user_check(x, ptr, size) \
 ({ \
long __gu_err = -EFAULT;\
-   const __typeof__(*(ptr)) *__gu_addr = (ptr);\
+   const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
if (access_ok(__gu_addr, size)) \
__get_user_size((x), __gu_addr, (size), __gu_err);  \
else\
-- 
2.20.1



[PATCH 3/3] xtensa: add missing __user annotations to asm/uaccess.h

2020-05-22 Thread Max Filippov
clear_user, strncpy_user, strnlen_user and their helpers operate on user
pointers, but don't have their arguments marked as __user.
Add __user annotation to userspace pointers of those functions.
Fix open-coded access check in the strnlen_user while at it.

Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/uaccess.h | 18 --
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/arch/xtensa/include/asm/uaccess.h 
b/arch/xtensa/include/asm/uaccess.h
index 445bb4cf3c28..e933ded0d07b 100644
--- a/arch/xtensa/include/asm/uaccess.h
+++ b/arch/xtensa/include/asm/uaccess.h
@@ -270,15 +270,15 @@ raw_copy_to_user(void __user *to, const void *from, 
unsigned long n)
  */
 
 static inline unsigned long
-__xtensa_clear_user(void *addr, unsigned long size)
+__xtensa_clear_user(void __user *addr, unsigned long size)
 {
-   if (!__memset(addr, 0, size))
+   if (!__memset((void __force *)addr, 0, size))
return size;
return 0;
 }
 
 static inline unsigned long
-clear_user(void *addr, unsigned long size)
+clear_user(void __user *addr, unsigned long size)
 {
if (access_ok(addr, size))
return __xtensa_clear_user(addr, size);
@@ -290,10 +290,10 @@ clear_user(void *addr, unsigned long size)
 
 #ifndef CONFIG_GENERIC_STRNCPY_FROM_USER
 
-extern long __strncpy_user(char *, const char *, long);
+extern long __strncpy_user(char *dst, const char __user *src, long count);
 
 static inline long
-strncpy_from_user(char *dst, const char *src, long count)
+strncpy_from_user(char *dst, const char __user *src, long count)
 {
if (access_ok(src, 1))
return __strncpy_user(dst, src, count);
@@ -306,13 +306,11 @@ long strncpy_from_user(char *dst, const char *src, long 
count);
 /*
  * Return the size of a string (including the ending 0!)
  */
-extern long __strnlen_user(const char *, long);
+extern long __strnlen_user(const char __user *str, long len);
 
-static inline long strnlen_user(const char *str, long len)
+static inline long strnlen_user(const char __user *str, long len)
 {
-   unsigned long top = __kernel_ok ? ~0UL : TASK_SIZE - 1;
-
-   if ((unsigned long)str > top)
+   if (!access_ok(str, 1))
return 0;
return __strnlen_user(str, len);
 }
-- 
2.20.1



[PATCH 2/3] xtensa: fix type conversion in __get_user_size

2020-05-22 Thread Max Filippov
8-byte access in __get_user_size converts pointer to temporary variable
to the type of original user pointer and then dereferences it, resulting
in the following sparse warning:

  sparse: warning: dereference of noderef expression

Instead dereference the original user pointer under the __typeof__ and
add indirection outside.

Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/uaccess.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/xtensa/include/asm/uaccess.h 
b/arch/xtensa/include/asm/uaccess.h
index 754a7c96b9da..445bb4cf3c28 100644
--- a/arch/xtensa/include/asm/uaccess.h
+++ b/arch/xtensa/include/asm/uaccess.h
@@ -204,7 +204,7 @@ do {
\
retval = -EFAULT;   \
(x) = 0;\
} else {\
-   (x) = *(__force __typeof__((ptr)))&__x; \
+   (x) = *(__force __typeof__(*(ptr)) *)&__x;  \
}   \
break;  \
}   \
-- 
2.20.1



Re: xtensa question, was Re: [PATCH V1 00/10] Remove duplicated kmap code

2020-05-01 Thread Max Filippov
On Fri, May 1, 2020 at 2:19 AM Christoph Hellwig  wrote:
>
> On Fri, May 01, 2020 at 02:02:19AM -0700, Max Filippov wrote:
> > Hi Christoph,
> >
> > On Fri, May 1, 2020 at 1:46 AM Christoph Hellwig  wrote:
> > > any idea why xtensa uses PAGE_KERNEL_EXEC instead of PAGE_KERNEL
> > > for kmap_prot?  Mapping all mapped highmem as executable seems rather
> > > dangerous.
> >
> > I sure do: to allow instruction cache flushing when writing to high user
> > pages temporarily mapped with kmap. Instruction cache management
> > opcodes that operate on virtual addresses would raise an exception if
> > the address is not executable.
>
> Seems like this should use kmap_atomic_prot with PAGE_KERNEL_EXEC just
> for that case.  Which of course didn't exist on xtensa so far, but with
> this series will.

Yeah, except it's the __access_remote_vm that does the kmap and then
calls copy_to_user_page...

-- 
Thanks.
-- Max


Re: xtensa question, was Re: [PATCH V1 00/10] Remove duplicated kmap code

2020-05-01 Thread Max Filippov
Hi Christoph,

On Fri, May 1, 2020 at 1:46 AM Christoph Hellwig  wrote:
> any idea why xtensa uses PAGE_KERNEL_EXEC instead of PAGE_KERNEL
> for kmap_prot?  Mapping all mapped highmem as executable seems rather
> dangerous.

I sure do: to allow instruction cache flushing when writing to high user
pages temporarily mapped with kmap. Instruction cache management
opcodes that operate on virtual addresses would raise an exception if
the address is not executable.

-- 
Thanks.
-- Max


[PULL 0/6] xtensa fixes for v5.4-rc4

2019-10-17 Thread Max Filippov
Hi Linus,

please pull the following batch of fixes for the Xtensa architecture:

The following changes since commit 4f5cafb5cb8471e54afdc9054d973535614f7675:

  Linux 5.4-rc3 (2019-10-13 16:37:36 -0700)

are available in the Git repository at:

  git://github.com/jcmvbkbc/linux-xtensa.git tags/xtensa-20191017

for you to fetch changes up to 775fd6bfefc66a8c33e91dd9687ed530643b954d:

  xtensa: fix change_bit in exclusive access option (2019-10-16 00:14:33 -0700)


Xtensa fixes for v5.4:

- fix {get,put}_user() for 64bit values;
- fix warning about static EXPORT_SYMBOL from modpost;
- fix PCI IO ports mapping for the virt board;
- fix pasto in change_bit for exclusive access option.


Al Viro (1):
  xtensa: fix {get,put}_user() for 64bit values

Max Filippov (5):
  xtensa: clean up assembly arguments in uaccess macros
  xtensa: fix type conversion in __get_user_[no]check
  xtensa: drop EXPORT_SYMBOL for outs*/ins*
  xtensa: virt: fix PCI IO ports mapping
  xtensa: fix change_bit in exclusive access option

 arch/xtensa/boot/dts/virt.dts |  2 +-
 arch/xtensa/include/asm/bitops.h  |  2 +-
 arch/xtensa/include/asm/uaccess.h | 94 ++-
 arch/xtensa/kernel/xtensa_ksyms.c |  7 ---
 4 files changed, 55 insertions(+), 50 deletions(-)

-- 
Thanks.
-- Max


Re: [PATCH] xtensa: implement arch_dma_coherent_to_pfn

2019-10-16 Thread Max Filippov
Hi Christoph,

On Wed, Oct 16, 2019 at 12:08 AM Christoph Hellwig  wrote:
> On Tue, Oct 15, 2019 at 02:25:26PM -0700, Max Filippov wrote:
> > Add trivial implementation for arch_dma_coherent_to_pfn.
> > This change enables communication with PCI ALSA devices through mmapped
> > buffers.
>
> This looks fine, although I'd much rather convert xtensa to the
> generic DMA remap / uncached segment support.

Thanks for the review.

> Do you want this fix for 5.4?  If so please queue it up ASAP so that
> I can do the proper thing for 5.5.  If you don't need it that urgent
> I'd rather go straight to the generic code.

There's no rush, I'll keep my version privately and will switch to the
generic version once it's available.

-- 
Thanks.
-- Max


[PATCH] xtensa: implement arch_dma_coherent_to_pfn

2019-10-15 Thread Max Filippov
Add trivial implementation for arch_dma_coherent_to_pfn.
This change enables communication with PCI ALSA devices through mmapped
buffers.

Signed-off-by: Max Filippov 
---
 arch/xtensa/Kconfig  | 1 +
 arch/xtensa/kernel/pci-dma.c | 6 ++
 2 files changed, 7 insertions(+)

diff --git a/arch/xtensa/Kconfig b/arch/xtensa/Kconfig
index bf492f9e1f75..f78e6b6f8b6f 100644
--- a/arch/xtensa/Kconfig
+++ b/arch/xtensa/Kconfig
@@ -3,6 +3,7 @@ config XTENSA
def_bool y
select ARCH_32BIT_OFF_T
select ARCH_HAS_BINFMT_FLAT if !MMU
+   select ARCH_HAS_DMA_COHERENT_TO_PFN
select ARCH_HAS_SYNC_DMA_FOR_CPU
select ARCH_HAS_SYNC_DMA_FOR_DEVICE
select ARCH_USE_QUEUED_RWLOCKS
diff --git a/arch/xtensa/kernel/pci-dma.c b/arch/xtensa/kernel/pci-dma.c
index 154979d62b73..6a114ce23084 100644
--- a/arch/xtensa/kernel/pci-dma.c
+++ b/arch/xtensa/kernel/pci-dma.c
@@ -200,3 +200,9 @@ void arch_dma_free(struct device *dev, size_t size, void 
*vaddr,
if (!dma_release_from_contiguous(dev, page, count))
__free_pages(page, get_order(size));
 }
+
+long arch_dma_coherent_to_pfn(struct device *dev, void *cpu_addr,
+ dma_addr_t dma_addr)
+{
+   return __phys_to_pfn(dma_to_phys(dev, dma_addr));
+}
-- 
2.20.1



Re: [PATCH 20/34] xtensa: Use CONFIG_PREEMPTION

2019-10-15 Thread Max Filippov
On Tue, Oct 15, 2019 at 12:18 PM Sebastian Andrzej Siewior
 wrote:
>
> From: Thomas Gleixner 
>
> CONFIG_PREEMPTION is selected by CONFIG_PREEMPT and by CONFIG_PREEMPT_RT.
> Both PREEMPT and PREEMPT_RT require the same functionality which today
> depends on CONFIG_PREEMPT.
>
> Switch the entry code over to use CONFIG_PREEMPTION. Add PREEMPT_RT
> output to die().
>
> Cc: Chris Zankel 
> Cc: Max Filippov 
> Cc: linux-xte...@linux-xtensa.org
> Signed-off-by: Thomas Gleixner 
> [bigeasy: +traps.c]
> Signed-off-by: Sebastian Andrzej Siewior 
> ---
>  arch/xtensa/kernel/entry.S | 2 +-
>  arch/xtensa/kernel/traps.c | 7 +--
>  2 files changed, 6 insertions(+), 3 deletions(-)

Acked-by: Max Filippov 

-- 
Thanks.
-- Max


[PATCH v3 3/3] xtensa: fix type conversion in __get_user_[no]check

2019-10-14 Thread Max Filippov
__get_user_[no]check uses temporary buffer of type long to store result
of __get_user_size and do sign extension on it when necessary. This
doesn't work correctly for 64-bit data. Fix it by moving temporary
buffer/sign extension logic to __get_user_asm.

Don't do assignment of __get_user_bad result to (x) as it may not always
be integer-compatible now and issue warning even when it's going to be
optimized. Instead do (x) = 0; and call __get_user_bad separately.

Zero initialize __x in __get_user_asm and use '+' constraint for its
assembly argument, so that its value is preserved in error cases. This
may add at most 1 cycle to the fast path, but saves an instruction and
two padding bytes in the fixup section for each use of this macro and
works for both misaligned store and store exception.

Signed-off-by: Max Filippov 
---
Changes v2->v3:
- rearrange result zero-initialization for error paths in __get_user_asm

Changes v1->v2:
- initialize result when access_ok check fails in __get_user_check

 arch/xtensa/include/asm/uaccess.h | 55 ---
 1 file changed, 29 insertions(+), 26 deletions(-)

diff --git a/arch/xtensa/include/asm/uaccess.h 
b/arch/xtensa/include/asm/uaccess.h
index da4d35445063..3f80386f1883 100644
--- a/arch/xtensa/include/asm/uaccess.h
+++ b/arch/xtensa/include/asm/uaccess.h
@@ -172,19 +172,19 @@ __asm__ __volatile__( 
\
 
 #define __get_user_nocheck(x, ptr, size)   \
 ({ \
-   long __gu_err, __gu_val;\
-   __get_user_size(__gu_val, (ptr), (size), __gu_err); \
-   (x) = (__force __typeof__(*(ptr)))__gu_val; \
+   long __gu_err;  \
+   __get_user_size((x), (ptr), (size), __gu_err);  \
__gu_err;   \
 })
 
 #define __get_user_check(x, ptr, size) \
 ({ \
-   long __gu_err = -EFAULT, __gu_val = 0;  \
+   long __gu_err = -EFAULT;\
const __typeof__(*(ptr)) *__gu_addr = (ptr);\
-   if (access_ok(__gu_addr, size)) \
-   __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
-   (x) = (__force __typeof__(*(ptr)))__gu_val; \
+   if (access_ok(__gu_addr, size)) \
+   __get_user_size((x), __gu_addr, (size), __gu_err);  \
+   else\
+   (x) = 0;\
__gu_err;   \
 })
 
@@ -208,7 +208,7 @@ do {
\
}   \
break;  \
}   \
-   default: (x) = __get_user_bad();\
+   default: (x) = 0; __get_user_bad(); \
}   \
 } while (0)
 
@@ -218,24 +218,27 @@ do {  
\
  * __check_align_* macros still work.
  */
 #define __get_user_asm(x_, addr_, err_, align, insn, cb) \
-__asm__ __volatile__(  \
-   __check_align_##align   \
-   "1: "insn"  %[x], %[addr], 0\n" \
-   "2: \n" \
-   "   .section  .fixup,\"ax\" \n" \
-   "   .align 4\n" \
-   "   .literal_position   \n" \
-   "5: \n" \
-   "   movi   %[tmp], 2b   \n" \
-   "   movi   %[x], 0  \n" \
-   "   movi   %[err], %[efault]\n" \
-   "   jx %[tmp]   \n" \
-   "   .previous   \n" \
-   "   .section  __ex_table,\"a\"  \n" \
-   "   .long   1b, 5b  \n" \
-   "   .previous"  \
-   :[err] "+r"(err_), [tmp] "=r"(cb), [x] "=r"(x_)\
-   :[addr] "r"(addr_), [efault] "i"(-EFAULT))
+do {   \
+   u32 __x = 0;\
+   __asm__ __volatile__(   

[PATCH v3 0/3] xtensa: fix {get,put}_user() for 64bit values

2019-10-14 Thread Max Filippov
Hello,

this series fixes return value, out-of-bound stack access and value
truncation in xtensa implementation of {get,put}_user() for 64bit
values. It also cleans up naming of assembly parameters in
__{get,put}_user_asm and __check_align_{1,2,4}.

Changes v2->v3:
- fix assembly argument constraint for error code
- rearrange result zero-initialization for error paths in __get_user_asm

Changes v1->v2:
- initialize result when access_ok check fails in __get_user_check
- initialize result in __get_user_asm for unaligned access

Al Viro (1):
  xtensa: fix {get,put}_user() for 64bit values

Max Filippov (2):
  xtensa: clean up assembly arguments in uaccess macros
  xtensa: fix type conversion in __get_user_[no]check

 arch/xtensa/include/asm/uaccess.h | 94 +--
 1 file changed, 53 insertions(+), 41 deletions(-)

-- 
2.20.1



[PATCH v3 1/3] xtensa: fix {get,put}_user() for 64bit values

2019-10-14 Thread Max Filippov
From: Al Viro 

First of all, on short copies __copy_{to,from}_user() return the amount
of bytes left uncopied, *not* -EFAULT.  get_user() and put_user() are
expected to return -EFAULT on failure.

Another problem is get_user(v32, (__u64 __user *)p); that should
fetch 64bit value and the assign it to v32, truncating it in process.
Current code, OTOH, reads 8 bytes of data and stores them at the
address of v32, stomping on the 4 bytes that follow v32 itself.

Signed-off-by: Al Viro 
Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/uaccess.h | 13 +++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/arch/xtensa/include/asm/uaccess.h 
b/arch/xtensa/include/asm/uaccess.h
index 6792928ba84a..f568c00392ec 100644
--- a/arch/xtensa/include/asm/uaccess.h
+++ b/arch/xtensa/include/asm/uaccess.h
@@ -100,7 +100,7 @@ do {
\
case 4: __put_user_asm(x, ptr, retval, 4, "s32i", __cb); break; \
case 8: {   \
 __typeof__(*ptr) __v64 = x;\
-retval = __copy_to_user(ptr, &__v64, 8);   \
+retval = __copy_to_user(ptr, &__v64, 8) ? -EFAULT : 0; 
\
 break; \
}   \
default: __put_user_bad();  \
@@ -198,7 +198,16 @@ do {   
\
case 1: __get_user_asm(x, ptr, retval, 1, "l8ui", __cb);  break;\
case 2: __get_user_asm(x, ptr, retval, 2, "l16ui", __cb); break;\
case 4: __get_user_asm(x, ptr, retval, 4, "l32i", __cb);  break;\
-   case 8: retval = __copy_from_user(, ptr, 8);break;\
+   case 8: {   \
+   u64 __x;\
+   if (unlikely(__copy_from_user(&__x, ptr, 8))) { \
+   retval = -EFAULT;   \
+   (x) = 0;\
+   } else {\
+   (x) = *(__force __typeof__((ptr)))&__x; \
+   }   \
+   break;  \
+   }   \
default: (x) = __get_user_bad();\
}   \
 } while (0)
-- 
2.20.1



[PATCH v3 2/3] xtensa: clean up assembly arguments in uaccess macros

2019-10-14 Thread Max Filippov
Numeric assembly arguments are hard to understand and assembly code that
uses them is hard to modify. Use named arguments in __check_align_*,
__get_user_asm and __put_user_asm. Modify macro parameter names so that
they don't affect argument names. Use '+' constraint for the [err]
argument instead of having it as both input and output.

Signed-off-by: Max Filippov 
---
Changes v2->v3:
- fix assembly argument constraint for error code

 arch/xtensa/include/asm/uaccess.h | 42 +++
 1 file changed, 21 insertions(+), 21 deletions(-)

diff --git a/arch/xtensa/include/asm/uaccess.h 
b/arch/xtensa/include/asm/uaccess.h
index f568c00392ec..da4d35445063 100644
--- a/arch/xtensa/include/asm/uaccess.h
+++ b/arch/xtensa/include/asm/uaccess.h
@@ -132,14 +132,14 @@ do {  
\
 #define __check_align_1  ""
 
 #define __check_align_2\
-   "   _bbci.l %3,  0, 1f  \n" \
-   "   movi%0, %4  \n" \
+   "   _bbci.l %[addr], 0, 1f  \n" \
+   "   movi%[err], %[efault]   \n" \
"   _j  2f  \n"
 
 #define __check_align_4\
-   "   _bbsi.l %3,  0, 0f  \n" \
-   "   _bbci.l %3,  1, 1f  \n" \
-   "0: movi%0, %4  \n" \
+   "   _bbsi.l %[addr], 0, 0f  \n" \
+   "   _bbci.l %[addr], 1, 1f  \n" \
+   "0: movi%[err], %[efault]   \n" \
"   _j  2f  \n"
 
 
@@ -151,24 +151,24 @@ do {  
\
  * WARNING: If you modify this macro at all, verify that the
  * __check_align_* macros still work.
  */
-#define __put_user_asm(x, addr, err, align, insn, cb)  \
+#define __put_user_asm(x_, addr_, err_, align, insn, cb)\
 __asm__ __volatile__(  \
__check_align_##align   \
-   "1: "insn"  %2, %3, 0   \n" \
+   "1: "insn"  %[x], %[addr], 0\n" \
"2: \n" \
"   .section  .fixup,\"ax\" \n" \
"   .align 4\n" \
"   .literal_position   \n" \
"5: \n" \
-   "   movi   %1, 2b   \n" \
-   "   movi   %0, %4   \n" \
-   "   jx %1   \n" \
+   "   movi   %[tmp], 2b   \n" \
+   "   movi   %[err], %[efault]\n" \
+   "   jx %[tmp]   \n" \
"   .previous   \n" \
"   .section  __ex_table,\"a\"  \n" \
"   .long   1b, 5b  \n" \
"   .previous"  \
-   :"=r" (err), "=r" (cb)  \
-   :"r" ((int)(x)), "r" (addr), "i" (-EFAULT), "0" (err))
+   :[err] "+r"(err_), [tmp] "=r"(cb)   \
+   :[x] "r"(x_), [addr] "r"(addr_), [efault] "i"(-EFAULT))
 
 #define __get_user_nocheck(x, ptr, size)   \
 ({ \
@@ -217,25 +217,25 @@ do {  
\
  * WARNING: If you modify this macro at all, verify that the
  * __check_align_* macros still work.
  */
-#define __get_user_asm(x, addr, err, align, insn, cb) \
-__asm__ __volatile__(  \
+#define __get_user_asm(x_, addr_, err_, align, insn, cb) \
+__asm__ __volatile__(  \
__check_align_##align   \
-   "1: "insn"  %2, %3, 0   \n" \
+   "1: "insn"  %[x], %[addr], 0\n" \
"2: \n" \
"   .section  .fixup,\"ax\" \n" \
"   .align 4\n" \
"   .literal_position   \n" \
"5: \n" \
-   "   movi   %1, 2b   \n" \
-   "   movi   %2, 0\n" \
-   "   movi   %0, %4   \n" \
-   "   jx %1 

[PATCH v2 4/4] xtensa: initialize result in __get_user_asm for unaligned access

2019-10-11 Thread Max Filippov
__get_user_asm macro leaves result register uninitialized when alignment
check fails. Add 'insn' parameter to __check_align_{1,2,4} and pass an
instruction that initializes result register from __get_user_asm.

Signed-off-by: Max Filippov 
---
 arch/xtensa/include/asm/uaccess.h | 12 +++-
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/arch/xtensa/include/asm/uaccess.h 
b/arch/xtensa/include/asm/uaccess.h
index 43e923678dfb..d8cff972f3cf 100644
--- a/arch/xtensa/include/asm/uaccess.h
+++ b/arch/xtensa/include/asm/uaccess.h
@@ -129,17 +129,19 @@ do {  
\
  * sync.
  */
 
-#define __check_align_1  ""
+#define __check_align_1(insn)  ""
 
-#define __check_align_2\
+#define __check_align_2(insn)  \
"   _bbci.l %[addr], 0, 1f  \n" \
"   movi%[err], %[efault]   \n" \
+   "   "insn"  \n" \
"   _j  2f  \n"
 
-#define __check_align_4\
+#define __check_align_4(insn)  \
"   _bbsi.l %[addr], 0, 0f  \n" \
"   _bbci.l %[addr], 1, 1f  \n" \
"0: movi%[err], %[efault]   \n" \
+   "   "insn"  \n" \
"   _j  2f  \n"
 
 
@@ -153,7 +155,7 @@ do {
\
  */
 #define __put_user_asm(x_, addr_, err_, align, insn, cb)\
 __asm__ __volatile__(  \
-   __check_align_##align   \
+   __check_align_##align("")   \
"1: "insn"  %[x], %[addr], 0\n" \
"2: \n" \
"   .section  .fixup,\"ax\" \n" \
@@ -221,7 +223,7 @@ do {
\
 do {   \
u32 __x;\
__asm__ __volatile__(   \
-   __check_align_##align   \
+   __check_align_##align("movi %[x], 0")   \
"1: "insn"  %[x], %[addr], 0\n" \
"2: \n" \
"   .section  .fixup,\"ax\" \n" \
-- 
2.20.1



[PATCH v2 3/4] xtensa: fix type conversion in __get_user_[no]check

2019-10-11 Thread Max Filippov
__get_user_[no]check uses temporary buffer of type long to store result
of __get_user_size and do sign extension on it when necessary. This
doesn't work correctly for 64-bit data. Fix it by moving temporary
buffer/sign extension logic to __get_user_asm.
Don't do assignment of __get_user_bad result to (x) as it may not always
be integer-compatible now and issue warning even when it's going to be
optimized. Instead do (x) = 0; and call __get_user_bad separately.

Signed-off-by: Max Filippov 
---
Changes v1->v2:
- initialize result when access_ok check fails in __get_user_check

 arch/xtensa/include/asm/uaccess.h | 56 +--
 1 file changed, 30 insertions(+), 26 deletions(-)

diff --git a/arch/xtensa/include/asm/uaccess.h 
b/arch/xtensa/include/asm/uaccess.h
index aca510707189..43e923678dfb 100644
--- a/arch/xtensa/include/asm/uaccess.h
+++ b/arch/xtensa/include/asm/uaccess.h
@@ -172,19 +172,19 @@ __asm__ __volatile__( 
\
 
 #define __get_user_nocheck(x, ptr, size)   \
 ({ \
-   long __gu_err, __gu_val;\
-   __get_user_size(__gu_val, (ptr), (size), __gu_err); \
-   (x) = (__force __typeof__(*(ptr)))__gu_val; \
+   long __gu_err;  \
+   __get_user_size((x), (ptr), (size), __gu_err);  \
__gu_err;   \
 })
 
 #define __get_user_check(x, ptr, size) \
 ({ \
-   long __gu_err = -EFAULT, __gu_val = 0;  \
+   long __gu_err = -EFAULT;\
const __typeof__(*(ptr)) *__gu_addr = (ptr);\
-   if (access_ok(__gu_addr, size)) \
-   __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
-   (x) = (__force __typeof__(*(ptr)))__gu_val; \
+   if (access_ok(__gu_addr, size)) \
+   __get_user_size((x), __gu_addr, (size), __gu_err);  \
+   else\
+   (x) = 0;\
__gu_err;   \
 })
 
@@ -208,7 +208,7 @@ do {
\
}   \
break;  \
}   \
-   default: (x) = __get_user_bad();\
+   default: (x) = 0; __get_user_bad(); \
}   \
 } while (0)
 
@@ -218,24 +218,28 @@ do {  
\
  * __check_align_* macros still work.
  */
 #define __get_user_asm(x_, addr_, err_, align, insn, cb) \
-__asm__ __volatile__(  \
-   __check_align_##align   \
-   "1: "insn"  %[x], %[addr], 0\n" \
-   "2: \n" \
-   "   .section  .fixup,\"ax\" \n" \
-   "   .align 4\n" \
-   "   .literal_position   \n" \
-   "5: \n" \
-   "   movi   %[tmp], 2b   \n" \
-   "   movi   %[x], 0  \n" \
-   "   movi   %[err], %[efault]\n" \
-   "   jx %[tmp]   \n" \
-   "   .previous   \n" \
-   "   .section  __ex_table,\"a\"  \n" \
-   "   .long   1b, 5b  \n" \
-   "   .previous"  \
-   :[err] "=r"(err_), [tmp] "=r"(cb), [x] "=r"(x_)\
-   :[addr] "r"(addr_), [efault] "i"(-EFAULT))
+do {   \
+   u32 __x;\
+   __asm__ __volatile__(   \
+   __check_align_##align   \
+   "1: "insn"  %[x], %[addr], 0\n" \
+   "2: \n" \
+   "   .section  .fixup,\"ax\" \n" \
+   "   .align 4\n" \
+   "   .literal_position   \n" \
+   "5:  

[PATCH v2 0/4] xtensa: fix {get,put}_user() for 64bit values

2019-10-11 Thread Max Filippov
Hello,

this series fixes return value, out-of-bound stack access and value
truncation in xtensa implementation of {get,put}_user() for 64bit
values. It also cleans up naming of assembly parameters in
__{get,put}_user_asm and __check_align_{1,2,4}.

Changes v1->v2:
- initialize result when access_ok check fails in __get_user_check
- initialize result in __get_user_asm for unaligned access

Al Viro (1):
  xtensa: fix {get,put}_user() for 64bit values

Max Filippov (3):
  xtensa: clean up assembly arguments in uaccess macros
  xtensa: fix type conversion in __get_user_[no]check
  xtensa: initialize result in __get_user_asm for unaligned access

 arch/xtensa/include/asm/uaccess.h | 105 +-
 1 file changed, 60 insertions(+), 45 deletions(-)

-- 
2.20.1



  1   2   3   4   5   6   7   8   9   >