Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp999982yba; Thu, 4 Apr 2019 02:06:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqwkIQ9/VTQSgZ+sGzoAKgO8QOs+z5V6ezlxMqOvDaD4lJtcR7aW6JKm14lLGpdhunOH2k2h X-Received: by 2002:a17:902:2888:: with SMTP id f8mr5251802plb.244.1554368816078; Thu, 04 Apr 2019 02:06:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554368816; cv=none; d=google.com; s=arc-20160816; b=PRn15MQ9DtXlOnzNXlkkwfGKjvpEOr9r8TwL4vQXHKaipyw9RNUl4Suuiq7lcKUScD rJdSs+8nvzwV/MiNtUx93wiTpMI8JREkndYvswSXYgtj8gTaYl7pB7j6fMLDTNvqvWPZ b99l6iyKQoq73J/KP1j2nj2DglcVRK+ja9XwNAYuwUgmsOEsHGTwv+9DNUbvTWFanTBQ dLGH4z45BMqKXMXZKHd6vw7cSQr4VfTMFBwP8d9MMkyN3U8IsqsP92TRIOSmD7+fTUJE ppaHIFcSQAwVtREXxAlYHOKnVRprJZvhmPksx+wA7J133Hfp/54cKGgL+PZLuUxn6x/7 mX1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=XHywpNoivs+rNzvUA4YzjMcqstGSnAy6RXsnhlADae8=; b=I7R/I4zFq4co6ILAUG92nW6cjrrSV/AxrY1sv4MaeDv5cyXEorz5AiziroDqPN6DPw 6Oddd4q568xguD8140sSWZ/myFgy3L8hkuhyRUuEcvujH8zvxCsIDc6tBeF/Bv2vOAoi IMXZpIq0FBA3//p/UHoeRTqWKcyCsj0JD/IZdkbzmBAb4WgLwotGNzSn2x2A8qr68OBN V/LHwMI4dnq+ZTqV226jzhy4ckAQ2Bcnjg4WlV0EPBP84Xm39spRLKvHt5wMbbjSD4cy 0+hoBdiwfsl+6N7yJ+it1HpVcAmiJnBvMQ2+Sefro0czwUwdvURm8X4zlH7PuqpVLXRH NFOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=tOX8DMgP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g5si15881217pgc.122.2019.04.04.02.06.40; Thu, 04 Apr 2019 02:06:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=tOX8DMgP; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731839AbfDDJGJ (ORCPT + 99 others); Thu, 4 Apr 2019 05:06:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:44518 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732253AbfDDJGH (ORCPT ); Thu, 4 Apr 2019 05:06:07 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2D0F02183E; Thu, 4 Apr 2019 09:06:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554368766; bh=RGWrctdTRpjuVkt/L+Nr0/zw2iq8zf3ayvkBiUEUYt4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tOX8DMgP5w7JWcv46VrPZfI/Y0CQRnmm1Qs7KUplthNIBj0XAenw8vaH22z4g31IM W1NsWc8AJ6KdmGd9FUc0LNRK5N+DNb1s8StaR430yys97RfWTfqZpNJ/BqQXhyJZhr p9bwhHZn4EsnTANC52hwgSH/B6dvKMwZ4OT/TVk0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Herton R. Krzesinski" , Jan Stancek , Oleg Nesterov , Tejun Heo , Sasha Levin Subject: [PATCH 4.19 146/187] cgroup/pids: turn cgroup_subsys->free() into cgroup_subsys->release() to fix the accounting Date: Thu, 4 Apr 2019 10:48:03 +0200 Message-Id: <20190404084610.035673359@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190404084603.119654039@linuxfoundation.org> References: <20190404084603.119654039@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 51bee5abeab2058ea5813c5615d6197a23dbf041 ] The only user of cgroup_subsys->free() callback is pids_cgrp_subsys which needs pids_free() to uncharge the pid. However, ->free() is called from __put_task_struct()->cgroup_free() and this is too late. Even the trivial program which does for (;;) { int pid = fork(); assert(pid >= 0); if (pid) wait(NULL); else exit(0); } can run out of limits because release_task()->call_rcu(delayed_put_task_struct) implies an RCU gp after the task/pid goes away and before the final put(). Test-case: mkdir -p /tmp/CG mount -t cgroup2 none /tmp/CG echo '+pids' > /tmp/CG/cgroup.subtree_control mkdir /tmp/CG/PID echo 2 > /tmp/CG/PID/pids.max perl -e 'while ($p = fork) { wait; } $p // die "fork failed: $!\n"' & echo $! > /tmp/CG/PID/cgroup.procs Without this patch the forking process fails soon after migration. Rename cgroup_subsys->free() to cgroup_subsys->release() and move the callsite into the new helper, cgroup_release(), called by release_task() which actually frees the pid(s). Reported-by: Herton R. Krzesinski Reported-by: Jan Stancek Signed-off-by: Oleg Nesterov Signed-off-by: Tejun Heo Signed-off-by: Sasha Levin --- include/linux/cgroup-defs.h | 2 +- include/linux/cgroup.h | 2 ++ kernel/cgroup/cgroup.c | 15 +++++++++------ kernel/cgroup/pids.c | 4 ++-- kernel/exit.c | 1 + 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index 22254c1fe1c5..6002275937f5 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -597,7 +597,7 @@ struct cgroup_subsys { void (*cancel_fork)(struct task_struct *task); void (*fork)(struct task_struct *task); void (*exit)(struct task_struct *task); - void (*free)(struct task_struct *task); + void (*release)(struct task_struct *task); void (*bind)(struct cgroup_subsys_state *root_css); bool early_init:1; diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 32c553556bbd..ca51b2c15bcc 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -119,6 +119,7 @@ extern int cgroup_can_fork(struct task_struct *p); extern void cgroup_cancel_fork(struct task_struct *p); extern void cgroup_post_fork(struct task_struct *p); void cgroup_exit(struct task_struct *p); +void cgroup_release(struct task_struct *p); void cgroup_free(struct task_struct *p); int cgroup_init_early(void); @@ -699,6 +700,7 @@ static inline int cgroup_can_fork(struct task_struct *p) { return 0; } static inline void cgroup_cancel_fork(struct task_struct *p) {} static inline void cgroup_post_fork(struct task_struct *p) {} static inline void cgroup_exit(struct task_struct *p) {} +static inline void cgroup_release(struct task_struct *p) {} static inline void cgroup_free(struct task_struct *p) {} static inline int cgroup_init_early(void) { return 0; } diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index e710ac7fbbbf..63dae7e0ccae 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -195,7 +195,7 @@ static u64 css_serial_nr_next = 1; */ static u16 have_fork_callback __read_mostly; static u16 have_exit_callback __read_mostly; -static u16 have_free_callback __read_mostly; +static u16 have_release_callback __read_mostly; static u16 have_canfork_callback __read_mostly; /* cgroup namespace for init task */ @@ -5240,7 +5240,7 @@ static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) have_fork_callback |= (bool)ss->fork << ss->id; have_exit_callback |= (bool)ss->exit << ss->id; - have_free_callback |= (bool)ss->free << ss->id; + have_release_callback |= (bool)ss->release << ss->id; have_canfork_callback |= (bool)ss->can_fork << ss->id; /* At system boot, before all subsystems have been @@ -5676,16 +5676,19 @@ void cgroup_exit(struct task_struct *tsk) } while_each_subsys_mask(); } -void cgroup_free(struct task_struct *task) +void cgroup_release(struct task_struct *task) { - struct css_set *cset = task_css_set(task); struct cgroup_subsys *ss; int ssid; - do_each_subsys_mask(ss, ssid, have_free_callback) { - ss->free(task); + do_each_subsys_mask(ss, ssid, have_release_callback) { + ss->release(task); } while_each_subsys_mask(); +} +void cgroup_free(struct task_struct *task) +{ + struct css_set *cset = task_css_set(task); put_css_set(cset); } diff --git a/kernel/cgroup/pids.c b/kernel/cgroup/pids.c index 9829c67ebc0a..c9960baaa14f 100644 --- a/kernel/cgroup/pids.c +++ b/kernel/cgroup/pids.c @@ -247,7 +247,7 @@ static void pids_cancel_fork(struct task_struct *task) pids_uncharge(pids, 1); } -static void pids_free(struct task_struct *task) +static void pids_release(struct task_struct *task) { struct pids_cgroup *pids = css_pids(task_css(task, pids_cgrp_id)); @@ -342,7 +342,7 @@ struct cgroup_subsys pids_cgrp_subsys = { .cancel_attach = pids_cancel_attach, .can_fork = pids_can_fork, .cancel_fork = pids_cancel_fork, - .free = pids_free, + .release = pids_release, .legacy_cftypes = pids_files, .dfl_cftypes = pids_files, .threaded = true, diff --git a/kernel/exit.c b/kernel/exit.c index d607e23fd0c3..5c0964dc805a 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -219,6 +219,7 @@ repeat: } write_unlock_irq(&tasklist_lock); + cgroup_release(p); release_thread(p); call_rcu(&p->rcu, delayed_put_task_struct); -- 2.19.1