qemu-thread-posix.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Wrappers around mutex/cond/thread functions
  3. *
  4. * Copyright Red Hat, Inc. 2009
  5. *
  6. * Author:
  7. * Marcelo Tosatti <mtosatti@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qemu/thread.h"
  15. #include "qemu/atomic.h"
  16. #include "qemu/notify.h"
  17. #include "qemu-thread-common.h"
  18. #include "qemu/tsan.h"
  19. #include "qemu/bitmap.h"
  20. #ifdef CONFIG_PTHREAD_SET_NAME_NP
  21. #include <pthread_np.h>
  22. #endif
  23. static bool name_threads;
  24. void qemu_thread_naming(bool enable)
  25. {
  26. name_threads = enable;
  27. #if !defined CONFIG_PTHREAD_SETNAME_NP_W_TID && \
  28. !defined CONFIG_PTHREAD_SETNAME_NP_WO_TID && \
  29. !defined CONFIG_PTHREAD_SET_NAME_NP
  30. /* This is a debugging option, not fatal */
  31. if (enable) {
  32. fprintf(stderr, "qemu: thread naming not supported on this host\n");
  33. }
  34. #endif
  35. }
  36. static void error_exit(int err, const char *msg)
  37. {
  38. fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
  39. abort();
  40. }
  41. static inline clockid_t qemu_timedwait_clockid(void)
  42. {
  43. #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
  44. return CLOCK_MONOTONIC;
  45. #else
  46. return CLOCK_REALTIME;
  47. #endif
  48. }
  49. static void compute_abs_deadline(struct timespec *ts, int ms)
  50. {
  51. clock_gettime(qemu_timedwait_clockid(), ts);
  52. ts->tv_nsec += (ms % 1000) * 1000000;
  53. ts->tv_sec += ms / 1000;
  54. if (ts->tv_nsec >= 1000000000) {
  55. ts->tv_sec++;
  56. ts->tv_nsec -= 1000000000;
  57. }
  58. }
  59. void qemu_mutex_init(QemuMutex *mutex)
  60. {
  61. int err;
  62. err = pthread_mutex_init(&mutex->lock, NULL);
  63. if (err)
  64. error_exit(err, __func__);
  65. qemu_mutex_post_init(mutex);
  66. }
  67. void qemu_mutex_destroy(QemuMutex *mutex)
  68. {
  69. int err;
  70. assert(mutex->initialized);
  71. mutex->initialized = false;
  72. err = pthread_mutex_destroy(&mutex->lock);
  73. if (err)
  74. error_exit(err, __func__);
  75. }
  76. void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
  77. {
  78. int err;
  79. assert(mutex->initialized);
  80. qemu_mutex_pre_lock(mutex, file, line);
  81. err = pthread_mutex_lock(&mutex->lock);
  82. if (err)
  83. error_exit(err, __func__);
  84. qemu_mutex_post_lock(mutex, file, line);
  85. }
  86. int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line)
  87. {
  88. int err;
  89. assert(mutex->initialized);
  90. err = pthread_mutex_trylock(&mutex->lock);
  91. if (err == 0) {
  92. qemu_mutex_post_lock(mutex, file, line);
  93. return 0;
  94. }
  95. if (err != EBUSY) {
  96. error_exit(err, __func__);
  97. }
  98. return -EBUSY;
  99. }
  100. void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
  101. {
  102. int err;
  103. assert(mutex->initialized);
  104. qemu_mutex_pre_unlock(mutex, file, line);
  105. err = pthread_mutex_unlock(&mutex->lock);
  106. if (err)
  107. error_exit(err, __func__);
  108. }
  109. void qemu_rec_mutex_init(QemuRecMutex *mutex)
  110. {
  111. int err;
  112. pthread_mutexattr_t attr;
  113. pthread_mutexattr_init(&attr);
  114. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  115. err = pthread_mutex_init(&mutex->m.lock, &attr);
  116. pthread_mutexattr_destroy(&attr);
  117. if (err) {
  118. error_exit(err, __func__);
  119. }
  120. mutex->m.initialized = true;
  121. }
  122. void qemu_rec_mutex_destroy(QemuRecMutex *mutex)
  123. {
  124. qemu_mutex_destroy(&mutex->m);
  125. }
  126. void qemu_rec_mutex_lock_impl(QemuRecMutex *mutex, const char *file, int line)
  127. {
  128. qemu_mutex_lock_impl(&mutex->m, file, line);
  129. }
  130. int qemu_rec_mutex_trylock_impl(QemuRecMutex *mutex, const char *file, int line)
  131. {
  132. return qemu_mutex_trylock_impl(&mutex->m, file, line);
  133. }
  134. void qemu_rec_mutex_unlock_impl(QemuRecMutex *mutex, const char *file, int line)
  135. {
  136. qemu_mutex_unlock_impl(&mutex->m, file, line);
  137. }
  138. void qemu_cond_init(QemuCond *cond)
  139. {
  140. pthread_condattr_t attr;
  141. int err;
  142. err = pthread_condattr_init(&attr);
  143. if (err) {
  144. error_exit(err, __func__);
  145. }
  146. #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
  147. err = pthread_condattr_setclock(&attr, qemu_timedwait_clockid());
  148. if (err) {
  149. error_exit(err, __func__);
  150. }
  151. #endif
  152. err = pthread_cond_init(&cond->cond, &attr);
  153. if (err) {
  154. error_exit(err, __func__);
  155. }
  156. err = pthread_condattr_destroy(&attr);
  157. if (err) {
  158. error_exit(err, __func__);
  159. }
  160. cond->initialized = true;
  161. }
  162. void qemu_cond_destroy(QemuCond *cond)
  163. {
  164. int err;
  165. assert(cond->initialized);
  166. cond->initialized = false;
  167. err = pthread_cond_destroy(&cond->cond);
  168. if (err)
  169. error_exit(err, __func__);
  170. }
  171. void qemu_cond_signal(QemuCond *cond)
  172. {
  173. int err;
  174. assert(cond->initialized);
  175. err = pthread_cond_signal(&cond->cond);
  176. if (err)
  177. error_exit(err, __func__);
  178. }
  179. void qemu_cond_broadcast(QemuCond *cond)
  180. {
  181. int err;
  182. assert(cond->initialized);
  183. err = pthread_cond_broadcast(&cond->cond);
  184. if (err)
  185. error_exit(err, __func__);
  186. }
  187. void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
  188. {
  189. int err;
  190. assert(cond->initialized);
  191. qemu_mutex_pre_unlock(mutex, file, line);
  192. err = pthread_cond_wait(&cond->cond, &mutex->lock);
  193. qemu_mutex_post_lock(mutex, file, line);
  194. if (err)
  195. error_exit(err, __func__);
  196. }
  197. static bool TSA_NO_TSA
  198. qemu_cond_timedwait_ts(QemuCond *cond, QemuMutex *mutex, struct timespec *ts,
  199. const char *file, const int line)
  200. {
  201. int err;
  202. assert(cond->initialized);
  203. trace_qemu_mutex_unlock(mutex, file, line);
  204. err = pthread_cond_timedwait(&cond->cond, &mutex->lock, ts);
  205. trace_qemu_mutex_locked(mutex, file, line);
  206. if (err && err != ETIMEDOUT) {
  207. error_exit(err, __func__);
  208. }
  209. return err != ETIMEDOUT;
  210. }
  211. bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms,
  212. const char *file, const int line)
  213. {
  214. struct timespec ts;
  215. compute_abs_deadline(&ts, ms);
  216. return qemu_cond_timedwait_ts(cond, mutex, &ts, file, line);
  217. }
  218. void qemu_sem_init(QemuSemaphore *sem, int init)
  219. {
  220. qemu_mutex_init(&sem->mutex);
  221. qemu_cond_init(&sem->cond);
  222. if (init < 0) {
  223. error_exit(EINVAL, __func__);
  224. }
  225. sem->count = init;
  226. }
  227. void qemu_sem_destroy(QemuSemaphore *sem)
  228. {
  229. qemu_cond_destroy(&sem->cond);
  230. qemu_mutex_destroy(&sem->mutex);
  231. }
  232. void qemu_sem_post(QemuSemaphore *sem)
  233. {
  234. qemu_mutex_lock(&sem->mutex);
  235. if (sem->count == UINT_MAX) {
  236. error_exit(EINVAL, __func__);
  237. } else {
  238. sem->count++;
  239. qemu_cond_signal(&sem->cond);
  240. }
  241. qemu_mutex_unlock(&sem->mutex);
  242. }
  243. int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
  244. {
  245. bool rc = true;
  246. struct timespec ts;
  247. compute_abs_deadline(&ts, ms);
  248. qemu_mutex_lock(&sem->mutex);
  249. while (sem->count == 0) {
  250. if (ms == 0) {
  251. rc = false;
  252. } else {
  253. rc = qemu_cond_timedwait_ts(&sem->cond, &sem->mutex, &ts,
  254. __FILE__, __LINE__);
  255. }
  256. if (!rc) { /* timeout */
  257. break;
  258. }
  259. }
  260. if (rc) {
  261. --sem->count;
  262. }
  263. qemu_mutex_unlock(&sem->mutex);
  264. return (rc ? 0 : -1);
  265. }
  266. void qemu_sem_wait(QemuSemaphore *sem)
  267. {
  268. qemu_mutex_lock(&sem->mutex);
  269. while (sem->count == 0) {
  270. qemu_cond_wait(&sem->cond, &sem->mutex);
  271. }
  272. --sem->count;
  273. qemu_mutex_unlock(&sem->mutex);
  274. }
  275. #ifdef __linux__
  276. #include "qemu/futex.h"
  277. #else
  278. static inline void qemu_futex_wake(QemuEvent *ev, int n)
  279. {
  280. assert(ev->initialized);
  281. pthread_mutex_lock(&ev->lock);
  282. if (n == 1) {
  283. pthread_cond_signal(&ev->cond);
  284. } else {
  285. pthread_cond_broadcast(&ev->cond);
  286. }
  287. pthread_mutex_unlock(&ev->lock);
  288. }
  289. static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)
  290. {
  291. assert(ev->initialized);
  292. pthread_mutex_lock(&ev->lock);
  293. if (ev->value == val) {
  294. pthread_cond_wait(&ev->cond, &ev->lock);
  295. }
  296. pthread_mutex_unlock(&ev->lock);
  297. }
  298. #endif
  299. /* Valid transitions:
  300. * - free->set, when setting the event
  301. * - busy->set, when setting the event, followed by qemu_futex_wake
  302. * - set->free, when resetting the event
  303. * - free->busy, when waiting
  304. *
  305. * set->busy does not happen (it can be observed from the outside but
  306. * it really is set->free->busy).
  307. *
  308. * busy->free provably cannot happen; to enforce it, the set->free transition
  309. * is done with an OR, which becomes a no-op if the event has concurrently
  310. * transitioned to free or busy.
  311. */
  312. #define EV_SET 0
  313. #define EV_FREE 1
  314. #define EV_BUSY -1
  315. void qemu_event_init(QemuEvent *ev, bool init)
  316. {
  317. #ifndef __linux__
  318. pthread_mutex_init(&ev->lock, NULL);
  319. pthread_cond_init(&ev->cond, NULL);
  320. #endif
  321. ev->value = (init ? EV_SET : EV_FREE);
  322. ev->initialized = true;
  323. }
  324. void qemu_event_destroy(QemuEvent *ev)
  325. {
  326. assert(ev->initialized);
  327. ev->initialized = false;
  328. #ifndef __linux__
  329. pthread_mutex_destroy(&ev->lock);
  330. pthread_cond_destroy(&ev->cond);
  331. #endif
  332. }
  333. void qemu_event_set(QemuEvent *ev)
  334. {
  335. assert(ev->initialized);
  336. /*
  337. * Pairs with both qemu_event_reset() and qemu_event_wait().
  338. *
  339. * qemu_event_set has release semantics, but because it *loads*
  340. * ev->value we need a full memory barrier here.
  341. */
  342. smp_mb();
  343. if (qatomic_read(&ev->value) != EV_SET) {
  344. int old = qatomic_xchg(&ev->value, EV_SET);
  345. /* Pairs with memory barrier in kernel futex_wait system call. */
  346. smp_mb__after_rmw();
  347. if (old == EV_BUSY) {
  348. /* There were waiters, wake them up. */
  349. qemu_futex_wake(ev, INT_MAX);
  350. }
  351. }
  352. }
  353. void qemu_event_reset(QemuEvent *ev)
  354. {
  355. assert(ev->initialized);
  356. /*
  357. * If there was a concurrent reset (or even reset+wait),
  358. * do nothing. Otherwise change EV_SET->EV_FREE.
  359. */
  360. qatomic_or(&ev->value, EV_FREE);
  361. /*
  362. * Order reset before checking the condition in the caller.
  363. * Pairs with the first memory barrier in qemu_event_set().
  364. */
  365. smp_mb__after_rmw();
  366. }
  367. void qemu_event_wait(QemuEvent *ev)
  368. {
  369. unsigned value;
  370. assert(ev->initialized);
  371. /*
  372. * qemu_event_wait must synchronize with qemu_event_set even if it does
  373. * not go down the slow path, so this load-acquire is needed that
  374. * synchronizes with the first memory barrier in qemu_event_set().
  375. *
  376. * If we do go down the slow path, there is no requirement at all: we
  377. * might miss a qemu_event_set() here but ultimately the memory barrier in
  378. * qemu_futex_wait() will ensure the check is done correctly.
  379. */
  380. value = qatomic_load_acquire(&ev->value);
  381. if (value != EV_SET) {
  382. if (value == EV_FREE) {
  383. /*
  384. * Leave the event reset and tell qemu_event_set that there are
  385. * waiters. No need to retry, because there cannot be a concurrent
  386. * busy->free transition. After the CAS, the event will be either
  387. * set or busy.
  388. *
  389. * This cmpxchg doesn't have particular ordering requirements if it
  390. * succeeds (moving the store earlier can only cause qemu_event_set()
  391. * to issue _more_ wakeups), the failing case needs acquire semantics
  392. * like the load above.
  393. */
  394. if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
  395. return;
  396. }
  397. }
  398. /*
  399. * This is the final check for a concurrent set, so it does need
  400. * a smp_mb() pairing with the second barrier of qemu_event_set().
  401. * The barrier is inside the FUTEX_WAIT system call.
  402. */
  403. qemu_futex_wait(ev, EV_BUSY);
  404. }
  405. }
  406. static __thread NotifierList thread_exit;
  407. /*
  408. * Note that in this implementation you can register a thread-exit
  409. * notifier for the main thread, but it will never be called.
  410. * This is OK because main thread exit can only happen when the
  411. * entire process is exiting, and the API allows notifiers to not
  412. * be called on process exit.
  413. */
  414. void qemu_thread_atexit_add(Notifier *notifier)
  415. {
  416. notifier_list_add(&thread_exit, notifier);
  417. }
  418. void qemu_thread_atexit_remove(Notifier *notifier)
  419. {
  420. notifier_remove(notifier);
  421. }
  422. static void qemu_thread_atexit_notify(void *arg)
  423. {
  424. /*
  425. * Called when non-main thread exits (via qemu_thread_exit()
  426. * or by returning from its start routine.)
  427. */
  428. notifier_list_notify(&thread_exit, NULL);
  429. }
  430. typedef struct {
  431. void *(*start_routine)(void *);
  432. void *arg;
  433. char *name;
  434. } QemuThreadArgs;
  435. static void *qemu_thread_start(void *args)
  436. {
  437. QemuThreadArgs *qemu_thread_args = args;
  438. void *(*start_routine)(void *) = qemu_thread_args->start_routine;
  439. void *arg = qemu_thread_args->arg;
  440. void *r;
  441. /* Attempt to set the threads name; note that this is for debug, so
  442. * we're not going to fail if we can't set it.
  443. */
  444. if (name_threads && qemu_thread_args->name) {
  445. # if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
  446. pthread_setname_np(pthread_self(), qemu_thread_args->name);
  447. # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
  448. pthread_setname_np(qemu_thread_args->name);
  449. # elif defined(CONFIG_PTHREAD_SET_NAME_NP)
  450. pthread_set_name_np(pthread_self(), qemu_thread_args->name);
  451. # endif
  452. }
  453. QEMU_TSAN_ANNOTATE_THREAD_NAME(qemu_thread_args->name);
  454. g_free(qemu_thread_args->name);
  455. g_free(qemu_thread_args);
  456. /*
  457. * GCC 11 with glibc 2.17 on PowerPC reports
  458. *
  459. * qemu-thread-posix.c:540:5: error: ‘__sigsetjmp’ accessing 656 bytes
  460. * in a region of size 528 [-Werror=stringop-overflow=]
  461. * 540 | pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
  462. * | ^~~~~~~~~~~~~~~~~~~~
  463. *
  464. * which is clearly nonsense.
  465. */
  466. #pragma GCC diagnostic push
  467. #ifndef __clang__
  468. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  469. #endif
  470. pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
  471. r = start_routine(arg);
  472. pthread_cleanup_pop(1);
  473. #pragma GCC diagnostic pop
  474. return r;
  475. }
  476. void qemu_thread_create(QemuThread *thread, const char *name,
  477. void *(*start_routine)(void*),
  478. void *arg, int mode)
  479. {
  480. sigset_t set, oldset;
  481. int err;
  482. pthread_attr_t attr;
  483. QemuThreadArgs *qemu_thread_args;
  484. err = pthread_attr_init(&attr);
  485. if (err) {
  486. error_exit(err, __func__);
  487. }
  488. if (mode == QEMU_THREAD_DETACHED) {
  489. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  490. }
  491. /* Leave signal handling to the iothread. */
  492. sigfillset(&set);
  493. /* Blocking the signals can result in undefined behaviour. */
  494. sigdelset(&set, SIGSEGV);
  495. sigdelset(&set, SIGFPE);
  496. sigdelset(&set, SIGILL);
  497. /* TODO avoid SIGBUS loss on macOS */
  498. pthread_sigmask(SIG_SETMASK, &set, &oldset);
  499. qemu_thread_args = g_new0(QemuThreadArgs, 1);
  500. qemu_thread_args->name = g_strdup(name);
  501. qemu_thread_args->start_routine = start_routine;
  502. qemu_thread_args->arg = arg;
  503. err = pthread_create(&thread->thread, &attr,
  504. qemu_thread_start, qemu_thread_args);
  505. if (err)
  506. error_exit(err, __func__);
  507. pthread_sigmask(SIG_SETMASK, &oldset, NULL);
  508. pthread_attr_destroy(&attr);
  509. }
  510. int qemu_thread_set_affinity(QemuThread *thread, unsigned long *host_cpus,
  511. unsigned long nbits)
  512. {
  513. #if defined(CONFIG_PTHREAD_AFFINITY_NP)
  514. const size_t setsize = CPU_ALLOC_SIZE(nbits);
  515. unsigned long value;
  516. cpu_set_t *cpuset;
  517. int err;
  518. cpuset = CPU_ALLOC(nbits);
  519. g_assert(cpuset);
  520. CPU_ZERO_S(setsize, cpuset);
  521. value = find_first_bit(host_cpus, nbits);
  522. while (value < nbits) {
  523. CPU_SET_S(value, setsize, cpuset);
  524. value = find_next_bit(host_cpus, nbits, value + 1);
  525. }
  526. err = pthread_setaffinity_np(thread->thread, setsize, cpuset);
  527. CPU_FREE(cpuset);
  528. return err;
  529. #else
  530. return -ENOSYS;
  531. #endif
  532. }
  533. int qemu_thread_get_affinity(QemuThread *thread, unsigned long **host_cpus,
  534. unsigned long *nbits)
  535. {
  536. #if defined(CONFIG_PTHREAD_AFFINITY_NP)
  537. unsigned long tmpbits;
  538. cpu_set_t *cpuset;
  539. size_t setsize;
  540. int i, err;
  541. tmpbits = CPU_SETSIZE;
  542. while (true) {
  543. setsize = CPU_ALLOC_SIZE(tmpbits);
  544. cpuset = CPU_ALLOC(tmpbits);
  545. g_assert(cpuset);
  546. err = pthread_getaffinity_np(thread->thread, setsize, cpuset);
  547. if (err) {
  548. CPU_FREE(cpuset);
  549. if (err != -EINVAL) {
  550. return err;
  551. }
  552. tmpbits *= 2;
  553. } else {
  554. break;
  555. }
  556. }
  557. /* Convert the result into a proper bitmap. */
  558. *nbits = tmpbits;
  559. *host_cpus = bitmap_new(tmpbits);
  560. for (i = 0; i < tmpbits; i++) {
  561. if (CPU_ISSET(i, cpuset)) {
  562. set_bit(i, *host_cpus);
  563. }
  564. }
  565. CPU_FREE(cpuset);
  566. return 0;
  567. #else
  568. return -ENOSYS;
  569. #endif
  570. }
  571. void qemu_thread_get_self(QemuThread *thread)
  572. {
  573. thread->thread = pthread_self();
  574. }
  575. bool qemu_thread_is_self(QemuThread *thread)
  576. {
  577. return pthread_equal(pthread_self(), thread->thread);
  578. }
  579. void qemu_thread_exit(void *retval)
  580. {
  581. pthread_exit(retval);
  582. }
  583. void *qemu_thread_join(QemuThread *thread)
  584. {
  585. int err;
  586. void *ret;
  587. err = pthread_join(thread->thread, &ret);
  588. if (err) {
  589. error_exit(err, __func__);
  590. }
  591. return ret;
  592. }