ntsync.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ntsync.c - Kernel driver for NT synchronization primitives
  4. *
  5. * Copyright (C) 2024 Elizabeth Figura <zfigura@codeweavers.com>
  6. */
  7. #include <linux/anon_inodes.h>
  8. #include <linux/atomic.h>
  9. #include <linux/file.h>
  10. #include <linux/fs.h>
  11. #include <linux/hrtimer.h>
  12. #include <linux/ktime.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/overflow.h>
  17. #include <linux/sched.h>
  18. #include <linux/sched/signal.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <uapi/linux/ntsync.h>
  22. #define NTSYNC_NAME "ntsync"
  23. enum ntsync_type {
  24. NTSYNC_TYPE_SEM,
  25. NTSYNC_TYPE_MUTEX,
  26. NTSYNC_TYPE_EVENT,
  27. };
  28. /*
  29. * Individual synchronization primitives are represented by
  30. * struct ntsync_obj, and each primitive is backed by a file.
  31. *
  32. * The whole namespace is represented by a struct ntsync_device also
  33. * backed by a file.
  34. *
  35. * Both rely on struct file for reference counting. Individual
  36. * ntsync_obj objects take a reference to the device when created.
  37. * Wait operations take a reference to each object being waited on for
  38. * the duration of the wait.
  39. */
  40. struct ntsync_obj {
  41. spinlock_t lock;
  42. int dev_locked;
  43. enum ntsync_type type;
  44. struct file *file;
  45. struct ntsync_device *dev;
  46. /* The following fields are protected by the object lock. */
  47. union {
  48. struct {
  49. __u32 count;
  50. __u32 max;
  51. } sem;
  52. struct {
  53. __u32 count;
  54. pid_t owner;
  55. bool ownerdead;
  56. } mutex;
  57. struct {
  58. bool manual;
  59. bool signaled;
  60. } event;
  61. } u;
  62. /*
  63. * any_waiters is protected by the object lock, but all_waiters is
  64. * protected by the device wait_all_lock.
  65. */
  66. struct list_head any_waiters;
  67. struct list_head all_waiters;
  68. /*
  69. * Hint describing how many tasks are queued on this object in a
  70. * wait-all operation.
  71. *
  72. * Any time we do a wake, we may need to wake "all" waiters as well as
  73. * "any" waiters. In order to atomically wake "all" waiters, we must
  74. * lock all of the objects, and that means grabbing the wait_all_lock
  75. * below (and, due to lock ordering rules, before locking this object).
  76. * However, wait-all is a rare operation, and grabbing the wait-all
  77. * lock for every wake would create unnecessary contention.
  78. * Therefore we first check whether all_hint is zero, and, if it is,
  79. * we skip trying to wake "all" waiters.
  80. *
  81. * Since wait requests must originate from user-space threads, we're
  82. * limited here by PID_MAX_LIMIT, so there's no risk of overflow.
  83. */
  84. atomic_t all_hint;
  85. };
  86. struct ntsync_q_entry {
  87. struct list_head node;
  88. struct ntsync_q *q;
  89. struct ntsync_obj *obj;
  90. __u32 index;
  91. };
  92. struct ntsync_q {
  93. struct task_struct *task;
  94. __u32 owner;
  95. /*
  96. * Protected via atomic_try_cmpxchg(). Only the thread that wins the
  97. * compare-and-swap may actually change object states and wake this
  98. * task.
  99. */
  100. atomic_t signaled;
  101. bool all;
  102. bool ownerdead;
  103. __u32 count;
  104. struct ntsync_q_entry entries[];
  105. };
  106. struct ntsync_device {
  107. /*
  108. * Wait-all operations must atomically grab all objects, and be totally
  109. * ordered with respect to each other and wait-any operations.
  110. * If one thread is trying to acquire several objects, another thread
  111. * cannot touch the object at the same time.
  112. *
  113. * This device-wide lock is used to serialize wait-for-all
  114. * operations, and operations on an object that is involved in a
  115. * wait-for-all.
  116. */
  117. struct mutex wait_all_lock;
  118. struct file *file;
  119. };
  120. /*
  121. * Single objects are locked using obj->lock.
  122. *
  123. * Multiple objects are 'locked' while holding dev->wait_all_lock.
  124. * In this case however, individual objects are not locked by holding
  125. * obj->lock, but by setting obj->dev_locked.
  126. *
  127. * This means that in order to lock a single object, the sequence is slightly
  128. * more complicated than usual. Specifically it needs to check obj->dev_locked
  129. * after acquiring obj->lock, if set, it needs to drop the lock and acquire
  130. * dev->wait_all_lock in order to serialize against the multi-object operation.
  131. */
  132. static void dev_lock_obj(struct ntsync_device *dev, struct ntsync_obj *obj)
  133. {
  134. lockdep_assert_held(&dev->wait_all_lock);
  135. lockdep_assert(obj->dev == dev);
  136. spin_lock(&obj->lock);
  137. /*
  138. * By setting obj->dev_locked inside obj->lock, it is ensured that
  139. * anyone holding obj->lock must see the value.
  140. */
  141. obj->dev_locked = 1;
  142. spin_unlock(&obj->lock);
  143. }
  144. static void dev_unlock_obj(struct ntsync_device *dev, struct ntsync_obj *obj)
  145. {
  146. lockdep_assert_held(&dev->wait_all_lock);
  147. lockdep_assert(obj->dev == dev);
  148. spin_lock(&obj->lock);
  149. obj->dev_locked = 0;
  150. spin_unlock(&obj->lock);
  151. }
  152. static void obj_lock(struct ntsync_obj *obj)
  153. {
  154. struct ntsync_device *dev = obj->dev;
  155. for (;;) {
  156. spin_lock(&obj->lock);
  157. if (likely(!obj->dev_locked))
  158. break;
  159. spin_unlock(&obj->lock);
  160. mutex_lock(&dev->wait_all_lock);
  161. spin_lock(&obj->lock);
  162. /*
  163. * obj->dev_locked should be set and released under the same
  164. * wait_all_lock section, since we now own this lock, it should
  165. * be clear.
  166. */
  167. lockdep_assert(!obj->dev_locked);
  168. spin_unlock(&obj->lock);
  169. mutex_unlock(&dev->wait_all_lock);
  170. }
  171. }
  172. static void obj_unlock(struct ntsync_obj *obj)
  173. {
  174. spin_unlock(&obj->lock);
  175. }
  176. static bool ntsync_lock_obj(struct ntsync_device *dev, struct ntsync_obj *obj)
  177. {
  178. bool all;
  179. obj_lock(obj);
  180. all = atomic_read(&obj->all_hint);
  181. if (unlikely(all)) {
  182. obj_unlock(obj);
  183. mutex_lock(&dev->wait_all_lock);
  184. dev_lock_obj(dev, obj);
  185. }
  186. return all;
  187. }
  188. static void ntsync_unlock_obj(struct ntsync_device *dev, struct ntsync_obj *obj, bool all)
  189. {
  190. if (all) {
  191. dev_unlock_obj(dev, obj);
  192. mutex_unlock(&dev->wait_all_lock);
  193. } else {
  194. obj_unlock(obj);
  195. }
  196. }
  197. #define ntsync_assert_held(obj) \
  198. lockdep_assert((lockdep_is_held(&(obj)->lock) != LOCK_STATE_NOT_HELD) || \
  199. ((lockdep_is_held(&(obj)->dev->wait_all_lock) != LOCK_STATE_NOT_HELD) && \
  200. (obj)->dev_locked))
  201. static bool is_signaled(struct ntsync_obj *obj, __u32 owner)
  202. {
  203. ntsync_assert_held(obj);
  204. switch (obj->type) {
  205. case NTSYNC_TYPE_SEM:
  206. return !!obj->u.sem.count;
  207. case NTSYNC_TYPE_MUTEX:
  208. if (obj->u.mutex.owner && obj->u.mutex.owner != owner)
  209. return false;
  210. return obj->u.mutex.count < UINT_MAX;
  211. case NTSYNC_TYPE_EVENT:
  212. return obj->u.event.signaled;
  213. }
  214. WARN(1, "bad object type %#x\n", obj->type);
  215. return false;
  216. }
  217. /*
  218. * "locked_obj" is an optional pointer to an object which is already locked and
  219. * should not be locked again. This is necessary so that changing an object's
  220. * state and waking it can be a single atomic operation.
  221. */
  222. static void try_wake_all(struct ntsync_device *dev, struct ntsync_q *q,
  223. struct ntsync_obj *locked_obj)
  224. {
  225. __u32 count = q->count;
  226. bool can_wake = true;
  227. int signaled = -1;
  228. __u32 i;
  229. lockdep_assert_held(&dev->wait_all_lock);
  230. if (locked_obj)
  231. lockdep_assert(locked_obj->dev_locked);
  232. for (i = 0; i < count; i++) {
  233. if (q->entries[i].obj != locked_obj)
  234. dev_lock_obj(dev, q->entries[i].obj);
  235. }
  236. for (i = 0; i < count; i++) {
  237. if (!is_signaled(q->entries[i].obj, q->owner)) {
  238. can_wake = false;
  239. break;
  240. }
  241. }
  242. if (can_wake && atomic_try_cmpxchg(&q->signaled, &signaled, 0)) {
  243. for (i = 0; i < count; i++) {
  244. struct ntsync_obj *obj = q->entries[i].obj;
  245. switch (obj->type) {
  246. case NTSYNC_TYPE_SEM:
  247. obj->u.sem.count--;
  248. break;
  249. case NTSYNC_TYPE_MUTEX:
  250. if (obj->u.mutex.ownerdead)
  251. q->ownerdead = true;
  252. obj->u.mutex.ownerdead = false;
  253. obj->u.mutex.count++;
  254. obj->u.mutex.owner = q->owner;
  255. break;
  256. case NTSYNC_TYPE_EVENT:
  257. if (!obj->u.event.manual)
  258. obj->u.event.signaled = false;
  259. break;
  260. }
  261. }
  262. wake_up_process(q->task);
  263. }
  264. for (i = 0; i < count; i++) {
  265. if (q->entries[i].obj != locked_obj)
  266. dev_unlock_obj(dev, q->entries[i].obj);
  267. }
  268. }
  269. static void try_wake_all_obj(struct ntsync_device *dev, struct ntsync_obj *obj)
  270. {
  271. struct ntsync_q_entry *entry;
  272. lockdep_assert_held(&dev->wait_all_lock);
  273. lockdep_assert(obj->dev_locked);
  274. list_for_each_entry(entry, &obj->all_waiters, node)
  275. try_wake_all(dev, entry->q, obj);
  276. }
  277. static void try_wake_any_sem(struct ntsync_obj *sem)
  278. {
  279. struct ntsync_q_entry *entry;
  280. ntsync_assert_held(sem);
  281. lockdep_assert(sem->type == NTSYNC_TYPE_SEM);
  282. list_for_each_entry(entry, &sem->any_waiters, node) {
  283. struct ntsync_q *q = entry->q;
  284. int signaled = -1;
  285. if (!sem->u.sem.count)
  286. break;
  287. if (atomic_try_cmpxchg(&q->signaled, &signaled, entry->index)) {
  288. sem->u.sem.count--;
  289. wake_up_process(q->task);
  290. }
  291. }
  292. }
  293. static void try_wake_any_mutex(struct ntsync_obj *mutex)
  294. {
  295. struct ntsync_q_entry *entry;
  296. ntsync_assert_held(mutex);
  297. lockdep_assert(mutex->type == NTSYNC_TYPE_MUTEX);
  298. list_for_each_entry(entry, &mutex->any_waiters, node) {
  299. struct ntsync_q *q = entry->q;
  300. int signaled = -1;
  301. if (mutex->u.mutex.count == UINT_MAX)
  302. break;
  303. if (mutex->u.mutex.owner && mutex->u.mutex.owner != q->owner)
  304. continue;
  305. if (atomic_try_cmpxchg(&q->signaled, &signaled, entry->index)) {
  306. if (mutex->u.mutex.ownerdead)
  307. q->ownerdead = true;
  308. mutex->u.mutex.ownerdead = false;
  309. mutex->u.mutex.count++;
  310. mutex->u.mutex.owner = q->owner;
  311. wake_up_process(q->task);
  312. }
  313. }
  314. }
  315. static void try_wake_any_event(struct ntsync_obj *event)
  316. {
  317. struct ntsync_q_entry *entry;
  318. ntsync_assert_held(event);
  319. lockdep_assert(event->type == NTSYNC_TYPE_EVENT);
  320. list_for_each_entry(entry, &event->any_waiters, node) {
  321. struct ntsync_q *q = entry->q;
  322. int signaled = -1;
  323. if (!event->u.event.signaled)
  324. break;
  325. if (atomic_try_cmpxchg(&q->signaled, &signaled, entry->index)) {
  326. if (!event->u.event.manual)
  327. event->u.event.signaled = false;
  328. wake_up_process(q->task);
  329. }
  330. }
  331. }
  332. /*
  333. * Actually change the semaphore state, returning -EOVERFLOW if it is made
  334. * invalid.
  335. */
  336. static int release_sem_state(struct ntsync_obj *sem, __u32 count)
  337. {
  338. __u32 sum;
  339. ntsync_assert_held(sem);
  340. if (check_add_overflow(sem->u.sem.count, count, &sum) ||
  341. sum > sem->u.sem.max)
  342. return -EOVERFLOW;
  343. sem->u.sem.count = sum;
  344. return 0;
  345. }
  346. static int ntsync_sem_release(struct ntsync_obj *sem, void __user *argp)
  347. {
  348. struct ntsync_device *dev = sem->dev;
  349. __u32 __user *user_args = argp;
  350. __u32 prev_count;
  351. __u32 args;
  352. bool all;
  353. int ret;
  354. if (copy_from_user(&args, argp, sizeof(args)))
  355. return -EFAULT;
  356. if (sem->type != NTSYNC_TYPE_SEM)
  357. return -EINVAL;
  358. all = ntsync_lock_obj(dev, sem);
  359. prev_count = sem->u.sem.count;
  360. ret = release_sem_state(sem, args);
  361. if (!ret) {
  362. if (all)
  363. try_wake_all_obj(dev, sem);
  364. try_wake_any_sem(sem);
  365. }
  366. ntsync_unlock_obj(dev, sem, all);
  367. if (!ret && put_user(prev_count, user_args))
  368. ret = -EFAULT;
  369. return ret;
  370. }
  371. /*
  372. * Actually change the mutex state, returning -EPERM if not the owner.
  373. */
  374. static int unlock_mutex_state(struct ntsync_obj *mutex,
  375. const struct ntsync_mutex_args *args)
  376. {
  377. ntsync_assert_held(mutex);
  378. if (mutex->u.mutex.owner != args->owner)
  379. return -EPERM;
  380. if (!--mutex->u.mutex.count)
  381. mutex->u.mutex.owner = 0;
  382. return 0;
  383. }
  384. static int ntsync_mutex_unlock(struct ntsync_obj *mutex, void __user *argp)
  385. {
  386. struct ntsync_mutex_args __user *user_args = argp;
  387. struct ntsync_device *dev = mutex->dev;
  388. struct ntsync_mutex_args args;
  389. __u32 prev_count;
  390. bool all;
  391. int ret;
  392. if (copy_from_user(&args, argp, sizeof(args)))
  393. return -EFAULT;
  394. if (!args.owner)
  395. return -EINVAL;
  396. if (mutex->type != NTSYNC_TYPE_MUTEX)
  397. return -EINVAL;
  398. all = ntsync_lock_obj(dev, mutex);
  399. prev_count = mutex->u.mutex.count;
  400. ret = unlock_mutex_state(mutex, &args);
  401. if (!ret) {
  402. if (all)
  403. try_wake_all_obj(dev, mutex);
  404. try_wake_any_mutex(mutex);
  405. }
  406. ntsync_unlock_obj(dev, mutex, all);
  407. if (!ret && put_user(prev_count, &user_args->count))
  408. ret = -EFAULT;
  409. return ret;
  410. }
  411. /*
  412. * Actually change the mutex state to mark its owner as dead,
  413. * returning -EPERM if not the owner.
  414. */
  415. static int kill_mutex_state(struct ntsync_obj *mutex, __u32 owner)
  416. {
  417. ntsync_assert_held(mutex);
  418. if (mutex->u.mutex.owner != owner)
  419. return -EPERM;
  420. mutex->u.mutex.ownerdead = true;
  421. mutex->u.mutex.owner = 0;
  422. mutex->u.mutex.count = 0;
  423. return 0;
  424. }
  425. static int ntsync_mutex_kill(struct ntsync_obj *mutex, void __user *argp)
  426. {
  427. struct ntsync_device *dev = mutex->dev;
  428. __u32 owner;
  429. bool all;
  430. int ret;
  431. if (get_user(owner, (__u32 __user *)argp))
  432. return -EFAULT;
  433. if (!owner)
  434. return -EINVAL;
  435. if (mutex->type != NTSYNC_TYPE_MUTEX)
  436. return -EINVAL;
  437. all = ntsync_lock_obj(dev, mutex);
  438. ret = kill_mutex_state(mutex, owner);
  439. if (!ret) {
  440. if (all)
  441. try_wake_all_obj(dev, mutex);
  442. try_wake_any_mutex(mutex);
  443. }
  444. ntsync_unlock_obj(dev, mutex, all);
  445. return ret;
  446. }
  447. static int ntsync_event_set(struct ntsync_obj *event, void __user *argp, bool pulse)
  448. {
  449. struct ntsync_device *dev = event->dev;
  450. __u32 prev_state;
  451. bool all;
  452. if (event->type != NTSYNC_TYPE_EVENT)
  453. return -EINVAL;
  454. all = ntsync_lock_obj(dev, event);
  455. prev_state = event->u.event.signaled;
  456. event->u.event.signaled = true;
  457. if (all)
  458. try_wake_all_obj(dev, event);
  459. try_wake_any_event(event);
  460. if (pulse)
  461. event->u.event.signaled = false;
  462. ntsync_unlock_obj(dev, event, all);
  463. if (put_user(prev_state, (__u32 __user *)argp))
  464. return -EFAULT;
  465. return 0;
  466. }
  467. static int ntsync_event_reset(struct ntsync_obj *event, void __user *argp)
  468. {
  469. struct ntsync_device *dev = event->dev;
  470. __u32 prev_state;
  471. bool all;
  472. if (event->type != NTSYNC_TYPE_EVENT)
  473. return -EINVAL;
  474. all = ntsync_lock_obj(dev, event);
  475. prev_state = event->u.event.signaled;
  476. event->u.event.signaled = false;
  477. ntsync_unlock_obj(dev, event, all);
  478. if (put_user(prev_state, (__u32 __user *)argp))
  479. return -EFAULT;
  480. return 0;
  481. }
  482. static int ntsync_sem_read(struct ntsync_obj *sem, void __user *argp)
  483. {
  484. struct ntsync_sem_args __user *user_args = argp;
  485. struct ntsync_device *dev = sem->dev;
  486. struct ntsync_sem_args args;
  487. bool all;
  488. if (sem->type != NTSYNC_TYPE_SEM)
  489. return -EINVAL;
  490. all = ntsync_lock_obj(dev, sem);
  491. args.count = sem->u.sem.count;
  492. args.max = sem->u.sem.max;
  493. ntsync_unlock_obj(dev, sem, all);
  494. if (copy_to_user(user_args, &args, sizeof(args)))
  495. return -EFAULT;
  496. return 0;
  497. }
  498. static int ntsync_mutex_read(struct ntsync_obj *mutex, void __user *argp)
  499. {
  500. struct ntsync_mutex_args __user *user_args = argp;
  501. struct ntsync_device *dev = mutex->dev;
  502. struct ntsync_mutex_args args;
  503. bool all;
  504. int ret;
  505. if (mutex->type != NTSYNC_TYPE_MUTEX)
  506. return -EINVAL;
  507. all = ntsync_lock_obj(dev, mutex);
  508. args.count = mutex->u.mutex.count;
  509. args.owner = mutex->u.mutex.owner;
  510. ret = mutex->u.mutex.ownerdead ? -EOWNERDEAD : 0;
  511. ntsync_unlock_obj(dev, mutex, all);
  512. if (copy_to_user(user_args, &args, sizeof(args)))
  513. return -EFAULT;
  514. return ret;
  515. }
  516. static int ntsync_event_read(struct ntsync_obj *event, void __user *argp)
  517. {
  518. struct ntsync_event_args __user *user_args = argp;
  519. struct ntsync_device *dev = event->dev;
  520. struct ntsync_event_args args;
  521. bool all;
  522. if (event->type != NTSYNC_TYPE_EVENT)
  523. return -EINVAL;
  524. all = ntsync_lock_obj(dev, event);
  525. args.manual = event->u.event.manual;
  526. args.signaled = event->u.event.signaled;
  527. ntsync_unlock_obj(dev, event, all);
  528. if (copy_to_user(user_args, &args, sizeof(args)))
  529. return -EFAULT;
  530. return 0;
  531. }
  532. static void ntsync_free_obj(struct ntsync_obj *obj)
  533. {
  534. fput(obj->dev->file);
  535. kfree(obj);
  536. }
  537. static int ntsync_obj_release(struct inode *inode, struct file *file)
  538. {
  539. ntsync_free_obj(file->private_data);
  540. return 0;
  541. }
  542. static long ntsync_obj_ioctl(struct file *file, unsigned int cmd,
  543. unsigned long parm)
  544. {
  545. struct ntsync_obj *obj = file->private_data;
  546. void __user *argp = (void __user *)parm;
  547. switch (cmd) {
  548. case NTSYNC_IOC_SEM_RELEASE:
  549. return ntsync_sem_release(obj, argp);
  550. case NTSYNC_IOC_SEM_READ:
  551. return ntsync_sem_read(obj, argp);
  552. case NTSYNC_IOC_MUTEX_UNLOCK:
  553. return ntsync_mutex_unlock(obj, argp);
  554. case NTSYNC_IOC_MUTEX_KILL:
  555. return ntsync_mutex_kill(obj, argp);
  556. case NTSYNC_IOC_MUTEX_READ:
  557. return ntsync_mutex_read(obj, argp);
  558. case NTSYNC_IOC_EVENT_SET:
  559. return ntsync_event_set(obj, argp, false);
  560. case NTSYNC_IOC_EVENT_RESET:
  561. return ntsync_event_reset(obj, argp);
  562. case NTSYNC_IOC_EVENT_PULSE:
  563. return ntsync_event_set(obj, argp, true);
  564. case NTSYNC_IOC_EVENT_READ:
  565. return ntsync_event_read(obj, argp);
  566. default:
  567. return -ENOIOCTLCMD;
  568. }
  569. }
  570. static const struct file_operations ntsync_obj_fops = {
  571. .owner = THIS_MODULE,
  572. .release = ntsync_obj_release,
  573. .unlocked_ioctl = ntsync_obj_ioctl,
  574. .compat_ioctl = compat_ptr_ioctl,
  575. };
  576. static struct ntsync_obj *ntsync_alloc_obj(struct ntsync_device *dev,
  577. enum ntsync_type type)
  578. {
  579. struct ntsync_obj *obj;
  580. obj = kzalloc_obj(*obj);
  581. if (!obj)
  582. return NULL;
  583. obj->type = type;
  584. obj->dev = dev;
  585. get_file(dev->file);
  586. spin_lock_init(&obj->lock);
  587. INIT_LIST_HEAD(&obj->any_waiters);
  588. INIT_LIST_HEAD(&obj->all_waiters);
  589. atomic_set(&obj->all_hint, 0);
  590. return obj;
  591. }
  592. static int ntsync_obj_get_fd(struct ntsync_obj *obj)
  593. {
  594. FD_PREPARE(fdf, O_CLOEXEC,
  595. anon_inode_getfile("ntsync", &ntsync_obj_fops, obj, O_RDWR));
  596. if (fdf.err)
  597. return fdf.err;
  598. obj->file = fd_prepare_file(fdf);
  599. return fd_publish(fdf);
  600. }
  601. static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp)
  602. {
  603. struct ntsync_sem_args args;
  604. struct ntsync_obj *sem;
  605. int fd;
  606. if (copy_from_user(&args, argp, sizeof(args)))
  607. return -EFAULT;
  608. if (args.count > args.max)
  609. return -EINVAL;
  610. sem = ntsync_alloc_obj(dev, NTSYNC_TYPE_SEM);
  611. if (!sem)
  612. return -ENOMEM;
  613. sem->u.sem.count = args.count;
  614. sem->u.sem.max = args.max;
  615. fd = ntsync_obj_get_fd(sem);
  616. if (fd < 0)
  617. ntsync_free_obj(sem);
  618. return fd;
  619. }
  620. static int ntsync_create_mutex(struct ntsync_device *dev, void __user *argp)
  621. {
  622. struct ntsync_mutex_args args;
  623. struct ntsync_obj *mutex;
  624. int fd;
  625. if (copy_from_user(&args, argp, sizeof(args)))
  626. return -EFAULT;
  627. if (!args.owner != !args.count)
  628. return -EINVAL;
  629. mutex = ntsync_alloc_obj(dev, NTSYNC_TYPE_MUTEX);
  630. if (!mutex)
  631. return -ENOMEM;
  632. mutex->u.mutex.count = args.count;
  633. mutex->u.mutex.owner = args.owner;
  634. fd = ntsync_obj_get_fd(mutex);
  635. if (fd < 0)
  636. ntsync_free_obj(mutex);
  637. return fd;
  638. }
  639. static int ntsync_create_event(struct ntsync_device *dev, void __user *argp)
  640. {
  641. struct ntsync_event_args args;
  642. struct ntsync_obj *event;
  643. int fd;
  644. if (copy_from_user(&args, argp, sizeof(args)))
  645. return -EFAULT;
  646. event = ntsync_alloc_obj(dev, NTSYNC_TYPE_EVENT);
  647. if (!event)
  648. return -ENOMEM;
  649. event->u.event.manual = args.manual;
  650. event->u.event.signaled = args.signaled;
  651. fd = ntsync_obj_get_fd(event);
  652. if (fd < 0)
  653. ntsync_free_obj(event);
  654. return fd;
  655. }
  656. static struct ntsync_obj *get_obj(struct ntsync_device *dev, int fd)
  657. {
  658. struct file *file = fget(fd);
  659. struct ntsync_obj *obj;
  660. if (!file)
  661. return NULL;
  662. if (file->f_op != &ntsync_obj_fops) {
  663. fput(file);
  664. return NULL;
  665. }
  666. obj = file->private_data;
  667. if (obj->dev != dev) {
  668. fput(file);
  669. return NULL;
  670. }
  671. return obj;
  672. }
  673. static void put_obj(struct ntsync_obj *obj)
  674. {
  675. fput(obj->file);
  676. }
  677. static int ntsync_schedule(const struct ntsync_q *q, const struct ntsync_wait_args *args)
  678. {
  679. ktime_t timeout = ns_to_ktime(args->timeout);
  680. clockid_t clock = CLOCK_MONOTONIC;
  681. ktime_t *timeout_ptr;
  682. int ret = 0;
  683. timeout_ptr = (args->timeout == U64_MAX ? NULL : &timeout);
  684. if (args->flags & NTSYNC_WAIT_REALTIME)
  685. clock = CLOCK_REALTIME;
  686. do {
  687. if (signal_pending(current)) {
  688. ret = -ERESTARTSYS;
  689. break;
  690. }
  691. set_current_state(TASK_INTERRUPTIBLE);
  692. if (atomic_read(&q->signaled) != -1) {
  693. ret = 0;
  694. break;
  695. }
  696. ret = schedule_hrtimeout_range_clock(timeout_ptr, 0, HRTIMER_MODE_ABS, clock);
  697. } while (ret < 0);
  698. __set_current_state(TASK_RUNNING);
  699. return ret;
  700. }
  701. /*
  702. * Allocate and initialize the ntsync_q structure, but do not queue us yet.
  703. */
  704. static int setup_wait(struct ntsync_device *dev,
  705. const struct ntsync_wait_args *args, bool all,
  706. struct ntsync_q **ret_q)
  707. {
  708. int fds[NTSYNC_MAX_WAIT_COUNT + 1];
  709. const __u32 count = args->count;
  710. size_t size = array_size(count, sizeof(fds[0]));
  711. struct ntsync_q *q;
  712. __u32 total_count;
  713. __u32 i, j;
  714. if (args->pad || (args->flags & ~NTSYNC_WAIT_REALTIME))
  715. return -EINVAL;
  716. if (size >= sizeof(fds))
  717. return -EINVAL;
  718. total_count = count;
  719. if (args->alert)
  720. total_count++;
  721. if (copy_from_user(fds, u64_to_user_ptr(args->objs), size))
  722. return -EFAULT;
  723. if (args->alert)
  724. fds[count] = args->alert;
  725. q = kmalloc_flex(*q, entries, total_count);
  726. if (!q)
  727. return -ENOMEM;
  728. q->task = current;
  729. q->owner = args->owner;
  730. atomic_set(&q->signaled, -1);
  731. q->all = all;
  732. q->ownerdead = false;
  733. q->count = count;
  734. for (i = 0; i < total_count; i++) {
  735. struct ntsync_q_entry *entry = &q->entries[i];
  736. struct ntsync_obj *obj = get_obj(dev, fds[i]);
  737. if (!obj)
  738. goto err;
  739. if (all) {
  740. /* Check that the objects are all distinct. */
  741. for (j = 0; j < i; j++) {
  742. if (obj == q->entries[j].obj) {
  743. put_obj(obj);
  744. goto err;
  745. }
  746. }
  747. }
  748. entry->obj = obj;
  749. entry->q = q;
  750. entry->index = i;
  751. }
  752. *ret_q = q;
  753. return 0;
  754. err:
  755. for (j = 0; j < i; j++)
  756. put_obj(q->entries[j].obj);
  757. kfree(q);
  758. return -EINVAL;
  759. }
  760. static void try_wake_any_obj(struct ntsync_obj *obj)
  761. {
  762. switch (obj->type) {
  763. case NTSYNC_TYPE_SEM:
  764. try_wake_any_sem(obj);
  765. break;
  766. case NTSYNC_TYPE_MUTEX:
  767. try_wake_any_mutex(obj);
  768. break;
  769. case NTSYNC_TYPE_EVENT:
  770. try_wake_any_event(obj);
  771. break;
  772. }
  773. }
  774. static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp)
  775. {
  776. struct ntsync_wait_args args;
  777. __u32 i, total_count;
  778. struct ntsync_q *q;
  779. int signaled;
  780. bool all;
  781. int ret;
  782. if (copy_from_user(&args, argp, sizeof(args)))
  783. return -EFAULT;
  784. ret = setup_wait(dev, &args, false, &q);
  785. if (ret < 0)
  786. return ret;
  787. total_count = args.count;
  788. if (args.alert)
  789. total_count++;
  790. /* queue ourselves */
  791. for (i = 0; i < total_count; i++) {
  792. struct ntsync_q_entry *entry = &q->entries[i];
  793. struct ntsync_obj *obj = entry->obj;
  794. all = ntsync_lock_obj(dev, obj);
  795. list_add_tail(&entry->node, &obj->any_waiters);
  796. ntsync_unlock_obj(dev, obj, all);
  797. }
  798. /*
  799. * Check if we are already signaled.
  800. *
  801. * Note that the API requires that normal objects are checked before
  802. * the alert event. Hence we queue the alert event last, and check
  803. * objects in order.
  804. */
  805. for (i = 0; i < total_count; i++) {
  806. struct ntsync_obj *obj = q->entries[i].obj;
  807. if (atomic_read(&q->signaled) != -1)
  808. break;
  809. all = ntsync_lock_obj(dev, obj);
  810. try_wake_any_obj(obj);
  811. ntsync_unlock_obj(dev, obj, all);
  812. }
  813. /* sleep */
  814. ret = ntsync_schedule(q, &args);
  815. /* and finally, unqueue */
  816. for (i = 0; i < total_count; i++) {
  817. struct ntsync_q_entry *entry = &q->entries[i];
  818. struct ntsync_obj *obj = entry->obj;
  819. all = ntsync_lock_obj(dev, obj);
  820. list_del(&entry->node);
  821. ntsync_unlock_obj(dev, obj, all);
  822. put_obj(obj);
  823. }
  824. signaled = atomic_read(&q->signaled);
  825. if (signaled != -1) {
  826. struct ntsync_wait_args __user *user_args = argp;
  827. /* even if we caught a signal, we need to communicate success */
  828. ret = q->ownerdead ? -EOWNERDEAD : 0;
  829. if (put_user(signaled, &user_args->index))
  830. ret = -EFAULT;
  831. } else if (!ret) {
  832. ret = -ETIMEDOUT;
  833. }
  834. kfree(q);
  835. return ret;
  836. }
  837. static int ntsync_wait_all(struct ntsync_device *dev, void __user *argp)
  838. {
  839. struct ntsync_wait_args args;
  840. struct ntsync_q *q;
  841. int signaled;
  842. __u32 i;
  843. int ret;
  844. if (copy_from_user(&args, argp, sizeof(args)))
  845. return -EFAULT;
  846. ret = setup_wait(dev, &args, true, &q);
  847. if (ret < 0)
  848. return ret;
  849. /* queue ourselves */
  850. mutex_lock(&dev->wait_all_lock);
  851. for (i = 0; i < args.count; i++) {
  852. struct ntsync_q_entry *entry = &q->entries[i];
  853. struct ntsync_obj *obj = entry->obj;
  854. atomic_inc(&obj->all_hint);
  855. /*
  856. * obj->all_waiters is protected by dev->wait_all_lock rather
  857. * than obj->lock, so there is no need to acquire obj->lock
  858. * here.
  859. */
  860. list_add_tail(&entry->node, &obj->all_waiters);
  861. }
  862. if (args.alert) {
  863. struct ntsync_q_entry *entry = &q->entries[args.count];
  864. struct ntsync_obj *obj = entry->obj;
  865. dev_lock_obj(dev, obj);
  866. list_add_tail(&entry->node, &obj->any_waiters);
  867. dev_unlock_obj(dev, obj);
  868. }
  869. /* check if we are already signaled */
  870. try_wake_all(dev, q, NULL);
  871. mutex_unlock(&dev->wait_all_lock);
  872. /*
  873. * Check if the alert event is signaled, making sure to do so only
  874. * after checking if the other objects are signaled.
  875. */
  876. if (args.alert) {
  877. struct ntsync_obj *obj = q->entries[args.count].obj;
  878. if (atomic_read(&q->signaled) == -1) {
  879. bool all = ntsync_lock_obj(dev, obj);
  880. try_wake_any_obj(obj);
  881. ntsync_unlock_obj(dev, obj, all);
  882. }
  883. }
  884. /* sleep */
  885. ret = ntsync_schedule(q, &args);
  886. /* and finally, unqueue */
  887. mutex_lock(&dev->wait_all_lock);
  888. for (i = 0; i < args.count; i++) {
  889. struct ntsync_q_entry *entry = &q->entries[i];
  890. struct ntsync_obj *obj = entry->obj;
  891. /*
  892. * obj->all_waiters is protected by dev->wait_all_lock rather
  893. * than obj->lock, so there is no need to acquire it here.
  894. */
  895. list_del(&entry->node);
  896. atomic_dec(&obj->all_hint);
  897. put_obj(obj);
  898. }
  899. mutex_unlock(&dev->wait_all_lock);
  900. if (args.alert) {
  901. struct ntsync_q_entry *entry = &q->entries[args.count];
  902. struct ntsync_obj *obj = entry->obj;
  903. bool all;
  904. all = ntsync_lock_obj(dev, obj);
  905. list_del(&entry->node);
  906. ntsync_unlock_obj(dev, obj, all);
  907. put_obj(obj);
  908. }
  909. signaled = atomic_read(&q->signaled);
  910. if (signaled != -1) {
  911. struct ntsync_wait_args __user *user_args = argp;
  912. /* even if we caught a signal, we need to communicate success */
  913. ret = q->ownerdead ? -EOWNERDEAD : 0;
  914. if (put_user(signaled, &user_args->index))
  915. ret = -EFAULT;
  916. } else if (!ret) {
  917. ret = -ETIMEDOUT;
  918. }
  919. kfree(q);
  920. return ret;
  921. }
  922. static int ntsync_char_open(struct inode *inode, struct file *file)
  923. {
  924. struct ntsync_device *dev;
  925. dev = kzalloc_obj(*dev);
  926. if (!dev)
  927. return -ENOMEM;
  928. mutex_init(&dev->wait_all_lock);
  929. file->private_data = dev;
  930. dev->file = file;
  931. return nonseekable_open(inode, file);
  932. }
  933. static int ntsync_char_release(struct inode *inode, struct file *file)
  934. {
  935. struct ntsync_device *dev = file->private_data;
  936. kfree(dev);
  937. return 0;
  938. }
  939. static long ntsync_char_ioctl(struct file *file, unsigned int cmd,
  940. unsigned long parm)
  941. {
  942. struct ntsync_device *dev = file->private_data;
  943. void __user *argp = (void __user *)parm;
  944. switch (cmd) {
  945. case NTSYNC_IOC_CREATE_EVENT:
  946. return ntsync_create_event(dev, argp);
  947. case NTSYNC_IOC_CREATE_MUTEX:
  948. return ntsync_create_mutex(dev, argp);
  949. case NTSYNC_IOC_CREATE_SEM:
  950. return ntsync_create_sem(dev, argp);
  951. case NTSYNC_IOC_WAIT_ALL:
  952. return ntsync_wait_all(dev, argp);
  953. case NTSYNC_IOC_WAIT_ANY:
  954. return ntsync_wait_any(dev, argp);
  955. default:
  956. return -ENOIOCTLCMD;
  957. }
  958. }
  959. static const struct file_operations ntsync_fops = {
  960. .owner = THIS_MODULE,
  961. .open = ntsync_char_open,
  962. .release = ntsync_char_release,
  963. .unlocked_ioctl = ntsync_char_ioctl,
  964. .compat_ioctl = compat_ptr_ioctl,
  965. };
  966. static struct miscdevice ntsync_misc = {
  967. .minor = MISC_DYNAMIC_MINOR,
  968. .name = NTSYNC_NAME,
  969. .fops = &ntsync_fops,
  970. .mode = 0666,
  971. };
  972. module_misc_device(ntsync_misc);
  973. MODULE_AUTHOR("Elizabeth Figura <zfigura@codeweavers.com>");
  974. MODULE_DESCRIPTION("Kernel driver for NT synchronization primitives");
  975. MODULE_LICENSE("GPL");