Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp369961pxt; Thu, 5 Aug 2021 01:33:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx3vhj3wEK15Bjt3YWezgydoJMf/2gZk97R7lTSTKhQf9Akib4q1Ip5iagY6HSupqcoLs0H X-Received: by 2002:a92:da0d:: with SMTP id z13mr399355ilm.95.1628152394793; Thu, 05 Aug 2021 01:33:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628152394; cv=none; d=google.com; s=arc-20160816; b=y/HKlYlwa2AkqMUkk/OoTI0KGEvdSJsigf30RepPjnbPO3i4UAxy7mvWfMkrhPq0iy HkgUTuHIeYr+ZWpYH/VD8MsOnzkXS3bIpbnoYk8tlDX0UgIN8iBS2NcztmuwO0tNpCOy S8PoU7S8AefZQcrmvj/7zgKFQCY02h6BWL8QjflpUApbq9qdLlOnVJNmYK/oRIEmikqV 6XHa7qFuxm8LYcksxSWRLaGbMIE89yWS4nSY7WITD0h5q4hBHwCPNuXIhuWS/imwXi0U F046+hMkXlraFkTPgTvbIHhipLUnq55G/5eNhRqdG3Lo3PqlGPxI0nyM3gCnf/Lq97ip dnFw== 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=oyvVbHlj5obJYiaLItZ1ogsQi32IrIb7/8pgtHva86I=; b=YdWVWrBjB0aFbKpWI+BPssTp9vT2hw6o4Pg6yQrp7vWE2OaaF+kSkkY9o6o9m4nRR6 2Weva4M9+X/iGLF306OKJYP6eo8x4bl0xHbtjsfmVrBcpZMsp74i5lJNDaKFsvSVe+wb y7Hqn/ukewzkZjFK02ppJN9yKZxIhRuc9NisFwCw44wO6fPJhL8S6Apbx0Sg8D6GyRUA ytv26ja6i4Mgqup6H3QgMl0awFDufFbUzGxS95MYF8Vpd+qqJKuZV1trjoekPP+BAqVF AOUH0RsnY6wnN6thfm0W2/KIvQpSVGk/EJpothwSzmgK79zHtWXYKrv0+js1LT3HUxsq WQlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=HIsf0MbB; 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 i4si418518ilv.135.2021.08.05.01.33.03; Thu, 05 Aug 2021 01:33:14 -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=HIsf0MbB; 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 S234994AbhHEHKk (ORCPT + 99 others); Thu, 5 Aug 2021 03:10:40 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:57236 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230037AbhHEHKi (ORCPT ); Thu, 5 Aug 2021 03:10:38 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 2FF061FE2A; Thu, 5 Aug 2021 07:10:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1628147424; 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=oyvVbHlj5obJYiaLItZ1ogsQi32IrIb7/8pgtHva86I=; b=HIsf0MbBWCBatjQdI2swaBz2R1bGGbFhxcyVQi5x5DWK696tjML7N9fcm0Sxj4DXVi9Fxp Uc5r3EXUpNXRGE40C252lVaTabOzVe1afUlBSWPmB2LC7lPHBabXgKvMItV3eAjZ1rScFI +aEPnIk9LS9kOf6YLNNeU3vbSy2V29k= 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 B6318A3B97; Thu, 5 Aug 2021 07:10:23 +0000 (UTC) Date: Thu, 5 Aug 2021 09:10:23 +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 v6 1/2] mm: introduce process_mrelease system call Message-ID: References: <20210804185004.1304692-1-surenb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210804185004.1304692-1-surenb@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > + 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); > + > + 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