summaryrefslogtreecommitdiff
path: root/kern/mutex/mutex_adaptive.c
blob: db92f9d53ead392ef39293a0c393d57afa6e8ecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * Copyright (c) 2017 Agustina Arzille.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include <kern/atomic.h>
#include <kern/clock.h>
#include <kern/error.h>
#include <kern/init.h>
#include <kern/mutex.h>
#include <kern/mutex_types.h>
#include <kern/sleepq.h>
#include <kern/syscnt.h>
#include <kern/thread.h>
#include <machine/cpu.h>

#ifdef CONFIG_MUTEX_DEBUG

enum {
    MUTEX_ADAPTIVE_SC_SPINS,
    MUTEX_ADAPTIVE_SC_WAIT_SUCCESSES,
    MUTEX_ADAPTIVE_SC_WAIT_ERRORS,
    MUTEX_ADAPTIVE_SC_DOWNGRADES,
    MUTEX_ADAPTIVE_SC_ERROR_DOWNGRADES,
    MUTEX_ADAPTIVE_SC_ERROR_CLEARCONT,
    MUTEX_ADAPTIVE_SC_ERROR_RESET,
    MUTEX_ADAPTIVE_SC_FAST_UNLOCKS,
    MUTEX_ADAPTIVE_SC_SLOW_UNLOCKS,
    MUTEX_ADAPTIVE_SC_EXTERNAL_UNLOCKS,
    MUTEX_ADAPTIVE_SC_SIGNALS,
    MUTEX_ADAPTIVE_NR_SCS
};

static struct syscnt mutex_adaptive_sc_array[MUTEX_ADAPTIVE_NR_SCS];

static void
mutex_adaptive_register_sc(unsigned int index, const char *name)
{
    assert(index < ARRAY_SIZE(mutex_adaptive_sc_array));
    syscnt_register(&mutex_adaptive_sc_array[index], name);
}

static void
mutex_adaptive_setup_debug(void)
{
    mutex_adaptive_register_sc(MUTEX_ADAPTIVE_SC_SPINS,
                               "mutex_adaptive_spins");
    mutex_adaptive_register_sc(MUTEX_ADAPTIVE_SC_WAIT_SUCCESSES,
                               "mutex_adaptive_wait_successes");
    mutex_adaptive_register_sc(MUTEX_ADAPTIVE_SC_WAIT_ERRORS,
                               "mutex_adaptive_wait_errors");
    mutex_adaptive_register_sc(MUTEX_ADAPTIVE_SC_DOWNGRADES,
                               "mutex_adaptive_downgrades");
    mutex_adaptive_register_sc(MUTEX_ADAPTIVE_SC_ERROR_DOWNGRADES,
                               "mutex_adaptive_error_downgrades");
    mutex_adaptive_register_sc(MUTEX_ADAPTIVE_SC_ERROR_CLEARCONT,
                               "mutex_adaptive_error_clearcont");
    mutex_adaptive_register_sc(MUTEX_ADAPTIVE_SC_ERROR_RESET,
                               "mutex_adaptive_error_reset");
    mutex_adaptive_register_sc(MUTEX_ADAPTIVE_SC_FAST_UNLOCKS,
                               "mutex_adaptive_fast_unlocks");
    mutex_adaptive_register_sc(MUTEX_ADAPTIVE_SC_SLOW_UNLOCKS,
                               "mutex_adaptive_slow_unlocks");
    mutex_adaptive_register_sc(MUTEX_ADAPTIVE_SC_EXTERNAL_UNLOCKS,
                               "mutex_adaptive_external_unlocks");
    mutex_adaptive_register_sc(MUTEX_ADAPTIVE_SC_SIGNALS,
                               "mutex_adaptive_signals");
}

static void
mutex_adaptive_inc_sc(unsigned int index)
{
    assert(index < ARRAY_SIZE(mutex_adaptive_sc_array));
    syscnt_inc(&mutex_adaptive_sc_array[index]);
}

#else /* CONFIG_MUTEX_DEBUG */
#define mutex_adaptive_setup_debug()
#define mutex_adaptive_inc_sc(x)
#endif /* CONFIG_MUTEX_DEBUG */


static struct thread *
mutex_adaptive_get_thread(uintptr_t owner)
{
    return (struct thread *)(owner & ~MUTEX_ADAPTIVE_CONTENDED);
}

static void
mutex_adaptive_set_contended(struct mutex *mutex)
{
    atomic_or(&mutex->owner, MUTEX_ADAPTIVE_CONTENDED, ATOMIC_RELEASE);
}

static inline bool
mutex_adaptive_is_owner(struct mutex *mutex, uintptr_t owner)
{
    uintptr_t prev;

    prev = atomic_load(&mutex->owner, ATOMIC_RELAXED);
    return mutex_adaptive_get_thread(prev) == mutex_adaptive_get_thread(owner);
}

static int
mutex_adaptive_lock_slow_common(struct mutex *mutex, bool timed, uint64_t ticks)
{
    uintptr_t self, owner;
    struct sleepq *sleepq;
    struct thread *thread;
    unsigned long flags;
    int error;

    error = 0;
    self = (uintptr_t)thread_self();

    sleepq = sleepq_lend(mutex, false, &flags);

    mutex_adaptive_set_contended(mutex);

    do {
        owner = atomic_cas_acquire(&mutex->owner, MUTEX_ADAPTIVE_CONTENDED,
                                   self | MUTEX_ADAPTIVE_CONTENDED);
        assert(owner & MUTEX_ADAPTIVE_CONTENDED);

        if (mutex_adaptive_get_thread(owner) == NULL) {
            break;
        }

        /*
         * The owner may not return from the unlock function if a thread is
         * spinning on it.
         */
        while (mutex_adaptive_is_owner(mutex, owner)) {
            if (thread_is_running(mutex_adaptive_get_thread(owner))) {
                mutex_adaptive_inc_sc(MUTEX_ADAPTIVE_SC_SPINS);

                if (timed && clock_time_occurred(ticks, clock_get_time())) {
                    error = ERROR_TIMEDOUT;
                    break;
                }

                cpu_pause();
            } else {
                if (!timed) {
                    sleepq_wait(sleepq, "mutex");
                } else {
                    error = sleepq_timedwait(sleepq, "mutex", ticks);

                    if (error) {
                        break;
                    }
                }
            }
        }
    } while (!error);

    /*
     * Attempt to clear the contended bit.
     *
     * In case of success, the current thread becomes the new owner, and
     * simply checking if the sleep queue is empty is enough.
     *
     * Keep in mind accesses to the mutex word aren't synchronized by
     * the sleep queue, i.e. an unlock may occur completely concurrently
     * while attempting to clear the contended bit .
     */

    if (error) {
        mutex_adaptive_inc_sc(MUTEX_ADAPTIVE_SC_WAIT_ERRORS);

        if (sleepq_empty(sleepq)) {
            mutex_adaptive_inc_sc(MUTEX_ADAPTIVE_SC_ERROR_DOWNGRADES);
            owner = atomic_load(&mutex->owner, ATOMIC_RELAXED);
            assert(owner & MUTEX_ADAPTIVE_CONTENDED);
            thread = mutex_adaptive_get_thread(owner);

            /* If there is an owner, try to clear the contended bit */
            if (thread != NULL) {
                mutex_adaptive_inc_sc(MUTEX_ADAPTIVE_SC_ERROR_CLEARCONT);
                owner = atomic_cas(&mutex->owner, owner,
                                   (uintptr_t)thread, ATOMIC_RELAXED);
                assert(owner & MUTEX_ADAPTIVE_CONTENDED);
                thread = mutex_adaptive_get_thread(owner);
            }

            /*
             * If there is no owner, the previous owner is currently unlocking
             * the mutex, waiting for either a successful signal, or the
             * value of the mutex to become different from the contended bit.
             */
            if (thread == NULL) {
                mutex_adaptive_inc_sc(MUTEX_ADAPTIVE_SC_ERROR_RESET);
                owner = atomic_cas(&mutex->owner, owner, 0, ATOMIC_RELAXED);
                assert(owner == MUTEX_ADAPTIVE_CONTENDED);
            }
        }

        goto out;
    }

    mutex_adaptive_inc_sc(MUTEX_ADAPTIVE_SC_WAIT_SUCCESSES);

    if (sleepq_empty(sleepq)) {
        mutex_adaptive_inc_sc(MUTEX_ADAPTIVE_SC_DOWNGRADES);
        atomic_store(&mutex->owner, self, ATOMIC_RELAXED);
    }

out:
    sleepq_return(sleepq, flags);

    return error;
}

void
mutex_adaptive_lock_slow(struct mutex *mutex)
{
    int error;

    error = mutex_adaptive_lock_slow_common(mutex, false, 0);
    assert(!error);
}

int
mutex_adaptive_timedlock_slow(struct mutex *mutex, uint64_t ticks)
{
    return mutex_adaptive_lock_slow_common(mutex, true, ticks);
}

void
mutex_adaptive_unlock_slow(struct mutex *mutex)
{
    uintptr_t self, owner;
    struct sleepq *sleepq;
    unsigned long flags;
    int error;

    self = (uintptr_t)thread_self() | MUTEX_ADAPTIVE_CONTENDED;

    for (;;) {
        owner = atomic_cas_release(&mutex->owner, self,
                                   MUTEX_ADAPTIVE_CONTENDED);

        if (owner == self) {
            break;
        } else {
            /*
             * The contended bit was cleared after the fast path failed,
             * but before the slow path (re)started.
             */
            assert(owner == (uintptr_t)thread_self());
            error = mutex_adaptive_unlock_fast(mutex);

            if (error) {
                continue;
            }

            mutex_adaptive_inc_sc(MUTEX_ADAPTIVE_SC_FAST_UNLOCKS);
            return;
        }
    }

    mutex_adaptive_inc_sc(MUTEX_ADAPTIVE_SC_SLOW_UNLOCKS);

    for (;;) {
        owner = atomic_load(&mutex->owner, ATOMIC_RELAXED);

        /*
         * This only happens if :
         *  1/ Another thread was able to become the new owner, in which
         *     case that thread isn't spinning on the current thread, i.e.
         *     there is no need for an additional reference.
         *  2/ A timeout cleared the contended bit.
         */
        if (owner != MUTEX_ADAPTIVE_CONTENDED) {
            mutex_adaptive_inc_sc(MUTEX_ADAPTIVE_SC_EXTERNAL_UNLOCKS);
            break;
        }

        /*
         * Avoid contending with incoming threads that are about to spin/wait
         * on the mutex. This is particularly expensive with queued locks.
         *
         * Also, this call returns NULL if another thread is currently spinning
         * on the current thread, in which case the latter doesn't return,
         * averting the need for an additional reference.
         */
        sleepq = sleepq_tryacquire(mutex, false, &flags);

        if (sleepq != NULL) {
            mutex_adaptive_inc_sc(MUTEX_ADAPTIVE_SC_SIGNALS);
            sleepq_signal(sleepq);
            sleepq_release(sleepq, flags);
            break;
        }

        /*
         * Acquiring the sleep queue may fail because of contention on
         * unrelated objects. Retry.
         */
    }
}

static int
mutex_adaptive_bootstrap(void)
{
    return 0;
}

INIT_OP_DEFINE(mutex_adaptive_bootstrap,
               INIT_OP_DEP(thread_setup_booter, true));

static int
mutex_adaptive_setup(void)
{
    mutex_adaptive_setup_debug();
    return 0;
}

#ifdef CONFIG_MUTEX_DEBUG
#define MUTEX_ADAPTIVE_DEBUG_INIT_OP_DEPS       \
               INIT_OP_DEP(syscnt_setup, true),
#else /* CONFIG_MUTEX_DEBUG */
#define MUTEX_ADAPTIVE_DEBUG_INIT_OP_DEPS
#endif /* CONFIG_MUTEX_DEBUG */

INIT_OP_DEFINE(mutex_adaptive_setup,
               INIT_OP_DEP(mutex_adaptive_bootstrap, true),
               MUTEX_ADAPTIVE_DEBUG_INIT_OP_DEPS
);