From: Jeongjun Park <aha310510@gmail.com>
To: muchun.song@linux.dev, osalvador@suse.de, david@redhat.com,
akpm@linux-foundation.org
Cc: leitao@debian.org, sidhartha.kumar@oracle.com,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org,
syzbot+417aeb05fd190f3a6da9@syzkaller.appspotmail.com
Subject: Re: [PATCH v2] mm/hugetlb: add missing hugetlb_lock in __unmap_hugepage_range()
Date: Tue, 26 Aug 2025 14:38:46 +0900 [thread overview]
Message-ID: <CAO9qdTF1OZRX0mbcG9hQy8m32RvrZaEBa0EWpDREBjfBSqrrYg@mail.gmail.com> (raw)
In-Reply-To: <20250823182115.1193563-1-aha310510@gmail.com>
Jeongjun Park <aha310510@gmail.com> wrote:
>
> When restoring a reservation for an anonymous page, we need to check to
> freeing a surplus. However, __unmap_hugepage_range() causes data race
> because it reads h->surplus_huge_pages without the protection of
> hugetlb_lock.
>
> And adjust_reservation is a boolean variable that indicates whether
> reservations for anonymous pages in each folio should be restored.
> Therefore, it should be initialized to false for each round of the loop.
> However, this variable is not initialized to false except when defining
> the current adjust_reservation variable.
>
> This means that once adjust_reservation is set to true even once within
> the loop, reservations for anonymous pages will be restored
> unconditionally in all subsequent rounds, regardless of the folio's state.
>
> To fix this, we need to add the missing hugetlb_lock, unlock the
> page_table_lock earlier so that we don't lock the hugetlb_lock inside the
> page_table_lock lock, and initialize adjust_reservation to false on each
> round within the loop.
>
> Cc: <stable@vger.kernel.org>
> Reported-by: syzbot+417aeb05fd190f3a6da9@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=417aeb05fd190f3a6da9
> Fixes: df7a6d1f6405 ("mm/hugetlb: restore the reservation if needed")
Reviewed-by: Sidhartha Kumar <sidhartha.kumar@oracle.com>
Sorry, I forgot to add the reviewed-by tag.
> Signed-off-by: Jeongjun Park <aha310510@gmail.com>
> ---
> v2: Fix issues with changing the page_table_lock unlock location and initializing adjust_reservation
> - Link to v1: https://lore.kernel.org/all/20250822055857.1142454-1-aha310510@gmail.com/
> ---
> mm/hugetlb.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index 753f99b4c718..eed59cfb5d21 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -5851,7 +5851,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
> spinlock_t *ptl;
> struct hstate *h = hstate_vma(vma);
> unsigned long sz = huge_page_size(h);
> - bool adjust_reservation = false;
> + bool adjust_reservation;
> unsigned long last_addr_mask;
> bool force_flush = false;
>
> @@ -5944,6 +5944,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
> sz);
> hugetlb_count_sub(pages_per_huge_page(h), mm);
> hugetlb_remove_rmap(folio);
> + spin_unlock(ptl);
>
> /*
> * Restore the reservation for anonymous page, otherwise the
> @@ -5951,14 +5952,16 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
> * If there we are freeing a surplus, do not set the restore
> * reservation bit.
> */
> + adjust_reservation = false;
> +
> + spin_lock_irq(&hugetlb_lock);
> if (!h->surplus_huge_pages && __vma_private_lock(vma) &&
> folio_test_anon(folio)) {
> folio_set_hugetlb_restore_reserve(folio);
> /* Reservation to be adjusted after the spin lock */
> adjust_reservation = true;
> }
> -
> - spin_unlock(ptl);
> + spin_unlock_irq(&hugetlb_lock);
>
> /*
> * Adjust the reservation for the region that will have the
> --
prev parent reply other threads:[~2025-08-26 5:38 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-23 18:21 [PATCH v2] mm/hugetlb: add missing hugetlb_lock in __unmap_hugepage_range() Jeongjun Park
2025-08-26 5:38 ` Jeongjun Park [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=CAO9qdTF1OZRX0mbcG9hQy8m32RvrZaEBa0EWpDREBjfBSqrrYg@mail.gmail.com \
--to=aha310510@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=leitao@debian.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=muchun.song@linux.dev \
--cc=osalvador@suse.de \
--cc=sidhartha.kumar@oracle.com \
--cc=stable@vger.kernel.org \
--cc=syzbot+417aeb05fd190f3a6da9@syzkaller.appspotmail.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;
as well as URLs for NNTP newsgroup(s).