Page MenuHomeFreeBSD

Ensure "init" (PID 1) also executes userret() initially
ClosedPublic

Authored by olce on Oct 17 2023, 2:39 PM.
Tags
None
Referenced Files
Unknown Object (File)
Apr 30 2024, 5:52 AM
Unknown Object (File)
Apr 30 2024, 5:52 AM
Unknown Object (File)
Apr 30 2024, 5:52 AM
Unknown Object (File)
Apr 30 2024, 5:10 AM
Unknown Object (File)
Apr 29 2024, 9:56 PM
Unknown Object (File)
Jan 5 2024, 10:33 PM
Unknown Object (File)
Dec 25 2023, 5:23 PM
Unknown Object (File)
Dec 23 2023, 2:05 AM

Details

Summary

Calling userret() from fork_return() misses the first return to
userspace of the "init" (PID 1) process. The latter is indeed created
by fork1() followed by a call to cpu_fork_kthread_handler() call that
replaces fork_return() by start_init() as the function to execute after
fork.

A new process' initial return to userspace in the end always happens
through returning from fork_exit(), so move userret() there instead to
fix the omission.

This problem was discovered as part of a revamp of scheduling priorities
that lead to experimenting with asserting and sometimes resetting
priorities in sched_userret(), in the course of which the author
stumbled on panics being triggered only in init() or only in other
processes, depending on the modifications to sched_userret(). This
change currently has no practical effect but will have some in the near
future.

MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

olce requested review of this revision.Oct 17 2023, 2:39 PM

How did you notice this?

sys/kern/kern_fork.c
1234

Shouldn't this move as well?

I would have loved to be able to say it was only through thorough code analysis, and it seems I was quite close at some point. But I stumbled into it in a more "traditional way", by playing with adding behaviors and assertions in sched_userret(), and getting panics because init didn't get its priority reset correctly, whereas all other processes would, to my amazement.

sys/kern/kern_fork.c
1234

I don't think so because, if I'm not mistaken, ktrsysret() is here to log syscall returns with the return value, which is useful for a regular fork() or (a failing) exec() called from userland, but not for init's initial return because there was initially no system call to give a return value to (it's the first userland execution).

I think it's worth mentioning in the commit message something about how this was found, or under what conditions it would cause a practical problem -- I think it's the case that there's no practical impact (correct?) and it would be good to convey that in the message.

I think it's worth mentioning in the commit message something about how this was found, or under what conditions it would cause a practical problem -- I think it's the case that there's no practical impact (correct?) and it would be good to convey that in the message.

Currently, there is no practical problem, but the priority changes will introduce one. Going to amend the message as you requested.

This revision is now accepted and ready to land.Oct 17 2023, 3:49 PM
olce edited the summary of this revision. (Show Details)

Indicate how the bug was found and its practical scope in the commit message.

This revision now requires review to proceed.Oct 19 2023, 7:56 AM
olce marked an inline comment as done.Oct 19 2023, 8:00 AM
This revision is now accepted and ready to land.Oct 19 2023, 11:30 AM
This revision was automatically updated to reflect the committed changes.