News for hackers. Stuff that matters.
Re:No nuances, just buggy code (was: related to Spinlock imp... by gwd on Sunday January 05, 2020 @09:51AM (#21961455)
I haven't read Linus' discussions, but often what happens when you take that approach is:

1. Scheduler looks through all the processes on the system, decides A is the one that should run right now

2. A decides it doesn't want to run, calls yield.

3. Scheduler looks again, thinks A is still the right one to run, and runs it again.

That is, in most cases, if the scheduler thought there was a better process to run, it would already have been running.

Re:No nuances, just buggy code (was: related to Spinlock imp... by barkingcat on Sunday January 05, 2020 @10:02AM (#21961496)
Please read Linus' discussion.

He states (paraphrasing) - it's undefined because there's no real way (unless in a realtime scheduler) to find what to yield to without instigating complexity.

The search itself is the quest that the kernel would rather not do at all. Step 3 is undefined and thus sched_yield itself is undefined.

Re:No nuances, just buggy code (was: related to Spinlock imp... by gwd on Sunday January 05, 2020 @12:03PM (#21962216)
GGP had a simple solution: "yield is the same as a preemption tick". I was trying to briefly explain what usually happens when you define things that way. I'm not advocating one thing or another.
Re:No nuances, just buggy code (was: related to Spinlock imp... by jcranmer on Sunday January 05, 2020 @12:13PM (#21962317)
That is somewhat surprising to me. My idea of the canonical use case for yield is interprocess communication: process A sent a message to process B, and it wants to yield the remainder of its scheduling quantum to process B because process A has nothing left to do. Although maybe this use case is satisfied by something other than sched_yield.
Re:No nuances, just buggy code (was: related to Spinlock imp... by DougBTX on Sunday January 05, 2020 @01:20PM (#21962866)
Yes, something else. Linus is pointing out that sched_yield doesn’t allow specifying where to yield control to, so it is a bad way to transfer control to a particular task.
Re:No nuances, just buggy code (was: related to Spinlock imp... by rowanG077 on Sunday January 05, 2020 @12:32PM (#21962476)
Why would it yield to process B and not one of the hundreds of other processes running?
Re:No nuances, just buggy code (was: related to Spinlock imp... by Dylan16807 on Sunday January 05, 2020 @01:36PM (#21962969)
In the case of the benchmark, at any point in time there might be 2 background processes that aren't asleep.

So let's look over our cores:

6 of them are churning through processes that do nothing but call sched_yield. Nothing has been waiting to run for more than 3 microseconds.

2 of them are doing real work, and the benchmark threads that happen to be on the same core have been waiting to run for most of a millisecond.

If you're stuck, then B is one of the processes that has been waiting.

So the obvious change would be to move threads around in sched_yield. Then B will get scheduled within microseconds.

Re:No nuances, just buggy code (was: related to Spinlock imp... by rowanG077 on Sunday January 05, 2020 @01:53PM (#21963098)
How is it possible that there are only 2 background processes? Any normal Linux system has hundreds maybe even thousands of processes running.
Re:No nuances, just buggy code (was: related to Spinlock imp... by Dylan16807 on Sunday January 05, 2020 @02:13PM (#21963225)
Two processes that aren't asleep.

What's your CPU use right now? I bet it's less than 200%. Or will be if you exclude your web browser.

The typical background process is active far less than one percent of the time.

Re:No nuances, just buggy code (was: related to Spinlock imp... by rowanG077 on Sunday January 05, 2020 @04:42PM (#21964336)
That doesn't matter that still doesn't guaranty yielding the thread will allow another specific process to run. Especially not if that process is soft locked to a different core.
Re:No nuances, just buggy code (was: related to Spinlock imp... by Dylan16807 on Sunday January 05, 2020 @05:55PM (#21964796)
> That doesn't matter that still doesn't guaranty yielding the thread will allow another specific process to run.

Right, but you would expect the "switch to another thread" function to eventually pick the one that's sitting there with a growing and growing time since it last got a chance to run.

It would be easy to guarantee that if any thread has been waiting for at least x amount of time, then any context switch without long-waiting local threads will pick that thread. (If it's on the same NUMA node, at least.) But in the actual implementation that time can build up to a huge amount with no effect, even as thousands of yield calls go by.

> Especially not if that process is soft locked to a different core.

Yeah, that's the problem, it's getting locked to a core even though other cores are desperate for threads to run.

Re:No nuances, just buggy code (was: related to Spinlock imp... by Filligree on Sunday January 05, 2020 @01:24PM (#21962897)
The best way to do that is with a blocking write. You're sending the messages using a mechanism such as a pipe, right?

Pipes have a small buffer. Once it's full, the caller will block, and the scheduler will know precisely what to schedule to unblock it.

Re:No nuances, just buggy code (was: related to Spinlock imp... by pmoriarty on Sunday January 05, 2020 @02:57PM (#21963606)
"there's no real way (unless in a realtime scheduler) to find what to yield to without instigating complexity"

What about just temporarily increasing the process' nice value?

That should be simple to do, and the scheduler already knows how to yield to processes with lower nice values.

Re:No nuances, just buggy code (was: related to Spinlock imp... by dooglius on Sunday January 05, 2020 @11:09AM (#21961868)
Well, the scheduler has the same problem when the preemption timer goes off, doesn't it?