Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp1253479pxb; Wed, 16 Feb 2022 14:48:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJz5pCMNBd00xC070uULPbtpwD7n4fqI54Fot3lCCjknhDDHm39jH5mmibw89d1EjWOXtACM X-Received: by 2002:a05:6402:2801:b0:40f:f179:c3ca with SMTP id h1-20020a056402280100b0040ff179c3camr5383837ede.226.1645051721733; Wed, 16 Feb 2022 14:48:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645051721; cv=none; d=google.com; s=arc-20160816; b=dp8yAffFkP4/c0JF4VUR9XHfL4pwOm7iVPrUB0908kp/CclpcHPG0ElSEJ44W8ilUV QxdYydFu3ZESXKhAxv5SDGP47yif6RYd6kk5gcl14nLvzPQsoJzr7lF8EgwI4zfaZc9O 5wMq53vE0Cf63P91q5v+kPZvhBJ3Q/Gmi/DJtw5hqhlDgnv8SbhEv9+m1+pv68PR3DaU /jlgdS8WbvgcGAOWEXc5V0iyHyemMCGfjKBFX+BI6D0w4SRxf4mYTfQiWUU74YKagiNf vFMIH5dDMkC27JXVvUfvWUV2Q8IdM2WhVQWagTlaSxnhbsVnNwK166/QSPpOlgHqrjNl 3Y/Q== 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=CLGpzGzLWEEIPQa2RoBV1b48UAm//y/cN1v178XuBnY=; b=ltPX4AXLNuzKb3g7FLEpm5wvyEgb4HslcIlEv3/pGOMnK6DLxJZnbBwp1VU/zD9tty Nm3P+7oY2aXg0XPbmEgKcXRvf+0jkBjqH8Uva3x1KhhI5Ck9hY5rRUstS9P+SwzvSL3/ 5wrR7WTFn8IYKjhK3mjynddt9c/Y7oogtGQS4YENISJu+KggE0/V70j32lBn5om0rDfJ PdaLVJVwGLj/vsig8rLZbhfZ6VUJoySW4h1XAxsY4IBgqphBp9kuH5ebRDUT0iyVqDBq 2G16xBAgdAh6En6AUiCaVoH4k8669snb/uGcuT6jfHI/Lw3z2N1OOvuJKfHvZ2e/umo0 XIrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=YIKcsqMF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c25si600887ejj.325.2022.02.16.14.47.55; Wed, 16 Feb 2022 14:48:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=YIKcsqMF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S237213AbiBPRih (ORCPT + 99 others); Wed, 16 Feb 2022 12:38:37 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:53162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237201AbiBPRig (ORCPT ); Wed, 16 Feb 2022 12:38:36 -0500 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B05A9A9AA for ; Wed, 16 Feb 2022 09:38:23 -0800 (PST) Received: by mail-lf1-x12f.google.com with SMTP id bu29so5315840lfb.0 for ; Wed, 16 Feb 2022 09:38:23 -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=CLGpzGzLWEEIPQa2RoBV1b48UAm//y/cN1v178XuBnY=; b=YIKcsqMF3eKCVXPEAvcBo0+LNPTAgkAASeO6TNkeW/s+2ROdkA/A6HTyBI6TEk091E B+exaNzlJPBcARjAcThzqD4S5H2FK9wus6v6wgaLIcKaG9xbSqNh5dr+JoMY/IeVZ6HC 26KBmcLh4TfDJEHR0OxdK2PearXUSrey6TC85sSCQM4kpIoBfcFgh5f7BJlHw0vEWk+s cjX7wCAZPK2PEtYu3K4aHj66MVQsSX+im8nuXKCvlrMiart2SBR8er73ICykTrReN3/r QEnKJtgYFY/mZbTIQCb6SI/3L5fSkEQPTmVqa4mH1IjhzaCsuObSUs93pQeBcOmqG2WH ZY4w== 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=CLGpzGzLWEEIPQa2RoBV1b48UAm//y/cN1v178XuBnY=; b=PfeF3WM0VG5G9BWV2lm0tcegxTOoJQBxoAau+ahpfzLGZ4DmSw9wzpSCBR6A3DKo7R mtCrlMUy4sUzEeOQ69brRH+wgfCuR3wJJ/HyW7+s3bfhSr7HTVAEPyY4yks/42RaGakO xEv8sVg4VSOvtsgbr9gxzmDpOjEDY9yN0FODYdp8xwaHEPS+Fkd6ppK0yTgmhjUJjBXx 9FonMlGjLzCTwTV0QbDJZSkeb2b9NG8B0A9ZIMQGJ6wF4xeDgjek7XYKECQXMA5fGsIE a0YkNmdypTpNrFKLabpmUI/hLL2efCaz31RFF3bQRiqSf9RHuKcK7Uom8HG7HRnQdysY fcAg== X-Gm-Message-State: AOAM531jdUwaDDHcGPgRMSYZz37Z0rDNFyBC5wHSaoti91dW/kh0kMwW g/VeCeeY2GDbG9Qtb09xC/zjdVwYMPUk9O6gy0uUCA== X-Received: by 2002:a05:6512:965:b0:443:7340:9893 with SMTP id v5-20020a056512096500b0044373409893mr2792075lft.119.1645033101394; Wed, 16 Feb 2022 09:38:21 -0800 (PST) MIME-Version: 1.0 References: <20211222225350.1912249-1-vipinsh@google.com> <20220105180420.GC6464@blackbody.suse.cz> <7a0bc562-9f25-392d-5c05-9dbcd350d002@redhat.com> <20220120150502.GC27269@blackbody.suse.cz> In-Reply-To: <20220120150502.GC27269@blackbody.suse.cz> From: Vipin Sharma Date: Wed, 16 Feb 2022 09:37:45 -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?= , Paolo Bonzini Cc: Tejun Heo , seanjc@google.com, 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 X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Paolo, Michal Paolo: Will you accept a patch which uses real_parent in kvm_vm_worker_thread() as suggested by Sean, while I figure out the recommendation from Michal about making kthread_stop() wait on kernel_wait()? cgroup_attach_task_all(current->real_parent, current) Michal: On Thu, Jan 20, 2022 at 7:05 AM Michal Koutn=C3=BD wrote= : > > On Wed, Jan 19, 2022 at 08:30:43AM -1000, Tejun Heo wrote= : > > It'd be nicer if we can make kthread_stop() waiting more regular but I > > couldn't find a good existing place and routing the usual parent > > signaling might be too complicated. Anyone has better ideas? > > The regular way is pictured in Paolo's diagram already, the > exit_notify/do_signal_parent -> wait4 path. > > Actually, I can see that there exists already kernel_wait() and is used > by a UMH wrapper kthread. kthreadd issues ignore_signals() so (besides > no well defined point of signalling a kthread) the signal notification > is moot and only waking up the waiter is relevant. So kthread_stop() > could wait via kernel_wait() based on pid (extracted from task_struct). > > Have I missed an obstacle? > I must admit I do not have a good understanding of kernel_wait() and kthread_stop() APIs. I tried making some changes in the kthread_stop() but I was not able to successfully use the API. I tested it by a writing a test module, where during the init I start a kthread which prints some message every few seconds and during the module exit I call kernel_stop(). This module worked as intended without the kernel_wait() changes in the kthread_stop() API. My changes were basically replacing wait_for_completion() with kernel_wait() call. @@ -645,8 +645,9 @@ int kthread_stop(struct task_struct *k) set_bit(KTHREAD_SHOULD_STOP, &kthread->flags); kthread_unpark(k); wake_up_process(k); - wait_for_completion(&kthread->exited); - ret =3D k->exit_code; + kernel_wait(k->pid, &ret); +// kernel_wait(task_pid_vnr(k), &ret); +// wait_for_completion(&kthread->exited); +// ret =3D k->exit_code; put_task_struct(k); I used few other combination where I put kernel_wait() call after put_task_struct(k) call. Every time during the module exit, kernel was crashing like: [ 285.014612] RIP: 0010:0xffffffffc04ed074 [ 285.018537] RSP: 0018:ffff9ccdc8365ee8 EFLAGS: 00010246 [ 285.023761] RAX: 0000000000000000 RBX: 0000000000000012 RCX: 00000000000= 00001 [ 285.030896] RDX: 0000000000000000 RSI: 0000000000000286 RDI: ffff9cce3f7= d9cc0 [ 285.038028] RBP: ffff9ccdc8365ef8 R08: 0000000000000000 R09: 00000000000= 15504 [ 285.045160] R10: 000000000000004b R11: ffffffff8dd92880 R12: 00000000000= 00012 [ 285.052293] R13: ffff9ccdc813db90 R14: ffff9ccdc7e66240 R15: ffffffffc04= ed000 [ 285.059425] FS: 0000000000000000(0000) GS:ffff9cce3f7c0000(0000) knlGS:0000000000000000 [ 285.067510] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 285.073258] CR2: ffffffffc04ed074 CR3: 000000c07f20e002 CR4: 00000000003= 62ef0 [ 285.080390] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 00000000000= 00000 [ 285.087522] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 00000000000= 00400 [ 285.094656] Call Trace: [ 285.097112] kthread+0x148/0x1b0 [ 285.100343] ? kthread_blkcg+0x30/0x30 [ 285.104096] ret_from_fork+0x3a/0x60 [ 285.107671] Code: Bad RIP value. [ 285.107671] IP: 0xffffffffc04ecff4: Crash is not observed if I keep wait_for_completion(&kthread->exited) along with kernel_wait(), but I guess the kernel_wait() should be sufficient by itself if I figure out proper way to use it. Do you have any suggestions what might be the right way to use this API? Thanks Vipin