From: Petr Mladek <pmladek@suse.com>
To: Rik van Riel <riel@surriel.com>
Cc: linux-kernel@vger.kernel.org, live-patching@vger.kernel.org,
kernel-team@fb.com, Josh Poimboeuf <jpoimboe@kernel.org>,
Jiri Kosina <jikos@kernel.org>, Miroslav Benes <mbenes@suse.cz>,
Joe Lawrence <joe.lawrence@redhat.com>,
Breno Leitao <leitao@debian.org>
Subject: Re: [PATCH v2] livepatch: fix race between fork and klp_reverse_transition
Date: Mon, 25 Jul 2022 15:32:22 +0200 [thread overview]
Message-ID: <Yt6bZo5ztnVSjLLC@alley> (raw)
In-Reply-To: <20220722150106.683f3704@imladris.surriel.com>
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 <riel@surriel.com>
> Reported-by: Breno Leitao <leitao@debian.org>
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 <pmladek@suse.com>
Best Regards,
Petr
next prev parent reply other threads:[~2022-07-25 13:32 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-20 16:10 [PATCH,RFC] livepatch: fix race between fork and klp_reverse_transition Rik van Riel
2022-07-21 23:23 ` Song Liu
2022-07-22 15:30 ` Petr Mladek
2022-07-22 19:01 ` [PATCH v2] " Rik van Riel
2022-07-25 13:32 ` Petr Mladek [this message]
2022-07-25 13:49 ` [PATCH v3] " Rik van Riel
2022-07-27 0:10 ` Josh Poimboeuf
2022-07-27 0:26 ` Rik van Riel
2022-07-28 15:20 ` Petr Mladek
2022-07-27 14:24 ` [PATCH v4] livepatch: fix race between fork and KLP transition Rik van Riel
2022-07-28 15:37 ` Petr Mladek
2022-08-02 20:07 ` Rik van Riel
2022-08-04 13:06 ` Petr Mladek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Yt6bZo5ztnVSjLLC@alley \
--to=pmladek@suse.com \
--cc=jikos@kernel.org \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=kernel-team@fb.com \
--cc=leitao@debian.org \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=riel@surriel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).