From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E053DC4338F for ; Sun, 8 Aug 2021 18:40:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 79A1A60EBD for ; Sun, 8 Aug 2021 18:40:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 79A1A60EBD Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 07CFE8D0003; Sun, 8 Aug 2021 14:40:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 055048D0002; Sun, 8 Aug 2021 14:40:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E85F18D0003; Sun, 8 Aug 2021 14:40:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0120.hostedemail.com [216.40.44.120]) by kanga.kvack.org (Postfix) with ESMTP id CAFC48D0002 for ; Sun, 8 Aug 2021 14:40:31 -0400 (EDT) Received: from forelay.prod.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by fograve02.hostedemail.com (Postfix) with ESMTP id 58A76202F8 for ; Sun, 8 Aug 2021 16:13:53 +0000 (UTC) Received: from smtpin34.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 27EB718098548 for ; Sun, 8 Aug 2021 16:13:43 +0000 (UTC) X-FDA: 78452409126.34.8E9A79E Received: from mail-yb1-f173.google.com (mail-yb1-f173.google.com [209.85.219.173]) by imf25.hostedemail.com (Postfix) with ESMTP id 7DC3BB001EB8 for ; Sun, 8 Aug 2021 16:13:42 +0000 (UTC) Received: by mail-yb1-f173.google.com with SMTP id z128so24887439ybc.10 for ; Sun, 08 Aug 2021 09:13:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=TCK5nBQQabIUN0WbQjXhg9KlErctfVr7limBHCaiG+A=; b=P09Mz/mILYaoYE3EajDRpkc8akaY7fTBsUTWh/DDjvyIVcbgMaZWzI+LtlcoWEveOD rhx8918aHZb+O4eacyH9xJ39yZ0R0Ijqpx/MepcAKa8/2gZ5+AVf01MP0FU5rtgj0aaI ZOn+r2ameWRT7D0AaL+uSp1MMJV6BIO04kwUPuwtrDbWxbL86dxGwXjCh8J+AwMkozcs y1mHfPiyIhI4iox17HTcyNBHoFvdDSpVFAZKWamRg7qw92Jqi0wzk6fCPKC2Ilmft8gc 6xtZM6GOlAFZn84PTqwvcOz6XFlG9JONgTvDHC8swDsPKC28qv4jvo5Vx9VKEuRlASq2 T1rQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=TCK5nBQQabIUN0WbQjXhg9KlErctfVr7limBHCaiG+A=; b=LDa0pT9/7/HfppJIvq+zwWLUupknf92YP82QYaOUmoa2BwjovfcQBaBuOwNAe28X2/ yDgSYzJGuxSouguU6gQqPr0bgOnCrvxo4ZV/uTnGsHnOhQD60XoiQYqMP7ZJyNGmQO+9 iY7WbnUZmPwtwPNmfteQp3NoZ51JtyqoYRKnXObfan4oGOVEJraPf07FeHyXA6f8JiAT MWAV5+KMgP1fPXEZlJ9Lr/9Lto+hrOC1WNPPXBshxUBbr+EBBHNgGkQYnmdh4caLaBlf DmZrU0ZsUD8ZK8kbnZPvxi4y0edAkW9ACeF+rtKr1K04n8asQcsBUYZHqOu6OEaLJzhE SwcA== X-Gm-Message-State: AOAM531V6ji7Aw8wVVmo+gvENQmqN5mUxLHyBAsVZJRTAcgigVRgaOh/ pTf5K6Oew1rl4g/fpilPPlBuPDfCj9wY73qb2WllYQ== X-Google-Smtp-Source: ABdhPJx0dWjYkHDsO+8kSP89UPL2BlSOBVWTieqq/F7t4NM8YSZ1slbEbkxEHATJiigRfMb9++IRuvn6g9kCzcz5GpU= X-Received: by 2002:a25:49c2:: with SMTP id w185mr23459074yba.294.1628439221480; Sun, 08 Aug 2021 09:13:41 -0700 (PDT) MIME-Version: 1.0 References: <20210805170859.2389276-1-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Sun, 8 Aug 2021 09:13:30 -0700 Message-ID: Subject: Re: [PATCH v7 1/2] mm: introduce process_mrelease system call To: Michal Hocko Cc: Andrew Morton , David Rientjes , Matthew Wilcox , Johannes Weiner , Roman Gushchin , Rik van Riel , Minchan Kim , Christian Brauner , Christoph Hellwig , Oleg Nesterov , David Hildenbrand , Jann Horn , Shakeel Butt , Andy Lutomirski , Christian Brauner , Florian Weimer , Jan Engelhardt , Tim Murray , Linux API , linux-mm , LKML , kernel-team Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 7DC3BB001EB8 Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b="P09Mz/mI"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of surenb@google.com designates 209.85.219.173 as permitted sender) smtp.mailfrom=surenb@google.com X-Stat-Signature: 4m1m7d9qbcnfmieznjzqoyd5ymcfbtiw X-HE-Tag: 1628439222-318937 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, Aug 6, 2021 at 9:07 AM Suren Baghdasaryan wrote: > > On Thu, Aug 5, 2021 at 11:40 PM Michal Hocko wrote: > > > > 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. > > True. > > > > > > + > > > + /* 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 > > Missed it again :( > > > > > > + > > > + 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? > > Yeah, looks like task_will_free_mem() covers that case already. > > > > > > + 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; > > > > This is indeed simpler to follow. I'll adopt your version. Thanks! v8 is posted at https://lore.kernel.org/patchwork/patch/1473697/ Testing shows performance improvement from replacing mmget with mmgrab. > > > -- > > Michal Hocko > > SUSE Labs