All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michal Hocko <mhocko@suse.com>
To: Suren Baghdasaryan <surenb@google.com>
Cc: akpm@linux-foundation.org, rientjes@google.com,
	willy@infradead.org, hannes@cmpxchg.org, guro@fb.com,
	riel@surriel.com, minchan@kernel.org, christian@brauner.io,
	hch@infradead.org, oleg@redhat.com, david@redhat.com,
	jannh@google.com, shakeelb@google.com, luto@kernel.org,
	christian.brauner@ubuntu.com, fweimer@redhat.com,
	jengelh@inai.de, timmurray@google.com, linux-api@vger.kernel.org,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	kernel-team@android.com
Subject: Re: [PATCH v7 1/2] mm: introduce process_mrelease system call
Date: Fri, 6 Aug 2021 08:40:10 +0200	[thread overview]
Message-ID: <YQzZSmRqYmxFJ61y@dhcp22.suse.cz> (raw)
In-Reply-To: <20210805170859.2389276-1-surenb@google.com>

On Thu 05-08-21 10:08:58, Suren Baghdasaryan wrote:
[...]
> +	/*
> +	 * If the task is dying and in the process of releasing its memory
> +	 * then get its mm.
> +	 */
> +	p = find_lock_task_mm(task);
> +	if (!p) {
> +		ret = -ESRCH;
> +		goto put_pid;
> +	}
> +	if (task != p) {
> +		get_task_struct(p);
> +		put_task_struct(task);
> +		task = p;
> +	}

Why do you need to take a reference to the p here? You are under
task_lock so this will not go away and you only need p to get your mm.

> +
> +	/* If the work has been done already, just exit with success */
> +	if (test_bit(MMF_OOM_SKIP, &task->mm->flags))
> +		goto put_task;

You want to release the task_lock

> +
> +	if (task_will_free_mem(task) && (task->flags & PF_KTHREAD) == 0) {

you want task_will_free_mem(p) and what is the point of the PF_KTHREAD
check?

> +		mm = task->mm;
> +		mmget(mm);

All you need is to make sure mm will not get released under your feet
once task_lock is released so mmgrab is the right thing to do here. The
address space can be torn down in parallel and that is OK and desirable.

I think you really want something like this:

	if (flags)
		return -EINVAL;
	
	pid = pidfd_get_pid(fd, &f_flags);
	if (IS_ERR(pid))
		return PTR_ERR(pid);
	task = get_pid_task(pid, PIDTYPE_PID);
	if (!task) {
		ret = -ESRCH;
		goto put_pid;
	}

	/*
	 * Make sure to chose a thread which still has a reference to mm
	 * during the group exit
	 */
	p = find_lock_task_mm(task);
	if (!p) {
		ret = -ESRCH;
		goto put_task;
	}

	mm = task->mm;
	mmgrab(mm);
	reap = true;
	/* If the work has been done already, just exit with success */
	if (test_bit(MMF_OOM_SKIP, &mm->flags)) {
		reap = false;
	} else if (!task_will_free_mem(p)) {
		reap = false;
		ret = -EINVAL;
	}
	task_unlock(p);

	if (!reap)
		goto dropmm;;

	/* Do the work*/


dropmm:
	mmdrop(mm);
put_task:
	put_task(task);
put_pid:
	put_pid(pid);

	return ret;

-- 
Michal Hocko
SUSE Labs

  parent reply	other threads:[~2021-08-06  6:40 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-05 17:08 [PATCH v7 1/2] mm: introduce process_mrelease system call Suren Baghdasaryan
2021-08-05 17:08 ` Suren Baghdasaryan
2021-08-05 17:08 ` [PATCH v7 2/2] mm: wire up syscall process_mrelease Suren Baghdasaryan
2021-08-05 17:08   ` Suren Baghdasaryan
2021-08-05 17:29 ` [PATCH v7 1/2] mm: introduce process_mrelease system call David Hildenbrand
2021-08-05 17:49   ` Suren Baghdasaryan
2021-08-05 17:49     ` Suren Baghdasaryan
2021-08-05 17:55     ` David Hildenbrand
2021-08-05 17:56     ` Shakeel Butt
2021-08-05 17:56       ` Shakeel Butt
2021-08-05 18:37       ` Suren Baghdasaryan
2021-08-05 18:37         ` Suren Baghdasaryan
2021-08-06  6:41         ` Michal Hocko
2021-08-06  6:40 ` Michal Hocko [this message]
2021-08-06  9:23   ` Shakeel Butt
2021-08-06  9:23     ` Shakeel Butt
2021-08-06 10:15     ` Michal Hocko
2021-08-06 16:07   ` Suren Baghdasaryan
2021-08-06 16:07     ` Suren Baghdasaryan
2021-08-08 16:13     ` Suren Baghdasaryan
2021-08-08 16:13       ` Suren Baghdasaryan

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=YQzZSmRqYmxFJ61y@dhcp22.suse.cz \
    --to=mhocko@suse.com \
    --cc=akpm@linux-foundation.org \
    --cc=christian.brauner@ubuntu.com \
    --cc=christian@brauner.io \
    --cc=david@redhat.com \
    --cc=fweimer@redhat.com \
    --cc=guro@fb.com \
    --cc=hannes@cmpxchg.org \
    --cc=hch@infradead.org \
    --cc=jannh@google.com \
    --cc=jengelh@inai.de \
    --cc=kernel-team@android.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@kernel.org \
    --cc=minchan@kernel.org \
    --cc=oleg@redhat.com \
    --cc=riel@surriel.com \
    --cc=rientjes@google.com \
    --cc=shakeelb@google.com \
    --cc=surenb@google.com \
    --cc=timmurray@google.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.