Commit 13d60f4b authored by Zhang Yi's avatar Zhang Yi Committed by Thomas Gleixner
Browse files

futex: Take hugepages into account when generating futex_key

The futex_keys of process shared futexes are generated from the page
offset, the mapping host and the mapping index of the futex user space
address. This should result in an unique identifier for each futex.

Though this is not true when futexes are located in different subpages
of an hugepage. The reason is, that the mapping index for all those
futexes evaluates to the index of the base page of the hugetlbfs
mapping. So a futex at offset 0 of the hugepage mapping and another
one at offset PAGE_SIZE of the same hugepage mapping have identical
futex_keys. This happens because the futex code blindly uses

Steps to reproduce the bug:

1. Map a file from hugetlbfs. Initialize pthread_mutex1 at offset 0
   and pthread_mutex2 at offset PAGE_SIZE of the hugetlbfs

   The mutexes must be initialized as PTHREAD_PROCESS_SHARED because
   PTHREAD_PROCESS_PRIVATE mutexes are not affected by this issue as
   their keys solely depend on the user space address.

2. Lock mutex1 and mutex2

3. Create thread1 and in the thread function lock mutex1, which
   results in thread1 blocking on the locked mutex1.

4. Create thread2 and in the thread function lock mutex2, which
   results in thread2 blocking on the locked mutex2.

5. Unlock mutex2. Despite the fact that mutex2 got unlocked, thread2
   still blocks on mutex2 because the futex_key points to mutex1.

To solve this issue we need to take the normal page index of the page
which contains the futex into account, if the futex is in an hugetlbfs
mapping. In other words, we calculate the normal page mapping index of
the subpage in the hugetlbfs mapping.

Mappings which are not based on hugetlbfs are not affected and still
use page->index.

Thanks to Mel Gorman who provided a patch for adding proper evaluation
functions to the hugetlbfs code to avoid exposing hugetlbfs specific
details to the futex code.

[ tglx: Massaged changelog ]
Signed-off-by: default avatarZhang Yi <>
Reviewed-by: default avatarJiang Biao <>
Tested-by: default avatarMa Chenggong <>
Reviewed-by: default avatar'Mel Gorman' <>
Acked-by: default avatar'Darren Hart' <>
Cc: 'Peter Zijlstra' <>

Signed-off-by: default avatarThomas Gleixner <>
parent 0c106173
......@@ -358,6 +358,17 @@ static inline int hstate_index(struct hstate *h)
return h - hstates;
pgoff_t __basepage_index(struct page *page);
/* Return page->index in PAGE_SIZE units */
static inline pgoff_t basepage_index(struct page *page)
if (!PageCompound(page))
return page->index;
return __basepage_index(page);
struct hstate {};
#define alloc_huge_page_node(h, nid) NULL
......@@ -378,6 +389,11 @@ static inline unsigned int pages_per_huge_page(struct hstate *h)
#define hstate_index_to_shift(index) 0
#define hstate_index(h) 0
static inline pgoff_t basepage_index(struct page *page)
return page->index;
#endif /* _LINUX_HUGETLB_H */
......@@ -61,6 +61,7 @@
#include <linux/nsproxy.h>
#include <linux/ptrace.h>
#include <linux/sched/rt.h>
#include <linux/hugetlb.h>
#include <asm/futex.h>
......@@ -365,7 +366,7 @@ get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw)
} else {
key->both.offset |= FUT_OFF_INODE; /* inode-based key */
key->shared.inode = page_head->mapping->host;
key->shared.pgoff = page_head->index;
key->shared.pgoff = basepage_index(page);
......@@ -690,6 +690,23 @@ int PageHuge(struct page *page)
pgoff_t __basepage_index(struct page *page)
struct page *page_head = compound_head(page);
pgoff_t index = page_index(page_head);
unsigned long compound_idx;
if (!PageHuge(page_head))
return page_index(page);
if (compound_order(page_head) >= MAX_ORDER)
compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
compound_idx = page - page_head;
return (index << compound_order(page_head)) + compound_idx;
static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
struct page *page;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment