Skip to contentSkip to conversation

mm/gup: Let __get_user_pages_locked() return -EINTR for fatal signal

PATCH In review Peter Xu 5 participants 6 messages Apr 8, 2020 – Apr 9, 2020

Subsystems (2)
lore
Share & tools
lore

Peter Xu

author reviewer
#0 lore raw

PATCH 1/1 mm/gup: Let __get_user_pages_locked() return -EINTR for fatal signal

From: Hillf Danton <[email protected]>

__get_user_pages_locked() will return 0 instead of -EINTR after commit 4426e945df588 which added extra code to allow gup detect fatal signal faster. Restore that behavior.

Cc Linus Torvalds <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Fixes 4426e945df58 ("mm/gup: allow VM_FAULT_RETRY for multiple times")
Reported-by [email protected]

---

PS. Patch verified with syzbot.

---
 mm/gup.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/gup.c +4 -1
index afce0bc47e70..6076df8e04a4 100644
@@ -1326,8 +1326,11 @@ static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
 		 * start trying again otherwise it can loop forever.
 		 */
 
-		if (fatal_signal_pending(current))
+		if (fatal_signal_pending(current)) {
+			if (!pages_done)
+				pages_done = -EINTR;
 			break;
+		}
 
 		ret = down_read_killable(&mm->mmap_sem);
 		if (ret) {
Signature · 1 line
2.24.1
#1 lore raw
Quoted 4 lines · Peter Xu

__get_user_pages_locked() will return 0 instead of -EINTR after commit 4426e945df588 which added extra code to allow gup detect fatal signal faster. Restore that behavior.

I've applied this, but it's worth noting that __get_user_pages_locked() can still return 0 in other situations.

I realize that "I got zero pages" is a valid return value, but I do wonder if we should make the rule be that a zero return value isn't possible (return -EAGAIN or whatever if you doin't have the EFAULT/EINTR conditions).

So that you'd always get either an error, or a successful number of pages.

The only case where __get_user_pages_locked() might return zero is if you pass in a zero 'nr_pages', although I suspect even for that case returning -EINVAL might be a better option.

Anyway, this is not a new issue, but I thought I'd mention it.

Linus

#2 lore raw
Quoted 14 lines · Linus Torvalds
Quoted 4 lines · Peter Xu

__get_user_pages_locked() will return 0 instead of -EINTR after commit 4426e945df588 which added extra code to allow gup detect fatal signal faster. Restore that behavior.

I've applied this, but it's worth noting that __get_user_pages_locked() can still return 0 in other situations.

I realize that "I got zero pages" is a valid return value, but I do wonder if we should make the rule be that a zero return value isn't possible (return -EAGAIN or whatever if you doin't have the EFAULT/EINTR conditions).

Quoted 5 lines

So that you'd always get either an error, or a successful number of pages.

The only case where __get_user_pages_locked() might return zero is if you pass in a zero 'nr_pages', although I suspect even for that case returning -EINVAL might be a better option.

Yeah, that makes sense to me. And get_user_pages_locked documentation would benefit from an actual documentation as well. What we have right now is more suited to the lower level (__get_user_pages_locked) and have a high level user oriented documentation.

Signature · 2 lines
Michal Hocko
SUSE Labs
#3 lore raw
In reply to Peter Xu
Quoted 29 lines · Peter Xu

From: Hillf Danton <[email protected]>

__get_user_pages_locked() will return 0 instead of -EINTR after commit 4426e945df588 which added extra code to allow gup detect fatal signal faster. Restore that behavior.

Cc Linus Torvalds <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Fixes 4426e945df58 ("mm/gup: allow VM_FAULT_RETRY for multiple times")
Reported-by [email protected]

---

PS. Patch verified with syzbot.

---
 mm/gup.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/gup.c +0 -0
index afce0bc47e70..6076df8e04a4 100644

mm/gup.c static __always_inline long __get_user_pages_locked(struct task_struct *tsk, · line 1331

Quoted 7 lines
mm/gup.c +3 -1
@@ -1326,8 +1326,11 @@ static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
 		 * start trying again otherwise it can loop forever.
 		 */
 
-		if (fatal_signal_pending(current))
+		if (fatal_signal_pending(current)) {
+			if (!pages_done)
+				pages_done = -EINTR;

Why -EINTR here and -ERESTARTSYS at the other site?

Quoted 8 lines
mm/gup.c +1 -0
 			break;
+		}
 
 		ret = down_read_killable(&mm->mmap_sem);
 		if (ret) {
Signature · 1 line
2.24.1
#4 lore raw
Quoted 7 lines · Peter Zijlstra

mm/gup.c static __always_inline long __get_user_pages_locked(struct task_struct *tsk, · line 1331

Quoted 5 lines
mm/gup.c +3 -1
-             if (fatal_signal_pending(current))
+             if (fatal_signal_pending(current)) {
+                     if (!pages_done)
+                             pages_done = -EINTR;

Why -EINTR here and -ERESTARTSYS at the other site?

I'd prefer EINTR for all fatal signals.

Not because it should matter (it's fatal, after all, the thread should die before it ever sees it), but because I think it's less confusing.

If something is fatal, it sure as hell isn't going to restart any system calls.

But interrupting things because of fatal signals sounds sane (even if the error code makes it to user space it's interrupting the flow of code).

So I'd say that the other place should probably be EINTR too. But it would obviously be a good idea to verify that no caller cares..

Linus

Linus

#5 lore raw
In reply to Peter Xu

Hi Peter

Quoted 7 lines · On Wed, 8 Apr 2020 19:27:23 +0200 Peter Zijlstra wrote:
Quoted 7 lines
mm/gup.c +0 -0
@@ -1326,8 +1326,11 @@ static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
 		 * start trying again otherwise it can loop forever.
 		 */
 
Quoted 6 lines

mm/gup.c static __always_inline long __get_user_pages_locked(struct task_struct *tsk, · line 1331

Quoted 4 lines
mm/gup.c +3 -1
-		if (fatal_signal_pending(current))
+		if (fatal_signal_pending(current)) {
+			if (!pages_done)
+				pages_done = -EINTR;

Why -EINTR here and -ERESTARTSYS at the other site?

EINTR was selected because it goes in the direction of mutex_lock_killable() and down_read_killable() as well.

Be open to s/ERESTARTSYS/EINTR/ in gup if it makes a sense to you.

Thanks
Hillf