Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2323011ybl; Thu, 9 Jan 2020 10:41:49 -0800 (PST) X-Google-Smtp-Source: APXvYqxaOM4hMZ7wiJvTVNVIo88dG6fg/tSPwAOLDlbTAuHWu0FuHzkwk/Ar1oEiq6vYliSqckVT X-Received: by 2002:aca:c38c:: with SMTP id t134mr4078250oif.175.1578595309270; Thu, 09 Jan 2020 10:41:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578595309; cv=none; d=google.com; s=arc-20160816; b=og730cKRlG+lUaSMTO9woDAC1WTKbPxffy0aalP7pMFrIZsgF2cfaU95hDxftQQn2b 3hbs3JNa+ehAmC3+ZBJi9zA2LxLJHhCTmjxb0YidgGcC75+b/ZfObvg2WM0Xl8uQBcJ2 jIPs2y9U2jKdNDuCTsQdkrFqGyZ/5pNB9DdgMT6vSdu+Z2v35ezsN2GpHu2Uq8MTfU7h 2zNa2S9X6bqH9cb4Zj/fF5dS6DaVB5U6qfMvYTfVOQwliO1YKiqdEX4mY8GtVn2ebOwb YoJswL7beTeJT8WNFMjjwQEQuqQAHYG043lZHEXybV9B/oROs+dMzYYjEZBn7tS9ZwWT G0Ig== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=FsL6M0hzZGNtRlw/nowx5IRSWnaob27Na9UCTqmZbUo=; b=NKwt7Pvr0x+bxk5dXFlnhVejREjtXj3HOTnytvQbq9vbK0kfA+44FJynY31U7CD9wt FYNgnv3/1ivI19KOlkaFCaFeO3Dyokhu9dlWSnT+/aveSkuc9+BN9dQrPaYEbMD0ccoU pvbLPKM7x+JlKjVYTczH77LFdxH5TVICKcudQ17J7FkKArXODdQjlAZpUhmL7wumpIVp bORI4457fbzw0nUuwu2+VnvEOsgzZEWRnmP73Y+V7IZF7/vGQj6pR61OITAmhaMa+rS7 XA+gjOE6+G/U36h1v02yqjCk8MPIh8bVbmLLk/tvA8jq7Q2jjKpJxy3/+H1yoqVyOTpW kdhg== ARC-Authentication-Results: i=1; mx.google.com; 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 p25si4299760oto.191.2020.01.09.10.41.36; Thu, 09 Jan 2020 10:41:49 -0800 (PST) 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; 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 S1732015AbgAIPGI (ORCPT + 99 others); Thu, 9 Jan 2020 10:06:08 -0500 Received: from mx2.suse.de ([195.135.220.15]:53916 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728346AbgAIPGI (ORCPT ); Thu, 9 Jan 2020 10:06:08 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 7E64DAE34; Thu, 9 Jan 2020 15:06:06 +0000 (UTC) From: =?UTF-8?q?Michal=20Koutn=C3=BD?= To: cgroups@vger.kernel.org Cc: Tejun Heo , Li Zefan , Johannes Weiner , linux-kernel@vger.kernel.org, Christian Brauner Subject: [PATCH] cgroup: Prevent double killing of css when enabling threaded cgroup Date: Thu, 9 Jan 2020 16:05:59 +0100 Message-Id: <20200109150559.14457-1-mkoutny@suse.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191219022716.o7vxxia6o67tyfmf@wittgenstein> References: <20191219022716.o7vxxia6o67tyfmf@wittgenstein> 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 The test_cgcore_no_internal_process_constraint_on_threads selftest when running with subsystem controlling noise triggers two warnings: > [ 597.443115] WARNING: CPU: 1 PID: 28167 at kernel/cgroup/cgroup.c:3131 cgroup_apply_control_enable+0xe0/0x3f0 > [ 597.443413] WARNING: CPU: 1 PID: 28167 at kernel/cgroup/cgroup.c:3177 cgroup_apply_control_disable+0xa6/0x160 Both stem from a call to cgroup_type_write. The first warning was also triggered by syzkaller. When we're switching cgroup to threaded mode shortly after a subsystem was disabled on it, we can see the respective subsystem css dying there. The warning in cgroup_apply_control_enable is harmless in this case since we're not adding new subsys anyway. The warning in cgroup_apply_control_disable indicates an attempt to kill css of recently disabled subsystem repeatedly. The commit prevents these situations by making cgroup_type_write wait for all dying csses to go away before re-applying subtree controls. When at it, the locations of WARN_ON_ONCE calls are moved so that warning is triggered only when we are about to misuse the dying css. Reported-by: syzbot+5493b2a54d31d6aea629@syzkaller.appspotmail.com Reported-by: Christian Brauner Signed-off-by: Michal Koutný --- kernel/cgroup/cgroup.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 53098c1d45e2..97cc713079df 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -3054,8 +3054,6 @@ static int cgroup_apply_control_enable(struct cgroup *cgrp) for_each_subsys(ss, ssid) { struct cgroup_subsys_state *css = cgroup_css(dsct, ss); - WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); - if (!(cgroup_ss_mask(dsct) & (1 << ss->id))) continue; @@ -3065,6 +3063,8 @@ static int cgroup_apply_control_enable(struct cgroup *cgrp) return PTR_ERR(css); } + WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt)); + if (css_visible(css)) { ret = css_populate_dir(css); if (ret) @@ -3100,11 +3100,11 @@ static void cgroup_apply_control_disable(struct cgroup *cgrp) for_each_subsys(ss, ssid) { struct cgroup_subsys_state *css = cgroup_css(dsct, ss); - WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); - if (!css) continue; + WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt)); + if (css->parent && !(cgroup_ss_mask(dsct) & (1 << ss->id))) { kill_css(css); @@ -3391,7 +3391,8 @@ static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf, if (strcmp(strstrip(buf), "threaded")) return -EINVAL; - cgrp = cgroup_kn_lock_live(of->kn, false); + /* drain dying csses before we re-apply (threaded) subtree control */ + cgrp = cgroup_kn_lock_live(of->kn, true); if (!cgrp) return -ENOENT; -- 2.24.1