Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp255662pxt; Fri, 6 Aug 2021 00:54:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQ6wYHNEOfyErj2ZfdL6lYJEm9sGTm+oPTOAJIylfMCaDqa8Ve7QJQDBNK218jPL47Yifn X-Received: by 2002:aa7:da02:: with SMTP id r2mr11465298eds.249.1628236480286; Fri, 06 Aug 2021 00:54:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628236480; cv=none; d=google.com; s=arc-20160816; b=f/2PZc2CxpxvbzXeorb+Abtcu/D0pSJOrc8Xj1WEjX1yYR+C/blXHXrTPC6v3uUsvz 9ax7npFVMP547dkXNbvrtWdTFpLYvqKCM+3JaavX4n8JzmX9J7Xj2jrwG2Zv1tMNBfnD 8JdFAv4zg3HrZiiMyFmg7c0z9eAnxgOSY3loiMqX7XOnOqTBoT+2CU9hFkEGV8KgCOIu UjHGRgtnTyGLDUmt68gZnyg+LsK8WxkUWlZu+HQU5F4r928cHGUjeiqvuTraBOX3z2j8 PEwgYv1NzfytfK3x4oJD/MxsbduVJfQV9tPqUP4EfQU3oi2xduxCFwKkQ9pnmjhdgzOC IGmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=1bzG7zeW8+sEcqaRFyAoUGVQNhJuHDuJNfvputWUxlU=; b=1C69/6ogi5t9xGowCVkC2Fx6wCFjcfj6fqIqFcPGPoluDxfmwcMvE8Z4OXMZgOT3R6 egvQtbQOtldQSuk0nZPkHQmc6v9tyV5KIy7ZMRR0j9BXfwZC9X6JEqxDUfk+LxsEv6St eHuxo6JG01FsKXW9hLoKfBXeZhTeHJXJhZxYudpPH480P8k7t2SmevHVFdqUYsmf3+9n X2U9uov5nYKAfbpgjTtijy8lSYNTz5lLtHCyHJHVamVe86btEGPl1yMLVIcewXOPEl0e RxBdeyyeuvQ9xL5xvNXce9l2nlF6XQ9hePw+pKjwq9pSrfac5w/HqNKT5RqWxT/3zzdV tC+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=DFMfWgoI; 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=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gx8si7900547ejb.15.2021.08.06.00.54.16; Fri, 06 Aug 2021 00:54:40 -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=@suse.com header.s=susede1 header.b=DFMfWgoI; 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=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243370AbhHFGkb (ORCPT + 99 others); Fri, 6 Aug 2021 02:40:31 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:52750 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229625AbhHFGka (ORCPT ); Fri, 6 Aug 2021 02:40:30 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 7CB871FEA4; Fri, 6 Aug 2021 06:40:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1628232014; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=1bzG7zeW8+sEcqaRFyAoUGVQNhJuHDuJNfvputWUxlU=; b=DFMfWgoIKq8k2jlf+f9b59bhzeS7lx/OpC0P2llTgssT7CnFQ41p1eTuaVxyugiTl6ZReo jLlb1T47D67orJMZCnQEoZcU32vZSOcHMLl9oZRTHw9csBnLC9EY6dzi4XYB2LVfOpkJBK xPFL7MtbPOvqOEo+fbdlh1G5pgmdTpY= Received: from suse.cz (unknown [10.100.201.86]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id BE076A3B8B; Fri, 6 Aug 2021 06:40:13 +0000 (UTC) Date: Fri, 6 Aug 2021 08:40:10 +0200 From: Michal Hocko To: Suren Baghdasaryan Cc: akpm@linux-foundation.org, rientjes@google.com, willy@infradead.org, hannes@cmpxchg.org, guro@fb.com, riel@surriel.com, minchan@kernel.org, christian@brauner.io, hch@infradead.org, oleg@redhat.com, david@redhat.com, jannh@google.com, shakeelb@google.com, luto@kernel.org, christian.brauner@ubuntu.com, fweimer@redhat.com, jengelh@inai.de, timmurray@google.com, linux-api@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH v7 1/2] mm: introduce process_mrelease system call Message-ID: References: <20210805170859.2389276-1-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210805170859.2389276-1-surenb@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > + > + /* 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 > + > + 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? > + 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; -- Michal Hocko SUSE Labs