linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Andrei Vagin <avagin@gmail.com>
Cc: "Konstantin Khlebnikov" <khlebnikov@yandex-team.ru>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	"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>,
	"Dmitry Safonov" <dima@arista.com>
Subject: Re: [PATCH v2 5/6] proc: use down_read_killable mmap_sem for /proc/pid/map_files
Date: Wed, 12 Jun 2019 17:04:29 -0700	[thread overview]
Message-ID: <20190612170429.baaae5fe6d84b864508a3ec5@linux-foundation.org> (raw)
In-Reply-To: <20190612231426.GA3639@gmail.com>

On Wed, 12 Jun 2019 16:14:28 -0700 Andrei Vagin <avagin@gmail.com> wrote:

> On Sun, Jun 09, 2019 at 01:09:00PM +0300, Konstantin Khlebnikov wrote:
> > Do not stuck forever if something wrong.
> > Killable lock allows to cleanup stuck tasks and simplifies investigation.
> 
> This patch breaks the CRIU project, because stat() returns EINTR instead
> of ENOENT:
> 
> [root@fc24 criu]# stat /proc/self/map_files/0-0
> stat: cannot stat '/proc/self/map_files/0-0': Interrupted system call
> 
> Here is one inline comment with the fix for this issue.
> 
> > @@ -2107,7 +2113,10 @@ static struct dentry *proc_map_files_lookup(struct inode *dir,
> >  	if (!mm)
> >  		goto out_put_task;
> >  
> > -	down_read(&mm->mmap_sem);
> > +	result = ERR_PTR(-EINTR);
> > +	if (down_read_killable(&mm->mmap_sem))
> > +		goto out_put_mm;
> > +
> 
> 	result = ERR_PTR(-ENOENT);
> 

yes, thanks.

--- a/fs/proc/base.c~proc-use-down_read_killable-mmap_sem-for-proc-pid-map_files-fix
+++ a/fs/proc/base.c
@@ -2117,6 +2117,7 @@ static struct dentry *proc_map_files_loo
 	if (down_read_killable(&mm->mmap_sem))
 		goto out_put_mm;
 
+	result = ERR_PTR(-ENOENT);
 	vma = find_exact_vma(mm, vm_start, vm_end);
 	if (!vma)
 		goto out_no_vma;



We started doing this trick of using

	ret = -EFOO;
	if (something)
		goto out;

decades ago because it generated slightly better code.  I rather doubt
whether that's still the case.

In fact this:

--- a/fs/proc/base.c~a
+++ a/fs/proc/base.c
@@ -2096,35 +2096,45 @@ static struct dentry *proc_map_files_loo
 	struct dentry *result;
 	struct mm_struct *mm;
 
-	result = ERR_PTR(-ENOENT);
 	task = get_proc_task(dir);
-	if (!task)
+	if (!task) {
+		result = ERR_PTR(-ENOENT);
 		goto out;
+	}
 
-	result = ERR_PTR(-EACCES);
-	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
+	if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
+		result = ERR_PTR(-EACCES);
 		goto out_put_task;
+	}
 
-	result = ERR_PTR(-ENOENT);
-	if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
+	if (dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
+		result = ERR_PTR(-ENOENT);
 		goto out_put_task;
+	}
 
 	mm = get_task_mm(task);
-	if (!mm)
+	if (!mm) {
+		result = ERR_PTR(-ENOENT);
 		goto out_put_task;
+	}
 
-	result = ERR_PTR(-EINTR);
-	if (down_read_killable(&mm->mmap_sem))
+	if (down_read_killable(&mm->mmap_sem)) {
+		result = ERR_PTR(-EINTR);
 		goto out_put_mm;
+	}
 
-	result = ERR_PTR(-ENOENT);
 	vma = find_exact_vma(mm, vm_start, vm_end);
-	if (!vma)
+	if (!vma) {
+		result = ERR_PTR(-ENOENT);
 		goto out_no_vma;
+	}
 
-	if (vma->vm_file)
+	if (vma->vm_file) {
 		result = proc_map_files_instantiate(dentry, task,
 				(void *)(unsigned long)vma->vm_file->f_mode);
+	} else {
+		result = ERR_PTR(-ENOENT);
+	}
 
 out_no_vma:
 	up_read(&mm->mmap_sem);

makes no change to the generated assembly with gcc-7.2.0.

And I do think that the above style is clearer and more reliable, as
this bug demonstrates.


  reply	other threads:[~2019-06-13 17:00 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 [this message]
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 ` [PATCH v2 6/6] mm: use down_read_killable for locking mmap_sem in access_remote_vm Konstantin Khlebnikov

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=20190612170429.baaae5fe6d84b864508a3ec5@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=avagin@gmail.com \
    --cc=dima@arista.com \
    --cc=gorcunov@gmail.com \
    --cc=guro@fb.com \
    --cc=khlebnikov@yandex-team.ru \
    --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).