[PATCH v4 8/8] x86/module: enable ROX caches for module text

2024-10-07 Thread Mike Rapoport
From: "Mike Rapoport (Microsoft)" 

Enable execmem's cache of PMD_SIZE'ed pages mapped as ROX for module
text allocations.

Signed-off-by: Mike Rapoport (Microsoft) 
---
 arch/x86/mm/init.c | 26 +-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c
index eb503f53c319..a0ec99fb9385 100644
--- a/arch/x86/mm/init.c
+++ b/arch/x86/mm/init.c
@@ -1053,6 +1053,15 @@ unsigned long arch_max_swapfile_size(void)
 #ifdef CONFIG_EXECMEM
 static struct execmem_info execmem_info __ro_after_init;
 
+static void execmem_fill_trapping_insns(void *ptr, size_t size, bool writeable)
+{
+   /* fill memory with INT3 instructions */
+   if (writeable)
+   memset(ptr, INT3_INSN_OPCODE, size);
+   else
+   text_poke_set(ptr, INT3_INSN_OPCODE, size);
+}
+
 struct execmem_info __init *execmem_arch_setup(void)
 {
unsigned long start, offset = 0;
@@ -1063,8 +1072,23 @@ struct execmem_info __init *execmem_arch_setup(void)
start = MODULES_VADDR + offset;
 
execmem_info = (struct execmem_info){
+   .fill_trapping_insns = execmem_fill_trapping_insns,
.ranges = {
-   [EXECMEM_DEFAULT] = {
+   [EXECMEM_MODULE_TEXT] = {
+   .flags  = EXECMEM_KASAN_SHADOW | 
EXECMEM_ROX_CACHE,
+   .start  = start,
+   .end= MODULES_END,
+   .pgprot = PAGE_KERNEL_ROX,
+   .alignment = MODULE_ALIGN,
+   },
+   [EXECMEM_KPROBES ... EXECMEM_BPF] = {
+   .flags  = EXECMEM_KASAN_SHADOW,
+   .start  = start,
+   .end= MODULES_END,
+   .pgprot = PAGE_KERNEL,
+   .alignment = MODULE_ALIGN,
+   },
+   [EXECMEM_MODULE_DATA] = {
.flags  = EXECMEM_KASAN_SHADOW,
.start  = start,
.end= MODULES_END,
-- 
2.43.0


___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc


[PATCH v4 7/8] execmem: add support for cache of large ROX pages

2024-10-07 Thread Mike Rapoport
From: "Mike Rapoport (Microsoft)" 

Using large pages to map text areas reduces iTLB pressure and improves
performance.

Extend execmem_alloc() with an ability to use huge pages with ROX
permissions as a cache for smaller allocations.

To populate the cache, a writable large page is allocated from vmalloc with
VM_ALLOW_HUGE_VMAP, filled with invalid instructions and then remapped as
ROX.

Portions of that large page are handed out to execmem_alloc() callers
without any changes to the permissions.

When the memory is freed with execmem_free() it is invalidated again so
that it won't contain stale instructions.

The cache is enabled when an architecture sets EXECMEM_ROX_CACHE flag in
definition of an execmem_range.

Signed-off-by: Mike Rapoport (Microsoft) 
---
 include/linux/execmem.h |   2 +
 mm/execmem.c| 317 +++-
 mm/internal.h   |   1 +
 mm/vmalloc.c|   5 +
 4 files changed, 320 insertions(+), 5 deletions(-)

diff --git a/include/linux/execmem.h b/include/linux/execmem.h
index dfdf19f8a5e8..7436aa547818 100644
--- a/include/linux/execmem.h
+++ b/include/linux/execmem.h
@@ -77,12 +77,14 @@ struct execmem_range {
 
 /**
  * struct execmem_info - architecture parameters for code allocations
+ * @fill_trapping_insns: set memory to contain instructions that will trap
  * @ranges: array of parameter sets defining architecture specific
  * parameters for executable memory allocations. The ranges that are not
  * explicitly initialized by an architecture use parameters defined for
  * @EXECMEM_DEFAULT.
  */
 struct execmem_info {
+   void (*fill_trapping_insns)(void *ptr, size_t size, bool writable);
struct execmem_rangeranges[EXECMEM_TYPE_MAX];
 };
 
diff --git a/mm/execmem.c b/mm/execmem.c
index 0f6691e9ffe6..9c6ff9687860 100644
--- a/mm/execmem.c
+++ b/mm/execmem.c
@@ -7,28 +7,109 @@
  */
 
 #include 
+#include 
 #include 
 #include 
+#include 
+#include 
 #include 
 #include 
 
+#include 
+
+#include "internal.h"
+
 static struct execmem_info *execmem_info __ro_after_init;
 static struct execmem_info default_execmem_info __ro_after_init;
 
-static void *__execmem_alloc(struct execmem_range *range, size_t size)
+#ifdef CONFIG_MMU
+struct execmem_cache {
+   struct mutex mutex;
+   struct maple_tree busy_areas;
+   struct maple_tree free_areas;
+};
+
+static struct execmem_cache execmem_cache = {
+   .mutex = __MUTEX_INITIALIZER(execmem_cache.mutex),
+   .busy_areas = MTREE_INIT_EXT(busy_areas, MT_FLAGS_LOCK_EXTERN,
+execmem_cache.mutex),
+   .free_areas = MTREE_INIT_EXT(free_areas, MT_FLAGS_LOCK_EXTERN,
+execmem_cache.mutex),
+};
+
+static inline unsigned long mas_range_len(struct ma_state *mas)
+{
+   return mas->last - mas->index + 1;
+}
+
+static int execmem_set_direct_map_valid(struct vm_struct *vm, bool valid)
+{
+   unsigned int nr = (1 << get_vm_area_page_order(vm));
+   unsigned int updated = 0;
+   int err = 0;
+
+   for (int i = 0; i < vm->nr_pages; i += nr) {
+   err = set_direct_map_valid_noflush(vm->pages[i], nr, valid);
+   if (err)
+   goto err_restore;
+   updated += nr;
+   }
+
+   return 0;
+
+err_restore:
+   for (int i = 0; i < updated; i += nr)
+   set_direct_map_valid_noflush(vm->pages[i], nr, !valid);
+
+   return err;
+}
+
+static void execmem_cache_clean(struct work_struct *work)
+{
+   struct maple_tree *free_areas = &execmem_cache.free_areas;
+   struct mutex *mutex = &execmem_cache.mutex;
+   MA_STATE(mas, free_areas, 0, ULONG_MAX);
+   void *area;
+
+   mutex_lock(mutex);
+   mas_for_each(&mas, area, ULONG_MAX) {
+   size_t size;
+
+   if (!area)
+   continue;
+
+   size = mas_range_len(&mas);
+
+   if (IS_ALIGNED(size, PMD_SIZE) &&
+   IS_ALIGNED(mas.index, PMD_SIZE)) {
+   struct vm_struct *vm = find_vm_area(area);
+
+   execmem_set_direct_map_valid(vm, true);
+   mas_store_gfp(&mas, NULL, GFP_KERNEL);
+   vfree(area);
+   }
+   }
+   mutex_unlock(mutex);
+}
+
+static DECLARE_WORK(execmem_cache_clean_work, execmem_cache_clean);
+
+static void *execmem_vmalloc(struct execmem_range *range, size_t size,
+pgprot_t pgprot, unsigned long vm_flags)
 {
bool kasan = range->flags & EXECMEM_KASAN_SHADOW;
-   unsigned long vm_flags  = VM_FLUSH_RESET_PERMS;
gfp_t gfp_flags = GFP_KERNEL | __GFP_NOWARN;
+   unsigned int align = range->alignment;
unsigned long start = range->start;
unsigned long end = range->end;
-   unsigned int align = range->alignment;
-   pgprot_t pgprot = range->pgprot;
void *p;
 
if (kasan)
   

Re: [PATCH] arc: rename aux.h to arc_aux.h

2024-10-07 Thread Vineet Gupta
On 10/7/24 05:04, Szőke Benjamin wrote:
> 2024. 10. 05. 21:40 keltezéssel, Vineet Gupta írta:
>> On 10/5/24 06:41, Szőke Benjamin wrote:
>>> 2024. 09. 25. 10:48 keltezéssel, Shahab Vahedi írta:
 +Add Vineet (with the right address)
 +Update Alexey's address to business instance

 September 23, "Szőke Benjamin" wrote:

> Long time ago i submitted a patch, when it will be reviewed? Is there any 
> activities here in the synopsys/ARC kernel list?
>
>
> https://lore.kernel.org/linux-snps-arc/20240520142647.70440-1-egyszer...@freemail.hu/T/#u
>
> https://patchwork.ozlabs.org/project/linux-arc/patch/20240520142647.70440-1-egyszer...@freemail.hu/
 They look good to me. Thank you Szőke!

 Reviewed-by: Shahab Vahedi 

 Cheers,
 Shahab
>>> Please update the e-mail addresses in the MAINTAINERS list of Linux kernel.
>>> https://github.com/torvalds/linux/blob/master/MAINTAINERS#L22379
>>>
>>> Which repo is to use for make and send patches? In the MAINTAINERS file of 
>>> Linux
>>> the following was mentioned, but since 2023 there are no any new commit
>> The eBPF jit went into ARC port 2024, and it can't land via the ARC tree.
>> Please try searching properly before putting random stuff out there.
>>
>> BTW what exactly is your rant about. The file rename patch - to allow 
>> building on windows ? Seriously.
>> Do you have a real patch and are trying to solve a real problem.
>>
>>
>>> in it ans still has no branch for latest Linux kernels.
>>> https://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc.git
>> That is the tree and stuff goes via it.
>>
>> -Vineet
> Goal is to improve Linux kernel codebase to able to develop in any platform 
> via 
> VS code (IntelliSense + remote compiler or local cross-compiler for ARM SoCs).
>
> In the Linux git repo tree there are two driver sources where the aux 
> filename 
> is exist for this improvement, it need to be solved with renaming.
>
> In drm/nouveau/i2c driver, aux files was already solved, it is merged.
> https://gitlab.freedesktop.org/drm/misc/kernel/-/commit/231bb9b4c42398db3114c087ba39ba00c4b7ac2c
>
> Only arc sources need to be fixed finally.
> https://patchwork.ozlabs.org/project/linux-arc/patch/20240520142647.70440-1-egyszer...@freemail.hu/
>
> It is not a critical bugfix for your company's daily work. But it is an open 
> source operating system, you have to care about this improvment also, if you 
> are 
> a maintainer.
>
>
> Why is it important?
> Because young developers will never be willing to join and contributing in 
> Linux 
> kernel in the future if there are no any high-quality easy-to-use IDE for 
> Linux 
> kernel coding.

Fair enough.
Added to for-curr - will be sent Linus' once I have another pending patch go 
thru some testing.

Thx for your patience.
-Vineet

___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc


Re: [PATCH] arc: rename aux.h to arc_aux.h

2024-10-07 Thread Szőke Benjamin

2024. 10. 05. 21:40 keltezéssel, Vineet Gupta írta:

On 10/5/24 06:41, Szőke Benjamin wrote:

2024. 09. 25. 10:48 keltezéssel, Shahab Vahedi írta:

+Add Vineet (with the right address)
+Update Alexey's address to business instance

September 23, "Szőke Benjamin" wrote:


Long time ago i submitted a patch, when it will be reviewed? Is there any 
activities here in the synopsys/ARC kernel list?

   
https://lore.kernel.org/linux-snps-arc/20240520142647.70440-1-egyszer...@freemail.hu/T/#u
   
https://patchwork.ozlabs.org/project/linux-arc/patch/20240520142647.70440-1-egyszer...@freemail.hu/

They look good to me. Thank you Szőke!

Reviewed-by: Shahab Vahedi 

Cheers,
Shahab

Please update the e-mail addresses in the MAINTAINERS list of Linux kernel.
https://github.com/torvalds/linux/blob/master/MAINTAINERS#L22379

Which repo is to use for make and send patches? In the MAINTAINERS file of Linux
the following was mentioned, but since 2023 there are no any new commit


The eBPF jit went into ARC port 2024, and it can't land via the ARC tree.
Please try searching properly before putting random stuff out there.

BTW what exactly is your rant about. The file rename patch - to allow building 
on windows ? Seriously.
Do you have a real patch and are trying to solve a real problem.



in it ans still has no branch for latest Linux kernels.
https://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc.git


That is the tree and stuff goes via it.

-Vineet


Goal is to improve Linux kernel codebase to able to develop in any platform via 
VS code (IntelliSense + remote compiler or local cross-compiler for ARM SoCs).


In the Linux git repo tree there are two driver sources where the aux filename 
is exist for this improvement, it need to be solved with renaming.


In drm/nouveau/i2c driver, aux files was already solved, it is merged.
https://gitlab.freedesktop.org/drm/misc/kernel/-/commit/231bb9b4c42398db3114c087ba39ba00c4b7ac2c

Only arc sources need to be fixed finally.
https://patchwork.ozlabs.org/project/linux-arc/patch/20240520142647.70440-1-egyszer...@freemail.hu/

It is not a critical bugfix for your company's daily work. But it is an open 
source operating system, you have to care about this improvment also, if you are 
a maintainer.



Why is it important?
Because young developers will never be willing to join and contributing in Linux 
kernel in the future if there are no any high-quality easy-to-use IDE for Linux 
kernel coding.


___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc


Re: [PATCH v4 5/8] arch: introduce set_direct_map_valid_noflush()

2024-10-07 Thread Huacai Chen
Hi, Mike,

On Mon, Oct 7, 2024 at 2:30 PM Mike Rapoport  wrote:
>
> From: "Mike Rapoport (Microsoft)" 
>
> Add an API that will allow updates of the direct/linear map for a set of
> physically contiguous pages.
>
> It will be used in the following patches.
>
> Signed-off-by: Mike Rapoport (Microsoft) 
> ---
>  arch/arm64/include/asm/set_memory.h |  1 +
>  arch/arm64/mm/pageattr.c| 10 ++
>  arch/loongarch/include/asm/set_memory.h |  1 +
>  arch/loongarch/mm/pageattr.c| 21 +
>  arch/riscv/include/asm/set_memory.h |  1 +
>  arch/riscv/mm/pageattr.c| 15 +++
>  arch/s390/include/asm/set_memory.h  |  1 +
>  arch/s390/mm/pageattr.c | 11 +++
>  arch/x86/include/asm/set_memory.h   |  1 +
>  arch/x86/mm/pat/set_memory.c|  8 
>  include/linux/set_memory.h  |  6 ++
>  11 files changed, 76 insertions(+)
>
> diff --git a/arch/arm64/include/asm/set_memory.h 
> b/arch/arm64/include/asm/set_memory.h
> index 917761feeffd..98088c043606 100644
> --- a/arch/arm64/include/asm/set_memory.h
> +++ b/arch/arm64/include/asm/set_memory.h
> @@ -13,6 +13,7 @@ int set_memory_valid(unsigned long addr, int numpages, int 
> enable);
>
>  int set_direct_map_invalid_noflush(struct page *page);
>  int set_direct_map_default_noflush(struct page *page);
> +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid);
>  bool kernel_page_present(struct page *page);
>
>  #endif /* _ASM_ARM64_SET_MEMORY_H */
> diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c
> index 0e270a1c51e6..01225900293a 100644
> --- a/arch/arm64/mm/pageattr.c
> +++ b/arch/arm64/mm/pageattr.c
> @@ -192,6 +192,16 @@ int set_direct_map_default_noflush(struct page *page)
>PAGE_SIZE, change_page_range, &data);
>  }
>
> +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid)
> +{
> +   unsigned long addr = (unsigned long)page_address(page);
> +
> +   if (!can_set_direct_map())
> +   return 0;
> +
> +   return set_memory_valid(addr, nr, valid);
> +}
> +
>  #ifdef CONFIG_DEBUG_PAGEALLOC
>  void __kernel_map_pages(struct page *page, int numpages, int enable)
>  {
> diff --git a/arch/loongarch/include/asm/set_memory.h 
> b/arch/loongarch/include/asm/set_memory.h
> index d70505b6676c..55dfaefd02c8 100644
> --- a/arch/loongarch/include/asm/set_memory.h
> +++ b/arch/loongarch/include/asm/set_memory.h
> @@ -17,5 +17,6 @@ int set_memory_rw(unsigned long addr, int numpages);
>  bool kernel_page_present(struct page *page);
>  int set_direct_map_default_noflush(struct page *page);
>  int set_direct_map_invalid_noflush(struct page *page);
> +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid);
>
>  #endif /* _ASM_LOONGARCH_SET_MEMORY_H */
> diff --git a/arch/loongarch/mm/pageattr.c b/arch/loongarch/mm/pageattr.c
> index ffd8d76021d4..f14b40c968b4 100644
> --- a/arch/loongarch/mm/pageattr.c
> +++ b/arch/loongarch/mm/pageattr.c
> @@ -216,3 +216,24 @@ int set_direct_map_invalid_noflush(struct page *page)
>
> return __set_memory(addr, 1, __pgprot(0), __pgprot(_PAGE_PRESENT | 
> _PAGE_VALID));
>  }
> +
> +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid)
> +{
> +   unsigned long addr = (unsigned long)page_address(page);
> +   pgprot_t set, clear;
> +
> +   return __set_memory((unsigned long)page_address(page), nr, set, 
> clear);
This line should be removed.

Huacai

> +
> +   if (addr < vm_map_base)
> +   return 0;
> +
> +   if (valid) {
> +   set = PAGE_KERNEL;
> +   clear = __pgprot(0);
> +   } else {
> +   set = __pgprot(0);
> +   clear = __pgprot(_PAGE_PRESENT | _PAGE_VALID);
> +   }
> +
> +   return __set_memory(addr, 1, set, clear);
> +}
> diff --git a/arch/riscv/include/asm/set_memory.h 
> b/arch/riscv/include/asm/set_memory.h
> index ab92fc84e1fc..ea263d3683ef 100644
> --- a/arch/riscv/include/asm/set_memory.h
> +++ b/arch/riscv/include/asm/set_memory.h
> @@ -42,6 +42,7 @@ static inline int set_kernel_memory(char *startp, char 
> *endp,
>
>  int set_direct_map_invalid_noflush(struct page *page);
>  int set_direct_map_default_noflush(struct page *page);
> +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid);
>  bool kernel_page_present(struct page *page);
>
>  #endif /* __ASSEMBLY__ */
> diff --git a/arch/riscv/mm/pageattr.c b/arch/riscv/mm/pageattr.c
> index 271d01a5ba4d..d815448758a1 100644
> --- a/arch/riscv/mm/pageattr.c
> +++ b/arch/riscv/mm/pageattr.c
> @@ -386,6 +386,21 @@ int set_direct_map_default_noflush(struct page *page)
> PAGE_KERNEL, __pgprot(_PAGE_EXEC));
>  }
>
> +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid)
> +{
> +   pgprot_t set, clear;
> +
> +   if (valid) {
> +

Re: [PATCH v4 5/8] arch: introduce set_direct_map_valid_noflush()

2024-10-07 Thread Mike Rapoport
Hi Huacai,

On Tue, Oct 08, 2024 at 10:11:25AM +0800, Huacai Chen wrote:
> Hi, Mike,
> 
> On Mon, Oct 7, 2024 at 2:30 PM Mike Rapoport  wrote:
> >
> > From: "Mike Rapoport (Microsoft)" 
> >
> > Add an API that will allow updates of the direct/linear map for a set of
> > physically contiguous pages.
> >
> > It will be used in the following patches.
> >
> > Signed-off-by: Mike Rapoport (Microsoft) 
> > ---
> >  arch/arm64/include/asm/set_memory.h |  1 +
> >  arch/arm64/mm/pageattr.c| 10 ++
> >  arch/loongarch/include/asm/set_memory.h |  1 +
> >  arch/loongarch/mm/pageattr.c| 21 +
> >  arch/riscv/include/asm/set_memory.h |  1 +
> >  arch/riscv/mm/pageattr.c| 15 +++
> >  arch/s390/include/asm/set_memory.h  |  1 +
> >  arch/s390/mm/pageattr.c | 11 +++
> >  arch/x86/include/asm/set_memory.h   |  1 +
> >  arch/x86/mm/pat/set_memory.c|  8 
> >  include/linux/set_memory.h  |  6 ++
> >  11 files changed, 76 insertions(+)
> >
> > diff --git a/arch/loongarch/include/asm/set_memory.h 
> > b/arch/loongarch/include/asm/set_memory.h
> > index d70505b6676c..55dfaefd02c8 100644
> > --- a/arch/loongarch/include/asm/set_memory.h
> > +++ b/arch/loongarch/include/asm/set_memory.h
> > @@ -17,5 +17,6 @@ int set_memory_rw(unsigned long addr, int numpages);
> >  bool kernel_page_present(struct page *page);
> >  int set_direct_map_default_noflush(struct page *page);
> >  int set_direct_map_invalid_noflush(struct page *page);
> > +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool 
> > valid);
> >
> >  #endif /* _ASM_LOONGARCH_SET_MEMORY_H */
> > diff --git a/arch/loongarch/mm/pageattr.c b/arch/loongarch/mm/pageattr.c
> > index ffd8d76021d4..f14b40c968b4 100644
> > --- a/arch/loongarch/mm/pageattr.c
> > +++ b/arch/loongarch/mm/pageattr.c
> > @@ -216,3 +216,24 @@ int set_direct_map_invalid_noflush(struct page *page)
> >
> > return __set_memory(addr, 1, __pgprot(0), __pgprot(_PAGE_PRESENT | 
> > _PAGE_VALID));
> >  }
> > +
> > +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool 
> > valid)
> > +{
> > +   unsigned long addr = (unsigned long)page_address(page);
> > +   pgprot_t set, clear;
> > +
> > +   return __set_memory((unsigned long)page_address(page), nr, set, 
> > clear);
> This line should be removed.

Argh, copy/paste is so hard...

Thanks, will do.
 
> Huacai

-- 
Sincerely yours,
Mike.

___
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc