From: Li RongQing <[email protected]>

Replace list_for_each_entry_rcu() with list_for_each_entry_srcu()
when traversing the encl->mm_list protected by SRCU. This ensures
proper synchronization annotation and avoids potential lockdep
warnings about incorrect RCU usage.

The list is protected by encl->srcu, not RCU, so the SRCU-specific
iterator with srcu_read_lock_held() annotation is required.

Signed-off-by: Li RongQing <[email protected]>
---
 arch/x86/kernel/cpu/sgx/encl.c | 12 ++++++++----
 arch/x86/kernel/cpu/sgx/main.c |  3 ++-
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c
index cf149b9..3c488a0 100644
--- a/arch/x86/kernel/cpu/sgx/encl.c
+++ b/arch/x86/kernel/cpu/sgx/encl.c
@@ -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;
 
diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index dc73194..ead0405 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -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


Reply via email to