Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp2034084pxt; Sun, 8 Aug 2021 09:16:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxJHPBlK4nxR6kfcwJDcnQCMQnAmtFn5j8PonV5KoqPFSIKbh5iVZo3NcFKyoOj6SG0tgDe X-Received: by 2002:a02:cac9:: with SMTP id f9mr7492687jap.104.1628439381618; Sun, 08 Aug 2021 09:16:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628439381; cv=none; d=google.com; s=arc-20160816; b=rfxsvoMZwfY8hfispQK1KDN++/2/JOC89skxbd6ZrcAd2PVgt8mq4QIoTKvM4ZplI2 Hq/RpqNoMlsKxtXjrwM7g03rsgMbsI0U5m3o+bezyXGPZDEH5BbEJaEG1nfIwWp/CLJP 6ap0ckNYDyGHjhFSwHfDZdMTc30No2+AUNk73U+wdbtRur5trBPuoX1y/6svYEe8Dcrt 2eW7Qg2tsU+JIy7uP1XLCgzFfttVyZOLtf5jt/ybnUgSt8KhmYOnsjJepyvuVgtvb/ln Dv9l4oh7c+MPSO/hCS8MzabCNDlIYiMyW38UxAyJ+rzz3jM1hGBOGU9x/6/MyIoWW1mz Y1GA== 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=TCK5nBQQabIUN0WbQjXhg9KlErctfVr7limBHCaiG+A=; b=v7W1npY3RVnxq2QcvTNQJlquaxyZ0n8oFjo7roQujbfI/4Cpb4IM5H4+4kGjik8I2v nKh9vcDxc3YVkgnBsjADNmy8G/+Ty0tw/dipkGkpgrNLW/QuW1AGSFPCXbf0Wiq1VXyb T4Yy/zeHt4++FqyMgyv7rd0emO1KZ1zcBEig5zQ1joWfDLLXTW4ADyeJwvV8UBYnf7Ku uJgBIQk939i4WGRd/Coy2hG1SVk4uukUVZbVLaTw3B01hL0CfOAKT7IyPmwVLgFUloel EF8nOIWQJohUnSsGvmmqpvA4UdkHRxgj5wAs9r59tkwGWV4avNZVarztyNU4Lg0U+lBn bmJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="P09Mz/mI"; 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 o1si16731045jah.81.2021.08.08.09.16.10; Sun, 08 Aug 2021 09:16:21 -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="P09Mz/mI"; 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 S231700AbhHHQOG (ORCPT + 99 others); Sun, 8 Aug 2021 12:14:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230448AbhHHQOF (ORCPT ); Sun, 8 Aug 2021 12:14:05 -0400 Received: from mail-yb1-xb2c.google.com (mail-yb1-xb2c.google.com [IPv6:2607:f8b0:4864:20::b2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9492CC061760 for ; Sun, 8 Aug 2021 09:13:42 -0700 (PDT) Received: by mail-yb1-xb2c.google.com with SMTP id j77so24933820ybj.3 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=pyYrAGne4obM5OYu7PKdttmq4Rd5FyONryIz2wbUG8FZfAMuCKs4rJjTey8HZjsgeb x3yNRzQVR6jYs3Kc4pEMSiqEEKnYK8dSVlPYKBOAQ95e6uxmHTfnfp+uNu7rauATgiEF HpFHpMbsdpPEZoUwnGPZhCOYuK+WRml4JNYHzmgM2IyJkQURprVqj68Jv+2uivkTBsxg wdJDsdZrtF8YBUJXp85h3JLd6jbwDzwhEIxJ6Ior3hpSX4TmaEGOS78k3bcXP/DT8I56 EHkBldnyQTSCmHegyHMkhqBSHJYLEEj3/xiJolk1uBSrsgP4lXFEPZWILGONOdfDKoyC ys5A== X-Gm-Message-State: AOAM533KvrIXpz51gSNUQa61vEEwdDC+cohl8ZonuzitXpTS2vKZyyDg VBqa5WArlBnw+5QdroOjxVMvnnoSjDKSAq9Rv3/WWg== 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" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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