exmutex.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: exmutex - ASL Mutex Acquire/Release functions
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acinterp.h"
  12. #include "acevents.h"
  13. #define _COMPONENT ACPI_EXECUTER
  14. ACPI_MODULE_NAME("exmutex")
  15. /* Local prototypes */
  16. static void
  17. acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
  18. struct acpi_thread_state *thread);
  19. /*******************************************************************************
  20. *
  21. * FUNCTION: acpi_ex_unlink_mutex
  22. *
  23. * PARAMETERS: obj_desc - The mutex to be unlinked
  24. *
  25. * RETURN: None
  26. *
  27. * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
  28. *
  29. ******************************************************************************/
  30. void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
  31. {
  32. struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
  33. if (!thread) {
  34. return;
  35. }
  36. /* Doubly linked list */
  37. if (obj_desc->mutex.next) {
  38. (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev;
  39. }
  40. if (obj_desc->mutex.prev) {
  41. (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next;
  42. /*
  43. * Migrate the previous sync level associated with this mutex to
  44. * the previous mutex on the list so that it may be preserved.
  45. * This handles the case where several mutexes have been acquired
  46. * at the same level, but are not released in opposite order.
  47. */
  48. (obj_desc->mutex.prev)->mutex.original_sync_level =
  49. obj_desc->mutex.original_sync_level;
  50. } else {
  51. thread->acquired_mutex_list = obj_desc->mutex.next;
  52. }
  53. }
  54. /*******************************************************************************
  55. *
  56. * FUNCTION: acpi_ex_link_mutex
  57. *
  58. * PARAMETERS: obj_desc - The mutex to be linked
  59. * thread - Current executing thread object
  60. *
  61. * RETURN: None
  62. *
  63. * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
  64. *
  65. ******************************************************************************/
  66. static void
  67. acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
  68. struct acpi_thread_state *thread)
  69. {
  70. union acpi_operand_object *list_head;
  71. list_head = thread->acquired_mutex_list;
  72. /* This object will be the first object in the list */
  73. obj_desc->mutex.prev = NULL;
  74. obj_desc->mutex.next = list_head;
  75. /* Update old first object to point back to this object */
  76. if (list_head) {
  77. list_head->mutex.prev = obj_desc;
  78. }
  79. /* Update list head */
  80. thread->acquired_mutex_list = obj_desc;
  81. }
  82. /*******************************************************************************
  83. *
  84. * FUNCTION: acpi_ex_acquire_mutex_object
  85. *
  86. * PARAMETERS: timeout - Timeout in milliseconds
  87. * obj_desc - Mutex object
  88. * thread_id - Current thread state
  89. *
  90. * RETURN: Status
  91. *
  92. * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
  93. * path that supports multiple acquires by the same thread.
  94. *
  95. * MUTEX: Interpreter must be locked
  96. *
  97. * NOTE: This interface is called from three places:
  98. * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator
  99. * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the
  100. * global lock
  101. * 3) From the external interface, acpi_acquire_global_lock
  102. *
  103. ******************************************************************************/
  104. acpi_status
  105. acpi_ex_acquire_mutex_object(u16 timeout,
  106. union acpi_operand_object *obj_desc,
  107. acpi_thread_id thread_id)
  108. {
  109. acpi_status status;
  110. ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
  111. if (!obj_desc) {
  112. return_ACPI_STATUS(AE_BAD_PARAMETER);
  113. }
  114. /* Support for multiple acquires by the owning thread */
  115. if (obj_desc->mutex.thread_id == thread_id) {
  116. /*
  117. * The mutex is already owned by this thread, just increment the
  118. * acquisition depth
  119. */
  120. obj_desc->mutex.acquisition_depth++;
  121. return_ACPI_STATUS(AE_OK);
  122. }
  123. /* Acquire the mutex, wait if necessary. Special case for Global Lock */
  124. if (obj_desc == acpi_gbl_global_lock_mutex) {
  125. status = acpi_ev_acquire_global_lock(timeout);
  126. } else {
  127. status =
  128. acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
  129. timeout);
  130. }
  131. if (ACPI_FAILURE(status)) {
  132. /* Includes failure from a timeout on time_desc */
  133. return_ACPI_STATUS(status);
  134. }
  135. /* Acquired the mutex: update mutex object */
  136. obj_desc->mutex.thread_id = thread_id;
  137. obj_desc->mutex.acquisition_depth = 1;
  138. obj_desc->mutex.original_sync_level = 0;
  139. obj_desc->mutex.owner_thread = NULL; /* Used only for AML Acquire() */
  140. return_ACPI_STATUS(AE_OK);
  141. }
  142. /*******************************************************************************
  143. *
  144. * FUNCTION: acpi_ex_acquire_mutex
  145. *
  146. * PARAMETERS: time_desc - Timeout integer
  147. * obj_desc - Mutex object
  148. * walk_state - Current method execution state
  149. *
  150. * RETURN: Status
  151. *
  152. * DESCRIPTION: Acquire an AML mutex
  153. *
  154. ******************************************************************************/
  155. acpi_status
  156. acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
  157. union acpi_operand_object *obj_desc,
  158. struct acpi_walk_state *walk_state)
  159. {
  160. acpi_status status;
  161. ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
  162. if (!obj_desc) {
  163. return_ACPI_STATUS(AE_BAD_PARAMETER);
  164. }
  165. /* Must have a valid thread state struct */
  166. if (!walk_state->thread) {
  167. ACPI_ERROR((AE_INFO,
  168. "Cannot acquire Mutex [%4.4s], null thread info",
  169. acpi_ut_get_node_name(obj_desc->mutex.node)));
  170. return_ACPI_STATUS(AE_AML_INTERNAL);
  171. }
  172. /*
  173. * Current sync level must be less than or equal to the sync level
  174. * of the mutex. This mechanism provides some deadlock prevention.
  175. */
  176. if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
  177. ACPI_ERROR((AE_INFO,
  178. "Cannot acquire Mutex [%4.4s], "
  179. "current SyncLevel is too large (%u)",
  180. acpi_ut_get_node_name(obj_desc->mutex.node),
  181. walk_state->thread->current_sync_level));
  182. return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
  183. }
  184. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  185. "Acquiring: Mutex SyncLevel %u, Thread SyncLevel %u, "
  186. "Depth %u TID %p\n",
  187. obj_desc->mutex.sync_level,
  188. walk_state->thread->current_sync_level,
  189. obj_desc->mutex.acquisition_depth,
  190. walk_state->thread));
  191. status = acpi_ex_acquire_mutex_object((u16)time_desc->integer.value,
  192. obj_desc,
  193. walk_state->thread->thread_id);
  194. if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
  195. /* Save Thread object, original/current sync levels */
  196. obj_desc->mutex.owner_thread = walk_state->thread;
  197. obj_desc->mutex.original_sync_level =
  198. walk_state->thread->current_sync_level;
  199. walk_state->thread->current_sync_level =
  200. obj_desc->mutex.sync_level;
  201. /* Link the mutex to the current thread for force-unlock at method exit */
  202. acpi_ex_link_mutex(obj_desc, walk_state->thread);
  203. }
  204. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  205. "Acquired: Mutex SyncLevel %u, Thread SyncLevel %u, Depth %u\n",
  206. obj_desc->mutex.sync_level,
  207. walk_state->thread->current_sync_level,
  208. obj_desc->mutex.acquisition_depth));
  209. return_ACPI_STATUS(status);
  210. }
  211. /*******************************************************************************
  212. *
  213. * FUNCTION: acpi_ex_release_mutex_object
  214. *
  215. * PARAMETERS: obj_desc - The object descriptor for this op
  216. *
  217. * RETURN: Status
  218. *
  219. * DESCRIPTION: Release a previously acquired Mutex, low level interface.
  220. * Provides a common path that supports multiple releases (after
  221. * previous multiple acquires) by the same thread.
  222. *
  223. * MUTEX: Interpreter must be locked
  224. *
  225. * NOTE: This interface is called from three places:
  226. * 1) From acpi_ex_release_mutex, via an AML Acquire() operator
  227. * 2) From acpi_ex_release_global_lock when an AML Field access requires the
  228. * global lock
  229. * 3) From the external interface, acpi_release_global_lock
  230. *
  231. ******************************************************************************/
  232. acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
  233. {
  234. acpi_status status = AE_OK;
  235. ACPI_FUNCTION_TRACE(ex_release_mutex_object);
  236. if (obj_desc->mutex.acquisition_depth == 0) {
  237. return_ACPI_STATUS(AE_NOT_ACQUIRED);
  238. }
  239. /* Match multiple Acquires with multiple Releases */
  240. obj_desc->mutex.acquisition_depth--;
  241. if (obj_desc->mutex.acquisition_depth != 0) {
  242. /* Just decrement the depth and return */
  243. return_ACPI_STATUS(AE_OK);
  244. }
  245. if (obj_desc->mutex.owner_thread) {
  246. /* Unlink the mutex from the owner's list */
  247. acpi_ex_unlink_mutex(obj_desc);
  248. obj_desc->mutex.owner_thread = NULL;
  249. }
  250. /* Release the mutex, special case for Global Lock */
  251. if (obj_desc == acpi_gbl_global_lock_mutex) {
  252. status = acpi_ev_release_global_lock();
  253. } else {
  254. acpi_os_release_mutex(obj_desc->mutex.os_mutex);
  255. }
  256. /* Clear mutex info */
  257. obj_desc->mutex.thread_id = 0;
  258. return_ACPI_STATUS(status);
  259. }
  260. /*******************************************************************************
  261. *
  262. * FUNCTION: acpi_ex_release_mutex
  263. *
  264. * PARAMETERS: obj_desc - The object descriptor for this op
  265. * walk_state - Current method execution state
  266. *
  267. * RETURN: Status
  268. *
  269. * DESCRIPTION: Release a previously acquired Mutex.
  270. *
  271. ******************************************************************************/
  272. acpi_status
  273. acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
  274. struct acpi_walk_state *walk_state)
  275. {
  276. u8 previous_sync_level;
  277. struct acpi_thread_state *owner_thread;
  278. acpi_status status = AE_OK;
  279. ACPI_FUNCTION_TRACE(ex_release_mutex);
  280. if (!obj_desc) {
  281. return_ACPI_STATUS(AE_BAD_PARAMETER);
  282. }
  283. owner_thread = obj_desc->mutex.owner_thread;
  284. /* The mutex must have been previously acquired in order to release it */
  285. if (!owner_thread) {
  286. ACPI_ERROR((AE_INFO,
  287. "Cannot release Mutex [%4.4s], not acquired",
  288. acpi_ut_get_node_name(obj_desc->mutex.node)));
  289. return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
  290. }
  291. /* Must have a valid thread ID */
  292. if (!walk_state->thread) {
  293. ACPI_ERROR((AE_INFO,
  294. "Cannot release Mutex [%4.4s], null thread info",
  295. acpi_ut_get_node_name(obj_desc->mutex.node)));
  296. return_ACPI_STATUS(AE_AML_INTERNAL);
  297. }
  298. /*
  299. * The Mutex is owned, but this thread must be the owner.
  300. * Special case for Global Lock, any thread can release
  301. */
  302. if ((owner_thread->thread_id != walk_state->thread->thread_id) &&
  303. (obj_desc != acpi_gbl_global_lock_mutex)) {
  304. ACPI_ERROR((AE_INFO,
  305. "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
  306. (u32)walk_state->thread->thread_id,
  307. acpi_ut_get_node_name(obj_desc->mutex.node),
  308. (u32)owner_thread->thread_id));
  309. return_ACPI_STATUS(AE_AML_NOT_OWNER);
  310. }
  311. /*
  312. * The sync level of the mutex must be equal to the current sync level. In
  313. * other words, the current level means that at least one mutex at that
  314. * level is currently being held. Attempting to release a mutex of a
  315. * different level can only mean that the mutex ordering rule is being
  316. * violated. This behavior is clarified in ACPI 4.0 specification.
  317. */
  318. if (obj_desc->mutex.sync_level != owner_thread->current_sync_level) {
  319. ACPI_ERROR((AE_INFO,
  320. "Cannot release Mutex [%4.4s], SyncLevel mismatch: "
  321. "mutex %u current %u",
  322. acpi_ut_get_node_name(obj_desc->mutex.node),
  323. obj_desc->mutex.sync_level,
  324. walk_state->thread->current_sync_level));
  325. return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
  326. }
  327. /*
  328. * Get the previous sync_level from the head of the acquired mutex list.
  329. * This handles the case where several mutexes at the same level have been
  330. * acquired, but are not released in reverse order.
  331. */
  332. previous_sync_level =
  333. owner_thread->acquired_mutex_list->mutex.original_sync_level;
  334. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  335. "Releasing: Object SyncLevel %u, Thread SyncLevel %u, "
  336. "Prev SyncLevel %u, Depth %u TID %p\n",
  337. obj_desc->mutex.sync_level,
  338. walk_state->thread->current_sync_level,
  339. previous_sync_level,
  340. obj_desc->mutex.acquisition_depth,
  341. walk_state->thread));
  342. status = acpi_ex_release_mutex_object(obj_desc);
  343. if (ACPI_FAILURE(status)) {
  344. return_ACPI_STATUS(status);
  345. }
  346. if (obj_desc->mutex.acquisition_depth == 0) {
  347. /* Restore the previous sync_level */
  348. owner_thread->current_sync_level = previous_sync_level;
  349. }
  350. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  351. "Released: Object SyncLevel %u, Thread SyncLevel, %u, "
  352. "Prev SyncLevel %u, Depth %u\n",
  353. obj_desc->mutex.sync_level,
  354. walk_state->thread->current_sync_level,
  355. previous_sync_level,
  356. obj_desc->mutex.acquisition_depth));
  357. return_ACPI_STATUS(status);
  358. }
  359. /*******************************************************************************
  360. *
  361. * FUNCTION: acpi_ex_release_all_mutexes
  362. *
  363. * PARAMETERS: thread - Current executing thread object
  364. *
  365. * RETURN: Status
  366. *
  367. * DESCRIPTION: Release all mutexes held by this thread
  368. *
  369. * NOTE: This function is called as the thread is exiting the interpreter.
  370. * Mutexes are not released when an individual control method is exited, but
  371. * only when the parent thread actually exits the interpreter. This allows one
  372. * method to acquire a mutex, and a different method to release it, as long as
  373. * this is performed underneath a single parent control method.
  374. *
  375. ******************************************************************************/
  376. void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
  377. {
  378. union acpi_operand_object *next = thread->acquired_mutex_list;
  379. union acpi_operand_object *obj_desc;
  380. ACPI_FUNCTION_TRACE(ex_release_all_mutexes);
  381. /* Traverse the list of owned mutexes, releasing each one */
  382. while (next) {
  383. obj_desc = next;
  384. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  385. "Mutex [%4.4s] force-release, SyncLevel %u Depth %u\n",
  386. obj_desc->mutex.node->name.ascii,
  387. obj_desc->mutex.sync_level,
  388. obj_desc->mutex.acquisition_depth));
  389. /* Release the mutex, special case for Global Lock */
  390. if (obj_desc == acpi_gbl_global_lock_mutex) {
  391. /* Ignore errors */
  392. (void)acpi_ev_release_global_lock();
  393. } else {
  394. acpi_os_release_mutex(obj_desc->mutex.os_mutex);
  395. }
  396. /* Update Thread sync_level (Last mutex is the important one) */
  397. thread->current_sync_level =
  398. obj_desc->mutex.original_sync_level;
  399. /* Mark mutex unowned */
  400. next = obj_desc->mutex.next;
  401. obj_desc->mutex.prev = NULL;
  402. obj_desc->mutex.next = NULL;
  403. obj_desc->mutex.acquisition_depth = 0;
  404. obj_desc->mutex.owner_thread = NULL;
  405. obj_desc->mutex.thread_id = 0;
  406. }
  407. return_VOID;
  408. }