Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3098354pxb; Fri, 5 Nov 2021 09:42:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwBCC5gJZwnGl/rNsRpD/CNUQX4PD80tWAUIhupENDZbWUC/tMRolwPNgpAE06OSWYgcJ5w X-Received: by 2002:a05:6402:350b:: with SMTP id b11mr22300513edd.184.1636130564401; Fri, 05 Nov 2021 09:42:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1636130564; cv=none; d=google.com; s=arc-20160816; b=wWWazcQn60qE7nGeC4rm9EatTelzGV0QBX2ZosieDb7l+lHIMM151HYQg7YOfVcBkH pPXqwtOvI8oIt4h+qrb329FHCcJIbPmays38Lk39K3BY2E3xaVSt3bzyN2TT2wxdeTXy yG1kP+YJJw4Uk/EOBdfGCvGDcrLRZgni0qOWJ3lxvHr4O8fr0XNqnU8cw7YGm2tlBoAa HT2eloZgZQO/5EOvlt7aRiGs7OsnasXHQhMKbCb2xd3Jn5zH0GCVRGY7YZxd+Ctk/7lt jweM68grc5UGatE4dYAqfO1vUYvRy/R8k9uyP27eosh1HcwdaERUe+Bvlzh+fjaXwNlD +mPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:subject:from :references:cc:to:dkim-signature; bh=usXdWUOIdQxMuchtyqFk9vaJkdyOXIN0o0UnmjgkBB8=; b=k53ShQR8VS34LHFS8Ig8bBd2HqvnyjpqVb1qH+KAvF3/VhxsfEp1MLwK0ZGQo7x+qY /vk+0FmO6cY126aZHx6v6jBfHGFmFzF1u8DowFHNjAnHSVBPz+GwqmAVbnT93BKXA62U hz0KaahtTYYaE5Z4ISpf2qgqU7PUqqThJBLTI7+XyM4FSHiW+1URdbplrwr6IwUO4hnG fU+ITUvuUDMbPksAcTVG8KvQ/cmU+2LCqCFTai7WQcC+qNM4L6hrfUrdUELADBkd50rG 9Jy4HTD12Ue+syp7aCf93M72YaaeAoUHjZOVVMJ+mWg85PYmwCGT+PLT4lR/sCdrfXW8 muQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@grsecurity.net header.s=grsec header.b=sfM7nxs9; 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=NONE sp=NONE dis=NONE) header.from=grsecurity.net Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i4si12651386edc.536.2021.11.05.09.42.20; Fri, 05 Nov 2021 09:42:44 -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=@grsecurity.net header.s=grsec header.b=sfM7nxs9; 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=NONE sp=NONE dis=NONE) header.from=grsecurity.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233285AbhKEO6T (ORCPT + 99 others); Fri, 5 Nov 2021 10:58:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233289AbhKEO6R (ORCPT ); Fri, 5 Nov 2021 10:58:17 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B77CBC061714 for ; Fri, 5 Nov 2021 07:55:37 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id r8so14121180wra.7 for ; Fri, 05 Nov 2021 07:55:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=grsecurity.net; s=grsec; h=to:cc:references:from:subject:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=usXdWUOIdQxMuchtyqFk9vaJkdyOXIN0o0UnmjgkBB8=; b=sfM7nxs9IOBwwA0qV5WQdskzlsJf/JhX9WI3JY6F3Cc/FJaFMJQoSHWOSkEglU4IUh prZS6nzvG6FPLpNbBwCpqF6f2+uEk5XmF4Zb8JIcDMtWkqyvU6dGrVWInFcnBrMNLG6c QWRGhCDRjyBS5uMN+LUBCufgiDAeUA66HX8OynzQXIEklIIiavF2mpRUWFdX3n1p6oOt SbWceP5Oun/v/sOtb0dVAp35Bt06XOqevvwphQF0qd/7XwZCUrM9Zk3XfwabWnlJ9efv 4WiyT8+lfzxuZ675POKecV0x0oGK6Vqzj/EbidJD0V3vGIEIArXDhOAuX+SM13LfBPJR pV8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:to:cc:references:from:subject:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=usXdWUOIdQxMuchtyqFk9vaJkdyOXIN0o0UnmjgkBB8=; b=hxb+Ccl5A5ctAfdl0N7IH2Zp71VUohuptLBy6vxitpGMnqEJLhpfApqdByOeuSvD7v pCFpSBa9rgkvdVLGkiXMrmSpCJ3Kip7VPgZ6UkhrcHUuY0RWQb6ixi2VQHo+dLotF3W1 oaEUmm90UCUIJd2XZU/94Qev0CFmW7KB9qZp5uhMfpf1T1A306toEZRnMsHZEp2TLD9u 5WlZWBHFCpeiOmqn+IqxtyLUskYnXPcn7s+3i4u1PoQ5R8ZxS0UVV1KuRc/tHe+TP/la seD1SkeR5YxVVsVOe3o3WlJeKm/SsWtt3M8JkIhM/J2uJ3Ck/rQ4/70Nbw6gy7/AOA38 9B9g== X-Gm-Message-State: AOAM531Qc9OaqOUSNuEl3C+Z7iEhxn2h0afZyM4o4fIr2aXtFlV9zYl0 vQsEgJuL7JauQjotcwBmSsIRbg== X-Received: by 2002:adf:efc6:: with SMTP id i6mr50750631wrp.428.1636124136345; Fri, 05 Nov 2021 07:55:36 -0700 (PDT) Received: from ?IPv6:2003:f6:af04:5300:7c00:c62b:b3aa:158b? (p200300f6af0453007c00c62bb3aa158b.dip0.t-ipconnect.de. [2003:f6:af04:5300:7c00:c62b:b3aa:158b]) by smtp.gmail.com with ESMTPSA id h16sm9250684wrm.27.2021.11.05.07.55.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 05 Nov 2021 07:55:36 -0700 (PDT) To: =?UTF-8?Q?Michal_Koutn=c3=bd?= Cc: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , linux-kernel@vger.kernel.org, Odin Ugedal , Kevin Tanguy , Brad Spengler References: <20211103190613.3595047-1-minipli@grsecurity.net> <20211104184939.GA23576@blackbody.suse.cz> From: Mathias Krause Subject: Re: [PATCH] sched/fair: Prevent dead task groups from regaining cfs_rq's Message-ID: Date: Fri, 5 Nov 2021 15:55:35 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: <20211104184939.GA23576@blackbody.suse.cz> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Am 04.11.21 um 19:49 schrieb Michal Koutný: > On Wed, Nov 03, 2021 at 08:06:13PM +0100, Mathias Krause wrote: >> When unregister_fair_sched_group() unlinks all cfs_rq's from the dying >> task group, it doesn't protect itself from getting interrupted. If the >> timer interrupt triggers while we iterate over all CPUs or after >> unregister_fair_sched_group() has finished but prior to unlinking the >> task group, sched_cfs_period_timer() will execute and walk the list of >> task groups, trying to unthrottle cfs_rq's, i.e. re-add them to the >> dying task group. These will later -- in free_fair_sched_group() -- be >> kfree()'ed while still being linked, leading to the fireworks Kevin and >> Michal are seeing. > > [...] > >> CPU1: CPU2: >> : timer IRQ: >> : do_sched_cfs_period_timer(): >> : : >> : distribute_cfs_runtime(): >> : rcu_read_lock(); >> : : >> : unthrottle_cfs_rq(): >> sched_offline_group(): : >> : walk_tg_tree_from(…,tg_unthrottle_up,…): >> list_del_rcu(&tg->list); : >> (1) : list_for_each_entry_rcu(child, &parent->children, siblings) >> : : >> (2) list_del_rcu(&tg->siblings); : >> : tg_unthrottle_up(): >> unregister_fair_sched_group(): struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; >> : : >> list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); : >> : : >> : if (!cfs_rq_is_decayed(cfs_rq) || cfs_rq->nr_running) >> (3) : list_add_leaf_cfs_rq(cfs_rq); >> : : >> : : >> : : >> : : >> : : >> (4) : rcu_read_unlock(); > > The list traversal (1) may happen in some scenarios (quota on non-leaf > task_group) but in the presented reproducer, the quota is set on the > leaf task_group. That means it has no children and this list iteration > is irrelevant. > The cause is that walk_tg_tree_from includes `from` task_group and > calls tg_unthrottle_up() on it too. > What I mean is that the unlinking of tg->list and tg->siblings is > irrelevant in this case. Interesting. > The timer can still fire after > sched_offline_group()/unregister_fair_sched_group() finished (i.e. after > synchronize_rcu()) Yeah, I also noticed the timer gets disabled rather late, in free_fair_sched_group() via destroy_cfs_bandwidth(). But as I saw no more warnings from my debug patch I was under the impression, do_sched_cfs_period_timer() won't see this thread group any more. Apparently, this is not true? Anyhow, see below. >> This patch survives Michal's reproducer[2] for 8h+ now, which used to >> trigger within minutes before. > > Note that the reproducer is sensitive to the sleep between last task > exit and cgroup rmdir. I assume that the added synchronize_rcu() before > list_del_leaf_cfs_rq() shifted the list removal after the last timer > callback and prevented re-adding of the offlined task_group in > unthrottle_cfs_rq(). As Vincent reported in the other thread, synchronize_rcu() is actually problematic, as we're not allowed to block here. :( So I'd go for the kfree_rcu() route and move unregister_fair_sched_group() to free_fair_sched_group(), after disabling the timers. > (Of course, it'd more convincing if I backed this theory by results from > the reproducer with the increased interval to crash again. I may get > down to that later.) > > Does your patch fix the crashes also in your real workload? I haven't heard back from Kevin since. But he might just be busy. Thanks, Mathias