News for hackers. Stuff that matters.
Re:Concurrent Memory Deallocation in the Objective-C Runtime by lgg on Saturday June 06, 2015 @04:50PM (#9672216)
It isn't actually stopping the threads. The thing to understand is that you don't need to ensure that all threads are outside of a critical section at the time you free the cache, what you need is the weaker guarantee that all threads have to have exited any critical sections they were in at the time of the pointer swap before you can call free.

You don't have to do anything heavy weight like stop all the threads, instead you can just pull their PCs out and check them. Sure, they may have moved on (and even back into the critical section) by the time you call free, but it doesn't matter because they'll be using the new cache buffer. The downside is that you might get false positives for the critical section, but you can either run the dealloc in a loop or defer it and try again later.

Re:Concurrent Memory Deallocation in the Objective-C Runtime by asveikau on Saturday June 06, 2015 @05:05PM (#9672263)
No, you pretty much have to stop them. If you observe the PC of other threads at a point in time while they are running, that is no guarantee that they aren't about to enter the unsafe region immediately after you make the check.

Your suggestion sounds akin to replacing a mutex acquire with an "is the lock held?" [like trylock with 0 timeout, then immediate release]. Observing that it's unheld at time t makes no guarantee it won't be held by the time of your next instruction. It therefore becomes a meaningless check, not useful at all for real synchronization.

[PS: mentioned this on HN before, but my favorite "observe the PC as part of a synchronization primitive" hack was this one from Linux on armv5: http://lwn.net/Articles/314561/]

[PPS: How much does objc_msgSend() do inline and how much is external calls? This PC hack seems like it could have huge holes if some of its critical work is done in a non-inlined function.]

Re:Concurrent Memory Deallocation in the Objective-C Runtime by dilap on Saturday June 06, 2015 @05:27PM (#9672337)
Hmm? No I don't think you have to stop them.

Basically you have:

    cache = <newval>
    if ok() {
        free(<oldval>)
    }
The problem you have is if anyone is still using <oldval> at the point you free it. So:

    // maybe someone here gets a reference to <oldval> (1) 
    cache = <newval>
    // from this point onward no one can reference <oldval>
    if ok() {
        free(<oldval>)
    }
So ok() only has to check if threads have a reference to <oldval> that they are still using.

Imagine ok() pauses all threads. It sees if any threads threads are in BAD=[PC_BAD_START, PC_BAD_END]. If yes, return false, if no, return true.

Now imagine PC doesn't pause the threads. What can happen?

A thread that was in BAD leaves BAD. That's fine.

A thread that wasn't in BAD enters BAD. But that will use <newval>, so that's fine too.

That thread that was in BAD leaves BAD, and then reenters. That's also fine.

So there's no problem.

(re your question, I believe objc_msgSend is hand optimized assembly that doesn't make any function calls; if it did, you'd just have to make sure to include those functions in the range of bad PC addresses.)

Re:Concurrent Memory Deallocation in the Objective-C Runtime by asveikau on Saturday June 06, 2015 @05:50PM (#9672411)
> So ok() only has to check if threads have a reference to <oldval> that they are still using.

Right. The problem with doing this while other threads are running is that ok() can return true, correctly so for its point in time, then immediately after ok() returns another thread could enter objc_msgSend() while you are inside free(). Maybe ok() ran on thread A while thread B was right at unrelated function foo()'s "call objc_msgSend" instruction. The check is OK at a point in time, but perhaps by the time you enter free() thread B did an unsafe read.

> A thread that wasn't in BAD enters BAD. But that will use <newval>, so that's fine too.

You can't make guarantees that it will see newval. Whether or not it does depends on timing. For example, maybe the guy who calls ok() gets a page fault or is on a very busy CPU with lots of preemption happening. That will alter timing in the direction of this being unsafe. Cache coherence may also be an issue here.

Re:Concurrent Memory Deallocation in the Objective-C Runtime by dilap on Saturday June 06, 2015 @05:59PM (#9672450)
You have to be atomically reading and writing cache (which you can do). So then after

    cache = <newval>
...any future reads of cache will use <newval> (or <newer-val>, but not <oldval>).

That's what makes the trick work w/o having to pause threads.

Re:Concurrent Memory Deallocation in the Objective-C Runtime by asveikau on Saturday June 06, 2015 @06:06PM (#9672470)
> cache = <newval>

As written, that is not a guarantee that all cores will see <newval> immediately. It's very CPU-specific but you may need memory fences to achieve this.

Further, in my opinion it's kind of playing with fire.

Edit: Also, it is my impression reading the article that <oldval> is actually a shared list of old caches to be freed (gOldCachesList). That makes it a lot more complicated than your example snippet and leaves more potential for nasty synchronization problems.

Re:Concurrent Memory Deallocation in the Objective-C Runtime by ridiculous_fish on Saturday June 06, 2015 @06:57PM (#9672642)
Strictly speaking you're right. However it's not necessary that other cores see the new store immediately: it's only necessary that they see it before the memory is freed, which occurs after a substantial delay sufficient to prevent these out-of-order issues on other CPUs. This is the technique by which memory barriers are elided on the read side (but not the write side).
Re:Concurrent Memory Deallocation in the Objective-C Runtime by asveikau on Monday June 08, 2015 @11:46AM (#9679813)
Yes, you kind of do need the read barrier, otherwise you get exactly the problem I described with the stale cache.

You can't "kinda" have synchronization just as a design for a mutex can't be a "kinda" lock... You may get away with it for a while, but like I said, playing with fire.

Re:Concurrent Memory Deallocation in the Objective-C Runtime by vitalyd on Saturday June 06, 2015 @11:53PM (#9673332)
This is both clever and terrifying :). How do they prevent compiler from scheduling the load early, observing, and using the stale cache pointer? Do they have at least a compiler fence on the read side?
Re:Concurrent Memory Deallocation in the Objective-C Runtime by bdonlan on Sunday June 07, 2015 @06:17AM (#9674057)
The read-side code in question is hand-written assembly code, isn't it? And on the hardware side, observing pc will probably result in an actual memory barrier happening anyway (even if no signal is sent internally the kernel must still suspend the thread to capture register state, and then it needs a barrier to communicate that result back to the asking thread).
Re:Concurrent Memory Deallocation in the Objective-C Runtime by vitalyd on Sunday June 07, 2015 @09:13AM (#9674520)
Yes, perhaps that part is in the hand written assembly portion that's mentioned.
Re:Concurrent Memory Deallocation in the Objective-C Runtime by ridiculous_fish on Sunday June 07, 2015 @04:08AM (#9673817)
Nothing prevents using a stale cache pointer. The stale cache still contains correct data, so using it is benign, up until it gets deallocated.

A stale cache pointer may become wrong if a method implementation is replaced, e.g. by a category, but replacing a method while calling it in another thread is inherently racey.

Re:Concurrent Memory Deallocation in the Objective-C Runtime by vitalyd on Sunday June 07, 2015 @09:05AM (#9674499)
By stale I meant holding a ref to a cache about to be deleted.
Re:Concurrent Memory Deallocation in the Objective-C Runtime by lgg on Saturday June 06, 2015 @08:05PM (#9672824)
Yes, obviously you may need barriers or fences depending on the exact architectural details, and as another commenter mentioned you may be able to elide the read barrier if you know what is going on.

This all about optimizing things so the read side doesn't have to do any synchronization. The actual implementation takes locks in functions that mutate the cache, it is just done in such a way that read side always sees consistent data without taking a lock. Since the write side has locks it can safely queue stuff for deletion is actually pretty trivial.

As for playing with fire... the core dispatch routine of your runtime is exactly the place you want to complicated things that exploit innate knowledge of your architecture. It is a small piece of code that is incredibly hot and will get a lot of scrutiny.

Re:Concurrent Memory Deallocation in the Objective-C Runtime by comex on Saturday June 06, 2015 @08:20PM (#9672882)
Indeed. One nitpick though: you can't get around pausing the thread briefly or else waiting for it to hit an interrupt, because there's no way to observe another CPU core's registers without its cooperation.
Re:Concurrent Memory Deallocation in the Objective-C Runtime by lgg on Saturday June 06, 2015 @10:16PM (#9673147)
True, I should have said it is not concurrently stopping all the threads. If you actually need the register state of a thread that is currently running on another processor it may be stopped to get it (though if you ask for the state of a thread that is not currently executing you may get its saved register state out of the kernel without having to actually pause its execution, of course being able to tell what is and is not actually running on other CPUs at that granularity is not really feasible).
Re:Concurrent Memory Deallocation in the Objective-C Runtime by dilap on Saturday June 06, 2015 @06:24PM (#9672537)
As written, it's psuedo-code. ;) Yup it's tricky and platform specific, but the point is it can be done (or at least that's my understanding -- I do not have first hand knowledge).
Re:Concurrent Memory Deallocation in the Objective-C Runtime by pcwalton on Saturday June 06, 2015 @05:43PM (#9672386)
How do you get the PC of a thread in a POSIX userland without sending it a signal? ptrace(PTRACE_GETREGS), I guess?
Re:Concurrent Memory Deallocation in the Objective-C Runtime by lgg on Saturday June 06, 2015 @08:07PM (#9672834)
Totally platform specific. On OS X you generally use thread_get_state().