linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
To: linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
	linux-kernel@vger.kernel.org
Cc: "Oleg Nesterov" <oleg@redhat.com>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Michal Hocko" <mhocko@kernel.org>,
	"Cyrill Gorcunov" <gorcunov@gmail.com>,
	"Kirill Tkhai" <ktkhai@virtuozzo.com>,
	"Michal Koutný" <mkoutny@suse.com>,
	"Al Viro" <viro@zeniv.linux.org.uk>,
	"Roman Gushchin" <guro@fb.com>
Subject: [PATCH v2 6/6] mm: use down_read_killable for locking mmap_sem in access_remote_vm
Date: Sun, 09 Jun 2019 13:09:02 +0300	[thread overview]
Message-ID: <156007494202.3335.16782303099589302087.stgit@buzz> (raw)
In-Reply-To: <156007465229.3335.10259979070641486905.stgit@buzz>

This function is used by ptrace and proc files like /proc/pid/cmdline and
/proc/pid/environ.

Access_remote_vm never returns error codes, all errors are ignored and
only size of successfully read data is returned. So, if current task was
killed we'll simply return 0 (bytes read).

Mmap_sem could be locked for a long time or forever if something wrong.
Killable lock allows to cleanup stuck tasks and simplifies investigation.

Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Reviewed-by: Michal Koutný <mkoutny@suse.com>
Acked-by: Oleg Nesterov <oleg@redhat.com>
Acked-by: Michal Hocko <mhocko@suse.com>
---
 mm/memory.c |    4 +++-
 mm/nommu.c  |    3 ++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index ddf20bd0c317..9a4401d21e94 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4349,7 +4349,9 @@ int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
 	void *old_buf = buf;
 	int write = gup_flags & FOLL_WRITE;
 
-	down_read(&mm->mmap_sem);
+	if (down_read_killable(&mm->mmap_sem))
+		return 0;
+
 	/* ignore errors, just check how much was successfully transferred */
 	while (len) {
 		int bytes, ret, offset;
diff --git a/mm/nommu.c b/mm/nommu.c
index d8c02fbe03b5..b2823519f8cd 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -1792,7 +1792,8 @@ int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
 	struct vm_area_struct *vma;
 	int write = gup_flags & FOLL_WRITE;
 
-	down_read(&mm->mmap_sem);
+	if (down_read_killable(&mm->mmap_sem))
+		return 0;
 
 	/* the access must start within one of the target process's mappings */
 	vma = find_vma(mm, addr);


      parent reply	other threads:[~2019-06-09 10:09 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-09 10:08 [PATCH v2 0/6] mm: use down_read_killable for locking mmap_sem in proc Konstantin Khlebnikov
2019-06-09 10:08 ` [PATCH v2 1/6] proc: use down_read_killable mmap_sem for /proc/pid/maps Konstantin Khlebnikov
2019-06-09 10:08 ` [PATCH v2 2/6] proc: use down_read_killable mmap_sem for /proc/pid/smaps_rollup Konstantin Khlebnikov
2019-06-09 10:08 ` [PATCH v2 3/6] proc: use down_read_killable mmap_sem for /proc/pid/pagemap Konstantin Khlebnikov
2019-06-09 10:08 ` [PATCH v2 4/6] proc: use down_read_killable mmap_sem for /proc/pid/clear_refs Konstantin Khlebnikov
2019-06-09 10:09 ` [PATCH v2 5/6] proc: use down_read_killable mmap_sem for /proc/pid/map_files Konstantin Khlebnikov
2019-06-12 23:14   ` Andrei Vagin
2019-06-13  0:04     ` Andrew Morton
2019-06-13  6:48     ` Cyrill Gorcunov
2019-06-13  8:15     ` Konstantin Khlebnikov
2019-06-13 20:32       ` Andrei Vagin
2019-06-09 10:09 ` Konstantin Khlebnikov [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=156007494202.3335.16782303099589302087.stgit@buzz \
    --to=khlebnikov@yandex-team.ru \
    --cc=akpm@linux-foundation.org \
    --cc=gorcunov@gmail.com \
    --cc=guro@fb.com \
    --cc=ktkhai@virtuozzo.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).