Skip to contentSkip to conversation

treewide: Move set_memory_* functions away from cacheflush.h

RFC PATCH Superseded Laura Abbott 5 participants 5 messages Feb 7, 2017 – Feb 9, 2017

Subsystems (5)
Arm (32-bit) x86 arm64 s390 +1 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 treewide: Move set_memory_* functions away from cacheflush.h

The set_memory_* APIs came out of a desire to have a better way to change memory attributes. Many of these attributes were linked to cache functionality so the prototypes were put in cacheflush.h. These days, the APIs have grown and have a much wider use than just cache APIs. To support this growth, split off set_memory_* and friends into a separate header file to avoid growing cacheflush.h for APIs that have nothing to do with caches.

---
This came out of a comment Russell made while reviewing RODATA test cases
http://lists.infradead.org/pipermail/linux-arm-kernel/2017-January/480855.html
While the final result of that series was the rodata code was refactored into its own header file, the set_memory_* APIs are still out of place.

This is a simple attempt at moving all the API stubs to their own file. Another idea I had was throwing set_memory_{x,nx,ro,rw} in an asm-generic file since those are commonly used for module setting across all arches.

This is an RFC to see if this is actually beneficial. The diffstat is not negative unfortunately due to header guards in newer files.
I have patches to convert call sites over to set_memory.h instead of cacheflush.h if there is sufficient interest.

---
 arch/arm/include/asm/cacheflush.h   |  21 +-------
 arch/arm/include/asm/set_memory.h   |  32 ++++++++++++
 arch/arm64/include/asm/cacheflush.h |   6 +--
 arch/arm64/include/asm/set_memory.h |  26 ++++++++++
 arch/s390/include/asm/cacheflush.h  |   6 +--
 arch/s390/include/asm/set_memory.h  |   9 ++++
 arch/x86/include/asm/cacheflush.h   |  96 +---------------------------------
 arch/x86/include/asm/set_memory.h   | 100 ++++++++++++++++++++++++++++++++++++
 8 files changed, 171 insertions(+), 125 deletions(-)
 create mode 100644 arch/arm/include/asm/set_memory.h
 create mode 100644 arch/arm64/include/asm/set_memory.h
 create mode 100644 arch/s390/include/asm/set_memory.h
 create mode 100644 arch/x86/include/asm/set_memory.h
arch/arm/include/asm/cacheflush.h +1 -20
index 02454fa..1cb9d11 100644
@@ -16,6 +16,7 @@
 #include <asm/shmparam.h>
 #include <asm/cachetype.h>
 #include <asm/outercache.h>
+#include <asm/set_memory.h>
 
 #define CACHE_COLOUR(vaddr)	((vaddr & (SHMLBA - 1)) >> PAGE_SHIFT)
 
@@ -478,26 +479,6 @@ static inline void __sync_cache_range_r(volatile void *p, size_t size)
 	: : : "r0","r1","r2","r3","r4","r5","r6","r7", \
 	      "r9","r10","lr","memory" )
 
-#ifdef CONFIG_MMU
-int set_memory_ro(unsigned long addr, int numpages);
-int set_memory_rw(unsigned long addr, int numpages);
-int set_memory_x(unsigned long addr, int numpages);
-int set_memory_nx(unsigned long addr, int numpages);
-#else
-static inline int set_memory_ro(unsigned long addr, int numpages) { return 0; }
-static inline int set_memory_rw(unsigned long addr, int numpages) { return 0; }
-static inline int set_memory_x(unsigned long addr, int numpages) { return 0; }
-static inline int set_memory_nx(unsigned long addr, int numpages) { return 0; }
-#endif
-
-#ifdef CONFIG_STRICT_KERNEL_RWX
-void set_kernel_text_rw(void);
-void set_kernel_text_ro(void);
-#else
-static inline void set_kernel_text_rw(void) { }
-static inline void set_kernel_text_ro(void) { }
-#endif
-
 void flush_uprobe_xol_access(struct page *page, unsigned long uaddr,
 			     void *kaddr, unsigned long len);
 
arch/arm/include/asm/set_memory.h new file +32 -0
new file mode 100644
index 0000000..5aa4315
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 1999-2002 Russell King
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef _ASMARM_SET_MEMORY_H
+#define _ASMARM_SET_MEMORY_H
+
+#ifdef CONFIG_MMU
+int set_memory_ro(unsigned long addr, int numpages);
+int set_memory_rw(unsigned long addr, int numpages);
+int set_memory_x(unsigned long addr, int numpages);
+int set_memory_nx(unsigned long addr, int numpages);
+#else
+static inline int set_memory_ro(unsigned long addr, int numpages) { return 0; }
+static inline int set_memory_rw(unsigned long addr, int numpages) { return 0; }
+static inline int set_memory_x(unsigned long addr, int numpages) { return 0; }
+static inline int set_memory_nx(unsigned long addr, int numpages) { return 0; }
+#endif
+
+#ifdef CONFIG_STRICT_KERNEL_RWX
+void set_kernel_text_rw(void);
+void set_kernel_text_ro(void);
+#else
+static inline void set_kernel_text_rw(void) { }
+static inline void set_kernel_text_ro(void) { }
+#endif
+
+#endif
arch/arm64/include/asm/cacheflush.h +1 -5
index 5a2a6ee..7db6962 100644
@@ -20,6 +20,7 @@
 #define __ASM_CACHEFLUSH_H
 
 #include <linux/mm.h>
+#include <asm/set_memory.h>
 
 /*
  * This flag is used to indicate that the page pointed to by a pte is clean
@@ -150,9 +151,4 @@ static inline void flush_cache_vunmap(unsigned long start, unsigned long end)
 {
 }
 
-int set_memory_ro(unsigned long addr, int numpages);
-int set_memory_rw(unsigned long addr, int numpages);
-int set_memory_x(unsigned long addr, int numpages);
-int set_memory_nx(unsigned long addr, int numpages);
-
 #endif
arch/arm64/include/asm/set_memory.h new file +26 -0
new file mode 100644
index 0000000..5b38ab80
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 1999-2002 Russell King.
+ * Copyright (C) 2012 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __ARM64_ASM_SET_MEMORY_H
+#define __ARM64_ASM_SET_MEMORY_H
+
+int set_memory_ro(unsigned long addr, int numpages);
+int set_memory_rw(unsigned long addr, int numpages);
+int set_memory_x(unsigned long addr, int numpages);
+int set_memory_nx(unsigned long addr, int numpages);
+
+#endif
arch/s390/include/asm/cacheflush.h +1 -5
index 58fae7d..afe2965 100644
@@ -3,10 +3,6 @@
 
 /* Caches aren't brain-dead on the s390. */
 #include <asm-generic/cacheflush.h>
-
-int set_memory_ro(unsigned long addr, int numpages);
-int set_memory_rw(unsigned long addr, int numpages);
-int set_memory_nx(unsigned long addr, int numpages);
-int set_memory_x(unsigned long addr, int numpages);
+#include <asm/set_memory.h>
 
 #endif /* _S390_CACHEFLUSH_H */
arch/s390/include/asm/set_memory.h new file +9 -0
new file mode 100644
index 0000000..1bb61c5
@@ -0,0 +1,9 @@
+#ifndef _S390_SET_MEMORY_H
+#define _S390_SET_MEMORY_H
+
+int set_memory_ro(unsigned long addr, int numpages);
+int set_memory_rw(unsigned long addr, int numpages);
+int set_memory_nx(unsigned long addr, int numpages);
+int set_memory_x(unsigned long addr, int numpages);
+
+#endif /* _S390_SET_MEMORY_H */
arch/x86/include/asm/cacheflush.h +1 -95

105 lines not on this page · raw message

arch/x86/include/asm/set_memory.h new file +100 -0

100 lines not on this page · raw message

Signature · 1 line
2.7.4
#1 lore raw
Quoted 8 lines · Laura Abbott

The set_memory_* APIs came out of a desire to have a better way to change memory attributes. Many of these attributes were linked to cache functionality so the prototypes were put in cacheflush.h. These days, the APIs have grown and have a much wider use than just cache APIs. To support this growth, split off set_memory_* and friends into a separate header file to avoid growing cacheflush.h for APIs that have nothing to do with caches.

Quoted 6 lines

---
This came out of a comment Russell made while reviewing RODATA test cases
http://lists.infradead.org/pipermail/linux-arm-kernel/2017-January/480855.html
While the final result of that series was the rodata code was refactored into its own header file, the set_memory_* APIs are still out of place.

Yeah, this is a nice clean up to start on.

Quoted 3 lines

This is a simple attempt at moving all the API stubs to their own file. Another idea I had was throwing set_memory_{x,nx,ro,rw} in an asm-generic file since those are commonly used for module setting across all arches.

It seems like asm-generic would be cleaner, yes? Only a few archs have anything "special" happening in the resulting header files.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#2 lore raw
Quoted 16 lines · Kees Cook
Quoted 8 lines · Laura Abbott

The set_memory_* APIs came out of a desire to have a better way to change memory attributes. Many of these attributes were linked to cache functionality so the prototypes were put in cacheflush.h. These days, the APIs have grown and have a much wider use than just cache APIs. To support this growth, split off set_memory_* and friends into a separate header file to avoid growing cacheflush.h for APIs that have nothing to do with caches.

Quoted 6 lines

---
This came out of a comment Russell made while reviewing RODATA test cases
http://lists.infradead.org/pipermail/linux-arm-kernel/2017-January/480855.html
While the final result of that series was the rodata code was refactored into its own header file, the set_memory_* APIs are still out of place.

Quoted 8 lines

Yeah, this is a nice clean up to start on.

Quoted 3 lines

This is a simple attempt at moving all the API stubs to their own file. Another idea I had was throwing set_memory_{x,nx,ro,rw} in an asm-generic file since those are commonly used for module setting across all arches.

It seems like asm-generic would be cleaner, yes? Only a few archs have anything "special" happening in the resulting header files.

I'd also prefer the asm-generic approach if it makes the arch part a simple addition to asm/Kbuild.

Will

Acked-by on v1 1/1
#3 lore raw
In reply to Laura Abbott
Quoted 23 lines · Laura Abbott

The set_memory_* APIs came out of a desire to have a better way to change memory attributes. Many of these attributes were linked to cache functionality so the prototypes were put in cacheflush.h. These days, the APIs have grown and have a much wider use than just cache APIs. To support this growth, split off set_memory_* and friends into a separate header file to avoid growing cacheflush.h for APIs that have nothing to do with caches.

---
This came out of a comment Russell made while reviewing RODATA test cases
http://lists.infradead.org/pipermail/linux-arm-kernel/2017-January/480855.html
While the final result of that series was the rodata code was refactored into its own header file, the set_memory_* APIs are still out of place.

This is a simple attempt at moving all the API stubs to their own file. Another idea I had was throwing set_memory_{x,nx,ro,rw} in an asm-generic file since those are commonly used for module setting across all arches.

This is an RFC to see if this is actually beneficial. The diffstat is not negative unfortunately due to header guards in newer files.
I have patches to convert call sites over to set_memory.h instead of cacheflush.h if there is sufficient interest.

Quoted 14 lines
---
 arch/arm/include/asm/cacheflush.h   |  21 +-------
 arch/arm/include/asm/set_memory.h   |  32 ++++++++++++
 arch/arm64/include/asm/cacheflush.h |   6 +--
 arch/arm64/include/asm/set_memory.h |  26 ++++++++++
 arch/s390/include/asm/cacheflush.h  |   6 +--
 arch/s390/include/asm/set_memory.h  |   9 ++++
 arch/x86/include/asm/cacheflush.h   |  96 +---------------------------------
 arch/x86/include/asm/set_memory.h   | 100 ++++++++++++++++++++++++++++++++++++
 8 files changed, 171 insertions(+), 125 deletions(-)
 create mode 100644 arch/arm/include/asm/set_memory.h
 create mode 100644 arch/arm64/include/asm/set_memory.h
 create mode 100644 arch/s390/include/asm/set_memory.h
 create mode 100644 arch/x86/include/asm/set_memory.h

LGTM:

Thanks,

Ingo

Acked-by on v1 1/1
#4 lore raw
In reply to Laura Abbott
Quoted 23 lines · Laura Abbott

The set_memory_* APIs came out of a desire to have a better way to change memory attributes. Many of these attributes were linked to cache functionality so the prototypes were put in cacheflush.h. These days, the APIs have grown and have a much wider use than just cache APIs. To support this growth, split off set_memory_* and friends into a separate header file to avoid growing cacheflush.h for APIs that have nothing to do with caches.

---
This came out of a comment Russell made while reviewing RODATA test cases
http://lists.infradead.org/pipermail/linux-arm-kernel/2017-January/480855.html
While the final result of that series was the rodata code was refactored into its own header file, the set_memory_* APIs are still out of place.

This is a simple attempt at moving all the API stubs to their own file. Another idea I had was throwing set_memory_{x,nx,ro,rw} in an asm-generic file since those are commonly used for module setting across all arches.

This is an RFC to see if this is actually beneficial. The diffstat is not negative unfortunately due to header guards in newer files.
I have patches to convert call sites over to set_memory.h instead of cacheflush.h if there is sufficient interest.

Quoted 14 lines
---
 arch/arm/include/asm/cacheflush.h   |  21 +-------
 arch/arm/include/asm/set_memory.h   |  32 ++++++++++++
 arch/arm64/include/asm/cacheflush.h |   6 +--
 arch/arm64/include/asm/set_memory.h |  26 ++++++++++
 arch/s390/include/asm/cacheflush.h  |   6 +--
 arch/s390/include/asm/set_memory.h  |   9 ++++
 arch/x86/include/asm/cacheflush.h   |  96 +---------------------------------
 arch/x86/include/asm/set_memory.h   | 100 ++++++++++++++++++++++++++++++++++++
 8 files changed, 171 insertions(+), 125 deletions(-)
 create mode 100644 arch/arm/include/asm/set_memory.h
 create mode 100644 arch/arm64/include/asm/set_memory.h
 create mode 100644 arch/s390/include/asm/set_memory.h
 create mode 100644 arch/x86/include/asm/set_memory.h

For s390: