Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp90199imi; Thu, 21 Jul 2022 16:39:38 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ugWSHrNs85AMAY5XH0LQ9t+5zYLfNZw4JPSq9w9B1kXshRpg5D+zuLs+6mod56KR2EIT5Z X-Received: by 2002:a17:906:974c:b0:72b:8cea:95c2 with SMTP id o12-20020a170906974c00b0072b8cea95c2mr813090ejy.65.1658446777853; Thu, 21 Jul 2022 16:39:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658446777; cv=none; d=google.com; s=arc-20160816; b=zfr46eU2OZoUY88VgUSHmDvXkVPEr7tTWM/PsqiDBf7SQwNEHOcS9RSscUmNGT+W6Z rdAIOwLlRgeNM0OMKxZWtmjkj9MC+I8mVs4ahnt2IamG1mD/Q/bapPEnmGc0MFD6Muco tArNz9wlB/V/nJF3WQgZs5T/GUSB9rJD6xIzEDOEOEGq4AzSW/trhns1NdJbpoj31NuA njvbZXikxsJQFiDKmKv2rOOJvUQ5qhrHAqrbR2LhkQcu9dc3DtLjUdw22ZOeLrznHTv/ MBrEl6xdWdR6uMGxmwlFehVAN+zFiq8DxyHzoua2Q6RXYKobTxbJx7GiJqNTr5dJh9Eq u5jQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=sjAIKv/N3B0YK3TLr/f/HSi1bEY5o4F7QuDv7EoXtlM=; b=f91AgvzRlW2uReUaci1zH4eKmzoeXRd5NCjsAyAlrqwaU185kVaiOI+eIf4nW5rE5D URKC1RZc0UfsebuymTe+SPPwcBk+6FMroqj2lxCQ7rSaEH7wh3pQDrDNkkp593JCYEN2 3TTAKZANf0UPkwLrWjVI/rI/3mRzCNWWaOKuiUrfAbQD7xIsWwD4b5yc87YHq1ALyDLL zXMnjDOpjNrQfzVSG1QJ18tdg1r5coy+hNbocgQstCZJeJcH0QVVO0JPle4s7ieiyk4M 0TsINirxbizRueNt2liZroNxpgq+OzC6dE/XbbE1LoPV+VfOHRqW4QEvkpPpohyL4kkB dNvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="LCjX/b+i"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sc23-20020a1709078a1700b0072ab5d0fc33si4095579ejc.863.2022.07.21.16.39.01; Thu, 21 Jul 2022 16:39:37 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b="LCjX/b+i"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233744AbiGUXYP (ORCPT + 99 others); Thu, 21 Jul 2022 19:24:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233762AbiGUXYL (ORCPT ); Thu, 21 Jul 2022 19:24:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 585688E6DC; Thu, 21 Jul 2022 16:24:10 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id DC33E61EC5; Thu, 21 Jul 2022 23:24:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44398C341CB; Thu, 21 Jul 2022 23:24:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1658445849; bh=P/6rQAdP9rzBa942itCTf9L8nhPLhGKaMmQZTpYf4lg=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=LCjX/b+iSIxZjZQvd2rfDS16Fe/nEBFHCWFQRZZiEGGGot0e0lmmUYR6uidyMQpSH X1V+KsxX1lNZ24O1aV3TFzo0DAbRI2Nlf7eJH3HJXKxakQhr9mHYx7m+30xpUWVApA 9yjbNFAkYT/OHttaaV/9B4mr9bSlPMJui4zJeYhIZ7YtkgMwVJtEdGQElyF+oAB2/9 8PP97tH6HEiif4pIS7/rFyhEpMRGV4avHThC/ayQVct7WfqKpZTyNRYpUQMzh3+qor enkacwShLkAXxtLmUKoT8jAurXoHZiecxr7wGs6GsgbZieunEGQIB+38zh5ixXW/vi p7jfStDD9CH4A== Received: by mail-yb1-f177.google.com with SMTP id 7so5470940ybw.0; Thu, 21 Jul 2022 16:24:09 -0700 (PDT) X-Gm-Message-State: AJIora/EPO1fx6ztpTlMKOIM/Pb2UOXcFNdns4dc6hCffgPuPMCQTmEK NTmSOUyWR3aDglflor9D7eLID3pY1P92mS9zJVQ= X-Received: by 2002:a25:8611:0:b0:66e:d9e7:debc with SMTP id y17-20020a258611000000b0066ed9e7debcmr798529ybk.257.1658445848324; Thu, 21 Jul 2022 16:24:08 -0700 (PDT) MIME-Version: 1.0 References: <20220720121023.043738bb@imladris.surriel.com> In-Reply-To: <20220720121023.043738bb@imladris.surriel.com> From: Song Liu Date: Thu, 21 Jul 2022 16:23:57 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH,RFC] livepatch: fix race between fork and klp_reverse_transition To: Rik van Riel Cc: open list , live-patching@vger.kernel.org, Kernel Team , Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence , Breno Leitao Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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 On Wed, Jul 20, 2022 at 9:20 AM Rik van Riel wrote: > > When a KLP fails to apply, klp_reverse_transition will clear the > TIF_PATCH_PENDING flag on all tasks, except for newly created tasks > which are not on the task list yet. > > Meanwhile, fork will copy over the TIF_PATCH_PENDING flag from the > parent to the child early on, in dup_task_struct -> setup_thread_stack. > > Much later, klp_copy_process will set child->patch_state to match > that of the parent. > > However, the parent's patch_state may have been changed by KLP loading > or unloading since it was initially copied over into the child. > > This results in the KLP code occasionally hitting this warning in > klp_complete_transition: > > for_each_process_thread(g, task) { > WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING)); > task->patch_state = KLP_UNDEFINED; > } > > This patch will set, or clear, the TIF_PATCH_PENDING flag in the child > process depending on whether or not it is needed at the time > klp_copy_process is called, at a point in copy_process where the > tasklist_lock is held exclusively, preventing races with the KLP > code. > > This should prevent this warning from triggering again in the > future. > > I have not yet figured out whether this would also help with races in > the other direction, where the child process fails to have TIF_PATCH_PENDING > set and somehow misses a transition, or whether the retries in > klp_try_complete_transition would catch that task and help it transition > later. > > Signed-off-by: Rik van Riel > Reported-by: Breno Leitao LGTM! Acked-by: Song Liu > --- > kernel/livepatch/transition.c | 10 +++++++++- > 1 file changed, 9 insertions(+), 1 deletion(-) > > diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c > index 5d03a2ad1066..7a90ad5e9224 100644 > --- a/kernel/livepatch/transition.c > +++ b/kernel/livepatch/transition.c > @@ -612,7 +612,15 @@ void klp_copy_process(struct task_struct *child) > { > child->patch_state = current->patch_state; > > - /* TIF_PATCH_PENDING gets copied in setup_thread_stack() */ > + /* > + * The parent process may have gone through a KLP transition since > + * the thread flag was copied in setup_thread_stack earlier. Set > + * the flag according to whether this task needs a KLP transition. > + */ > + if (child->patch_state != klp_target_state) > + set_tsk_thread_flag(child, TIF_PATCH_PENDING); > + else > + clear_tsk_thread_flag(child, TIF_PATCH_PENDING); > } > > /* > -- > 2.35.1 >