Skip to contentSkip to conversation

arm64: refactor the rodata=xxx

PATCH Superseded Huang Shijie 5 participants 10 messages Dec 9, 2024 – Dec 10, 2024

Subsystems (2)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover arm64: refactor the rodata=xxx

From Documentation/admin-guide/kernel-parameters.txt, we know that:
rodata= [KNL,EARLY]
on Mark read-only kernel memory as read-only (default).
off Leave read-only kernel memory writable for debugging.
full Mark read-only kernel memory and aliases as read-only
[arm64]

So the "rodata=on" is the default.

But the current code does not follow the document, it makes "rodata=full" as the default.

This patch set follows Anshuman Khandual's suggetions.
It makes the "rodata=on" as the default, and removes the CONFIG_RODATA_FULL_DEFAULT_ENABLED.

v3:
Follows Anshuman Khandual's suggetions:
- Merge patch 1 and patch 3 into one patch.
- Remove patch 4
- update comments and document.

v2:
Follows Will's suggetions.
Add a new file fine-tuning-tips.rst for the expert users.
https://lists.infradead.org/pipermail/linux-arm-kernel/2024-November/981190.html

v1:
https://lists.infradead.org/pipermail/linux-arm-kernel/2024-October/971415.html

Huang Shijie (2):
arm64: refactor the rodata=xxx
arm64: remove CONFIG_RODATA_FULL_DEFAULT_ENABLED

 .../admin-guide/kernel-parameters.txt         |  2 +-
 arch/arm64/Kconfig                            | 14 -----------
 arch/arm64/include/asm/setup.h                | 25 +++++++++++++++++--
 arch/arm64/mm/pageattr.c                      |  2 +-
 4 files changed, 25 insertions(+), 18 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH v3 1/2 arm64: refactor the rodata=xxx

From Documentation/admin-guide/kernel-parameters.txt:

	rodata=		[KNL,EARLY]
		on	Mark read-only kernel memory as read-only (default).

So "rodata=on" is the default.

But the current code does not follow the document, it makes "rodata=full" as the default.

This patch refactors the "rodata=xxx" :
1.) Make rodata=on behaviour be the original rodata=full.
(Drop the original rodata=full.)
2.) Introduce "rodata=noalias" which is the original "rodata=on" 3.) Add more comment for arch_parse_debug_rodata(),
update kernel-parameters.txt.

After this patch, the rodata=on will be the default.

---
 .../admin-guide/kernel-parameters.txt         |  2 +-
 arch/arm64/include/asm/setup.h                | 25 +++++++++++++++++--
 2 files changed, 24 insertions(+), 3 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +1 -1
index a22b7e621007..51bce7b9d805 100644
@@ -5901,7 +5901,7 @@
 	rodata=		[KNL,EARLY]
 		on	Mark read-only kernel memory as read-only (default).
 		off	Leave read-only kernel memory writable for debugging.
-		full	Mark read-only kernel memory and aliases as read-only
+		noalias	Use more block mappings,may have better performance.
 		        [arm64]
 
 	rockchip.usb_uart
arch/arm64/include/asm/setup.h +23 -2
index ba269a7a3201..b3063326b79a 100644
@@ -13,6 +13,27 @@
 extern phys_addr_t __fdt_pointer __initdata;
 extern u64 __cacheline_aligned boot_args[4];
 
+/*
+ * rodata=on (default):
+ *    Apply read-only attributes of VM areas to the linear alias of
+ *    the backing pages as well. This prevents code or read-only data
+ *    from being modified (inadvertently or intentionally) via another
+ *    mapping of the same memory page.
+ *
+ *    This requires the linear region to be mapped down to pages,
+ *    which may adversely affect performance in some cases.
+ *
+ * rodata=off:
+ *    It provides us more block mappings and contiguous hits
+ *    to map the linear region which minimize the TLB footprint.
+ *    Leave read-only kernel memory writable for debugging.
+ *
+ * rodata=noalias:
+ *    It provides us more block mappings and contiguous hits
+ *    to map the linear region which minimize the TLB footprint.
+ *    And the linear aliases of pages belonging to read-only mappings
+ *    in vmalloc region are also marked as read-only.
+ */
 static inline bool arch_parse_debug_rodata(char *arg)
 {
 	extern bool rodata_enabled;
@@ -21,7 +42,7 @@ static inline bool arch_parse_debug_rodata(char *arg)
 	if (!arg)
 		return false;
 
-	if (!strcmp(arg, "full")) {
+	if (!strcmp(arg, "on")) {
 		rodata_enabled = rodata_full = true;
 		return true;
 	}
@@ -31,7 +52,7 @@ static inline bool arch_parse_debug_rodata(char *arg)
 		return true;
 	}
 
-	if (!strcmp(arg, "on")) {
+	if (!strcmp(arg, "noalias")) {
 		rodata_enabled = true;
 		rodata_full = false;
 		return true;
Signature · 1 line
2.40.1
#2 lore raw

Documentation/admin-guide/kernel-parameters.txt line 5905

Quoted 9 lines · Huang Shijie
Documentation/admin-guide/kernel-parameters.txt +1 -1
@@ -5901,7 +5901,7 @@
 	rodata=		[KNL,EARLY]
 		on	Mark read-only kernel memory as read-only (default).
 		off	Leave read-only kernel memory writable for debugging.
-		full	Mark read-only kernel memory and aliases as read-only
+		noalias	Use more block mappings,may have better performance.
 		        [arm64]

Maybe use
noalias Do not check aliases in order to allow larger kernel page
sizes on a platforms without FEAT_BBM2 support[arm64])

instead?

Quoted 53 lines
Documentation/admin-guide/kernel-parameters.txt +0 -0
 	rockchip.usb_uart
arch/arm64/include/asm/setup.h +23 -2
index ba269a7a3201..b3063326b79a 100644
@@ -13,6 +13,27 @@
 extern phys_addr_t __fdt_pointer __initdata;
 extern u64 __cacheline_aligned boot_args[4];
 
+/*
+ * rodata=on (default):
+ *    Apply read-only attributes of VM areas to the linear alias of
+ *    the backing pages as well. This prevents code or read-only data
+ *    from being modified (inadvertently or intentionally) via another
+ *    mapping of the same memory page.
+ *
+ *    This requires the linear region to be mapped down to pages,
+ *    which may adversely affect performance in some cases.
+ *
+ * rodata=off:
+ *    It provides us more block mappings and contiguous hits
+ *    to map the linear region which minimize the TLB footprint.
+ *    Leave read-only kernel memory writable for debugging.
+ *
+ * rodata=noalias:
+ *    It provides us more block mappings and contiguous hits
+ *    to map the linear region which minimize the TLB footprint.
+ *    And the linear aliases of pages belonging to read-only mappings
+ *    in vmalloc region are also marked as read-only.
+ */
 static inline bool arch_parse_debug_rodata(char *arg)
 {
 	extern bool rodata_enabled;
@@ -21,7 +42,7 @@ static inline bool arch_parse_debug_rodata(char *arg)
 	if (!arg)
 		return false;
 
-	if (!strcmp(arg, "full")) {
+	if (!strcmp(arg, "on")) {
 		rodata_enabled = rodata_full = true;
 		return true;
 	}
@@ -31,7 +52,7 @@ static inline bool arch_parse_debug_rodata(char *arg)
 		return true;
 	}
 
-	if (!strcmp(arg, "on")) {
+	if (!strcmp(arg, "noalias")) {
 		rodata_enabled = true;
 		rodata_full = false;
 		return true;
#3 lore raw
Quoted 13 lines · Christoph Lameter (Ampere)

Documentation/admin-guide/kernel-parameters.txt line 5905

Quoted 9 lines
Documentation/admin-guide/kernel-parameters.txt +1 -1
@@ -5901,7 +5901,7 @@
  	rodata=		[KNL,EARLY]
  		on	Mark read-only kernel memory as read-only (default).
  		off	Leave read-only kernel memory writable for debugging.
-		full	Mark read-only kernel memory and aliases as read-only
+		noalias	Use more block mappings,may have better performance.
  		        [arm64]

Maybe use
noalias Do not check aliases in order to allow larger kernel page
sizes on a platforms without FEAT_BBM2 support[arm64])

Quoted 1 line

instead?

The "rodata=noalias" here is equal to original "rodata=on".

But Yang Shi's patch set works with original "rodata=full" :

https://lists.infradead.org/pipermail/linux-arm-kernel/2024-November/979770.html

IMHO, it's not proper to mention the FEAT_BBM in "rodata=noalias".

Thanks

Huang Shijie

#4 lore raw
In reply to Huang Shijie

Hello Huang,

Quoted 9 lines · Huang Shijie
Quoted 1 line

From Documentation/admin-guide/kernel-parameters.txt:

	rodata=		[KNL,EARLY]
		on	Mark read-only kernel memory as read-only (default).

So "rodata=on" is the default.

But the current code does not follow the document, it makes "rodata=full" as the default.

Quoted 8 lines

This patch refactors the "rodata=xxx" :
1.) Make rodata=on behaviour be the original rodata=full.
(Drop the original rodata=full.)
2.) Introduce "rodata=noalias" which is the original "rodata=on" 3.) Add more comment for arch_parse_debug_rodata(),
update kernel-parameters.txt.

After this patch, the rodata=on will be the default.

Could this commit message be reworded/reformatted something like the following instead ? Although feel free to improve this as required.

-----------------------------------------------------------------------
As per admin guide documentation, "rodata=on" should be the default on platforms. Documentation/admin-guide/kernel-parameters.txt describes these options as

        rodata=         [KNL,EARLY]
                on      Mark read-only kernel memory as read-only (default).
                off     Leave read-only kernel memory writable for debugging.
                full    Mark read-only kernel memory and aliases as read-only
                        [arm64]

But on arm64 platform, "rodata=full" is the default instead. This patch implements the following changes.

- Make "rodata=on" behaviour same as the original "rodata=full"
- Make "rodata=noalias" (new) behaviour same as the original "rodata=on"
- Drop the original "rodata=full"
- Add comment for arch_parse_debug_rodata()
- Update kernel-parameters.txt as required

After this patch, the "rodata=on" will be the default on arm64 platform as well.
-----------------------------------------------------------------------

Quoted 39 lines
---
 .../admin-guide/kernel-parameters.txt         |  2 +-
 arch/arm64/include/asm/setup.h                | 25 +++++++++++++++++--
 2 files changed, 24 insertions(+), 3 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +1 -1
index a22b7e621007..51bce7b9d805 100644
@@ -5901,7 +5901,7 @@
 	rodata=		[KNL,EARLY]
 		on	Mark read-only kernel memory as read-only (default).
 		off	Leave read-only kernel memory writable for debugging.
-		full	Mark read-only kernel memory and aliases as read-only
+		noalias	Use more block mappings,may have better performance.
 		        [arm64]
 
 	rockchip.usb_uart
arch/arm64/include/asm/setup.h +11 -0
index ba269a7a3201..b3063326b79a 100644
@@ -13,6 +13,27 @@
 extern phys_addr_t __fdt_pointer __initdata;
 extern u64 __cacheline_aligned boot_args[4];
 
+/*
+ * rodata=on (default):
+ *    Apply read-only attributes of VM areas to the linear alias of
+ *    the backing pages as well. This prevents code or read-only data
+ *    from being modified (inadvertently or intentionally) via another
+ *    mapping of the same memory page.
+ *
+ *    This requires the linear region to be mapped down to pages,
+ *    which may adversely affect performance in some cases.
+ *
+ * rodata=off:

arch/arm64/include/asm/setup.h line 36

Quoted 10 lines
arch/arm64/include/asm/setup.h +10 -0
+ *    It provides us more block mappings and contiguous hits
+ *    to map the linear region which minimize the TLB footprint.
+ *    Leave read-only kernel memory writable for debugging.
+ *
+ * rodata=noalias:
+ *    It provides us more block mappings and contiguous hits
+ *    to map the linear region which minimize the TLB footprint.
+ *    And the linear aliases of pages belonging to read-only mappings
+ *    in vmalloc region are also marked as read-only.
+ */

Reformatted and cleaned up the above comment a bit but feel free to improve it further.

/*
* rodata=on (default)
*

 *    This applies read-only attributes to VM areas and to the linear
 *    alias of the backing pages as well. This prevents code or read-
 *    only data from being modified (inadvertently or intentionally),
 *    via another mapping for the same memory page.
 *
 *    But this might cause linear map region to be mapped down to base
 *    pages, which may adversely affect performance in some cases.
 *
 * rodata=off
 *
 *    This provides more block mappings and contiguous hints for linear
 *    map region which would minimize TLB footprint. This also leaves
 *    read-only kernel memory writable for debugging.
 *
 * rodata=noalias
 *
 *    This provides more block mappings and contiguous hints for linear
 *    map region which would minimize TLB footprint. Linear aliases of
 *    pages belonging to read-only mappings in vmalloc region are also
 *    marked as read-only.
Quoted 21 lines
arch/arm64/include/asm/setup.h +2 -2
 static inline bool arch_parse_debug_rodata(char *arg)
 {
 	extern bool rodata_enabled;
@@ -21,7 +42,7 @@ static inline bool arch_parse_debug_rodata(char *arg)
 	if (!arg)
 		return false;
 
-	if (!strcmp(arg, "full")) {
+	if (!strcmp(arg, "on")) {
 		rodata_enabled = rodata_full = true;
 		return true;
 	}
@@ -31,7 +52,7 @@ static inline bool arch_parse_debug_rodata(char *arg)
 		return true;
 	}
 
-	if (!strcmp(arg, "on")) {
+	if (!strcmp(arg, "noalias")) {
 		rodata_enabled = true;
 		rodata_full = false;
 		return true;
#5 lore raw
Quoted 1 line · Anshuman Khandual

...

Quoted 19 lines

Reformatted and cleaned up the above comment a bit but feel free to improve it further.

/*
* rodata=on (default)
*

 *    This applies read-only attributes to VM areas and to the linear
 *    alias of the backing pages as well. This prevents code or read-
 *    only data from being modified (inadvertently or intentionally),
 *    via another mapping for the same memory page.
 *
 *    But this might cause linear map region to be mapped down to base
 *    pages, which may adversely affect performance in some cases.
 *
 * rodata=off
 *
 *    This provides more block mappings and contiguous hints for linear
 *    map region which would minimize TLB footprint. This also leaves
Quoted 9 lines
 *    read-only kernel memory writable for debugging.
 *
 * rodata=noalias
 *
 *    This provides more block mappings and contiguous hints for linear
 *    map region which would minimize TLB footprint. Linear aliases of
 *    pages belonging to read-only mappings in vmalloc region are also
 *    marked as read-only.

If linear aliases are marked as read-only, how does 'noalias' differ from 'on'?

#6 lore raw
Quoted 33 lines · Ard Biesheuvel
Quoted 1 line · Anshuman Khandual

...

Quoted 19 lines

Reformatted and cleaned up the above comment a bit but feel free to improve it further.

/*
* rodata=on (default)
*

 *    This applies read-only attributes to VM areas and to the linear
 *    alias of the backing pages as well. This prevents code or read-
 *    only data from being modified (inadvertently or intentionally),
 *    via another mapping for the same memory page.
 *
 *    But this might cause linear map region to be mapped down to base
 *    pages, which may adversely affect performance in some cases.
 *
 * rodata=off
 *
 *    This provides more block mappings and contiguous hints for linear
 *    map region which would minimize TLB footprint. This also leaves
Quoted 9 lines
 *    read-only kernel memory writable for debugging.
 *
 * rodata=noalias
 *
 *    This provides more block mappings and contiguous hints for linear
 *    map region which would minimize TLB footprint. Linear aliases of
 *    pages belonging to read-only mappings in vmalloc region are also
 *    marked as read-only.
Quoted 1 line

If linear aliases are marked as read-only, how does 'noalias' differ from 'on'?

Right, the last sentence can be can dropped.

#7 lore raw
Quoted 32 lines · Anshuman Khandual
Quoted 30 lines · Ard Biesheuvel

On Tue, 10 Dec 2024 at 08:17, Anshuman Khandual
<[email protected]> wrote:
...

Quoted 27 lines

Reformatted and cleaned up the above comment a bit but feel free to improve it further.

/*
* rodata=on (default)
*

  *    This applies read-only attributes to VM areas and to the linear
  *    alias of the backing pages as well. This prevents code or read-
  *    only data from being modified (inadvertently or intentionally),
  *    via another mapping for the same memory page.
  *
  *    But this might cause linear map region to be mapped down to base
  *    pages, which may adversely affect performance in some cases.
  *
  * rodata=off
  *
  *    This provides more block mappings and contiguous hints for linear
  *    map region which would minimize TLB footprint. This also leaves
  *    read-only kernel memory writable for debugging.
  *
  * rodata=noalias
  *
  *    This provides more block mappings and contiguous hints for linear
  *    map region which would minimize TLB footprint. Linear aliases of
  *    pages belonging to read-only mappings in vmalloc region are also
  *    marked as read-only.
Quoted 2 lines
Quoted 1 line

If linear aliases are marked as read-only, how does 'noalias' differ from 'on'?

Right, the last sentence can be can dropped.

Okay, no problem.

Thanks

Huang Shijie

#8 lore raw

PATCH v3 2/2 arm64: remove CONFIG_RODATA_FULL_DEFAULT_ENABLED

In reply to Huang Shijie

After patch "arm64: refacotr the rodata=xxx",
the "rodata=on" becomes the default.

     ......................................
	if (!strcmp(arg, "on")) {
		rodata_enabled = rodata_full = true;
		return true;
	}
     ......................................

The rodata_full is always "true" via "rodata=on" and does not depend on the config RODATA_FULL_DEFAULT_ENABLED anymore, so it can be dropped.

---
 arch/arm64/Kconfig       | 14 --------------
 arch/arm64/mm/pageattr.c |  2 +-
 2 files changed, 1 insertion(+), 15 deletions(-)
arch/arm64/Kconfig +0 -14
index cbfd357f94a6..1c69982302ed 100644
@@ -1666,20 +1666,6 @@ config MITIGATE_SPECTRE_BRANCH_HISTORY
 	  When taking an exception from user-space, a sequence of branches
 	  or a firmware call overwrites the branch history.
 
-config RODATA_FULL_DEFAULT_ENABLED
-	bool "Apply r/o permissions of VM areas also to their linear aliases"
-	default y
-	help
-	  Apply read-only attributes of VM areas to the linear alias of
-	  the backing pages as well. This prevents code or read-only data
-	  from being modified (inadvertently or intentionally) via another
-	  mapping of the same memory page. This additional enhancement can
-	  be turned off at runtime by passing rodata=[off|on] (and turned on
-	  with rodata=full if this option is set to 'n')
-
-	  This requires the linear region to be mapped down to pages,
-	  which may adversely affect performance in some cases.
-
 config ARM64_SW_TTBR0_PAN
 	bool "Emulate Privileged Access Never using TTBR0_EL1 switching"
 	depends on !KCSAN
arch/arm64/mm/pageattr.c +1 -1
index 39fd1f7ff02a..6eef08d8451e 100644
@@ -20,7 +20,7 @@ struct page_change_data {
 	pgprot_t clear_mask;
 };
 
-bool rodata_full __ro_after_init = IS_ENABLED(CONFIG_RODATA_FULL_DEFAULT_ENABLED);
+bool rodata_full __ro_after_init = true;
 
 bool can_set_direct_map(void)
 {
Signature · 1 line
2.40.1
Reviewed-by on v3 2/2
#9 lore raw

Re: [PATCH v3 2/2] arm64: remove CONFIG_RODATA_FULL_DEFAULT_ENABLED

A small nit:

arm64/Kconfig: Remove CONFIG_RODATA_FULL_DEFAULT_ENABLED

Quoted 50 lines · Huang Shijie

After patch "arm64: refacotr the rodata=xxx",
the "rodata=on" becomes the default.

     ......................................
	if (!strcmp(arg, "on")) {
		rodata_enabled = rodata_full = true;
		return true;
	}
     ......................................

The rodata_full is always "true" via "rodata=on" and does not depend on the config RODATA_FULL_DEFAULT_ENABLED anymore, so it can be dropped.

---
 arch/arm64/Kconfig       | 14 --------------
 arch/arm64/mm/pageattr.c |  2 +-
 2 files changed, 1 insertion(+), 15 deletions(-)
arch/arm64/Kconfig +0 -14
index cbfd357f94a6..1c69982302ed 100644
@@ -1666,20 +1666,6 @@ config MITIGATE_SPECTRE_BRANCH_HISTORY
 	  When taking an exception from user-space, a sequence of branches
 	  or a firmware call overwrites the branch history.
 
-config RODATA_FULL_DEFAULT_ENABLED
-	bool "Apply r/o permissions of VM areas also to their linear aliases"
-	default y
-	help
-	  Apply read-only attributes of VM areas to the linear alias of
-	  the backing pages as well. This prevents code or read-only data
-	  from being modified (inadvertently or intentionally) via another
-	  mapping of the same memory page. This additional enhancement can
-	  be turned off at runtime by passing rodata=[off|on] (and turned on
-	  with rodata=full if this option is set to 'n')
-
-	  This requires the linear region to be mapped down to pages,
-	  which may adversely affect performance in some cases.
-
 config ARM64_SW_TTBR0_PAN
 	bool "Emulate Privileged Access Never using TTBR0_EL1 switching"
 	depends on !KCSAN
arch/arm64/mm/pageattr.c +0 -0
index 39fd1f7ff02a..6eef08d8451e 100644

arch/arm64/mm/pageattr.c struct page_change_data { · line 26

Quoted 8 lines
arch/arm64/mm/pageattr.c +1 -1
@@ -20,7 +20,7 @@ struct page_change_data {
 	pgprot_t clear_mask;
 };
 
-bool rodata_full __ro_after_init = IS_ENABLED(CONFIG_RODATA_FULL_DEFAULT_ENABLED);
+bool rodata_full __ro_after_init = true;
 
 bool can_set_direct_map(void)
 {

Otherwise LGTM