Avoiding ping pong

By: Linus Torvalds (torvalds.delete@this.linux-foundation.org), December 5, 2014 12:58 pm
Room: Moderated Discussions
Konrad Schwarz (no.spam.delete@this.no.spam) on December 4, 2014 11:08 pm wrote:
>
> Since it is being accessed in a multi-threaded way, via multiple access paths,
> generally it needs its own mutex -- otherwise, reference counting would not
> be required to be atomic and a lock of a higher-level object would suffice

The problem with reference counts is that you often need to take them *before* you take the lock that protects the object data.

The thing is, you have two different cases:

- object *reference*

- object data

and they have completely different locking.

Object data locking is generally per-object. Well, unless you don't have huge scalability issues, in which case you may have some external bigger lock (extreme case: one single global lock).

But object *referencing* is mostly about finding the object (and removing/freeing it). Is it on a hash chain? Is it in a tree? Linked list? When the reference count goes down to zero, it's not the object data that you need to protect (the object is not used by anything else, so there's nothing to protect!), it's the ways to find the object you need to protect.

And the lock for the lookup operation cannot be in the object, because - by definition - you don't know what the object is! You're trying to look it up, after all.

So generally you have a lock that protects the lookup operation some way, and the reference count needs to be atomic with respect to that lock.

And yes, that lock may well be sufficient, and now you're back to non-atomic reference counts. But you usually don't have just one way to look things up: you might have pointers from other objects (and that pointer is protected by the object locking of the other object), but there may be multiple such objects that point to this (which is why you have a reference count in the first place!).

See what happens? There is no longer one single lock for lookup. Imagine walking a graph of objects, where objects have pointers to each other. Each pointer implies a reference to an object, but as you walk the graph, you have to release the lock from the source object, so you have to take a new reference to the object you are now entering.

And in order to avoid deadlocks, you can not in the general case take the lock of the new object first - you have to release the lock on the source object, because otherwise (in a complex graph), how do you avoid simple ABBA deadlock?

So atomic reference counts fix that. They work because when you move from object A to object B, you can do this:

(a) you have a reference count to A, and you can lock A

(b) once object A is locked, the pointer from A to B is stable, and you know you have a reference to B (because of that pointer from A to B)

(c) but you cannot take the object lock for B (ABBA deadlock) while holding the lock on A

(d) increment the atomic reference count on B

(e) now you can drop the lock on A (you're "exiting" A)

(f) your reference count means that B cannot go away from under you despite unlocking A, so now you can lock B.

See? Atomic reference counts make this kind of situation possible. Yes, you want to avoid the overhead if at all possible (for example, maybe you have a strict ordering of objects, so you know you can walk from A to B, and never walk from B to A, so there is no ABBA deadlock, and you can just lock B while still holding the lock on A).

But if you don't have some kind of forced ordering, and if you have multiple ways to reach an object (and again - why have reference counts in the first place if that isn't true!) then atomic reference counts really are the simple and sane answer.

If you think atomic refcounts are unnecessary, that's a big flag that you don't actually understand the complexities of locking.

Trust me, concurrency is hard. There's a reason all the examples of "look how easy it is to parallellize things" tend to use simple arrays and don't ever have allocations or freeing of the objects.

People who think that the future is highly parallel are invariably completely unaware of just how hard concurrency really is. They've seen Linpack, they've seen all those wonderful examples of sorting an array in parallel, they've seen all these things that have absolutely no actual real complexity - and often very limited real usefulness.

Linus
< Previous Post in ThreadNext Post in Thread >
TopicPosted ByDate
ARMv8 getting atomic operationsdmcq12/02/14 05:32 PM
  ARMv8 getting atomic operationsMaynard Handley12/02/14 07:33 PM
    ARMv8 getting atomic operationsDoug S12/02/14 10:30 PM
      ARMv8 getting atomic operationsdmcq12/03/14 03:16 AM
      ARMv8 getting atomic operationsMaynard Handley12/03/14 09:20 AM
      ARMv8 getting atomic operationsBrett12/03/14 04:46 PM
    ARMv8 getting atomic operationsAndreas12/03/14 06:51 AM
      ARMv8 getting atomic operationsLinus Torvalds12/03/14 11:15 AM
        ARMv8 getting atomic operationsanon12/03/14 05:08 PM
          Guaranteed transactionsPaul A. Clayton12/03/14 08:04 PM
            Guaranteed transactionsanon12/03/14 08:38 PM
              Avoiding ping pongPaul A. Clayton12/04/14 09:11 AM
                Avoiding ping ponganon12/04/14 10:15 AM
                  OoO window is limitedPaul A. Clayton12/04/14 01:06 PM
                Avoiding ping pongAaron Spink12/04/14 12:01 PM
                  Avoiding ping pongKonrad Schwarz12/04/14 01:10 PM
                    Avoiding ping pongAaron Spink12/04/14 02:31 PM
                    Avoiding ping pongGabriele Svelto12/04/14 02:49 PM
                      Avoiding ping pongKonrad Schwarz12/04/14 11:08 PM
                        Avoiding ping pongGabriele Svelto12/05/14 12:04 AM
                          Avoiding ping pongEric Bron nli12/05/14 02:28 AM
                            Avoiding ping pongKonrad Schwarz12/05/14 03:37 AM
                              Avoiding ping pongEric Bron nli12/05/14 04:23 AM
                                Avoiding ping pongKlimax12/05/14 05:47 AM
                                  Avoiding ping pongEric Bron12/05/14 06:24 AM
                              Avoiding ping pongGabriele Svelto12/05/14 10:38 AM
                                Avoiding ping pongKonrad Schwarz12/07/14 02:28 PM
                                  Avoiding ping pongGabriele Svelto12/08/14 07:10 PM
                                    Avoiding ping pongKonrad Schwarz12/09/14 05:12 AM
                                      Avoiding ping pongGabriele Svelto12/09/14 07:31 AM
                                        Avoiding ping ponganon12/09/14 11:24 PM
                            Avoiding ping pongGabriele Svelto12/05/14 10:17 AM
                              Avoiding ping pongEric Bron12/05/14 10:32 AM
                                Avoiding ping pongGabriele Svelto12/05/14 12:45 PM
                                  Avoiding ping pongEric Bron12/06/14 02:20 AM
                                    Avoiding ping pongnksingh12/06/14 03:42 AM
                                      Avoiding ping pongEric Bron12/06/14 04:04 AM
                                        Avoiding ping pongGiGNiC12/06/14 06:27 AM
                                          Avoiding ping pongEric Bron nli12/06/14 06:44 AM
                                          Avoiding ping pongEric Bron12/06/14 07:07 AM
                                            Avoiding ping pongnksingh12/07/14 04:06 PM
                                              Avoiding ping pongEric Bron12/08/14 04:17 AM
                                                Avoiding ping pongGiGNiC12/08/14 11:53 AM
                                                Avoiding ping pongnksingh12/08/14 05:53 PM
                                                  Avoiding ping pongEric Bron12/09/14 01:33 AM
                                    Avoiding ping pongdmsc12/06/14 04:12 AM
                                      Avoiding ping pongEric Bron12/06/14 04:25 AM
                                        Avoiding ping pongKlimax12/06/14 05:49 AM
                                          Avoiding ping pongrwessel12/07/14 02:34 AM
                                        Avoiding ping pongdmsc12/06/14 07:39 AM
                                        Avoiding ping pongKonrad Schwarz12/07/14 02:37 PM
                                          Avoiding ping pongMichael S12/07/14 04:37 PM
                                            Avoiding ping pongKonrad Schwarz12/08/14 04:35 AM
                          Avoiding ping pongKonrad Schwarz12/05/14 03:30 AM
                        Avoiding ping pongLinus Torvalds12/05/14 12:58 PM
                          Avoiding ping pongEric Bron12/06/14 02:42 AM
                            Avoiding ping pongnksingh12/06/14 03:51 AM
                              Avoiding ping pongEric Bron12/06/14 04:08 AM
                            Avoiding ping pongLinus Torvalds12/06/14 01:25 PM
                              Avoiding ping pongnksingh12/07/14 03:26 PM
                                Avoiding ping pongEric Bron12/08/14 04:35 AM
                                  Avoiding ping pongBrett12/08/14 10:00 AM
                                    Avoiding ping pongEric Bron12/08/14 10:48 AM
                                    Avoiding ping pongrwessel12/08/14 12:52 PM
                                      Avoiding ping pongBrett12/08/14 01:58 PM
                                      Avoiding ping pongDoug S12/08/14 02:04 PM
                              Avoiding ping pongJouni Osmala12/08/14 02:45 AM
                                Avoiding ping ponganon12/08/14 05:44 AM
                                  Avoiding ping pongJouni Osmala12/08/14 01:10 PM
                                    Avoiding ping pongLinus Torvalds12/08/14 01:34 PM
                                      Avoiding ping pongJouni Osmala12/08/14 03:47 PM
                                        Avoiding ping pongLinus Torvalds12/08/14 08:08 PM
                                          Avoiding ping pongGabriele Svelto12/09/14 07:48 AM
                                            Avoiding ping pongMaynard Handley12/09/14 11:41 AM
                                              Avoiding ping pongPatrick Chase12/09/14 01:06 PM
                                              Avoiding ping pongGabriele Svelto12/09/14 01:52 PM
                                                Avoiding ping pongPatrick Chase12/09/14 02:08 PM
                                            Why read RWT or Reddit when you can get journalists to do it for you?Rob Thorpe01/02/15 08:20 AM
                                              Why read RWT or Reddit when you can get journalists to do it for you?juanrga01/02/15 11:21 AM
                                                Why read RWT or Reddit when you can get journalists to do it for you?EduardoS01/02/15 11:37 AM
                                                  Why read RWT or Reddit when you can get journalists to do it for you?juanrga01/03/15 12:00 PM
                                                Why read RWT or Reddit when you can get journalists to do it for you?Eric Bron nli01/02/15 02:28 PM
                                                  Why read RWT or Reddit when you can get journalists to do it for you?juanrga01/03/15 12:02 PM
                                                    Why read RWT or Reddit when you can get journalists to do it for you?Michael S01/03/15 12:36 PM
                                                      Why read RWT or Reddit when you can get journalists to do it for you?juanrga01/03/15 01:11 PM
                                                        Why read RWT or Reddit when you can get journalists to do it for you?Michael S01/03/15 01:30 PM
                                                          Why read RWT or Reddit when you can get journalists to do it for you?Eric Bron01/03/15 02:57 PM
                                                            KNL cacheDavid Kanter01/03/15 07:36 PM
                                                              KNL cacheEric Bron01/04/15 03:34 AM
                                                                KNL cacheMichael S01/04/15 04:11 AM
                                                                  KNL cacheEric Bron01/04/15 04:57 AM
                                                                    KNL cacheMichael S01/04/15 05:21 AM
                                                                      KNL cacheEric Bron01/04/15 05:58 AM
                                                          Why read RWT or Reddit when you can get journalists to do it for you?juanrga01/07/15 05:47 AM
                                                            Why read RWT or Reddit when you can get journalists to do it for you?Michael S01/07/15 08:27 AM
                                                              Manycores vs multicoresjuanrga01/10/15 04:10 PM
                                                                Manycores vs multicoresAaron Spink01/10/15 05:32 PM
                                                                  Manycores vs multicoresjuanrga01/10/15 06:32 PM
                                                                    Manycores vs multicoresExophase01/10/15 06:49 PM
                                                                      Manycores vs multicoresjuanrga01/10/15 08:21 PM
                                                                        Manycores vs multicoresExophase01/10/15 08:51 PM
                                                                        Manycores vs multicoresAaron Spink01/10/15 09:03 PM
                                                                    Manycores vs multicoresAaron Spink01/10/15 07:21 PM
                                                                      Manycores vs multicoresjuanrga01/10/15 08:25 PM
                                                                        Manycores vs multicoresAaron Spink01/10/15 09:11 PM
                                                                          Manycores vs multicoresJouni Osmala01/11/15 04:50 AM
                                                                            Manycores vs multicoresjuanrga01/11/15 08:58 AM
                                                                            Manycores vs multicorescoppice01/12/15 10:01 PM
                                                                              Manycores vs multicoresJouni Osmala01/13/15 04:38 AM
                                                                        Manycores vs multicoresanon01/11/15 03:19 AM
                                                                      Manycores vs multicoresMichael S01/11/15 05:44 AM
                                                                        Manycores vs multicoresAaron Spink01/11/15 05:55 PM
                                                                          Manycores vs multicoresMichael S01/12/15 04:41 AM
                                                                            Manycores vs multicoresEric Bron01/12/15 06:29 AM
                                                                              Manycores vs multicoresEric Bron01/12/15 06:30 AM
                                                        Why read RWT or Reddit when you can get journalists to do it for you?Eric Bron01/03/15 02:54 PM
                                                          Why read RWT or Reddit when you can get journalists to do it for you?juanrga01/07/15 05:48 AM
                                                            Why read RWT or Reddit when you can get journalists to do it for you?Eric Bron01/07/15 07:41 AM
                                                              Manycores vs multicoresjuanrga01/10/15 04:14 PM
                                                    Why read RWT or Reddit when you can get journalists to do it for you?Eric Bron01/03/15 02:42 PM
                                                      Why read RWT or Reddit when you can get journalists to do it for you?juanrga01/07/15 06:03 AM
                                                        Why read RWT or Reddit when you can get journalists to do it for you?Eric Bron01/07/15 07:45 AM
                                                        Why read RWT or Reddit when you can get journalists to do it for you?Linus Torvalds01/08/15 03:09 PM
                                                          Pink unicorns for salejuanrga01/10/15 05:09 PM
                                                        Intentionally picking a competitors slow part is cheating ...Mark Roulo01/08/15 06:37 PM
                                                          Intentionally picking a competitors slow part is cheating ...coppice01/08/15 11:38 PM
                                                            Intentionally picking a competitors slow part is cheating ...Mark Roulo01/09/15 09:13 AM
                                                              Intentionally picking a competitors slow part is cheating ...Anon01/10/15 02:00 AM
                                                              Intentionally picking a competitors slow part is cheating ...David Hess01/11/15 01:03 PM
                                                            Intentionally picking a competitors slow part is cheating ...someone01/09/15 10:31 AM
                                                              Intentionally picking a competitors slow part is cheating ...coppice01/12/15 09:45 PM
                                                                Intentionally picking a competitors slow part is cheating ...coppice01/12/15 09:47 PM
                                                                  Intentionally picking a competitors slow part is cheating ...Michael S01/13/15 07:53 AM
                                                                    Intentionally picking a competitors slow part is cheating ...coppice01/13/15 09:44 AM
                                                                      Intentionally picking a competitors slow part is cheating ...Michael S01/13/15 10:01 AM
                                                                        Intentionally picking a competitors slow part is cheating ...coppice01/13/15 08:35 PM
                                                                      Core sizesjuanrga01/13/15 12:28 PM
                                                          NVIDIA'S FIRST CPU IS A WINNER (Linley Gwennap)juanrga01/10/15 04:34 PM
                                                        Why read RWT or Reddit when you can get journalists to do it for you?Patrick Chase01/08/15 07:02 PM
                                                        Why read RWT or Reddit when you can get journalists to do it for you?coppice01/08/15 10:18 PM
                                                          Why read RWT or Reddit when you can get journalists to do it for you?Patrick Chase01/09/15 11:54 AM
                                                            Why read RWT or Reddit when you can get journalists to do it for you?Mark Roulo01/09/15 12:59 PM
                                                              Why read RWT or Reddit when you can get journalists to do it for you?Patrick Chase01/09/15 03:20 PM
                                                                Why read RWT or Reddit when you can get journalists to do it for you?Eric Bron01/09/15 03:30 PM
                                                            Alternatives to OOOE (again)juanrga01/10/15 04:50 PM
                                                              Alternatives to OOOE (again)David Kanter01/11/15 12:10 AM
                                                                Alternatives to OOOE (again)juanrga01/11/15 08:30 AM
                                                            Why read RWT or Reddit when you can get journalists to do it for you?Gabriele Svelto01/11/15 12:53 AM
                                              Why read RWT or Reddit when you can get journalists to do it for you?Fake Linus Torvalds01/03/15 12:14 PM
                                                Why read RWT or Reddit when you can get journalists to do it for you?Rob Thorpe01/03/15 08:25 PM
                                          Avoiding ping pongMaynard Handley12/09/14 11:33 AM
                                            Avoiding ping pongPatrick Chase12/09/14 01:54 PM
                                              Avoiding ping pongMaynard Handley12/09/14 06:56 PM
                                      Avoiding ping pongSalvatore De Dominicis12/09/14 08:51 AM
                                        Avoiding ping pongPatrick Chase12/09/14 02:00 PM
                                      Avoiding ping pongook12/11/14 03:31 AM
                                      Avoiding ping pongArt Scott12/19/14 10:19 PM
                                        Avoiding ping pongEric Bron nli12/20/14 04:05 AM
                                      What about specialization?Troll?01/02/15 07:55 AM
                                        What about specialization?Ungo01/04/15 03:27 PM
                                      Avoiding ping pongfewwef01/05/15 08:16 PM
                                      Avoiding ping pongV.Krishn01/08/15 06:11 AM
                                    Avoiding ping pongGabriele Svelto12/08/14 07:32 PM
                                    Avoiding ping ponganon12/08/14 11:37 PM
                            Avoiding ping pongKonrad Schwarz12/10/14 06:23 AM
                              Avoiding ping pongLinus Torvalds12/10/14 11:56 AM
                          Object reference lockingDavid W12/08/14 11:36 PM
                            Object reference lockingPatrick Chase12/09/14 04:52 PM
                              Object reference lockingDavid W12/11/14 05:18 AM
                    ISA != interface for "most programmers"Paul A. Clayton12/04/14 03:34 PM
                      ISA != interface for "most programmers"rwessel12/04/14 07:50 PM
                  Interesting! (exporting hot lines/cache-aware ISA); "Please sir, I want some more" (NT)Paul A. Clayton12/04/14 02:26 PM
                  Avoiding ping pongMichael S12/06/14 03:48 PM
          ARMv8 getting atomic operationsLinus Torvalds12/04/14 12:05 PM
            LL/SC idiom recognition is not admitting RMW superiorityPaul A. Clayton12/04/14 02:34 PM
            ARMv8 getting atomic operationsanon12/04/14 10:17 PM
    ARMv8 getting atomic operationsPatrick Chase12/03/14 12:09 PM
  limited ordernksingh12/04/14 10:17 PM
    I didn't understand this either. (NT)Konrad Schwarz12/04/14 10:32 PM
    limited orderdmcq12/05/14 02:13 AM
    limited orderbakaneko12/05/14 09:11 AM
Reply to this Topic
Name:
Email:
Topic:
Body: No Text
How do you spell blue?