Skip to contentSkip to conversation

mm_slot: fix the usage of mm_slot_entry()

PATCH In review Wei Yang 2 participants 7 messages Oct 1, 2025

Subsystems (3)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

cover mm_slot: fix the usage of mm_slot_entry()

This is the fixup based on mm-stable.

The base commit:

mm: swap: check for stable address space before operating on the VMA

The reviewed version is at [1].

[1]: https://lkml.kernel.org/r/[email protected]

Wei Yang (2):
mm/ksm: don't call mm_slot_entry() when the slot is NULL mm/khugepaged: remove definition of struct khugepaged_mm_slot

 mm/khugepaged.c |  5 +----
 mm/ksm.c        | 27 ++++++++++++++-------------
 2 files changed, 15 insertions(+), 17 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 mm/ksm: don't call mm_slot_entry() when the slot is NULL

When using mm_slot in ksm, there is code like:

     slot = mm_slot_lookup(mm_slots_hash, mm);
     mm_slot = mm_slot_entry(slot, struct ksm_mm_slot, slot);
     if (mm_slot && ..) {
     }

The mm_slot_entry() won't return a valid value if slot is NULL generally. But currently it works since slot is the first element of struct ksm_mm_slot.

To reduce the ambiguity and make it robust, only call mm_slot_entry() when we have a valid slot.

---
 mm/ksm.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
mm/ksm.c +14 -13
index 04019a15b25d..7bc726b50b2f 100644
@@ -2921,7 +2921,7 @@ int __ksm_enter(struct mm_struct *mm)
 
 void __ksm_exit(struct mm_struct *mm)
 {
-	struct ksm_mm_slot *mm_slot;
+	struct ksm_mm_slot *mm_slot = NULL;
 	struct mm_slot *slot;
 	int easy_to_free = 0;
 
@@ -2936,19 +2936,20 @@ void __ksm_exit(struct mm_struct *mm)
 
 	spin_lock(&ksm_mmlist_lock);
 	slot = mm_slot_lookup(mm_slots_hash, mm);
-	if (slot) {
-		mm_slot = mm_slot_entry(slot, struct ksm_mm_slot, slot);
-		if (ksm_scan.mm_slot != mm_slot) {
-			if (!mm_slot->rmap_list) {
-				hash_del(&slot->hash);
-				list_del(&slot->mm_node);
-				easy_to_free = 1;
-			} else {
-				list_move(&slot->mm_node,
-					  &ksm_scan.mm_slot->slot.mm_node);
-			}
-		}
+	if (!slot)
+		goto unlock;
+	mm_slot = mm_slot_entry(slot, struct ksm_mm_slot, slot);
+	if (ksm_scan.mm_slot == mm_slot)
+		goto unlock;
+	if (!mm_slot->rmap_list) {
+		hash_del(&slot->hash);
+		list_del(&slot->mm_node);
+		easy_to_free = 1;
+	} else {
+		list_move(&slot->mm_node,
+			  &ksm_scan.mm_slot->slot.mm_node);
 	}
+unlock:
 	spin_unlock(&ksm_mmlist_lock);
 
 	if (easy_to_free) {
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/2 mm/khugepaged: remove definition of struct khugepaged_mm_slot

In reply to Wei Yang

Current code calls mm_slot_entry() even when we don't have a valid slot, which is not future proof. Currently, this is not a problem because "slot" is the first member in struct khugepaged_mm_slot.

While struct khugepaged_mm_slot is just a wrapper of struct mm_slot, there is no need to define it.

Remove the definition of struct khugepaged_mm_slot, so there is not chance to miss use mm_slot_entry().

---
 mm/khugepaged.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
mm/khugepaged.c +1 -4
index 7ab2d1a42df3..abe54f0043c7 100644
@@ -376,10 +376,7 @@ int hugepage_madvise(struct vm_area_struct *vma,
 
 int __init khugepaged_init(void)
 {
-	mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
-					  sizeof(struct mm_slot),
-					  __alignof__(struct mm_slot),
-					  0, NULL);
+	mm_slot_cache = KMEM_CACHE(mm_slot, 0);
 	if (!mm_slot_cache)
 		return -ENOMEM;
 
Signature · 1 line
2.34.1
#3 lore raw
In reply to Wei Yang
Quoted 7 lines · Wei Yang

This is the fixup based on mm-stable.

The base commit:

mm: swap: check for stable address space before operating on the VMA

The reviewed version is at [1].

I'm confused: why "fixup"?

Usually fixups are applied on top of other patches (to be squashed in).

Signature · 3 lines
Cheers

David / dhildenb
author
#4 lore raw
Quoted 5 lines · David Hildenbrand
Quoted 4 lines · Wei Yang

This is the fixup based on mm-stable.

The base commit:

Quoted 8 lines
Quoted 3 lines

mm: swap: check for stable address space before operating on the VMA

The reviewed version is at [1].

I'm confused: why "fixup"?

Usually fixups are applied on top of other patches (to be squashed in).

Andrew mention he has merged v2 into mm-stable and ask for a fixup.

Not sure I misunderstand the process.

Quoted 4 lines
Signature · 3 lines
Cheers

David / dhildenb
Signature · 2 lines
Wei Yang
Help you, Help me
#5 lore raw
Quoted 8 lines · Wei Yang
Quoted 7 lines · David Hildenbrand
Quoted 6 lines · Wei Yang

This is the fixup based on mm-stable.

The base commit:

mm: swap: check for stable address space before operating on the VMA

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

The reviewed version is at [1].

I'm confused: why "fixup"?

Usually fixups are applied on top of other patches (to be squashed in).

Andrew mention he has merged v2 into mm-stable and ask for a fixup.

Ah, now I understand, some part is already in mm-stable.

In that case the patch descriptions+subjects have to be updated to clarify that these are only cleanups on top of the existing commits.

Then, also mention the merged commit in mm-stable.

E.g., for Patch #1

"
mm/ksm: cleanup mm_slot_entry() invocation

We got some late review commits during review of commit XXX ("XXX"). Let's reduce the indentation level and make the code easier to follow by using gotos to a new label.

"

E.g., for Patch #2

"
mm/khugepaged: use KMEM_CACHE()

We got some late review commits during review of commit XXX ("XXX"). No need to keep the old cache name "khugepaged_mm_slot", let's simply use KMEM_CACHE().

"

Signature · 3 lines
Cheers

David / dhildenb
author
#6 lore raw
Quoted 16 lines · David Hildenbrand
Quoted 15 lines · Wei Yang
Quoted 13 lines · David Hildenbrand
Quoted 7 lines · Wei Yang

This is the fixup based on mm-stable.

The base commit:

mm: swap: check for stable address space before operating on the VMA

The reviewed version is at [1].

I'm confused: why "fixup"?

Usually fixups are applied on top of other patches (to be squashed in).

Quoted 9 lines
Quoted 1 line

Andrew mention he has merged v2 into mm-stable and ask for a fixup.

Ah, now I understand, some part is already in mm-stable.

In that case the patch descriptions+subjects have to be updated to clarify that these are only cleanups on top of the existing commits.

Then, also mention the merged commit in mm-stable.

Ok, got it.

Let me update it.

Quoted 17 lines

E.g., for Patch #1

"
mm/ksm: cleanup mm_slot_entry() invocation

We got some late review commits during review of commit XXX ("XXX"). Let's reduce the indentation level and make the code easier to follow by using gotos to a new label.

"

E.g., for Patch #2

"
mm/khugepaged: use KMEM_CACHE()

Quoted 7 lines

We got some late review commits during review of commit XXX ("XXX"). No need to keep the old cache name "khugepaged_mm_slot", let's simply use KMEM_CACHE().

"

Thanks for these detailed description.

Signature · 2 lines
Wei Yang
Help you, Help me