From: Suren Baghdasaryan <surenb@google.com>
To: Michal Hocko <mhocko@suse.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
David Rientjes <rientjes@google.com>,
Matthew Wilcox <willy@infradead.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Roman Gushchin <guro@fb.com>, Rik van Riel <riel@surriel.com>,
Minchan Kim <minchan@kernel.org>,
Christian Brauner <christian@brauner.io>,
Christoph Hellwig <hch@infradead.org>,
Oleg Nesterov <oleg@redhat.com>,
David Hildenbrand <david@redhat.com>,
Jann Horn <jannh@google.com>, Shakeel Butt <shakeelb@google.com>,
Andy Lutomirski <luto@kernel.org>,
Christian Brauner <christian.brauner@ubuntu.com>,
Florian Weimer <fweimer@redhat.com>,
Jan Engelhardt <jengelh@inai.de>,
Tim Murray <timmurray@google.com>,
Linux API <linux-api@vger.kernel.org>,
linux-mm <linux-mm@kvack.org>,
LKML <linux-kernel@vger.kernel.org>,
kernel-team <kernel-team@android.com>
Subject: Re: [PATCH v6 1/2] mm: introduce process_mrelease system call
Date: Thu, 5 Aug 2021 08:29:57 -0700 [thread overview]
Message-ID: <CAJuCfpF1JSTSRu5v8s9wG0J-S+-p57tMO+0dUF+P16_6yYV7Mg@mail.gmail.com> (raw)
In-Reply-To: <YQuO36AeQUwsAyU1@dhcp22.suse.cz>
On Thu, Aug 5, 2021 at 12:10 AM Michal Hocko <mhocko@suse.com> wrote:
>
> On Wed 04-08-21 11:50:03, Suren Baghdasaryan wrote:
> [...]
> > +SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
> > +{
> > +#ifdef CONFIG_MMU
> > + struct mm_struct *mm = NULL;
> > + struct task_struct *task;
> > + unsigned int f_flags;
> > + struct pid *pid;
> > + long ret = 0;
> > +
> > + if (flags)
> > + return -EINVAL;
> > +
> > + pid = pidfd_get_pid(pidfd, &f_flags);
> > + if (IS_ERR(pid))
> > + return PTR_ERR(pid);
> > +
> > + task = get_pid_task(pid, PIDTYPE_PID);
> > + if (!task) {
> > + ret = -ESRCH;
> > + goto put_pid;
> > + }
> > +
> > + /*
> > + * If the task is dying and in the process of releasing its memory
> > + * then get its mm.
> > + */
> > + task = find_lock_task_mm(task);
>
> You want a different task_struct because the returned one might be
> different from the given one and you already hold a reference which you
> do not want to leak
Ah, right. I was looking at the task locking and find_lock_task_mm()
handles that but I missed the task pinning part. Will fix.
>
> > + if (!task) {
> > + ret = -ESRCH;
> > + goto put_pid;
> > + }
> > + if (task_will_free_mem(task) && (task->flags & PF_KTHREAD) == 0) {
> > + mm = task->mm;
> > + mmget(mm);
> > + }
> > + task_unlock(task);
> > + if (!mm) {
> > + ret = -EINVAL;
> > + goto put_task;
> > + }
> > +
> > + if (test_bit(MMF_OOM_SKIP, &mm->flags))
> > + goto put_mm;
>
> This is too late to check for MMF_OOM_SKIP. task_will_free_mem will fail
> with the flag being set. I believe you want something like the
> following:
>
> p = find_lock_task_mm(task);
> mm = p->mm;
>
> /* The work has been done already */
> if (test_bit(MMF_OOM_SKIP, &mm->flags)) {
> task_unlock(p);
> goto put_task;
> }
>
> i
> if (!task_will_free_mem(p)) {
> task_unlock(p);
> goto put_task;
> }
>
> mmget(mm);
> task_unlock(p);
>
I see. Let me update the patch and will ask Andrew to remove the
previous version from mm tree.
Thanks for reviewing and pointing out the issues!
>
> > +
> > + if (mmap_read_lock_killable(mm)) {
> > + ret = -EINTR;
> > + goto put_mm;
> > + }
> > + if (!__oom_reap_task_mm(mm))
> > + ret = -EAGAIN;
> > + mmap_read_unlock(mm);
> > +
> > +put_mm:
> > + mmput(mm);
> > +put_task:
> > + put_task_struct(task);
> > +put_pid:
> > + put_pid(pid);
> > + return ret;
> > +#else
> > + return -ENOSYS;
> > +#endif /* CONFIG_MMU */
> > +}
> > --
> > 2.32.0.554.ge1b32706d8-goog
>
> --
> Michal Hocko
> SUSE Labs
next prev parent reply other threads:[~2021-08-05 15:30 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-04 18:50 [PATCH v6 1/2] mm: introduce process_mrelease system call Suren Baghdasaryan
2021-08-04 18:50 ` [PATCH v6 2/2] mm: wire up syscall process_mrelease Suren Baghdasaryan
2021-08-04 18:58 ` [PATCH v6 1/2] mm: introduce process_mrelease system call David Hildenbrand
2021-08-04 22:50 ` Andrew Morton
2021-08-04 22:55 ` Suren Baghdasaryan
2021-08-04 22:59 ` Shakeel Butt
2021-08-05 7:10 ` Michal Hocko
2021-08-05 15:29 ` Suren Baghdasaryan [this message]
2021-08-05 15:31 ` David Hildenbrand
2021-08-05 17:11 ` 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=CAJuCfpF1JSTSRu5v8s9wG0J-S+-p57tMO+0dUF+P16_6yYV7Mg@mail.gmail.com \
--to=surenb@google.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=mhocko@suse.com \
--cc=minchan@kernel.org \
--cc=oleg@redhat.com \
--cc=riel@surriel.com \
--cc=rientjes@google.com \
--cc=shakeelb@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 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).