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,URIBL_BLOCKED,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 06A2FC4338F for ; Thu, 5 Aug 2021 15:30:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8A46E6104F for ; Thu, 5 Aug 2021 15:30:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8A46E6104F 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 2212C8D0002; Thu, 5 Aug 2021 11:30:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1AA2A8D0001; Thu, 5 Aug 2021 11:30:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 04A268D0002; Thu, 5 Aug 2021 11:30:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0057.hostedemail.com [216.40.44.57]) by kanga.kvack.org (Postfix) with ESMTP id DBA428D0001 for ; Thu, 5 Aug 2021 11:30:09 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 8B0EA252DD for ; Thu, 5 Aug 2021 15:30:09 +0000 (UTC) X-FDA: 78441412938.05.E53CD49 Received: from mail-yb1-f176.google.com (mail-yb1-f176.google.com [209.85.219.176]) by imf21.hostedemail.com (Postfix) with ESMTP id 3A0F0D016158 for ; Thu, 5 Aug 2021 15:30:09 +0000 (UTC) Received: by mail-yb1-f176.google.com with SMTP id w17so9613955ybl.11 for ; Thu, 05 Aug 2021 08:30:09 -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=2tU8mV4kGo7B6sAAhPR8hBvbRKDUXIJ345PeY/aAOBg=; b=f42bJF/1Dk3qBScrNnooaWUVAZzkLtIj7HBfdCOwEgtizu7GOgYeHzhTLcHYniO5F6 RUlhXZJ1NNv0m4d/N4jpYoMs/zsueUVLfWdpg32nakElzLVGY8ajhUKdySHoSr2NtLlr WJheTug+4Cbr13BVfdGuZj4ZUVtzQCecn+o6GQkgOBmPnv6QoQqprx6zAUQYfI5J3j8Y ysTwi12ZG2dpM8CUh1bywMCrTFnmv8pgkKTAshspGbXaZU9u8WqgFHth7/1RlhOAS/jv lhqAalia4DP3BPSMN/cvfzFbrEzR0VVEvk+KSP19fPYmndIQ9+8hiD3URpC82jIongpY zmoA== 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=2tU8mV4kGo7B6sAAhPR8hBvbRKDUXIJ345PeY/aAOBg=; b=GHGB6f7URNXCQgHa8t7JldQi4iTyzygbuDv0He4MvUZVoBcTueqSXOA2z+qH5uJbGz LTy9TqHZ0bwjTi3gtmniR4sCGMqGJjYAlCSoWkV0sBG98Ztz+d4XdJQRdmT8mR3Reo6Z ae33tGaGMJweQnFdmOyWmnh9E8dXocVNEIUlypjIB86m4PS4sEit86VozfTlrBDDADOA QHNuELM1X3iyk7AXb3NIW7n5YrJS95l0DDHoDmRZguwc0JJXW59o6B0KhcpuAxd+NW2s EAzLBgMCmFACAf6meAunbS1xNNMiaDenBMh/MDrzWK2IBz2iDzewAkBofG3y11gr/o6t WU1w== X-Gm-Message-State: AOAM533pKPtlSg9X/y6HuiYu+A8LRm8Psgnhjbm5ONbwlSSjQArRTHCv u42l7HYeytLpX0OdmwJUPNYLagIph0P5vHB1NYdwsw== X-Google-Smtp-Source: ABdhPJxUS4czW0V1Clmo2rkCzA8soyzkXMnFhkpuCErHWLlAEPCH8nIhEIa3XsBc7kIhmxFERhlxoO1fRLGjHS5wrn0= X-Received: by 2002:a25:4216:: with SMTP id p22mr6237785yba.397.1628177408208; Thu, 05 Aug 2021 08:30:08 -0700 (PDT) MIME-Version: 1.0 References: <20210804185004.1304692-1-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Thu, 5 Aug 2021 08:29:57 -0700 Message-ID: Subject: Re: [PATCH v6 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: rspam06 X-Rspamd-Queue-Id: 3A0F0D016158 Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b="f42bJF/1"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of surenb@google.com designates 209.85.219.176 as permitted sender) smtp.mailfrom=surenb@google.com X-Stat-Signature: yu9uo3pm3f8ch1sokntxz5nkgd9qff7y X-HE-Tag: 1628177409-298820 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 Thu, Aug 5, 2021 at 12:10 AM Michal Hocko 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