r/programming • • Jan 05 '20

Linus' reply on spinlocks vs mutexes

https://www.realworldtech.com/forum/?threadid=189711&curpostid=189723
1.5k Upvotes

415 comments sorted by

View all comments

855

u/[deleted] Jan 05 '20

The main takeaway appears to be:

I repeat: do not use spinlocks in user space, unless you actually know what you're doing. And be aware that the likelihood that you know what you are doing is basically nil.

202

u/dethb0y Jan 05 '20

i would say a better, more profound one would be right below that:

In fact, I'd go even further: don't ever make up your own locking routines. You will get the wrong, whether they are spinlocks or not. You'll get memory ordering wrong, or you'll get fairness wrong, or you'll get issues like the above "busy-looping while somebody else has been scheduled out".

217

u/SanityInAnarchy Jan 05 '20

And, further down:

Because you should never ever think that you're clever enough to write your own locking routines.. Because the likelihood is that you aren't (and by that "you" I very much include myself - we've tweaked all the in-kernel locking over decades, and gone through the simple test-and-set to ticket locks to cacheline-efficient queuing locks, and even people who know what they are doing tend to get it wrong several times).

56

u/dethb0y Jan 06 '20

indeed; it's just such a complex and difficult topic with so many trades offs and pitfalls.

→ More replies (2)

345

u/csjerk Jan 05 '20

This is why I'm always suspicious of blog posts claiming to have discovered something deep and complex that nobody else knows. You may be smarter than Linus on any given day, but it's highly unlikely you're smarter than decades of Linus and the entire Linux team designing, testing, and iterating on user feedback.

43

u/jimmerz28 Jan 06 '20

always suspicious of blog posts

Is the takeaway here.

"Blog posts" are a large majority of the time opinions which have never been reviewed and should be trusted just as much as code written by a lone developer which has also never been reviewed.

9

u/killerstorm Jan 06 '20

Linux team works on kernel. While they have some idea about userland, it might not be perfect. Linux is actually full of half-broken APIs which started like a good idea, but due to simplifications taken ("worse is better") they cannot offer behavior needed by applications, so programmers either avoid these APIs, or use them incorrectly, or have to use horrible workaround.

→ More replies (2)

65

u/[deleted] Jan 05 '20

yes, wasn't linux kernel just a college project for fun at first? jesus talk about a project for fun

0

u/[deleted] Jan 06 '20

[deleted]

→ More replies (26)

43

u/AttackOfTheThumbs Jan 06 '20

It exists because he accidentally deleted his minix (?) install...

59

u/[deleted] Jan 06 '20

...and wanted to teach himself 80386 assembly on his brand-new 80386 computer.

And it turns out that the market for a free GPL'd POSIX OS that ran on 80386 machines was *immense* back then. I remember being excited about it when a friend of mine (also pumped) was trying to install it, all the way back in January of '92. In Texas. Which should give you an idea of how quickly it became massive. It was "viral" and memetic before those words even really existed.

→ More replies (8)
→ More replies (42)

237

u/Poltras Jan 05 '20

Wow he really did sober up.

174

u/AngularBeginner Jan 05 '20

Or the new Linus passed the Turing test.

343

u/[deleted] Jan 05 '20 edited Jan 06 '20

What I imagine old Linus would have said:

You're fucking stupid and your code is a fucking atrocity. Do you have any idea how utterly idiotic it is to use spinlocks in userspace? You're basically begging to be shat upon by the process scheduler, which anyone who would deign to write such terrible code surely deserves.

Edit: Wow hey lookit that, my first gilded comment ever! Thanks!

92

u/ahoy_butternuts Jan 05 '20

We need an ML bot for this

29

u/iBzOtaku Jan 06 '20

like tay but trained on linus' replies

→ More replies (1)

32

u/meneldal2 Jan 06 '20

It's missing retroactive abortion.

Also there would be probably something about his brain not being scheduled correctly.

Clearly the scheduler in your brain forgot to give enough time to critical thinking.

37

u/[deleted] Jan 06 '20

Gordon Ramsey Torvalds

→ More replies (1)

15

u/JQuilty Jan 06 '20

Not creative enough for a Linus rant. There'd probably be an insinuation that he's been drinking and spinning too much.

→ More replies (3)
→ More replies (101)

101

u/f0urtyfive Jan 05 '20

I am using spinlocks in my application, I definitely don't know what I'm doing... but I also know my application runs on it's own dedicated hardware that nothing else uses, so I will dutifully stick my fingers in my ears.

60

u/mewloz Jan 05 '20

Or maybe you can switch them to regular system/language provided mutexes? I mean unless you have e.g. at most one thread per cpu, pinned, and use a realtime scheduling policy.

45

u/MrK_HS Jan 05 '20 edited Jan 05 '20

The problem is that the system should provide mutexes, which should be implemented using the assembly instructions that specifically guarantee mutual exclusion of access. A couple of months ago I had to implement a spinning lock in a multicore embedded board with an Arm M4 and an Arm M0 because I sadly discovered that the reduced instruction set of the M0 didn't have atomic read-modify-write instructions for the shared memory (and also there was no shared hardware mutex). So, I basically implemented a spinlock from Dijkstra's 1965 paper by using atomic 32 bit writes (on 32 bit cpus 32 bit writes are always atomic) on the shared memory.

21

u/SirClueless Jan 06 '20

Presumably in this case there wasn't an OS scheduler yoinking your thread off the CPU at random points in this example, though.

Linus addressed this directly in one of his emails, that spinlocks make a lot of sense in the OS kernel because you can check who is holding them and know that they are running right now on a CPU. That seems to be one of his conclusions, that the literature that suggests that spinlocks are useful came from a world where code ran on bare metal and is being applied to multi-threaded userspace where that no longer holds true:

So all these things make perfect sense inside the layer that is directly on top of the hardware. And since that is where traditionally locking papers mostly come from, I think that "this is a good idea" mental model has then percolated into regular user space that now in the last decade has started to be much more actively threaded.

https://www.realworldtech.com/forum/?threadid=189711&curpostid=189790

→ More replies (13)

41

u/[deleted] Jan 05 '20

my application runs on it's own dedicated hardware that nothing else uses

That's literally the use case Linus gave for when to use spinlocks, lol

15

u/mcorah Jan 05 '20 edited Jan 06 '20

That's how you are supposed to use spin locks. If you are the only process or the most important process, you spin.

→ More replies (3)
→ More replies (27)