Skip to contentSkip to conversation

x86/sgx: Use list_for_each_entry_srcu() for mm_list traversal

PATCH In review lirongqing 2 participants 5 messages Feb 26 – May 8

Subsystems (2)
lore
Share & tools
lore

lirongqing

author
#0 lore raw

PATCH v2 1/1 x86/sgx: Use list_for_each_entry_srcu() for mm_list traversal

From: Li RongQing <[email protected]>

In commit 1728ab54b4be ("x86/sgx: Add a page reclaimer") (v5.11), list_for_each_entry_rcu() was used to traverse the enclave's mm_list. However, this is incorrect because the list is protected by a Sleepable RCU (SRCU) lock (encl->srcu).

Since commit 28875945ba98 ("rcu: Add support for consolidated-RCU reader checking") (v5.4), RCU lockdep checking has become stricter. When CONFIG_PROVE_RCU is enabled, using the standard list_for_each_entry_rcu() while only holding an SRCU lock triggers "suspicious RCU usage" false positive warnings, as it does not recognize SRCU read-side critical sections.

Fix this by switching to list_for_each_entry_srcu(), which was introduced specifically for this purpose in commit ae2212a7216b ("rculist: Introduce list/hlist_for_each_entry_srcu() macros") (v5.10). This correctly associates the traversal with the SRCU lock and eliminates the lockdep warnings.

Fixes 1728ab54b4be ("x86/sgx: Add a page reclaimer")

---
Diff with v1: rewrite changelog

 arch/x86/kernel/cpu/sgx/encl.c | 12 ++++++++----
 arch/x86/kernel/cpu/sgx/main.c |  3 ++-
 2 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +8 -4
index ac60ebd..91362d7 100644
@@ -822,7 +822,8 @@ static struct sgx_encl_mm *sgx_encl_find_mm(struct sgx_encl *encl,
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(tmp, &encl->mm_list, list) {
+	list_for_each_entry_srcu(tmp, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (tmp->mm == mm) {
 			encl_mm = tmp;
 			break;
@@ -933,7 +934,8 @@ const cpumask_t *sgx_encl_cpumask(struct sgx_encl *encl)
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
 
@@ -1018,7 +1020,8 @@ static struct mem_cgroup *sgx_encl_get_mem_cgroup(struct sgx_encl *encl)
 	 */
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
 
@@ -1212,7 +1215,8 @@ void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr)
 
 		idx = srcu_read_lock(&encl->srcu);
 
-		list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+		list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+				srcu_read_lock_held(&encl->srcu)) {
 			if (!mmget_not_zero(encl_mm->mm))
 				continue;
 
arch/x86/kernel/cpu/sgx/main.c +2 -1
index 38b7fd2..581e0c4 100644
@@ -120,7 +120,8 @@ static bool sgx_reclaimer_age(struct sgx_epc_page *epc_page)
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
 
Signature · 1 line
2.9.4
#1 lore raw
Quoted 14 lines

From: Li RongQing <[email protected]>

In commit 1728ab54b4be ("x86/sgx: Add a page reclaimer") (v5.11), list_for_each_entry_rcu() was used to traverse the enclave's mm_list. However, this is incorrect because the list is protected by a Sleepable RCU (SRCU) lock (encl->srcu).

Since commit 28875945ba98 ("rcu: Add support for consolidated-RCU reader checking") (v5.4), RCU lockdep checking has become stricter. When CONFIG_PROVE_RCU is enabled, using the standard list_for_each_entry_rcu() while only holding an SRCU lock triggers "suspicious RCU usage" false positive warnings, as it does not recognize SRCU read-side critical sections.

Quoted 6 lines

Fix this by switching to list_for_each_entry_srcu(), which was introduced specifically for this purpose in commit ae2212a7216b
("rculist: Introduce list/hlist_for_each_entry_srcu() macros") (v5.10). This correctly associates the traversal with the SRCU lock and eliminates the lockdep warnings.

Ping

[Li,Rongqing]

Quoted 75 lines
Fixes 1728ab54b4be ("x86/sgx: Add a page reclaimer")

---
Diff with v1: rewrite changelog

 arch/x86/kernel/cpu/sgx/encl.c | 12 ++++++++----
arch/x86/kernel/cpu/sgx/main.c |  3 ++-
 2 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +2 -1
index ac60ebd..91362d7 100644
@@ -822,7 +822,8 @@ static struct sgx_encl_mm *sgx_encl_find_mm(struct
 sgx_encl *encl,
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(tmp, &encl->mm_list, list) {
+	list_for_each_entry_srcu(tmp, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (tmp->mm == mm) {
 			encl_mm = tmp;
arch/x86/kernel/cpu/sgx/encl.c +6 -3
 			break;
@@ -933,7 +934,8 @@ const cpumask_t *sgx_encl_cpumask(struct sgx_encl
 *encl)
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
@@ -1018,7 +1020,8 @@ static struct mem_cgroup
 *sgx_encl_get_mem_cgroup(struct sgx_encl *encl)
 	 */
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
@@ -1212,7 +1215,8 @@ void sgx_zap_enclave_ptes(struct sgx_encl *encl,
 unsigned long addr)
 
 		idx = srcu_read_lock(&encl->srcu);
 
-		list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+		list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+				srcu_read_lock_held(&encl->srcu)) {
 			if (!mmget_not_zero(encl_mm->mm))
 				continue;
arch/x86/kernel/cpu/sgx/main.c +2 -2
index 38b7fd2..581e0c4 100644
@@ -120,7 +120,8 @@ static bool sgx_reclaimer_age(struct sgx_epc_page
 *epc_page)
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
 
--

2.9.4

#2 lore raw
In reply to lirongqing
Quoted 21 lines

From: Li RongQing <[email protected]>

In commit 1728ab54b4be ("x86/sgx: Add a page reclaimer") (v5.11), list_for_each_entry_rcu() was used to traverse the enclave's mm_list. However, this is incorrect because the list is protected by a Sleepable RCU (SRCU) lock (encl->srcu).

Since commit 28875945ba98 ("rcu: Add support for consolidated-RCU reader checking") (v5.4), RCU lockdep checking has become stricter. When CONFIG_PROVE_RCU is enabled, using the standard list_for_each_entry_rcu() while only holding an SRCU lock triggers "suspicious RCU usage" false positive warnings, as it does not recognize SRCU read-side critical sections.

Fix this by switching to list_for_each_entry_srcu(), which was introduced specifically for this purpose in commit ae2212a7216b
("rculist: Introduce list/hlist_for_each_entry_srcu() macros") (v5.10). This correctly associates the traversal with the SRCU lock and eliminates the lockdep warnings.

Quoted 4 lines
Fixes 1728ab54b4be ("x86/sgx: Add a page reclaimer")

---

Ping

thanks

[Li,Rongqing]

Quoted 71 lines

Diff with v1: rewrite changelog

 arch/x86/kernel/cpu/sgx/encl.c | 12 ++++++++----
arch/x86/kernel/cpu/sgx/main.c |  3 ++-
 2 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +2 -1
index ac60ebd..91362d7 100644
@@ -822,7 +822,8 @@ static struct sgx_encl_mm *sgx_encl_find_mm(struct
 sgx_encl *encl,
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(tmp, &encl->mm_list, list) {
+	list_for_each_entry_srcu(tmp, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (tmp->mm == mm) {
 			encl_mm = tmp;
arch/x86/kernel/cpu/sgx/encl.c +6 -3
 			break;
@@ -933,7 +934,8 @@ const cpumask_t *sgx_encl_cpumask(struct sgx_encl
 *encl)
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
@@ -1018,7 +1020,8 @@ static struct mem_cgroup
 *sgx_encl_get_mem_cgroup(struct sgx_encl *encl)
 	 */
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
@@ -1212,7 +1215,8 @@ void sgx_zap_enclave_ptes(struct sgx_encl *encl,
 unsigned long addr)
 
 		idx = srcu_read_lock(&encl->srcu);
 
-		list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+		list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+				srcu_read_lock_held(&encl->srcu)) {
 			if (!mmget_not_zero(encl_mm->mm))
 				continue;
arch/x86/kernel/cpu/sgx/main.c +2 -2
index 38b7fd2..581e0c4 100644
@@ -120,7 +120,8 @@ static bool sgx_reclaimer_age(struct sgx_epc_page
 *epc_page)
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
 
--

2.9.4

maintainer Reviewed-by
#3 lore raw
Quoted 97 lines · Li,Rongqing(ACG CCN)
Quoted 21 lines

From: Li RongQing <[email protected]>

In commit 1728ab54b4be ("x86/sgx: Add a page reclaimer") (v5.11), list_for_each_entry_rcu() was used to traverse the enclave's mm_list. However, this is incorrect because the list is protected by a Sleepable RCU (SRCU) lock (encl->srcu).

Since commit 28875945ba98 ("rcu: Add support for consolidated-RCU reader checking") (v5.4), RCU lockdep checking has become stricter. When CONFIG_PROVE_RCU is enabled, using the standard list_for_each_entry_rcu() while only holding an SRCU lock triggers "suspicious RCU usage" false positive warnings, as it does not recognize SRCU read-side critical sections.

Fix this by switching to list_for_each_entry_srcu(), which was introduced specifically for this purpose in commit ae2212a7216b
("rculist: Introduce list/hlist_for_each_entry_srcu() macros") (v5.10). This correctly associates the traversal with the SRCU lock and eliminates the lockdep warnings.

Quoted 4 lines
Fixes 1728ab54b4be ("x86/sgx: Add a page reclaimer")

---

Ping

thanks

[Li,Rongqing]

Quoted 63 lines

Diff with v1: rewrite changelog

 arch/x86/kernel/cpu/sgx/encl.c | 12 ++++++++----
arch/x86/kernel/cpu/sgx/main.c |  3 ++-
 2 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +2 -1
index ac60ebd..91362d7 100644
@@ -822,7 +822,8 @@ static struct sgx_encl_mm *sgx_encl_find_mm(struct
 sgx_encl *encl,
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(tmp, &encl->mm_list, list) {
+	list_for_each_entry_srcu(tmp, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (tmp->mm == mm) {
 			encl_mm = tmp;
arch/x86/kernel/cpu/sgx/encl.c +6 -3
 			break;
@@ -933,7 +934,8 @@ const cpumask_t *sgx_encl_cpumask(struct sgx_encl
 *encl)
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
@@ -1018,7 +1020,8 @@ static struct mem_cgroup
 *sgx_encl_get_mem_cgroup(struct sgx_encl *encl)
 	 */
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
@@ -1212,7 +1215,8 @@ void sgx_zap_enclave_ptes(struct sgx_encl *encl,
 unsigned long addr)
 
 		idx = srcu_read_lock(&encl->srcu);
 
-		list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+		list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+				srcu_read_lock_held(&encl->srcu)) {
 			if (!mmget_not_zero(encl_mm->mm))
 				continue;
arch/x86/kernel/cpu/sgx/main.c +0 -0
index 38b7fd2..581e0c4 100644
@@ -120,7 +120,8 @@ static bool sgx_reclaimer_age(struct sgx_epc_page
 *epc_page)
 
 	idx = srcu_read_lock(&encl->srcu);
 
Quoted 9 lines
Quoted 8 lines
arch/x86/kernel/cpu/sgx/main.c +2 -2
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
 
--

2.9.4

BR, Jarkko

#4 lore raw

答复: [????] Re: ??: [PATCH][v2] x86/sgx: Use list_for_each_entry_srcu() for mm_list traversal

Quoted 108 lines · Li,Rongqing(ACG CCN)
Quoted 9 lines
Quoted 9 lines

From: Li RongQing <[email protected]>

In commit 1728ab54b4be ("x86/sgx: Add a page reclaimer") (v5.11), list_for_each_entry_rcu() was used to traverse the enclave's mm_list. However, this is incorrect because the list is protected by a Sleepable RCU (SRCU) lock (encl->srcu).

Quoted 6 lines
Quoted 6 lines

Since commit 28875945ba98 ("rcu: Add support for consolidated-RCU reader
checking") (v5.4), RCU lockdep checking has become stricter. When CONFIG_PROVE_RCU is enabled, using the standard list_for_each_entry_rcu() while only holding an SRCU lock triggers "suspicious RCU usage" false positive warnings, as it does not recognize SRCU

read-side critical sections.

Quoted 91 lines
Quoted 11 lines

Fix this by switching to list_for_each_entry_srcu(), which was introduced specifically for this purpose in commit ae2212a7216b ("rculist: Introduce list/hlist_for_each_entry_srcu() macros") (v5.10). This correctly associates the traversal with the SRCU lock and eliminates the lockdep warnings.

Fixes 1728ab54b4be ("x86/sgx: Add a page reclaimer")

---

Ping

thanks

[Li,Rongqing]

Quoted 71 lines

Diff with v1: rewrite changelog

 arch/x86/kernel/cpu/sgx/encl.c | 12 ++++++++----
arch/x86/kernel/cpu/sgx/main.c |  3 ++-
 2 files changed, 10 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/sgx/encl.c index ac60ebd..91362d7 100644

arch/x86/kernel/cpu/sgx/encl.c +2 -1
@@ -822,7 +822,8 @@ static struct sgx_encl_mm
 *sgx_encl_find_mm(struct sgx_encl *encl,
 
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(tmp, &encl->mm_list, list) {
+	list_for_each_entry_srcu(tmp, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (tmp->mm == mm) {
 			encl_mm = tmp;
arch/x86/kernel/cpu/sgx/encl.c +0 -0
 			break;
@@ -933,7 +934,8 @@ const cpumask_t *sgx_encl_cpumask(struct

sgx_encl
*encl)

arch/x86/kernel/cpu/sgx/encl.c +6 -3
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
@@ -1018,7 +1020,8 @@ static struct mem_cgroup
 *sgx_encl_get_mem_cgroup(struct sgx_encl *encl)
 	 */
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
@@ -1212,7 +1215,8 @@ void sgx_zap_enclave_ptes(struct sgx_encl
 *encl, unsigned long addr)
 
 		idx = srcu_read_lock(&encl->srcu);
 
-		list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+		list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+				srcu_read_lock_held(&encl->srcu)) {
 			if (!mmget_not_zero(encl_mm->mm))
 				continue;
(quoted diff) +0 -0

b/arch/x86/kernel/cpu/sgx/main.c index 38b7fd2..581e0c4 100644

arch/x86/kernel/cpu/sgx/main.c +0 -0
@@ -120,7 +120,8 @@ static bool sgx_reclaimer_age(struct

sgx_epc_page
*epc_page)

arch/x86/kernel/cpu/sgx/main.c +2 -1
 	idx = srcu_read_lock(&encl->srcu);
 
-	list_for_each_entry_rcu(encl_mm, &encl->mm_list, list) {
+	list_for_each_entry_srcu(encl_mm, &encl->mm_list, list,
+			srcu_read_lock_held(&encl->srcu)) {
 		if (!mmget_not_zero(encl_mm->mm))
 			continue;
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

--
2.9.4

Thanks for your review.

And ping

[Li,Rongqing]

Quoted 1 line

BR, Jarkko