Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp657237pxt; Thu, 5 Aug 2021 08:33:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+54KbuZCteAyNa1f13PhIepy9v8Y6p7GdSente1cp/V6M2qB3z1WgJlKgmAlWL/PLquHR X-Received: by 2002:a05:6402:1458:: with SMTP id d24mr7420050edx.281.1628177637126; Thu, 05 Aug 2021 08:33:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628177637; cv=none; d=google.com; s=arc-20160816; b=J4tsgwHCP5EATHnz+2kK5rGPp36BEfQrig49p+8uktQ/QrOSqVfI1iIJjpsNaidHPE rRlA96Bm/G1hTYS4nSfal5gznuSs4reIkeR2Cv+51FAVqa3h+jStqkh3cl82XrQZqNN7 ogI19kMTv+DGE+XplvGgpoBtrxuzh4BA3qobjwkrfJcwxON30wJnuk29i7RJG0bA8lqQ FxowBNLyg+GGimECsYp3Fy9F7jLsCVzmeRWsDcX5M8CIt9D/hZWw8DVjp5lhUTQ0eX95 9C7nh/hB80zHqP3LZtuYLBf9yi7M0jnYJZjGPaBt5u2UACSD47RWFNC6OyPT0uvEG6bw 5INA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=2tU8mV4kGo7B6sAAhPR8hBvbRKDUXIJ345PeY/aAOBg=; b=FzrUZdQ7bdy1WOE9fHLxRfbRcHUpmeuAIR16YzyYBroDfMq9NIGkYT6e436LCQaeF8 5poqNaNw8taAPcPYzZkuQIEKh+yIgixPt/8ZqDLFi/81XRDYszZw13Q7ZJG214jYuWzq G9/Y4a+pFB96Vrke7Lh9yy0CkS/k3YK+MYNny0rL7m+YbTkKYwCD+vMmoIhLD947rfsp IYKuP+5cnV9ny3LLdUr5txaoIYYHmkVP03o54dFyH3cXOnaoG0ZGVvB7RjRM9HZr+NWf FnAept2R53WvdeSRH7SW6Ffa0VcmvExZU63AYGBcp7gP7WgR33bGankrsG8USHN2rDZz WKTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="f42bJF/1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v16si5933416ejg.83.2021.08.05.08.33.33; Thu, 05 Aug 2021 08:33:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="f42bJF/1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233497AbhHEPaZ (ORCPT + 99 others); Thu, 5 Aug 2021 11:30:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233376AbhHEPaY (ORCPT ); Thu, 5 Aug 2021 11:30:24 -0400 Received: from mail-yb1-xb2e.google.com (mail-yb1-xb2e.google.com [IPv6:2607:f8b0:4864:20::b2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49EECC061765 for ; Thu, 5 Aug 2021 08:30:09 -0700 (PDT) Received: by mail-yb1-xb2e.google.com with SMTP id k65so9568164yba.13 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=nU1H3JjWMxn6m+6lk2gmJTNeea1OHTOVQmH/iQQQw3Qhe956yA2h25JTTc0ABE/jNH SXWRlviypSFamkV+5pJxcSEZkgTo8GnCNjXeBgVyl2T4G4rGiv2X+XjAuycmfblNKh2k eYhUz+obC+AyoIGL/47yawefBCJRo7EFpwIbnzvVZLnkpzCx91xCwqNaMztsKkVXk+Uh lHMw6ecMpgtwJwxBrFQSgNeN5J4DRlvwT2NRrtzc+r0qaSZJvLk0LlwKh/EGc0l23J/Z lgRpq0POi5cppez5C68sdS85RL3WpSqJo3vVm8fXamoyfW1kZ17u5MaPcsjqN9BXiu4K Im8g== X-Gm-Message-State: AOAM532RWUZoKV4EMUjp1uOT9hKweQDM3SgW4fW2cgSAOs7H+Lz1JAOA jOMnqQ5GrrrUDW8LKpMS3/IenvM6EBvxPzYOyq5Ehw== 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" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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