Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp881444pxt; Fri, 6 Aug 2021 16:50:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9vOVpXWKPIjKLeZdo8GHfXnbeEbLqb+mj3US1VW6cKSV5hGUn6IkECgoa8R9I31CrpHLk X-Received: by 2002:a02:354d:: with SMTP id y13mr6732024jae.54.1628293838166; Fri, 06 Aug 2021 16:50:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628293838; cv=none; d=google.com; s=arc-20160816; b=0uxbA9Sq7gTQ7qlScQd8s1jRzcIg2Gev+7VIc/i4JlnitmQMLXI1wg2AiIA5ehA71m 3fK01wTkUIedXrqskkouctCZmPBAMMfmmJUcm0D6NJ9Gn9PaxgzJOcvJ1bWCP1osqzYr +6PreYbFzgHPlA32cXko3fl5/gm9YD9FggR1oBKRv0Bz3yMyw0/HBgG+Ei0UmskH2jMP QzNZtpoa80YXTuny2sr9/CKVFU8Ep1Cr7ooOO92wNoi+oxZzyhXzcRNQf9i11qx8n9cE xeeJExrb9QZo1LTmJm20atRdNG0/vxaCkF9ZDyfZjMCbOCy0B3sR047ISM0VmaFE8GDQ a2Ug== 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=iPa6gpCFto1S8Anmjbtf+lIZ5pc8QBEuFieat3/oBzA=; b=ssIL4xejfuPz0SOjH3BYsWzXN2BGYmyZtPuwVCy2oMCwK1ScF+9bAxbMZL7CD3VZ9L pmgQVC6/v/OWbAozzpVJbtTKuDlt9ZYQaFc2GTRo9aJkmMNfXETQBGOGxxOZePsXT62T LVivoZEQkKrGmQqsEzVLurrvBfJitvSnaT0GC+EEnxZ/MqC0XE4PAv/Tx1t9O9B4/51w bTwBSatrCCpHZuiIac/h02KNNqG2paC1sDzryrdhcbfL31B8rjvW/ITgY/T+Ctjmu+ur 5OB06iSfXrBBCUgvYY9EA934nLVrqwN7JrWufBKo+9Q54XuKstEISkZZ7ThNuOngfFUz 0ntg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=kTUbPF5b; 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 az34si11048199jab.63.2021.08.06.16.50.27; Fri, 06 Aug 2021 16:50:38 -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=kTUbPF5b; 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 S229445AbhHFQH7 (ORCPT + 99 others); Fri, 6 Aug 2021 12:07:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229773AbhHFQH5 (ORCPT ); Fri, 6 Aug 2021 12:07:57 -0400 Received: from mail-yb1-xb30.google.com (mail-yb1-xb30.google.com [IPv6:2607:f8b0:4864:20::b30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E75C1C061798 for ; Fri, 6 Aug 2021 09:07:41 -0700 (PDT) Received: by mail-yb1-xb30.google.com with SMTP id m193so16054833ybf.9 for ; Fri, 06 Aug 2021 09:07:41 -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=iPa6gpCFto1S8Anmjbtf+lIZ5pc8QBEuFieat3/oBzA=; b=kTUbPF5bYWWgLbhYQS3gr9X4Bbz9A7Hw/wdSPQ6B3cZ6W5X64uvJT65/hMuDYACbXt B6E92tLsooMGIp4kZPX8L5GZxuGWZHgjloEYiIIRNkb4dOEw0hLST1NZXysc3aIhRUl9 B5SODDpfG9MLdOeu/BuGEFmaCqEhLum1pKTtbtWXwzAs0dY7jU6qHCZsuV2z4k1SX/m5 hq+kO0uclbmfSwmSpPtRdARCKy6lvWxemLl4dS43drZ+FJO2h7z1L039l+mkNfYk2N8H p7/J0QTh16ACpV+AeHzFrS1kwWVWHekATOYLVjf1CUbF/25QMpBhvAVuyeee/25cO0RW W2GQ== 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=iPa6gpCFto1S8Anmjbtf+lIZ5pc8QBEuFieat3/oBzA=; b=mib/9UIfVPefR9SkLyKzE7JSOKGdYjfF+6N6+gAOaHt9T15bI9nay3EbFn+NXxBlyR Ncs9qJMVRKo+o5WFOZb+9s7WF6g7rG7IvqRWdV0fBE4UCLfx3Q/mGDN0MP+6Nm99+6NF KxoYFfgpY+RapgPb/foKkxDnRe9ZskgDhlsyHLbqlAR5pSWaMr19hWSOrf4KE5fGSTPa PaIF+1fE90ejdDNq9I+pphsXxZmSPMATIDUg7+dnqOwkt7i1LF5+D0JHwWtaR1XquN/C 1UMuetPki2C2ucV81sYswZLelDArQw4Or9/hp3lcg9rTzyD8fE0oL8x1hFJPoVk0L2rh 7vfg== X-Gm-Message-State: AOAM530iX8yCtP0ac76JUq0/2FxeJ5albLkw121JHvL6M1KyFmBi0GAs d9UjtPj5ElhNOmfwtNA8Lger5YzhhHbE4Ei7pnqijA== X-Received: by 2002:a25:7ec4:: with SMTP id z187mr14175379ybc.136.1628266059961; Fri, 06 Aug 2021 09:07:39 -0700 (PDT) MIME-Version: 1.0 References: <20210805170859.2389276-1-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Fri, 6 Aug 2021 09:07:28 -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 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! > -- > Michal Hocko > SUSE Labs