Skip to content

Commit 377a796

Browse files
Hugh DickinsSasha Levin
authored andcommitted
mm, futex: fix shared futex pgoff on shmem huge page
commit fe19bd3 upstream. If more than one futex is placed on a shmem huge page, it can happen that waking the second wakes the first instead, and leaves the second waiting: the key's shared.pgoff is wrong. When 3.11 commit 13d60f4 ("futex: Take hugepages into account when generating futex_key"), the only shared huge pages came from hugetlbfs, and the code added to deal with its exceptional page->index was put into hugetlb source. Then that was missed when 4.8 added shmem huge pages. page_to_pgoff() is what others use for this nowadays: except that, as currently written, it gives the right answer on hugetlbfs head, but nonsense on hugetlbfs tails. Fix that by calling hugetlbfs-specific hugetlb_basepage_index() on PageHuge tails as well as on head. Yes, it's unconventional to declare hugetlb_basepage_index() there in pagemap.h, rather than in hugetlb.h; but I do not expect anything but page_to_pgoff() ever to need it. [akpm@linux-foundation.org: give hugetlb_basepage_index() prototype the correct scope] Link: https://lkml.kernel.org/r/b17d946b-d09-326e-b42a-52884c36df32@google.com Fixes: 800d8c6 ("shmem: add huge pages support") Reported-by: Neel Natu <neelnatu@google.com> Signed-off-by: Hugh Dickins <hughd@google.com> Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org> Acked-by: Thomas Gleixner <tglx@linutronix.de> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Cc: Zhang Yi <wetpzy@gmail.com> Cc: Mel Gorman <mgorman@techsingularity.net> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Darren Hart <dvhart@infradead.org> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ab9d178 commit 377a796

4 files changed

Lines changed: 9 additions & 28 deletions

File tree

‎include/linux/hugetlb.h‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -628,17 +628,6 @@ static inline int hstate_index(struct hstate *h)
628628
return h - hstates;
629629
}
630630

631-
pgoff_t __basepage_index(struct page *page);
632-
633-
/* Return page->index in PAGE_SIZE units */
634-
static inline pgoff_t basepage_index(struct page *page)
635-
{
636-
if (!PageCompound(page))
637-
return page->index;
638-
639-
return __basepage_index(page);
640-
}
641-
642631
extern int dissolve_free_huge_page(struct page *page);
643632
extern int dissolve_free_huge_pages(unsigned long start_pfn,
644633
unsigned long end_pfn);
@@ -871,11 +860,6 @@ static inline int hstate_index(struct hstate *h)
871860
return 0;
872861
}
873862

874-
static inline pgoff_t basepage_index(struct page *page)
875-
{
876-
return page->index;
877-
}
878-
879863
static inline int dissolve_free_huge_page(struct page *page)
880864
{
881865
return 0;

‎include/linux/pagemap.h‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ static inline struct page *read_mapping_page(struct address_space *mapping,
501501
}
502502

503503
/*
504-
* Get index of the page with in radix-tree
504+
* Get index of the page within radix-tree (but not for hugetlb pages).
505505
* (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE)
506506
*/
507507
static inline pgoff_t page_to_index(struct page *page)
@@ -520,15 +520,16 @@ static inline pgoff_t page_to_index(struct page *page)
520520
return pgoff;
521521
}
522522

523+
extern pgoff_t hugetlb_basepage_index(struct page *page);
524+
523525
/*
524-
* Get the offset in PAGE_SIZE.
525-
* (TODO: hugepage should have ->index in PAGE_SIZE)
526+
* Get the offset in PAGE_SIZE (even for hugetlb pages).
527+
* (TODO: hugetlb pages should have ->index in PAGE_SIZE)
526528
*/
527529
static inline pgoff_t page_to_pgoff(struct page *page)
528530
{
529-
if (unlikely(PageHeadHuge(page)))
530-
return page->index << compound_order(page);
531-
531+
if (unlikely(PageHuge(page)))
532+
return hugetlb_basepage_index(page);
532533
return page_to_index(page);
533534
}
534535

‎kernel/futex.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <linux/jhash.h>
3636
#include <linux/pagemap.h>
3737
#include <linux/syscalls.h>
38-
#include <linux/hugetlb.h>
3938
#include <linux/freezer.h>
4039
#include <linux/memblock.h>
4140
#include <linux/fault-inject.h>
@@ -652,7 +651,7 @@ static int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key,
652651

653652
key->both.offset |= FUT_OFF_INODE; /* inode-based key */
654653
key->shared.i_seq = get_inode_sequence_number(inode);
655-
key->shared.pgoff = basepage_index(tail);
654+
key->shared.pgoff = page_to_pgoff(tail);
656655
rcu_read_unlock();
657656
}
658657

‎mm/hugetlb.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,15 +1635,12 @@ struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage)
16351635
return NULL;
16361636
}
16371637

1638-
pgoff_t __basepage_index(struct page *page)
1638+
pgoff_t hugetlb_basepage_index(struct page *page)
16391639
{
16401640
struct page *page_head = compound_head(page);
16411641
pgoff_t index = page_index(page_head);
16421642
unsigned long compound_idx;
16431643

1644-
if (!PageHuge(page_head))
1645-
return page_index(page);
1646-
16471644
if (compound_order(page_head) >= MAX_ORDER)
16481645
compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
16491646
else

0 commit comments

Comments
 (0)