Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp2726781imi; Mon, 25 Jul 2022 06:47:32 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ttawqotPmhPpYAti4pbT1ar1R8GqAov8FDJo5pQyh9C7eW1kURyUkXCc6bmDHozd1H5n43 X-Received: by 2002:a05:6808:e89:b0:33a:315a:eb90 with SMTP id k9-20020a0568080e8900b0033a315aeb90mr5432352oil.162.1658756851501; Mon, 25 Jul 2022 06:47:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658756851; cv=none; d=google.com; s=arc-20160816; b=GrWhdncpdRvRp5ppwvkJsdaTEzqltpYeND5lX0dwezttbiBFiFJxtCCYYMEk+mvuG+ LuC1+xIycdvrUOVJfCnsccbf47+Fx2jJJpN6JK+NW7VZ6PS4lbkljvWvVb9oLDSokoax D2Heh5+/CMWwnWj2moU1772qjmaUgTTYD5dIUo7p8A5JkiZiGeRZa3ZFgXBZUusurrjA FmUmbXKv0+eZMnyW5J73zGILKKqkGofwT880cXTU9MAX1Oq6lokajg54Hnb9mvSbV/OQ zQOS6k4PKwm6K73yjYxkQHXFBr6NhMno3TZweNbawdGAhJr+5IRF4c77czfq6H/28Lgx iwbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=t9LEZiaIxr7qWut5KwVWqnFFQS4Gg1WXbelNaKHOXxs=; b=YwqzGjbatCYLVfZVcuYYrDjiVp5ehq9eDJQI3TDG5UdJyWEHel8pivIUa8QKE6yMia IM3EheY8UKSQh95caMoXbV1B+W3Fw+f4dt8bguNDDdzlDkY30B2TkxzZxdxbEluwN0mK JNafbvyRbp0u7HFyoyiornTvshRoP55mEsx1d4c7A1LDR/gK8441V1T+S9uJgwUlMWdb KRKJN9bSRdkwLjVYTqK2nrWeRkrT9+WjP87Gy4xRj7cXtVA4rjtOjclvAlPu5vhWc7y2 dWZ66QdfF63WEUHL49gWh505ZTXp4qYfywIo4gf7IRAJoQ6kbq8l29Hwfvnnu3EaMVf5 HqoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=Zawmajlq; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f20-20020a056870d15400b0010c13584e3bsi10227240oac.99.2022.07.25.06.47.15; Mon, 25 Jul 2022 06:47:31 -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=@suse.com header.s=susede1 header.b=Zawmajlq; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235131AbiGYNc2 (ORCPT + 99 others); Mon, 25 Jul 2022 09:32:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234946AbiGYNc0 (ORCPT ); Mon, 25 Jul 2022 09:32:26 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4BBFDE96; Mon, 25 Jul 2022 06:32:24 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 6B7F61FB52; Mon, 25 Jul 2022 13:32:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1658755943; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=t9LEZiaIxr7qWut5KwVWqnFFQS4Gg1WXbelNaKHOXxs=; b=ZawmajlquknUUrsNaNFuwHIe/SAjv+wxqrOqyi2DXAo1rlqiTjetta3AbZl0nNBVHRZwgT 0hzfjG5w1QGJ43WbAPCdIOCiVlxnEfms76oYWwAz/GQ3zoYbE4+RvTXe7eeo9gToJjI1oV eONWmLAqsWsWHuvU5u1dCRkiz/g8FrA= Received: from suse.cz (unknown [10.100.208.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 4198F2C153; Mon, 25 Jul 2022 13:32:22 +0000 (UTC) Date: Mon, 25 Jul 2022 15:32:22 +0200 From: Petr Mladek To: Rik van Riel Cc: linux-kernel@vger.kernel.org, live-patching@vger.kernel.org, kernel-team@fb.com, Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Joe Lawrence , Breno Leitao Subject: Re: [PATCH v2] livepatch: fix race between fork and klp_reverse_transition Message-ID: References: <20220720121023.043738bb@imladris.surriel.com> <20220722150106.683f3704@imladris.surriel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220722150106.683f3704@imladris.surriel.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 Fri 2022-07-22 15:01:06, Rik van Riel wrote: > v2: a better approach, suggested by Petr (thank you) > ---8<--- > > 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. It should fix these races as well. Both task->patch_state and TIF_PATCH_PENDING flag are almost always modified under tasklist_lock. One exception is klp_update_patch_state(current) but it could not race because klp_copy_process() is called under spinlock. So that "current" can't sleep and can't get migrated in the middle of klp_copy_process(). Another exception is klp_check_and_switch_task() that is called under p->pi_lock. It prevents rescheduling and the task will be migrated only when sleeping. As a result "current" again can't get migrated inside klp_copy_process(). Finally, the state of "idle" tasks (idle_task(cpu)) is updated without tasklist_lock. But they are not forked so that we are on safe side. > Signed-off-by: Rik van Riel > Reported-by: Breno Leitao We should update the commit message and mention also the other two locations where the state is manipulated without tasklist_lock. I am sorry that I did not mention it on Friday. Also we should remove "I have not figured yet whether". The patch should fix these races as well. With the above changes: Reviewed-by: Petr Mladek Best Regards, Petr