History

mm, futex: fix shared futex pgoff on shmem huge page

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 13d60f4b6ab5 ("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]

Hugh Dickins committed 5 years ago

Commit fe19bd31 parent 5fa54344 files · +9 −28Browse files

Unified · Split

4 files
  1. include/linux/hugetlb.h +0 −16
  2. include/linux/pagemap.h +7 −6
  3. kernel/futex.c +1 −2
  4. mm/hugetlb.c +1 −4
include/linux/hugetlb.h+0−16
@@ -741,17 +741,6 @@ static inline int hstate_index(struct hstate *h) · 740 unmodified lines
741741 return h - hstates;
742742 }
743743
744−pgoff_t __basepage_index(struct page *page);
745−
746−/* Return page->index in PAGE_SIZE units */
747−static inline pgoff_t basepage_index(struct page *page)
748−{
749− if (!PageCompound(page))
750− return page->index;
751−
752− return __basepage_index(page);
753−}
754−
755744 extern int dissolve_free_huge_page(struct page *page);
756745 extern int dissolve_free_huge_pages(unsigned long start_pfn,
757746 unsigned long end_pfn);
@@ -988,11 +977,6 @@ static inline int hstate_index(struct hstate *h) · 230 unmodified lines
988977 return 0;
989978 }
990979
991−static inline pgoff_t basepage_index(struct page *page)
992−{
993− return page->index;
994−}
995−
996980 static inline int dissolve_free_huge_page(struct page *page)
997981 {
998982 return 0;
include/linux/pagemap.h+7−6
@@ -516,7 +516,7 @@ static inline struct page *read_mapping_page(struct address_space *mapping, · 515 unmodified lines
516516 }
517517
518518 /*
519− * Get index of the page with in radix-tree
519+ * Get index of the page within radix-tree (but not for hugetlb pages).
520520 * (TODO: remove once hugetlb pages will have ->index in PAGE_SIZE)
521521 */
522522 static inline pgoff_t page_to_index(struct page *page)
@@ -535,15 +535,16 @@ static inline pgoff_t page_to_index(struct page *page) · 12 unmodified lines
535535 return pgoff;
536536 }
537537
538+extern pgoff_t hugetlb_basepage_index(struct page *page);
539+
538540 /*
539− * Get the offset in PAGE_SIZE.
540− * (TODO: hugepage should have ->index in PAGE_SIZE)
541+ * Get the offset in PAGE_SIZE (even for hugetlb pages).
542+ * (TODO: hugetlb pages should have ->index in PAGE_SIZE)
541543 */
542544 static inline pgoff_t page_to_pgoff(struct page *page)
543545 {
544− if (unlikely(PageHeadHuge(page)))
545− return page->index << compound_order(page);
546−
546+ if (unlikely(PageHuge(page)))
547+ return hugetlb_basepage_index(page);
547548 return page_to_index(page);
548549 }
549550
kernel/futex.c+1−2
@@ -35,7 +35,6 @@ · 34 unmodified lines
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>
@@ -650,7 +649,7 @@ again: · 608 unmodified lines
650649
651650 key->both.offset |= FUT_OFF_INODE; /* inode-based key */
652651 key->shared.i_seq = get_inode_sequence_number(inode);
653− key->shared.pgoff = basepage_index(tail);
652+ key->shared.pgoff = page_to_pgoff(tail);
654653 rcu_read_unlock();
655654 }
656655
mm/hugetlb.c+1−4
@@ -1588,15 +1588,12 @@ struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage) · 1587 unmodified lines
15881588 return NULL;
15891589 }
15901590
1591−pgoff_t __basepage_index(struct page *page)
1591+pgoff_t hugetlb_basepage_index(struct page *page)
15921592 {
15931593 struct page *page_head = compound_head(page);
15941594 pgoff_t index = page_index(page_head);
15951595 unsigned long compound_idx;
15961596
1597− if (!PageHuge(page_head))
1598− return page_index(page);
1599−
16001597 if (compound_order(page_head) >= MAX_ORDER)
16011598 compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
16021599 else