Commit f5ecfe8f authored by Hugh Dickins's avatar Hugh Dickins Committed by Linus Torvalds

[PATCH] Fix futex hashing bugs

This fixes two buts that the glibc NPTL verification tests found, one
new and one old.

The new bug is that "offset" has been declared as an alternative in
the union, instead of as an element in the structures comprising it,
effectively eliminating it from the key: keys match which should not.

The old bug is that if futex_requeue were called with identical
key1 and key2 (sensible? tended to happen given the first bug),
it was liable to loop for a long time holding futex_lock: guard
against that, still respecting the semantics of futex_requeue.

While here, please let's also fix the get_futex_key VM_NONLINEAR
case, which was returning the 1 from get_user_pages, taken as an
error by its callers.  And save a few bytes and improve debuggability
by uninlining the top-level futex_wake, futex_requeue, futex_wait.
parent faff7e92
...@@ -49,16 +49,18 @@ union futex_key { ...@@ -49,16 +49,18 @@ union futex_key {
struct { struct {
unsigned long pgoff; unsigned long pgoff;
struct inode *inode; struct inode *inode;
int offset;
} shared; } shared;
struct { struct {
unsigned long uaddr; unsigned long uaddr;
struct mm_struct *mm; struct mm_struct *mm;
int offset;
} private; } private;
struct { struct {
unsigned long word; unsigned long word;
void *ptr; void *ptr;
int offset;
} both; } both;
int offset;
}; };
/* /*
...@@ -91,7 +93,7 @@ static inline struct list_head *hash_futex(union futex_key *key) ...@@ -91,7 +93,7 @@ static inline struct list_head *hash_futex(union futex_key *key)
{ {
return &futex_queues[hash_long(key->both.word return &futex_queues[hash_long(key->both.word
+ (unsigned long) key->both.ptr + (unsigned long) key->both.ptr
+ key->offset, FUTEX_HASHBITS)]; + key->both.offset, FUTEX_HASHBITS)];
} }
/* /*
...@@ -101,7 +103,7 @@ static inline int match_futex(union futex_key *key1, union futex_key *key2) ...@@ -101,7 +103,7 @@ static inline int match_futex(union futex_key *key1, union futex_key *key2)
{ {
return (key1->both.word == key2->both.word return (key1->both.word == key2->both.word
&& key1->both.ptr == key2->both.ptr && key1->both.ptr == key2->both.ptr
&& key1->offset == key2->offset); && key1->both.offset == key2->both.offset);
} }
/* /*
...@@ -127,10 +129,10 @@ static int get_futex_key(unsigned long uaddr, union futex_key *key) ...@@ -127,10 +129,10 @@ static int get_futex_key(unsigned long uaddr, union futex_key *key)
/* /*
* The futex address must be "naturally" aligned. * The futex address must be "naturally" aligned.
*/ */
key->offset = uaddr % PAGE_SIZE; key->both.offset = uaddr % PAGE_SIZE;
if (unlikely((key->offset % sizeof(u32)) != 0)) if (unlikely((key->both.offset % sizeof(u32)) != 0))
return -EINVAL; return -EINVAL;
uaddr -= key->offset; uaddr -= key->both.offset;
/* /*
* The futex is hashed differently depending on whether * The futex is hashed differently depending on whether
...@@ -199,6 +201,7 @@ static int get_futex_key(unsigned long uaddr, union futex_key *key) ...@@ -199,6 +201,7 @@ static int get_futex_key(unsigned long uaddr, union futex_key *key)
key->shared.pgoff = key->shared.pgoff =
page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
put_page(page); put_page(page);
return 0;
} }
return err; return err;
} }
...@@ -208,7 +211,7 @@ static int get_futex_key(unsigned long uaddr, union futex_key *key) ...@@ -208,7 +211,7 @@ static int get_futex_key(unsigned long uaddr, union futex_key *key)
* Wake up all waiters hashed on the physical page that is mapped * Wake up all waiters hashed on the physical page that is mapped
* to this virtual address: * to this virtual address:
*/ */
static inline int futex_wake(unsigned long uaddr, int num) static int futex_wake(unsigned long uaddr, int num)
{ {
struct list_head *i, *next, *head; struct list_head *i, *next, *head;
union futex_key key; union futex_key key;
...@@ -247,7 +250,7 @@ static inline int futex_wake(unsigned long uaddr, int num) ...@@ -247,7 +250,7 @@ static inline int futex_wake(unsigned long uaddr, int num)
* Requeue all waiters hashed on one physical page to another * Requeue all waiters hashed on one physical page to another
* physical page. * physical page.
*/ */
static inline int futex_requeue(unsigned long uaddr1, unsigned long uaddr2, static int futex_requeue(unsigned long uaddr1, unsigned long uaddr2,
int nr_wake, int nr_requeue) int nr_wake, int nr_requeue)
{ {
struct list_head *i, *next, *head1, *head2; struct list_head *i, *next, *head1, *head2;
...@@ -282,6 +285,9 @@ static inline int futex_requeue(unsigned long uaddr1, unsigned long uaddr2, ...@@ -282,6 +285,9 @@ static inline int futex_requeue(unsigned long uaddr1, unsigned long uaddr2,
this->key = key2; this->key = key2;
if (ret - nr_wake >= nr_requeue) if (ret - nr_wake >= nr_requeue)
break; break;
/* Make sure to stop if key1 == key2 */
if (head1 == head2 && head1 != next)
head1 = i;
} }
} }
} }
...@@ -320,7 +326,7 @@ static inline int unqueue_me(struct futex_q *q) ...@@ -320,7 +326,7 @@ static inline int unqueue_me(struct futex_q *q)
return ret; return ret;
} }
static inline int futex_wait(unsigned long uaddr, int val, unsigned long time) static int futex_wait(unsigned long uaddr, int val, unsigned long time)
{ {
DECLARE_WAITQUEUE(wait, current); DECLARE_WAITQUEUE(wait, current);
int ret, curval; int ret, curval;
......
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