Skip to contentSkip to conversation

x86/sgx: Add code to inject hwpoison into SGX memory

PATCH Superseded Thomas Tai 3 participants 7 messages Sep 28, 2022 – Oct 11, 2022

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Thomas Tai

author
#0 lore raw

v2 cover x86/sgx: Add code to inject hwpoison into SGX memory

This patch adds code to inject hwpoison into SGX memory. The SGX memory is processor reserved memory that cannot be directly accessed by system software.

Changes since v1:
- Add a comment in hwpoison_inject as suggested by Miaohe
- v1: Reviewed-by: Tony Luck <[email protected]>
- v1: Reviewed-by: Miaohe Lin <[email protected]>

Thomas Tai (1):
x86/sgx: Add code to inject hwpoison into SGX memory

 Documentation/mm/hwpoison.rst | 44 +++++++++++++++++++++++++++++++++++
 mm/hwpoison-inject.c          |  4 ++++
 2 files changed, 48 insertions(+)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v2 1/1 x86/sgx: Add code to inject hwpoison into SGX memory

Inspired by commit c6acb1e7bf46 (x86/sgx: Add hook to error injection address validation), add a similar code in hwpoison_inject function to check if the address is located in SGX Memory. The error will then be handled by the arch_memory_failure function in the SGX driver.

---
 Documentation/mm/hwpoison.rst | 44 +++++++++++++++++++++++++++++++++++
 mm/hwpoison-inject.c          |  4 ++++
 2 files changed, 48 insertions(+)
Documentation/mm/hwpoison.rst +44 -0
index b9d5253c1305..8a542aca4744 100644
@@ -162,6 +162,50 @@ Testing
 
   Some portable hwpoison test programs in mce-test, see below.
 
+* Special notes for injection into SGX enclaves
+
+  1) Determine physical address of enclave page
+
+	dmesg | grep "sgx: EPC"
+
+	sgx: EPC section 0x8000c00000-0x807f7fffff
+	sgx: EPC section 0x10000c00000-0x1007fffffff
+
+  2) Convert the EPC address to page frame number.
+
+	For 4K page size, the page frame number for 0x8000c00000 is
+	0x8000c00000 / 0x1000 = 0x8000c00.
+
+  3) Trace memory_failure
+
+	echo nop > /sys/kernel/tracing/current_tracer
+	echo *memory_failure > /sys/kernel/tracing/set_ftrace_filter
+	echo function > /sys/kernel/tracing/current_tracer
+
+  4) Inject a memory error
+
+	modprobe hwpoison-inject
+	echo "0x8000c00" > /sys/kernel/debug/hwpoison/corrupt-pfn
+
+  5) Check the trace output
+
+	cat /sys/kernel/tracing/trace
+
+	# tracer: function
+	#
+	# entries-in-buffer/entries-written: 2/2   #P:128
+	#
+	#                            _-----=> irqs-off
+	#                           / _----=> need-resched
+	#                          | / _---=> hardirq/softirq
+	#                          || / _--=> preempt-depth
+	#                          ||| / _-=> migrate-disable
+	#                          |||| /     delay
+	#       TASK-PID     CPU#  |||||  TIMESTAMP  FUNCTION
+	#          | |         |   |||||     |         |
+	        bash-12167   [002] .....   113.136808: memory_failure<-simple_attr_write
+	        bash-12167   [002] .....   113.136810: arch_memory_failure<-memory_failure
+
 References
 ==========
 
mm/hwpoison-inject.c +4 -0
index 65e242b5a432..bf83111c1d9b 100644
@@ -21,6 +21,10 @@ static int hwpoison_inject(void *data, u64 val)
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
+	/* Inject the error if the page is part of the processor reserved memory */
+	if (arch_is_platform_page(pfn << PAGE_SHIFT))
+		goto inject;
+
 	if (!pfn_valid(pfn))
 		return -ENXIO;
 
Signature · 1 line
2.31.1
#2 lore raw
Quoted 10 lines · Thomas Tai

Inspired by commit c6acb1e7bf46 (x86/sgx: Add hook to error injection address validation), add a similar code in hwpoison_inject function to check if the address is located in SGX Memory. The error will then be handled by the arch_memory_failure function in the SGX driver.

---
 Documentation/mm/hwpoison.rst | 44 +++++++++++++++++++++++++++++++++++
 mm/hwpoison-inject.c          |  4 ++++
 2 files changed, 48 insertions(+)

Hmm.. no change log.

What was the change in-between v1 and v2?

BR, Jarkko

author
#3 lore raw
Quoted 21 lines

-----Original Message-----
From: Jarkko Sakkinen <[email protected]>
Sent: September 30, 2022 5:51 PM
To: Thomas Tai <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH V2 1/1] x86/sgx: Add code to inject hwpoison into SGX memory

Quoted 8 lines · Thomas Tai

Inspired by commit c6acb1e7bf46 (x86/sgx: Add hook to error injection address validation), add a similar code in hwpoison_inject function to check if the address is located in SGX Memory. The error will then be handled by the arch_memory_failure function in the SGX driver.

---
 Documentation/mm/hwpoison.rst | 44
Quoted 7 lines

+++++++++++++++++++++++++++++++++++

Quoted 2 lines
 mm/hwpoison-inject.c          |  4 ++++
 2 files changed, 48 insertions(+)

Hmm.. no change log.

What was the change in-between v1 and v2?

Hi Jarkko,
I put the change log in [PATCH V2 0/1]. Following is the details, hope you find it useful.

Changes since v1:
- Add a comment in hwpoison_inject as suggested by Miaohe
- v1: Reviewed-by: Tony Luck <[email protected]>
- v1: Reviewed-by: Miaohe Lin <[email protected]>

Thanks,
Thomas

Quoted 2 lines

BR, Jarkko

Reviewed-by
#4 lore raw
Quoted 32 lines · Thomas Tai
Quoted 21 lines

-----Original Message-----
From: Jarkko Sakkinen <[email protected]>
Sent: September 30, 2022 5:51 PM
To: Thomas Tai <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH V2 1/1] x86/sgx: Add code to inject hwpoison into SGX memory

Quoted 8 lines · Thomas Tai

Inspired by commit c6acb1e7bf46 (x86/sgx: Add hook to error injection address validation), add a similar code in hwpoison_inject function to check if the address is located in SGX Memory. The error will then be handled by the arch_memory_failure function in the SGX driver.

---
 Documentation/mm/hwpoison.rst | 44
Quoted 7 lines

+++++++++++++++++++++++++++++++++++

Quoted 2 lines
 mm/hwpoison-inject.c          |  4 ++++
 2 files changed, 48 insertions(+)

Hmm.. no change log.

What was the change in-between v1 and v2?

Hi Jarkko,
I put the change log in [PATCH V2 0/1]. Following is the details, hope you find it useful.

Quoted 7 lines

Changes since v1:
- Add a comment in hwpoison_inject as suggested by Miaohe
- v1: Reviewed-by: Tony Luck <[email protected]>
- v1: Reviewed-by: Miaohe Lin <[email protected]>

Thanks,
Thomas

Also,

BR, Jarkko

#5 lore raw
In reply to Thomas Tai
Quoted 6 lines · Thomas Tai

Inspired by commit c6acb1e7bf46 (x86/sgx: Add hook to error injection address validation), add a similar code in hwpoison_inject function to check if the address is located in SGX Memory. The error will then be handled by the arch_memory_failure function in the SGX driver.

Thank you for sending patch.

Quoted 46 lines
---
 Documentation/mm/hwpoison.rst | 44 +++++++++++++++++++++++++++++++++++
 mm/hwpoison-inject.c          |  4 ++++
 2 files changed, 48 insertions(+)
Documentation/mm/hwpoison.rst +33 -0
index b9d5253c1305..8a542aca4744 100644
@@ -162,6 +162,50 @@ Testing
 
   Some portable hwpoison test programs in mce-test, see below.
 
+* Special notes for injection into SGX enclaves
+
+  1) Determine physical address of enclave page
+
+	dmesg | grep "sgx: EPC"
+
+	sgx: EPC section 0x8000c00000-0x807f7fffff
+	sgx: EPC section 0x10000c00000-0x1007fffffff
+
+  2) Convert the EPC address to page frame number.
+
+	For 4K page size, the page frame number for 0x8000c00000 is
+	0x8000c00000 / 0x1000 = 0x8000c00.
+
+  3) Trace memory_failure
+
+	echo nop > /sys/kernel/tracing/current_tracer
+	echo *memory_failure > /sys/kernel/tracing/set_ftrace_filter
+	echo function > /sys/kernel/tracing/current_tracer
+
+  4) Inject a memory error
+
+	modprobe hwpoison-inject
+	echo "0x8000c00" > /sys/kernel/debug/hwpoison/corrupt-pfn
+
+  5) Check the trace output
+
+	cat /sys/kernel/tracing/trace
+
+	# tracer: function
+	#
+	# entries-in-buffer/entries-written: 2/2   #P:128
+	#

Documentation/mm/hwpoison.rst Testing · line 207

Quoted 10 lines
Documentation/mm/hwpoison.rst +10 -0
+	#                            _-----=> irqs-off
+	#                           / _----=> need-resched
+	#                          | / _---=> hardirq/softirq
+	#                          || / _--=> preempt-depth
+	#                          ||| / _-=> migrate-disable
+	#                          |||| /     delay
+	#       TASK-PID     CPU#  |||||  TIMESTAMP  FUNCTION
+	#          | |         |   |||||     |         |
+	        bash-12167   [002] .....   113.136808: memory_failure<-simple_attr_write
+	        bash-12167   [002] .....   113.136810: arch_memory_failure<-memory_failure

In other cases of page types, memory_failure() leaves some kernel message like "Memory failure: 0x10cf09: recovery action for free buddy page: Recovered", which is printed out by action_result(). So I think it's better to adjust to this convention also in SGX page's case. Then, you don't have to use ftrace to confirm the result of error injection.

Thanks,
Naoya Horiguchi

Quoted 21 lines
Documentation/mm/hwpoison.rst +1 -0
+
 References
 ==========
 
mm/hwpoison-inject.c +4 -0
index 65e242b5a432..bf83111c1d9b 100644
@@ -21,6 +21,10 @@ static int hwpoison_inject(void *data, u64 val)
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
+	/* Inject the error if the page is part of the processor reserved memory */
+	if (arch_is_platform_page(pfn << PAGE_SHIFT))
+		goto inject;
+
 	if (!pfn_valid(pfn))
 		return -ENXIO;
 
Signature · 1 line
2.31.1
author
#6 lore raw
Quoted 81 lines

-----Original Message-----
From: HORIGUCHI NAOYA(堀口 直也) <[email protected]>
Sent: October 7, 2022 2:34 AM
To: Thomas Tai <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; akpm@linux- foundation.org; [email protected]; [email protected]
Subject: Re: [PATCH V2 1/1] x86/sgx: Add code to inject hwpoison into SGX memory

Quoted 6 lines · Thomas Tai

Inspired by commit c6acb1e7bf46 (x86/sgx: Add hook to error injection address validation), add a similar code in hwpoison_inject function to check if the address is located in SGX Memory. The error will then be handled by the arch_memory_failure function in the SGX driver.

Thank you for sending patch.

Quoted 2 lines
---
 Documentation/mm/hwpoison.rst | 44

+++++++++++++++++++++++++++++++++++

Review

Quoted 4 lines
 mm/hwpoison-inject.c          |  4 ++++
 2 files changed, 48 insertions(+)
(quoted diff) +0 -0

b/Documentation/mm/hwpoison.rst

Quoted 39 lines

index b9d5253c1305..8a542aca4744 100644

Documentation/mm/hwpoison.rst +32 -0
@@ -162,6 +162,50 @@ Testing
 
   Some portable hwpoison test programs in mce-test, see below.
 
+* Special notes for injection into SGX enclaves
+
+  1) Determine physical address of enclave page
+
+	dmesg | grep "sgx: EPC"
+
+	sgx: EPC section 0x8000c00000-0x807f7fffff
+	sgx: EPC section 0x10000c00000-0x1007fffffff
+
+  2) Convert the EPC address to page frame number.
+
+	For 4K page size, the page frame number for 0x8000c00000 is
+	0x8000c00000 / 0x1000 = 0x8000c00.
+
+  3) Trace memory_failure
+
+	echo nop > /sys/kernel/tracing/current_tracer
+	echo *memory_failure > /sys/kernel/tracing/set_ftrace_filter
+	echo function > /sys/kernel/tracing/current_tracer
+
+  4) Inject a memory error
+
+	modprobe hwpoison-inject
+	echo "0x8000c00" > /sys/kernel/debug/hwpoison/corrupt-pfn
+
+  5) Check the trace output
+
+	cat /sys/kernel/tracing/trace
+
+	# tracer: function
+	#
+	# entries-in-buffer/entries-written: 2/2   #P:128

Documentation/mm/hwpoison.rst Testing · line 206

Quoted 10 lines
Documentation/mm/hwpoison.rst +10 -0
+	#
+	#                            _-----=> irqs-off
+	#                           / _----=> need-resched
+	#                          | / _---=> hardirq/softirq
+	#                          || / _--=> preempt-depth
+	#                          ||| / _-=> migrate-disable
+	#                          |||| /     delay
+	#       TASK-PID     CPU#  |||||  TIMESTAMP  FUNCTION
+	#          | |         |   |||||     |         |
+	        bash-12167   [002] .....   113.136808: memory_failure<-

simple_attr_write

Documentation/mm/hwpoison.rst Testing · line 207

Quoted 1 line
Documentation/mm/hwpoison.rst +1 -0
+	        bash-12167   [002] .....   113.136810: arch_memory_failure<-

memory_failure

Quoted 6 lines

In other cases of page types, memory_failure() leaves some kernel message like "Memory failure: 0x10cf09: recovery action for free buddy page:
Recovered",
which is printed out by action_result(). So I think it's better to adjust to this convention also in SGX page's case. Then, you don't have to use ftrace to confirm the result of error injection.

Hi Naoya,
Thanks for your suggestion, I will look into it.

Thomas

Quoted 26 lines

Thanks,
Naoya Horiguchi

mm/hwpoison-inject.c static int hwpoison_inject(void *data, u64 val) · line 24

Quoted 13 lines
Documentation/mm/hwpoison.rst +1 -0
+
 References
 ==========
 
mm/hwpoison-inject.c +1 -0
index 65e242b5a432..bf83111c1d9b 100644
@@ -21,6 +21,10 @@ static int hwpoison_inject(void *data, u64 val)
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
+	/* Inject the error if the page is part of the processor reserved memory

*/

Quoted 8 lines
mm/hwpoison-inject.c +3 -1
+	if (arch_is_platform_page(pfn << PAGE_SHIFT))
+		goto inject;
+
 	if (!pfn_valid(pfn))
 		return -ENXIO;
 
--

2.31.1