linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: Wei Yang <richard.weiyang@gmail.com>, Lance Yang <lance.yang@linux.dev>
Cc: linux-mm@kvack.org, chengming.zhou@linux.dev,
	ryan.roberts@arm.com, baohua@kernel.org, xu.xin16@zte.com.cn,
	dev.jain@arm.com, npache@redhat.com, lorenzo.stoakes@oracle.com,
	Liam.Howlett@oracle.com, akpm@linux-foundation.org,
	ziy@nvidia.com, baolin.wang@linux.alibaba.com
Subject: Re: [PATCH] mm/mm_slot: make sure slot is the first element of its wrapper structure
Date: Mon, 15 Sep 2025 09:32:27 +0200	[thread overview]
Message-ID: <e368ffcc-0de9-4c8c-8e9c-6cbf8cdd22ee@redhat.com> (raw)
In-Reply-To: <20250915014738.slkw4nwihb4aavas@master>

On 15.09.25 03:47, Wei Yang wrote:
> On Sun, Sep 14, 2025 at 01:05:48PM +0800, Lance Yang wrote:
>> Hi Wei,
>>
>> On 2025/9/14 08:00, Wei Yang wrote:
>>> 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.
>>
>> Good catch! That's indeed quite brittle ;)
>>
>> Just one nit below.
>>
>>>
>>> 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");
>>> +
>>
>> I wonder if this BUILD_BUG_ON_MSG() in khugepaged_scan_mm_slot() would
>> be better placed in khugepaged_init(), like you did in ksm_init()?
> 
> khugepaged_mm_slot is defined in khugepaged.c, maybe we don't want to export
> it.
> 

We should probably just use a static_assert right nxt to the struct and 
make sure that offsetof(struct khugepaged_mm_slot, slot) == 0 ?

-- 
Cheers

David / dhildenb



  reply	other threads:[~2025-09-15  7:32 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-14  0:00 Wei Yang
2025-09-14  5:05 ` Lance Yang
2025-09-15  1:47   ` Wei Yang
2025-09-15  7:32     ` David Hildenbrand [this message]
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=e368ffcc-0de9-4c8c-8e9c-6cbf8cdd22ee@redhat.com \
    --to=david@redhat.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=dev.jain@arm.com \
    --cc=lance.yang@linux.dev \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=npache@redhat.com \
    --cc=richard.weiyang@gmail.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