The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Dev Jain <dev.jain@arm.com>
To: "David Hildenbrand (Arm)" <david@kernel.org>,
	Lance Yang <lance.yang@linux.dev>
Cc: ye.liu@linux.dev, akpm@linux-foundation.org, ljs@kernel.org,
	xhao@linux.alibaba.com, liuye@kylinos.cn, ziy@nvidia.com,
	baolin.wang@linux.alibaba.com, liam@infradead.org,
	npache@redhat.com, ryan.roberts@arm.com, baohua@kernel.org,
	akpm@linux-foudation.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] mm/khugepaged: clear MMF_VM_HUGEPAGE on mm_slot_alloc() failure
Date: Mon, 11 May 2026 11:14:45 +0530	[thread overview]
Message-ID: <fcf4693f-1dc2-44a0-a30a-5daa60de85a9@arm.com> (raw)
In-Reply-To: <5db3ccc6-7184-4068-918f-72dbfc31a781@kernel.org>



On 11/05/26 11:10 am, David Hildenbrand (Arm) wrote:
> On 5/11/26 06:00, Dev Jain wrote:
>>
>>
>> On 09/05/26 3:11 am, David Hildenbrand (Arm) wrote:
>>> On 5/6/26 12:51, Lance Yang wrote:
>>>>
>>>>
>>>> Right. A racing khugepaged_enter_vma() can see MMF_VM_HUGEPAGE is set
>>>> and return, then !slot clears it again. If there is no later
>>>> khugepaged_enter_vma(), the mm still wouldn't get registered :)
>>>
>>> So why not
>>>
>>> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
>>> index 5f4e009593e0..78735f34250a 100644
>>> --- a/mm/khugepaged.c
>>> +++ b/mm/khugepaged.c
>>> @@ -437,13 +437,16 @@ void __khugepaged_enter(struct mm_struct *mm)
>>>
>>>         /* __khugepaged_exit() must not run from under us */
>>>         VM_BUG_ON_MM(collapse_test_exit(mm), mm);
>>> -       if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm)))
>>> -               return;
>>>
>>>         slot = mm_slot_alloc(mm_slot_cache);
>>>         if (!slot)
>>>                 return;
>>>
>>> +       if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm))) {
>>> +               mm_slot_free(mm_slot_cache, slot);
>>> +               return;
>>> +       }
>>> +
>>>         spin_lock(&khugepaged_mm_lock);
>>>         mm_slot_insert(mm_slots_hash, mm, slot);
>>>         /*
>>>
>>>
>>> Arguably, on the race described above, likely the thread seeing the
>>> MMF_VM_HUGEPAGE would likely similarly have failed the allocation.
>>>
>>> I'm fine with either, just wanted to raise the (cleaner looking?) alternative
>>> where we just properly back off?
>>
>> Yes this is also fine - I am overthinking but I wasn't going this way because ...
>> A process doing THP allocations will fail on the mm_flags_test_and_set everytime
>> after the first time.
> We should perform a mm_flags_test(MMF_VM_HUGEPAGE, vma->vm_mm) test before
> calling the function when the flag might not be set yet: in khugepaged_enter_vma()

Ah that slipped my mind, you are right.

> 
> khugepaged_fork() should only get called once per process.
> 
> Which makes sense, because mm_flags_test_and_set() is expensive.
> 


      reply	other threads:[~2026-05-11  5:45 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-06  1:21 [PATCH v2] mm/khugepaged: clear MMF_VM_HUGEPAGE on mm_slot_alloc() failure Ye Liu
2026-05-06  2:05 ` Lance Yang
2026-05-06  5:17 ` Baolin Wang
2026-05-06  6:46 ` Dev Jain
2026-05-06 10:51   ` Lance Yang
2026-05-08 21:41     ` David Hildenbrand (Arm)
2026-05-09  1:57       ` Lance Yang
2026-05-11  4:00       ` Dev Jain
2026-05-11  5:40         ` David Hildenbrand (Arm)
2026-05-11  5:44           ` Dev Jain [this message]

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=fcf4693f-1dc2-44a0-a30a-5daa60de85a9@arm.com \
    --to=dev.jain@arm.com \
    --cc=akpm@linux-foudation.org \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=liuye@kylinos.cn \
    --cc=ljs@kernel.org \
    --cc=npache@redhat.com \
    --cc=ryan.roberts@arm.com \
    --cc=xhao@linux.alibaba.com \
    --cc=ye.liu@linux.dev \
    --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