Skip to contentSkip to conversation

mm: pgtable: Ensure pml spinlock gets unlock

PATCH In review I Hsin Cheng 2 participants 7 messages Feb 8, 2025 – Feb 10, 2025 Memory management

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: pgtable: Ensure pml spinlock gets unlock

When !start_pte is true, the "pml" spinlock is still being holded and the branch "out_pte" is taken. If "ptl" is equal to "pml", the lock "pml" will still be locked when the function returns.

It'll be better to set a new branch "out_pte" and jump to it when !start_pte is true at the first place, therefore no additional check for "start_pte" or "ptl != pml" is needed, simply unlock "pml" and return.

---
 mm/pt_reclaim.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
mm/pt_reclaim.c +5 -1
index 7e9455a18aae..163e38f1728d 100644
@@ -43,7 +43,7 @@ void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
 	pml = pmd_lock(mm, pmd);
 	start_pte = pte_offset_map_rw_nolock(mm, pmd, addr, &pmdval, &ptl);
 	if (!start_pte)
-		goto out_ptl;
+		goto out_pte;
 	if (ptl != pml)
 		spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
 
@@ -68,4 +68,8 @@ void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
 		pte_unmap_unlock(start_pte, ptl);
 	if (ptl != pml)
 		spin_unlock(pml);
+	return;
+
+out_pte:
+	spin_unlock(pml);
 }
Signature · 1 line
2.43.0
#1 lore raw
Quoted 3 lines · I Hsin Cheng

When !start_pte is true, the "pml" spinlock is still being holded and the branch "out_pte" is taken. If "ptl" is equal to "pml", the lock "pml" will still be locked when the function returns.

No. When start_pte is NULL, the ptl must also be NULL, so the ptl and pml will not be equal.

Quoted 32 lines

It'll be better to set a new branch "out_pte" and jump to it when !start_pte is true at the first place, therefore no additional check for "start_pte" or "ptl != pml" is needed, simply unlock "pml" and return.

---
  mm/pt_reclaim.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
mm/pt_reclaim.c +5 -1
index 7e9455a18aae..163e38f1728d 100644
@@ -43,7 +43,7 @@ void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
  	pml = pmd_lock(mm, pmd);
  	start_pte = pte_offset_map_rw_nolock(mm, pmd, addr, &pmdval, &ptl);
  	if (!start_pte)
-		goto out_ptl;
+		goto out_pte;
  	if (ptl != pml)
  		spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
 
@@ -68,4 +68,8 @@ void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
  		pte_unmap_unlock(start_pte, ptl);
  	if (ptl != pml)
  		spin_unlock(pml);
+	return;
+
+out_pte:
+	spin_unlock(pml);
  }
#2 lore raw
Quoted 33 lines · Qi Zheng
Quoted 3 lines · I Hsin Cheng

When !start_pte is true, the "pml" spinlock is still being holded and the branch "out_pte" is taken. If "ptl" is equal to "pml", the lock "pml" will still be locked when the function returns.

No. When start_pte is NULL, the ptl must also be NULL, so the ptl and pml will not be equal.

Quoted 23 lines

It'll be better to set a new branch "out_pte" and jump to it when !start_pte is true at the first place, therefore no additional check for "start_pte" or "ptl != pml" is needed, simply unlock "pml" and return.

---
  mm/pt_reclaim.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
mm/pt_reclaim.c +1 -1
index 7e9455a18aae..163e38f1728d 100644
@@ -43,7 +43,7 @@ void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
  	pml = pmd_lock(mm, pmd);
  	start_pte = pte_offset_map_rw_nolock(mm, pmd, addr, &pmdval, &ptl);
  	if (!start_pte)
-		goto out_ptl;
+		goto out_pte;
  	if (ptl != pml)
  		spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
@@ -68,4 +68,8 @@ void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
Quoted 8 lines

mm/pt_reclaim.c void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr, · line 75

Quoted 8 lines
mm/pt_reclaim.c +4 -0
  		pte_unmap_unlock(start_pte, ptl);
  	if (ptl != pml)
  		spin_unlock(pml);
+	return;
+
+out_pte:
+	spin_unlock(pml);
  }

Hi Qi,

Thanks for your kindly review!

Quoted 2 lines

No. When start_pte is NULL, the ptl must also be NULL, so the ptl and pml will not be equal.

Since this is the case, we don't have to do any addtional check for "start_pte" and "ptl != pml", we can be sure that only the second check will be true so we can unlock "pml" without the redundant check, that's my understanding, what do you think?

Best regards,
I Hsin Cheng

#3 lore raw
Quoted 47 lines · I Hsin Cheng
Quoted 33 lines · Qi Zheng
Quoted 3 lines · I Hsin Cheng

When !start_pte is true, the "pml" spinlock is still being holded and the branch "out_pte" is taken. If "ptl" is equal to "pml", the lock "pml" will still be locked when the function returns.

No. When start_pte is NULL, the ptl must also be NULL, so the ptl and pml will not be equal.

Quoted 23 lines

It'll be better to set a new branch "out_pte" and jump to it when !start_pte is true at the first place, therefore no additional check for "start_pte" or "ptl != pml" is needed, simply unlock "pml" and return.

---
   mm/pt_reclaim.c | 6 +++++-
   1 file changed, 5 insertions(+), 1 deletion(-)
mm/pt_reclaim.c +1 -1
index 7e9455a18aae..163e38f1728d 100644
@@ -43,7 +43,7 @@ void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
   	pml = pmd_lock(mm, pmd);
   	start_pte = pte_offset_map_rw_nolock(mm, pmd, addr, &pmdval, &ptl);
   	if (!start_pte)
-		goto out_ptl;
+		goto out_pte;
   	if (ptl != pml)
   		spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
@@ -68,4 +68,8 @@ void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr,
Quoted 8 lines

mm/pt_reclaim.c void try_to_free_pte(struct mm_struct *mm, pmd_t *pmd, unsigned long addr, · line 75

Quoted 8 lines
mm/pt_reclaim.c +4 -0
   		pte_unmap_unlock(start_pte, ptl);
   	if (ptl != pml)
   		spin_unlock(pml);
+	return;
+
+out_pte:
+	spin_unlock(pml);
   }

Hi Qi,

Thanks for your kindly review!

Quoted 7 lines
Quoted 2 lines

No. When start_pte is NULL, the ptl must also be NULL, so the ptl and pml will not be equal.

Since this is the case, we don't have to do any addtional check for "start_pte" and "ptl != pml", we can be sure that only the second check will be true so we can unlock "pml" without the redundant check, that's my understanding, what do you think?

Adding a label

vs

Redundant check in rare cases

Not sure if this is worth it. ;)

Quoted 5 lines

Best regards,
I Hsin Cheng

#4 lore raw
Quoted 1 line · Qi Zheng

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

mm/pt_reclaim.c void try_to_free_pte(struct mm_struct *mm, pmd_t · line 46

Quoted 10 lines
mm/pt_reclaim.c +0 -0
index 7e9455a18aae..163e38f1728d 100644
@@ -43,7 +43,7 @@ void try_to_free_pte(struct mm_struct *mm, pmd_t 
 *pmd, unsigned long addr,
        pml = pmd_lock(mm, pmd);
        start_pte = pte_offset_map_rw_nolock(mm, pmd, addr, &pmdval, &ptl);
        if (!start_pte)

Maybe we can return directly here:

            if (!start_pte) {
		spin_unlock(pml);
                return;
            }
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 14 lines
mm/pt_reclaim.c +5 -1
-        goto out_ptl;
+        goto out_pte;
        if (ptl != pml)
            spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
@@ -68,4 +68,8 @@ void try_to_free_pte(struct mm_struct *mm, pmd_t 
 *pmd, unsigned long addr,
            pte_unmap_unlock(start_pte, ptl);
        if (ptl != pml)
            spin_unlock(pml);
+    return;
+
+out_pte:
+    spin_unlock(pml);

   }

#5 lore raw
Quoted 21 lines · Qi Zheng
Quoted 1 line · Qi Zheng

[...]

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

mm/pt_reclaim.c void try_to_free_pte(struct mm_struct *mm, · line 47

Quoted 11 lines
mm/pt_reclaim.c +0 -0
index 7e9455a18aae..163e38f1728d 100644
@@ -43,7 +43,7 @@ void try_to_free_pte(struct mm_struct *mm,
 pmd_t *pmd, unsigned long addr,
        pml = pmd_lock(mm, pmd);
        start_pte = pte_offset_map_rw_nolock(mm, pmd, addr,
 &pmdval, &ptl);
        if (!start_pte)

Maybe we can return directly here:

Quoted 20 lines
            if (!start_pte) {
		spin_unlock(pml);
                return;
            }
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 14 lines
mm/pt_reclaim.c +1 -1
-        goto out_ptl;
+        goto out_pte;
        if (ptl != pml)

           spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);

mm/pt_reclaim.c +4 -0
@@ -68,4 +68,8 @@ void try_to_free_pte(struct mm_struct *mm,
 pmd_t *pmd, unsigned long addr,
            pte_unmap_unlock(start_pte, ptl);
        if (ptl != pml)
            spin_unlock(pml);
+    return;
+
+out_pte:
+    spin_unlock(pml);

   }

Hi Qi,

Quoted 6 lines

Maybe we can return directly here:

            if (!start_pte) {
              spin_unlock(pml);
                return;
            }

Ahh that's right I think it's better than adding another label, it doesn't even need to jump.

Should I send patch v2 for it or wait for the maintainer's review first?

Best regards,
I Hsin Cheng

#6 lore raw
In reply to Qi Zheng
Quoted 21 lines · Qi Zheng
Quoted 1 line · Qi Zheng

[...]

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

mm/pt_reclaim.c void try_to_free_pte(struct mm_struct *mm, · line 47

Quoted 11 lines
mm/pt_reclaim.c +0 -0
index 7e9455a18aae..163e38f1728d 100644
@@ -43,7 +43,7 @@ void try_to_free_pte(struct mm_struct *mm,
 pmd_t *pmd, unsigned long addr,
        pml = pmd_lock(mm, pmd);
        start_pte = pte_offset_map_rw_nolock(mm, pmd, addr,
 &pmdval, &ptl);
        if (!start_pte)

Maybe we can return directly here:

Quoted 20 lines
            if (!start_pte) {
		spin_unlock(pml);
                return;
            }
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 14 lines
mm/pt_reclaim.c +1 -1
-        goto out_ptl;
+        goto out_pte;
        if (ptl != pml)

           spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);

mm/pt_reclaim.c +4 -0
@@ -68,4 +68,8 @@ void try_to_free_pte(struct mm_struct *mm,
 pmd_t *pmd, unsigned long addr,
            pte_unmap_unlock(start_pte, ptl);
        if (ptl != pml)
            spin_unlock(pml);
+    return;
+
+out_pte:
+    spin_unlock(pml);

   }

I've send a new patch stating for this change and change the title, because pml will sure get unlocked, we just prevent the redundant branches.

Best regards,
I Hsin Cheng