linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Wei Yang <richard.weiyang@gmail.com>
To: akpm@linux-foundation.org, david@redhat.com,
	lorenzo.stoakes@oracle.com, ziy@nvidia.com,
	baolin.wang@linux.alibaba.com, Liam.Howlett@oracle.com,
	npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com,
	baohua@kernel.org, lance.yang@linux.dev, xu.xin16@zte.com.cn,
	chengming.zhou@linux.dev
Cc: linux-mm@kvack.org, Wei Yang <richard.weiyang@gmail.com>
Subject: [PATCH] mm/mm_slot: make sure slot is the first element of its wrapper structure
Date: Sun, 14 Sep 2025 00:00:26 +0000	[thread overview]
Message-ID: <20250914000026.17986-1-richard.weiyang@gmail.com> (raw)

When using mm_slot in ksm/khugepaged, there is code snip like:

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

This is only valid when mm_slot is the first element of its wrapper
structure, otherwise a NULL slot would converted to a mm_slot with
negative value. And current code thinks it is valid and continue.

Current code works since mm_slot is the first element, but make sure it
won't be disturbed.

Signed-off-by: Wei Yang <richard.weiyang@gmail.com>
---
 mm/khugepaged.c | 5 ++++-
 mm/ksm.c        | 5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index af5f5c80fe4e..668e74ad33b7 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -108,7 +108,7 @@ struct collapse_control {
  * @slot: hash lookup from mm to mm_slot
  */
 struct khugepaged_mm_slot {
-	struct mm_slot slot;
+	struct mm_slot slot;	/* keep it the first element */
 };
 
 /**
@@ -2382,6 +2382,9 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
 	struct vm_area_struct *vma;
 	int progress = 0;
 
+	BUILD_BUG_ON_MSG(mm_slot_entry(NULL, struct khugepaged_mm_slot, slot),
+			"slot should be the first element");
+
 	VM_BUG_ON(!pages);
 	lockdep_assert_held(&khugepaged_mm_lock);
 	*result = SCAN_FAIL;
diff --git a/mm/ksm.c b/mm/ksm.c
index 2ef29802a49b..0d486dbdf7d3 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -124,7 +124,7 @@ typedef u8 rmap_age_t;
  * @rmap_list: head for this mm_slot's singly-linked list of rmap_items
  */
 struct ksm_mm_slot {
-	struct mm_slot slot;
+	struct mm_slot slot;	/* keep it the first element */
 	struct ksm_rmap_item *rmap_list;
 };
 
@@ -3842,6 +3842,9 @@ static int __init ksm_init(void)
 	struct task_struct *ksm_thread;
 	int err;
 
+	BUILD_BUG_ON_MSG(mm_slot_entry(NULL, struct ksm_mm_slot, slot),
+			"slot should be the first element");
+
 	/* The correct value depends on page size and endianness */
 	zero_checksum = calc_checksum(ZERO_PAGE(0));
 	/* Default to false for backwards compatibility */
-- 
2.34.1



             reply	other threads:[~2025-09-14  0:00 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-14  0:00 Wei Yang [this message]
2025-09-14  5:05 ` Lance Yang
2025-09-15  1:47   ` Wei Yang
2025-09-15  7:32     ` David Hildenbrand
2025-09-15  7:42       ` Wei Yang
2025-09-15  7:48         ` David Hildenbrand
2025-09-15  7:52           ` Wei Yang
2025-09-14  6:21 ` Dev Jain
2025-09-14  7:03   ` Lance Yang
2025-09-14  7:29     ` Dev Jain
2025-09-14  7:39       ` Lance Yang
2025-09-14 14:16       ` xu.xin16
2025-09-15  1:57       ` Wei Yang
2025-09-15  3:49         ` Dev Jain
2025-09-15  4:05           ` Dev Jain
2025-09-15  7:46           ` Wei Yang
2025-09-15  7:57             ` David Hildenbrand
2025-09-15  7:56       ` David Hildenbrand
2025-09-15  8:08         ` Lance Yang
2025-09-15  8:26           ` David Hildenbrand
2025-09-15  8:11         ` Dev Jain
2025-09-15  8:25           ` David Hildenbrand
2025-09-15  9:07         ` Wei Yang
2025-09-15  9:15         ` Wei Yang
2025-09-15  9:39 ` Kiryl Shutsemau
2025-09-15 13:37   ` Wei Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250914000026.17986-1-richard.weiyang@gmail.com \
    --to=richard.weiyang@gmail.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=chengming.zhou@linux.dev \
    --cc=david@redhat.com \
    --cc=dev.jain@arm.com \
    --cc=lance.yang@linux.dev \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=npache@redhat.com \
    --cc=ryan.roberts@arm.com \
    --cc=xu.xin16@zte.com.cn \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox