Skip to contentSkip to conversation

hugetlb: only adjust reservation during unmapping if mapcount is 0

PATCH Applied Guillaume Morin 3 participants 4 messages Jul 28 – Jul 31

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 hugetlb: only adjust reservation during unmapping if mapcount is 0

Since df7a6d1f6405, __unmap_hugepage_range can adjust reservations. In the case of folio mapped in both a parent and a child, if the parent unmaps the range first, the reservation adjustment will result in an underflow of the reserved count. Once the child unmaps the range, the count is restored.
Change __unmap_hugepage_range() to check the mapcount before adjusting the reservation.

Fixes df7a6d1f6405 ("mm/hugetlb: restore the reservation if needed")
Cc Muchun Song <[email protected]>
Cc Breno Leitao <[email protected]>
Cc Rik van Riel <[email protected]>
Cc David Hildenbrand <[email protected]>

---
Changes in v3:
- Document locking requirements
- Use folio_mapped() instead folio_mapcount()

Changes in v2:
- Document the intent in comment
- Tweak the commit message

---
 mm/hugetlb.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
mm/hugetlb.c +6 -2
index e93c4d2456aa..77022c4c59af 100644
@@ -5157,6 +5157,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 	bool adjust_reservation;
 	unsigned long last_addr_mask;
 
+	i_mmap_assert_write_locked(vma->vm_file->f_mapping);
 	WARN_ON(!is_vm_hugetlb_page(vma));
 	BUG_ON(start & ~huge_page_mask(h));
 	BUG_ON(end & ~huge_page_mask(h));
@@ -5248,7 +5249,10 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 
 		/*
 		 * Restore the reservation for anonymous page, otherwise the
-		 * backing page could be stolen by someone.
+		 * backing page could be stolen by someone. Restore only on the
+		 * last unmap, otherwise the owner could empty its resv map
+		 * while the folio is still mapped by a child. Note that holding
+		 * i_mmap_lock_write is needed to check the number of mappings.
 		 * If there we are freeing a surplus, do not set the restore
 		 * reservation bit.
 		 */
@@ -5256,7 +5260,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 
 		spin_lock_irq(&hugetlb_lock);
 		if (!h->surplus_huge_pages && __vma_private_lock(vma) &&
-		    folio_test_anon(folio)) {
+		    !folio_mapped(folio) && folio_test_anon(folio)) {
 			folio_set_hugetlb_restore_reserve(folio);
 			/* Reservation to be adjusted after the spin lock */
 			adjust_reservation = true;
Signature · 5 lines
2.39.1


-- 
Guillaume Morin <[email protected]>
Reviewed-by on v3 1/1
#1 lore raw
Quoted 13 lines · Guillaume Morin

Since df7a6d1f6405, __unmap_hugepage_range can adjust reservations. In
the case of folio mapped in both a parent and a child, if the parent unmaps the range first, the reservation adjustment will result in an underflow of the reserved count. Once the child unmaps the range, the
count is restored.
Change __unmap_hugepage_range() to check the mapcount before adjusting
the reservation.

Link:
https://lore.kernel.org/all/[email protected]/

Quoted 8 lines
Fixes df7a6d1f6405 ("mm/hugetlb: restore the reservation if needed")
Cc Muchun Song <[email protected]>
Cc Breno Leitao <[email protected]>
Cc Rik van Riel <[email protected]>
Cc David Hildenbrand <[email protected]>
Signature · 1 line
All Rights Reversed.
maintainer
#2 lore raw
In reply to Guillaume Morin
Quoted 7 lines · On Tue, 28 Jul 2026 21:29:03 +0200 Guillaume Morin <[email protected]>…

Since df7a6d1f6405, __unmap_hugepage_range can adjust reservations. In the case of folio mapped in both a parent and a child, if the parent unmaps the range first, the reservation adjustment will result in an underflow of the reserved count. Once the child unmaps the range, the count is restored.
Change __unmap_hugepage_range() to check the mapcount before adjusting the reservation.

Thanks. AI review mentions a couple of things, one pre-existing:
https://sashiko.dev/#/patchset/[email protected]

mm/hugetlb.c void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 5265

Quoted 10 lines
mm/hugetlb.c +1 -1
@@ -5256,7 +5260,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 
 		spin_lock_irq(&hugetlb_lock);
 		if (!h->surplus_huge_pages && __vma_private_lock(vma) &&
-		    folio_test_anon(folio)) {
+		    !folio_mapped(folio) && folio_test_anon(folio)) {
 			folio_set_hugetlb_restore_reserve(folio);
 			/* Reservation to be adjusted after the spin lock */

I assume that meant "after the spin_unlock".

What are the locking requirements for vma_add_reservation() (__vma_reservation_common()), btw?

#3 lore raw
Quoted 11 lines · Andrew Morton
Quoted 7 lines · On Tue, 28 Jul 2026 21:29:03 +0200 Guillaume Morin <[email protected]>…

Since df7a6d1f6405, __unmap_hugepage_range can adjust reservations. In the case of folio mapped in both a parent and a child, if the parent unmaps the range first, the reservation adjustment will result in an underflow of the reserved count. Once the child unmaps the range, the count is restored.
Change __unmap_hugepage_range() to check the mapcount before adjusting the reservation.

Quoted 2 lines

Thanks. AI review mentions a couple of things, one pre-existing:
https://sashiko.dev/#/patchset/[email protected]

Not sure about the pre-existing one, perhaps Bruno/Rik can comment?

But if I understand the bot scenario correctly, the new issue is incorrect.

The bot is correct that during the 2nd fault region_add() will return 0 when the new reservation is committed. But that return value is changed into a 1 at the end of __vma_reservation_common() for private mappings (there is a lengthy comment about that). Therefore it does not fallback to the 'hugepage_subpool_put_pages(spool, 1)' path and therefore there shouldn't be any resv count change.

I wrote a quick repro of the scenario as I understand it and the reserved count is as expected.

#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

int hugetlb_nr_resv_pages(unsigned long *num)

{
       const char* field = "HugePages_Rsvd: ";
       FILE *f = fopen("/proc/meminfo", "r");
       char line[256];

       if (!f) {
               perror("Could not open /proc/meminfo");
               return 0;
       }
       while (fgets(line, sizeof(line), f)) {
               if (strncmp(line, field, strlen(field)))
                       continue;
               const char *p = line + strlen(field);
               while (*p && isspace(*p))
                       ++p;
               if (!*p) {
                       break;
               }
               *num = strtoul(p, NULL, 10);
               fclose(f);
               return 1;
       }
       fclose(f);
       return 0;
}
int main(void) {
    pid_t pid;
    unsigned long num;
    void *mem = mmap(NULL, 2048*1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
    if (mem == MAP_FAILED) {
        perror("mmap");
        return 1;
    }
    if (!hugetlb_nr_resv_pages(&num)) {
        perror("hugetlb_nr_resv_pages");
        return 1;
    }
    printf("Before the initial fault, number of reserved huge pages: %lu\n", num);
    *(char *)mem = 1;
    if (!hugetlb_nr_resv_pages(&num)) {
        perror("hugetlb_nr_resv_pages");
        return 1;
    }
    printf("After the initial fault, number of reserved huge pages: %lu\n", num);

    pid = fork();
    if (pid == -1) {
        perror("fork");
        return 1;
    }
    if (pid == 0) {
        // Child process
        pause();
        return 0;
    } 
    // Parent process
    if (madvise(mem, 2048*1024, MADV_DONTNEED)) {
        perror("madvise");
        return 1;
    }
    if (!hugetlb_nr_resv_pages(&num)) {
        perror("hugetlb_nr_resv_pages");
        return 1;
    }
    printf("After MADV_DONTNEED, number of reserved huge pages: %lu\n", num);
    if (!hugetlb_nr_resv_pages(&num)) {
        perror("hugetlb_nr_resv_pages");
        return 1;
    }
    *(char *)mem = 1;
    if (!hugetlb_nr_resv_pages(&num)) {
        perror("hugetlb_nr_resv_pages");
        return 1;
    }
    printf("After the new fault, number of reserved huge pages: %lu\n", num);
    kill(pid, SIGINT);

    return 0;
}
Signature · 1 line
Guillaume Morin <[email protected]>