Re: [PATCH 1/2] mm: fix double mmap_sem unlock on MMF_UNSTABLE enforced SIGBUS

2017-08-14 Thread David Rientjes
On Mon, 7 Aug 2017, Michal Hocko wrote:

> From: Michal Hocko 
> 
> Tetsuo Handa has noticed that MMF_UNSTABLE SIGBUS path in
> handle_mm_fault causes a lockdep splat
> [   58.539455] Out of memory: Kill process 1056 (a.out) score 603 or 
> sacrifice child
> [   58.543943] Killed process 1056 (a.out) total-vm:4268108kB, 
> anon-rss:2246048kB, file-rss:0kB, shmem-rss:0kB
> [   58.544245] a.out (1169) used greatest stack depth: 11664 bytes left
> [   58.557471] DEBUG_LOCKS_WARN_ON(depth <= 0)
> [   58.557480] [ cut here ]
> [   58.564407] WARNING: CPU: 6 PID: 1339 at kernel/locking/lockdep.c:3617 
> lock_release+0x172/0x1e0
> [   58.599401] CPU: 6 PID: 1339 Comm: a.out Not tainted 
> 4.13.0-rc3-next-20170803+ #142
> [   58.604126] Hardware name: VMware, Inc. VMware Virtual Platform/440BX 
> Desktop Reference Platform, BIOS 6.00 07/02/2015
> [   58.609790] task: 9d90df888040 task.stack: a07084854000
> [   58.613944] RIP: 0010:lock_release+0x172/0x1e0
> [   58.617622] RSP: :a07084857e58 EFLAGS: 00010082
> [   58.621533] RAX: 001f RBX: 9d90df888040 RCX: 
> 
> [   58.626074] RDX:  RSI: 0001 RDI: 
> a30d4ba4
> [   58.630572] RBP: a07084857e98 R08:  R09: 
> 0001
> [   58.635016] R10:  R11: 001f R12: 
> a07084857f58
> [   58.639694] R13: 9d90f60d6cd0 R14:  R15: 
> a305cb6e
> [   58.644200] FS:  7fb932730740() GS:9d90f9f8() 
> knlGS:
> [   58.648989] CS:  0010 DS:  ES:  CR0: 80050033
> [   58.652903] CR2: 0040092f CR3: 000135229000 CR4: 
> 000606e0
> [   58.657280] Call Trace:
> [   58.659989]  up_read+0x1a/0x40
> [   58.662825]  __do_page_fault+0x28e/0x4c0
> [   58.665946]  do_page_fault+0x30/0x80
> [   58.668911]  page_fault+0x28/0x30
> 
> The reason is that the page fault path might have dropped the mmap_sem
> and returned with VM_FAULT_RETRY. MMF_UNSTABLE check however rewrites
> the error path to VM_FAULT_SIGBUS and we always expect mmap_sem taken in
> that path. Fix this by taking mmap_sem when VM_FAULT_RETRY is held in
> the MMF_UNSTABLE path. We cannot simply add VM_FAULT_SIGBUS to the
> existing error code because all arch specific page fault handlers and
> g-u-p would have to learn a new error code combination.
> 
> Reported-by: Tetsuo Handa 
> Fixes: 3f70dc38cec2 ("mm: make sure that kthreads will not refault oom reaped 
> memory")
> Cc: stable # 4.9+
> Signed-off-by: Michal Hocko 

Acked-by: David Rientjes 


Re: [PATCH 1/2] mm: fix double mmap_sem unlock on MMF_UNSTABLE enforced SIGBUS

2017-08-14 Thread David Rientjes
On Mon, 7 Aug 2017, Michal Hocko wrote:

> From: Michal Hocko 
> 
> Tetsuo Handa has noticed that MMF_UNSTABLE SIGBUS path in
> handle_mm_fault causes a lockdep splat
> [   58.539455] Out of memory: Kill process 1056 (a.out) score 603 or 
> sacrifice child
> [   58.543943] Killed process 1056 (a.out) total-vm:4268108kB, 
> anon-rss:2246048kB, file-rss:0kB, shmem-rss:0kB
> [   58.544245] a.out (1169) used greatest stack depth: 11664 bytes left
> [   58.557471] DEBUG_LOCKS_WARN_ON(depth <= 0)
> [   58.557480] [ cut here ]
> [   58.564407] WARNING: CPU: 6 PID: 1339 at kernel/locking/lockdep.c:3617 
> lock_release+0x172/0x1e0
> [   58.599401] CPU: 6 PID: 1339 Comm: a.out Not tainted 
> 4.13.0-rc3-next-20170803+ #142
> [   58.604126] Hardware name: VMware, Inc. VMware Virtual Platform/440BX 
> Desktop Reference Platform, BIOS 6.00 07/02/2015
> [   58.609790] task: 9d90df888040 task.stack: a07084854000
> [   58.613944] RIP: 0010:lock_release+0x172/0x1e0
> [   58.617622] RSP: :a07084857e58 EFLAGS: 00010082
> [   58.621533] RAX: 001f RBX: 9d90df888040 RCX: 
> 
> [   58.626074] RDX:  RSI: 0001 RDI: 
> a30d4ba4
> [   58.630572] RBP: a07084857e98 R08:  R09: 
> 0001
> [   58.635016] R10:  R11: 001f R12: 
> a07084857f58
> [   58.639694] R13: 9d90f60d6cd0 R14:  R15: 
> a305cb6e
> [   58.644200] FS:  7fb932730740() GS:9d90f9f8() 
> knlGS:
> [   58.648989] CS:  0010 DS:  ES:  CR0: 80050033
> [   58.652903] CR2: 0040092f CR3: 000135229000 CR4: 
> 000606e0
> [   58.657280] Call Trace:
> [   58.659989]  up_read+0x1a/0x40
> [   58.662825]  __do_page_fault+0x28e/0x4c0
> [   58.665946]  do_page_fault+0x30/0x80
> [   58.668911]  page_fault+0x28/0x30
> 
> The reason is that the page fault path might have dropped the mmap_sem
> and returned with VM_FAULT_RETRY. MMF_UNSTABLE check however rewrites
> the error path to VM_FAULT_SIGBUS and we always expect mmap_sem taken in
> that path. Fix this by taking mmap_sem when VM_FAULT_RETRY is held in
> the MMF_UNSTABLE path. We cannot simply add VM_FAULT_SIGBUS to the
> existing error code because all arch specific page fault handlers and
> g-u-p would have to learn a new error code combination.
> 
> Reported-by: Tetsuo Handa 
> Fixes: 3f70dc38cec2 ("mm: make sure that kthreads will not refault oom reaped 
> memory")
> Cc: stable # 4.9+
> Signed-off-by: Michal Hocko 

Acked-by: David Rientjes 


[PATCH 1/2] mm: fix double mmap_sem unlock on MMF_UNSTABLE enforced SIGBUS

2017-08-07 Thread Michal Hocko
From: Michal Hocko 

Tetsuo Handa has noticed that MMF_UNSTABLE SIGBUS path in
handle_mm_fault causes a lockdep splat
[   58.539455] Out of memory: Kill process 1056 (a.out) score 603 or sacrifice 
child
[   58.543943] Killed process 1056 (a.out) total-vm:4268108kB, 
anon-rss:2246048kB, file-rss:0kB, shmem-rss:0kB
[   58.544245] a.out (1169) used greatest stack depth: 11664 bytes left
[   58.557471] DEBUG_LOCKS_WARN_ON(depth <= 0)
[   58.557480] [ cut here ]
[   58.564407] WARNING: CPU: 6 PID: 1339 at kernel/locking/lockdep.c:3617 
lock_release+0x172/0x1e0
[   58.599401] CPU: 6 PID: 1339 Comm: a.out Not tainted 
4.13.0-rc3-next-20170803+ #142
[   58.604126] Hardware name: VMware, Inc. VMware Virtual Platform/440BX 
Desktop Reference Platform, BIOS 6.00 07/02/2015
[   58.609790] task: 9d90df888040 task.stack: a07084854000
[   58.613944] RIP: 0010:lock_release+0x172/0x1e0
[   58.617622] RSP: :a07084857e58 EFLAGS: 00010082
[   58.621533] RAX: 001f RBX: 9d90df888040 RCX: 
[   58.626074] RDX:  RSI: 0001 RDI: a30d4ba4
[   58.630572] RBP: a07084857e98 R08:  R09: 0001
[   58.635016] R10:  R11: 001f R12: a07084857f58
[   58.639694] R13: 9d90f60d6cd0 R14:  R15: a305cb6e
[   58.644200] FS:  7fb932730740() GS:9d90f9f8() 
knlGS:
[   58.648989] CS:  0010 DS:  ES:  CR0: 80050033
[   58.652903] CR2: 0040092f CR3: 000135229000 CR4: 000606e0
[   58.657280] Call Trace:
[   58.659989]  up_read+0x1a/0x40
[   58.662825]  __do_page_fault+0x28e/0x4c0
[   58.665946]  do_page_fault+0x30/0x80
[   58.668911]  page_fault+0x28/0x30

The reason is that the page fault path might have dropped the mmap_sem
and returned with VM_FAULT_RETRY. MMF_UNSTABLE check however rewrites
the error path to VM_FAULT_SIGBUS and we always expect mmap_sem taken in
that path. Fix this by taking mmap_sem when VM_FAULT_RETRY is held in
the MMF_UNSTABLE path. We cannot simply add VM_FAULT_SIGBUS to the
existing error code because all arch specific page fault handlers and
g-u-p would have to learn a new error code combination.

Reported-by: Tetsuo Handa 
Fixes: 3f70dc38cec2 ("mm: make sure that kthreads will not refault oom reaped 
memory")
Cc: stable # 4.9+
Signed-off-by: Michal Hocko 
---
 mm/memory.c | 12 +++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/mm/memory.c b/mm/memory.c
index 0e517be91a89..4fe5b6254688 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3881,8 +3881,18 @@ int handle_mm_fault(struct vm_area_struct *vma, unsigned 
long address,
 * further.
 */
if (unlikely((current->flags & PF_KTHREAD) && !(ret & VM_FAULT_ERROR)
-   && test_bit(MMF_UNSTABLE, >vm_mm->flags)))
+   && test_bit(MMF_UNSTABLE, >vm_mm->flags))) 
{
+
+   /*
+* We are going to enforce SIGBUS but the PF path might have
+* dropped the mmap_sem already so take it again so that
+* we do not break expectations of all arch specific PF paths
+* and g-u-p
+*/
+   if (ret & VM_FAULT_RETRY)
+   down_read(>vm_mm->mmap_sem);
ret = VM_FAULT_SIGBUS;
+   }
 
return ret;
 }
-- 
2.13.2



[PATCH 1/2] mm: fix double mmap_sem unlock on MMF_UNSTABLE enforced SIGBUS

2017-08-07 Thread Michal Hocko
From: Michal Hocko 

Tetsuo Handa has noticed that MMF_UNSTABLE SIGBUS path in
handle_mm_fault causes a lockdep splat
[   58.539455] Out of memory: Kill process 1056 (a.out) score 603 or sacrifice 
child
[   58.543943] Killed process 1056 (a.out) total-vm:4268108kB, 
anon-rss:2246048kB, file-rss:0kB, shmem-rss:0kB
[   58.544245] a.out (1169) used greatest stack depth: 11664 bytes left
[   58.557471] DEBUG_LOCKS_WARN_ON(depth <= 0)
[   58.557480] [ cut here ]
[   58.564407] WARNING: CPU: 6 PID: 1339 at kernel/locking/lockdep.c:3617 
lock_release+0x172/0x1e0
[   58.599401] CPU: 6 PID: 1339 Comm: a.out Not tainted 
4.13.0-rc3-next-20170803+ #142
[   58.604126] Hardware name: VMware, Inc. VMware Virtual Platform/440BX 
Desktop Reference Platform, BIOS 6.00 07/02/2015
[   58.609790] task: 9d90df888040 task.stack: a07084854000
[   58.613944] RIP: 0010:lock_release+0x172/0x1e0
[   58.617622] RSP: :a07084857e58 EFLAGS: 00010082
[   58.621533] RAX: 001f RBX: 9d90df888040 RCX: 
[   58.626074] RDX:  RSI: 0001 RDI: a30d4ba4
[   58.630572] RBP: a07084857e98 R08:  R09: 0001
[   58.635016] R10:  R11: 001f R12: a07084857f58
[   58.639694] R13: 9d90f60d6cd0 R14:  R15: a305cb6e
[   58.644200] FS:  7fb932730740() GS:9d90f9f8() 
knlGS:
[   58.648989] CS:  0010 DS:  ES:  CR0: 80050033
[   58.652903] CR2: 0040092f CR3: 000135229000 CR4: 000606e0
[   58.657280] Call Trace:
[   58.659989]  up_read+0x1a/0x40
[   58.662825]  __do_page_fault+0x28e/0x4c0
[   58.665946]  do_page_fault+0x30/0x80
[   58.668911]  page_fault+0x28/0x30

The reason is that the page fault path might have dropped the mmap_sem
and returned with VM_FAULT_RETRY. MMF_UNSTABLE check however rewrites
the error path to VM_FAULT_SIGBUS and we always expect mmap_sem taken in
that path. Fix this by taking mmap_sem when VM_FAULT_RETRY is held in
the MMF_UNSTABLE path. We cannot simply add VM_FAULT_SIGBUS to the
existing error code because all arch specific page fault handlers and
g-u-p would have to learn a new error code combination.

Reported-by: Tetsuo Handa 
Fixes: 3f70dc38cec2 ("mm: make sure that kthreads will not refault oom reaped 
memory")
Cc: stable # 4.9+
Signed-off-by: Michal Hocko 
---
 mm/memory.c | 12 +++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/mm/memory.c b/mm/memory.c
index 0e517be91a89..4fe5b6254688 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3881,8 +3881,18 @@ int handle_mm_fault(struct vm_area_struct *vma, unsigned 
long address,
 * further.
 */
if (unlikely((current->flags & PF_KTHREAD) && !(ret & VM_FAULT_ERROR)
-   && test_bit(MMF_UNSTABLE, >vm_mm->flags)))
+   && test_bit(MMF_UNSTABLE, >vm_mm->flags))) 
{
+
+   /*
+* We are going to enforce SIGBUS but the PF path might have
+* dropped the mmap_sem already so take it again so that
+* we do not break expectations of all arch specific PF paths
+* and g-u-p
+*/
+   if (ret & VM_FAULT_RETRY)
+   down_read(>vm_mm->mmap_sem);
ret = VM_FAULT_SIGBUS;
+   }
 
return ret;
 }
-- 
2.13.2



[PATCH 1/2] mm: fix double mmap_sem unlock on MMF_UNSTABLE enforced SIGBUS

2017-08-04 Thread Michal Hocko
From: Michal Hocko 

Tetsuo Handa has noticed that MMF_UNSTABLE SIGBUS path in
handle_mm_fault causes a lockdep splat
[   58.539455] Out of memory: Kill process 1056 (a.out) score 603 or sacrifice 
child
[   58.543943] Killed process 1056 (a.out) total-vm:4268108kB, 
anon-rss:2246048kB, file-rss:0kB, shmem-rss:0kB
[   58.544245] a.out (1169) used greatest stack depth: 11664 bytes left
[   58.557471] DEBUG_LOCKS_WARN_ON(depth <= 0)
[   58.557480] [ cut here ]
[   58.564407] WARNING: CPU: 6 PID: 1339 at kernel/locking/lockdep.c:3617 
lock_release+0x172/0x1e0
[   58.599401] CPU: 6 PID: 1339 Comm: a.out Not tainted 
4.13.0-rc3-next-20170803+ #142
[   58.604126] Hardware name: VMware, Inc. VMware Virtual Platform/440BX 
Desktop Reference Platform, BIOS 6.00 07/02/2015
[   58.609790] task: 9d90df888040 task.stack: a07084854000
[   58.613944] RIP: 0010:lock_release+0x172/0x1e0
[   58.617622] RSP: :a07084857e58 EFLAGS: 00010082
[   58.621533] RAX: 001f RBX: 9d90df888040 RCX: 
[   58.626074] RDX:  RSI: 0001 RDI: a30d4ba4
[   58.630572] RBP: a07084857e98 R08:  R09: 0001
[   58.635016] R10:  R11: 001f R12: a07084857f58
[   58.639694] R13: 9d90f60d6cd0 R14:  R15: a305cb6e
[   58.644200] FS:  7fb932730740() GS:9d90f9f8() 
knlGS:
[   58.648989] CS:  0010 DS:  ES:  CR0: 80050033
[   58.652903] CR2: 0040092f CR3: 000135229000 CR4: 000606e0
[   58.657280] Call Trace:
[   58.659989]  up_read+0x1a/0x40
[   58.662825]  __do_page_fault+0x28e/0x4c0
[   58.665946]  do_page_fault+0x30/0x80
[   58.668911]  page_fault+0x28/0x30

The reason is that the page fault path might have dropped the mmap_sem
and returned with VM_FAULT_RETRY. MMF_UNSTABLE check however rewrites
the error path to VM_FAULT_SIGBUS and we always expect mmap_sem taken in
that path. Fix this by taking mmap_sem when VM_FAULT_RETRY is held in
the MMF_UNSTABLE path. We cannot simply add VM_FAULT_SIGBUS to the
existing error code because all arch specific page fault handlers and
g-u-p would have to learn a new error code combination.

Reported-by: Tetsuo Handa 
Fixes: 3f70dc38cec2 ("mm: make sure that kthreads will not refault oom reaped 
memory")
Cc: stable # 4.9+
Signed-off-by: Michal Hocko 
---
 mm/memory.c | 12 +++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/mm/memory.c b/mm/memory.c
index 0e517be91a89..4fe5b6254688 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3881,8 +3881,18 @@ int handle_mm_fault(struct vm_area_struct *vma, unsigned 
long address,
 * further.
 */
if (unlikely((current->flags & PF_KTHREAD) && !(ret & VM_FAULT_ERROR)
-   && test_bit(MMF_UNSTABLE, >vm_mm->flags)))
+   && test_bit(MMF_UNSTABLE, >vm_mm->flags))) 
{
+
+   /*
+* We are going to enforce SIGBUS but the PF path might have
+* dropped the mmap_sem already so take it again so that
+* we do not break expectations of all arch specific PF paths
+* and g-u-p
+*/
+   if (ret & VM_FAULT_RETRY)
+   down_read(>vm_mm->mmap_sem);
ret = VM_FAULT_SIGBUS;
+   }
 
return ret;
 }
-- 
2.13.2



[PATCH 1/2] mm: fix double mmap_sem unlock on MMF_UNSTABLE enforced SIGBUS

2017-08-04 Thread Michal Hocko
From: Michal Hocko 

Tetsuo Handa has noticed that MMF_UNSTABLE SIGBUS path in
handle_mm_fault causes a lockdep splat
[   58.539455] Out of memory: Kill process 1056 (a.out) score 603 or sacrifice 
child
[   58.543943] Killed process 1056 (a.out) total-vm:4268108kB, 
anon-rss:2246048kB, file-rss:0kB, shmem-rss:0kB
[   58.544245] a.out (1169) used greatest stack depth: 11664 bytes left
[   58.557471] DEBUG_LOCKS_WARN_ON(depth <= 0)
[   58.557480] [ cut here ]
[   58.564407] WARNING: CPU: 6 PID: 1339 at kernel/locking/lockdep.c:3617 
lock_release+0x172/0x1e0
[   58.599401] CPU: 6 PID: 1339 Comm: a.out Not tainted 
4.13.0-rc3-next-20170803+ #142
[   58.604126] Hardware name: VMware, Inc. VMware Virtual Platform/440BX 
Desktop Reference Platform, BIOS 6.00 07/02/2015
[   58.609790] task: 9d90df888040 task.stack: a07084854000
[   58.613944] RIP: 0010:lock_release+0x172/0x1e0
[   58.617622] RSP: :a07084857e58 EFLAGS: 00010082
[   58.621533] RAX: 001f RBX: 9d90df888040 RCX: 
[   58.626074] RDX:  RSI: 0001 RDI: a30d4ba4
[   58.630572] RBP: a07084857e98 R08:  R09: 0001
[   58.635016] R10:  R11: 001f R12: a07084857f58
[   58.639694] R13: 9d90f60d6cd0 R14:  R15: a305cb6e
[   58.644200] FS:  7fb932730740() GS:9d90f9f8() 
knlGS:
[   58.648989] CS:  0010 DS:  ES:  CR0: 80050033
[   58.652903] CR2: 0040092f CR3: 000135229000 CR4: 000606e0
[   58.657280] Call Trace:
[   58.659989]  up_read+0x1a/0x40
[   58.662825]  __do_page_fault+0x28e/0x4c0
[   58.665946]  do_page_fault+0x30/0x80
[   58.668911]  page_fault+0x28/0x30

The reason is that the page fault path might have dropped the mmap_sem
and returned with VM_FAULT_RETRY. MMF_UNSTABLE check however rewrites
the error path to VM_FAULT_SIGBUS and we always expect mmap_sem taken in
that path. Fix this by taking mmap_sem when VM_FAULT_RETRY is held in
the MMF_UNSTABLE path. We cannot simply add VM_FAULT_SIGBUS to the
existing error code because all arch specific page fault handlers and
g-u-p would have to learn a new error code combination.

Reported-by: Tetsuo Handa 
Fixes: 3f70dc38cec2 ("mm: make sure that kthreads will not refault oom reaped 
memory")
Cc: stable # 4.9+
Signed-off-by: Michal Hocko 
---
 mm/memory.c | 12 +++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/mm/memory.c b/mm/memory.c
index 0e517be91a89..4fe5b6254688 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3881,8 +3881,18 @@ int handle_mm_fault(struct vm_area_struct *vma, unsigned 
long address,
 * further.
 */
if (unlikely((current->flags & PF_KTHREAD) && !(ret & VM_FAULT_ERROR)
-   && test_bit(MMF_UNSTABLE, >vm_mm->flags)))
+   && test_bit(MMF_UNSTABLE, >vm_mm->flags))) 
{
+
+   /*
+* We are going to enforce SIGBUS but the PF path might have
+* dropped the mmap_sem already so take it again so that
+* we do not break expectations of all arch specific PF paths
+* and g-u-p
+*/
+   if (ret & VM_FAULT_RETRY)
+   down_read(>vm_mm->mmap_sem);
ret = VM_FAULT_SIGBUS;
+   }
 
return ret;
 }
-- 
2.13.2