Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp660410pxb; Thu, 20 Jan 2022 23:38:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJw/Z1W9M6II7A18tRY1VLEeiEaWJ5Y72hQgllsS7t/cHKh9YIje1QGDp6AlGkoyu7sTopRx X-Received: by 2002:a63:36ca:: with SMTP id d193mr2126993pga.88.1642750688748; Thu, 20 Jan 2022 23:38:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642750688; cv=none; d=google.com; s=arc-20160816; b=LpohG4oSJMp+s2JtykRD0H/XCALrka5Y8tjNcccoKCk+lwpA3dBD8AK+/CoDxmDegS E+DDOJ0FUIxX05mqUiiTPpilDP9/hjGAN+HvXfK2kL8wg06uENc95+/sh/Wbeisc3avS GhkBbGBlqk10Zv//YmU+KwPhsYgq5I+nnzOEnLYAsdmRDVUfGnfcAFtYu2oX/GB0JA7z qVpGUeCWfJibETI30QpxzeoJfF//yujyh8fhXFbPLxBvHay+TqjE5P+gSIgHe4nWrx/M Kf4ugLKN8xdYTMKsdJ9stM24K71AtqQCNMq+ETOH5SbP2htUcqaW/sRuGqw9GeEYHP9B wSMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=03H0oTsaoOJAVZroctwt5LNhXL/5/pvtJL1WK73zL4I=; b=g4t9QlpZSnpqkYXRzpcspL7QHxumzit92lGUyn+3W6TI5JZH+P7McDHw0MhhmR4Xg8 sljtVL0WxHFwb3pNehE0pxJ0Jmg05HqSK7JjlQAjW1ayzKVAQJvu5kfvYJZ0OHgD2+fN sIE2BH3SdIivO+Sqw/hAbCA6psNMv23aYw7eq8u9QoQo+IUaXXMWhTSAzDptv9ywiLLR DKC8gjvdb/NruqmRIwXPx9J5/C8d+1y6dVuVNQvVwTcN6qt0qcB6gRGAbPoBXbtG7gM9 AilRg88RzylDH8b5e4+XvKPKY1H64/MoteZ1US/gsgW2glF2CoEOPA5JwseOjU7fv0l5 0DAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=EZI+N5TP; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w23si5631367pll.352.2022.01.20.23.37.56; Thu, 20 Jan 2022 23:38:08 -0800 (PST) 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=@google.com header.s=20210112 header.b=EZI+N5TP; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349022AbiARU02 (ORCPT + 99 others); Tue, 18 Jan 2022 15:26:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349214AbiARU01 (ORCPT ); Tue, 18 Jan 2022 15:26:27 -0500 Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [IPv6:2a00:1450:4864:20::135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D074EC06173E for ; Tue, 18 Jan 2022 12:26:26 -0800 (PST) Received: by mail-lf1-x135.google.com with SMTP id m1so149669lfq.4 for ; Tue, 18 Jan 2022 12:26:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=03H0oTsaoOJAVZroctwt5LNhXL/5/pvtJL1WK73zL4I=; b=EZI+N5TPNsYGSEEP5udv90ztg+d10rOBWo0oS6CZy6ayJg9vspmRINQQ23ZsK2x3WE fsVZ6UjHH11JuwZd2hHEdulDYeF0i/12p1N1+s6RAZ55S+4R0Oqe9IIPXwKFWG86TAPP MOOwht0RoXbyTwIzY0O/saU54N3BAvWM7e6tCAlScWSMZiXr5F2tI/kV4pApStDBfMUw d1JBMfvUzGjVD7DKs8P2oN54ssu0br9vA/ADCNDadV2WNfKCU+7kgpKh7+bHe9u6k/3j Q+Nobgcj8jm7ElmI6/ATWDOa8y4K9ktRrsOA9QZea9/WUpoQDnV4SPpO9Tz147NM/xry XTTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=03H0oTsaoOJAVZroctwt5LNhXL/5/pvtJL1WK73zL4I=; b=Tz0vrKEWXY+16vaOCQmClXwa22tof6vHAgsJRQcALjxKCPdG0ym8xw+kG1nbRGprbB +QnnwuKfpAyLrsZas3g5x/eGdc0FwleanE3w4K6JOLGUPShwwZdbhPPgBmCAyabnkmOb LoPRXRH21FC3peR7aktDk06kqkKcPpa5HgZmihQacaApNVCh0DOhKhLFmA/LdWctsGaG 6BylcEtgVMsf5VDxcZp7FDmmgIyLiExhBMwBmX4Z+azF/MXWSomUhH0LfR7Y/iSoOEuE 4jLh0pLPCs+CNGDG/s71r7RbxkjvLD+eYpsWY49eNNWLKQGNy0SzsQZAVyWnI5EOjtCZ Cd5g== X-Gm-Message-State: AOAM532Zd201oHMiobesG8iIXH4YTwNB+SUPpUabQpH4FMZebkQQYEn3 ELFxZFFkOIFXvE8FtvZkEknoTjo6VY4ecJM9edMo/Q== X-Received: by 2002:a2e:bd11:: with SMTP id n17mr21127646ljq.508.1642537584879; Tue, 18 Jan 2022 12:26:24 -0800 (PST) MIME-Version: 1.0 References: <20211222225350.1912249-1-vipinsh@google.com> <20220105180420.GC6464@blackbody.suse.cz> In-Reply-To: <20220105180420.GC6464@blackbody.suse.cz> From: Vipin Sharma Date: Tue, 18 Jan 2022 12:25:48 -0800 Message-ID: Subject: Re: [PATCH v2] KVM: Move VM's worker kthreads back to the original cgroups before exiting. To: =?UTF-8?Q?Michal_Koutn=C3=BD?= Cc: pbonzini@redhat.com, seanjc@google.com, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, dmatlack@google.com, jiangshanlai@gmail.com, kvm@vger.kernel.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Michal, Thanks for looking into this patch. I will be using Sean's suggestion and use real_parent of the task. This will avoid my ugly code in the cgroup APIs. On Wed, Jan 5, 2022 at 10:04 AM Michal Koutn=C3=BD wrote= : > > Hi Vipin. > > On Wed, Dec 22, 2021 at 10:53:50PM +0000, Vipin Sharma wrote: > > VM worker kthreads can linger in the VM process's cgroup for sometime > > after KVM terminates the VM process. > > Why is it a problem? And how long are we talking about? > Automated tools/scripts which delete VM cgroups after the main KVM process ends were seeing deletion errors because kernel worker threads were still running inside those cgroups. This is not a very frequent issue but we noticed it happens every now and then. > > A VM process can terminate between the time window of exit_mm() to > > cgroup_exit(), leaving only worker kthreads in the cgroup. > > Even kthreads should eventually have PF_EXITING set, they shouldd be > treated as "user-space" zombies by cgroups, i.e. mostly invisible (e.g. > it doesn't prevent rmdir'ing the cgroup). > Since that eventual time period is not known, we can either pause the script for sometime before starting the cleanup or add some x number of retries. Both of which are not preferable due to indeterministic nature. > (And after the last task_struct reference is gone, the cgroup structs > can be released too. Maybe the cause is holding the reference to the KVM > worker thread somewhere for too long.) > > > Moving worker kthreads back to the original cgroup (kthreadd_task's > > cgroup) makes sure that cgroup is empty as soon as the main VM process > > is terminated. > > BTW this used to be done for "user-space" tasks too (migrate to root > cgroup) but it was replaced with the less transactional "ignore zombies" > approach. So this change seems inconsistent. > Interesting, however, until PF_EXITING is set those threads will also exhibit the same behavior if it comes to deletion. Thanks for the background, good to know. Thanks