gc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * security/tomoyo/gc.c
  4. *
  5. * Copyright (C) 2005-2011 NTT DATA CORPORATION
  6. */
  7. #include "common.h"
  8. #include <linux/kthread.h>
  9. #include <linux/slab.h>
  10. /**
  11. * tomoyo_memory_free - Free memory for elements.
  12. *
  13. * @ptr: Pointer to allocated memory.
  14. *
  15. * Returns nothing.
  16. *
  17. * Caller holds tomoyo_policy_lock mutex.
  18. */
  19. static inline void tomoyo_memory_free(void *ptr)
  20. {
  21. tomoyo_memory_used[TOMOYO_MEMORY_POLICY] -= ksize(ptr);
  22. kfree(ptr);
  23. }
  24. /* Lock for protecting tomoyo_io_buffer_list. */
  25. static DEFINE_SPINLOCK(tomoyo_io_buffer_list_lock);
  26. /* The list for "struct tomoyo_io_buffer". */
  27. static __guarded_by(&tomoyo_io_buffer_list_lock) LIST_HEAD(tomoyo_io_buffer_list);
  28. /**
  29. * tomoyo_struct_used_by_io_buffer - Check whether the list element is used by /sys/kernel/security/tomoyo/ users or not.
  30. *
  31. * @element: Pointer to "struct list_head".
  32. *
  33. * Returns true if @element is used by /sys/kernel/security/tomoyo/ users,
  34. * false otherwise.
  35. */
  36. static bool tomoyo_struct_used_by_io_buffer(const struct list_head *element)
  37. {
  38. struct tomoyo_io_buffer *head;
  39. bool in_use = false;
  40. spin_lock(&tomoyo_io_buffer_list_lock);
  41. list_for_each_entry(head, &tomoyo_io_buffer_list, list) {
  42. head->users++;
  43. spin_unlock(&tomoyo_io_buffer_list_lock);
  44. mutex_lock(&head->io_sem);
  45. if (head->r.domain == element || head->r.group == element ||
  46. head->r.acl == element || &head->w.domain->list == element)
  47. in_use = true;
  48. mutex_unlock(&head->io_sem);
  49. spin_lock(&tomoyo_io_buffer_list_lock);
  50. head->users--;
  51. if (in_use)
  52. break;
  53. }
  54. spin_unlock(&tomoyo_io_buffer_list_lock);
  55. return in_use;
  56. }
  57. /**
  58. * tomoyo_name_used_by_io_buffer - Check whether the string is used by /sys/kernel/security/tomoyo/ users or not.
  59. *
  60. * @string: String to check.
  61. *
  62. * Returns true if @string is used by /sys/kernel/security/tomoyo/ users,
  63. * false otherwise.
  64. */
  65. static bool tomoyo_name_used_by_io_buffer(const char *string)
  66. {
  67. struct tomoyo_io_buffer *head;
  68. const size_t size = strlen(string) + 1;
  69. bool in_use = false;
  70. spin_lock(&tomoyo_io_buffer_list_lock);
  71. list_for_each_entry(head, &tomoyo_io_buffer_list, list) {
  72. int i;
  73. head->users++;
  74. spin_unlock(&tomoyo_io_buffer_list_lock);
  75. mutex_lock(&head->io_sem);
  76. for (i = 0; i < TOMOYO_MAX_IO_READ_QUEUE; i++) {
  77. const char *w = head->r.w[i];
  78. if (w < string || w > string + size)
  79. continue;
  80. in_use = true;
  81. break;
  82. }
  83. mutex_unlock(&head->io_sem);
  84. spin_lock(&tomoyo_io_buffer_list_lock);
  85. head->users--;
  86. if (in_use)
  87. break;
  88. }
  89. spin_unlock(&tomoyo_io_buffer_list_lock);
  90. return in_use;
  91. }
  92. /**
  93. * tomoyo_del_transition_control - Delete members in "struct tomoyo_transition_control".
  94. *
  95. * @element: Pointer to "struct list_head".
  96. *
  97. * Returns nothing.
  98. */
  99. static inline void tomoyo_del_transition_control(struct list_head *element)
  100. {
  101. struct tomoyo_transition_control *ptr =
  102. container_of(element, typeof(*ptr), head.list);
  103. tomoyo_put_name(ptr->domainname);
  104. tomoyo_put_name(ptr->program);
  105. }
  106. /**
  107. * tomoyo_del_aggregator - Delete members in "struct tomoyo_aggregator".
  108. *
  109. * @element: Pointer to "struct list_head".
  110. *
  111. * Returns nothing.
  112. */
  113. static inline void tomoyo_del_aggregator(struct list_head *element)
  114. {
  115. struct tomoyo_aggregator *ptr =
  116. container_of(element, typeof(*ptr), head.list);
  117. tomoyo_put_name(ptr->original_name);
  118. tomoyo_put_name(ptr->aggregated_name);
  119. }
  120. /**
  121. * tomoyo_del_manager - Delete members in "struct tomoyo_manager".
  122. *
  123. * @element: Pointer to "struct list_head".
  124. *
  125. * Returns nothing.
  126. */
  127. static inline void tomoyo_del_manager(struct list_head *element)
  128. {
  129. struct tomoyo_manager *ptr =
  130. container_of(element, typeof(*ptr), head.list);
  131. tomoyo_put_name(ptr->manager);
  132. }
  133. /**
  134. * tomoyo_del_acl - Delete members in "struct tomoyo_acl_info".
  135. *
  136. * @element: Pointer to "struct list_head".
  137. *
  138. * Returns nothing.
  139. */
  140. static void tomoyo_del_acl(struct list_head *element)
  141. {
  142. struct tomoyo_acl_info *acl =
  143. container_of(element, typeof(*acl), list);
  144. tomoyo_put_condition(acl->cond);
  145. switch (acl->type) {
  146. case TOMOYO_TYPE_PATH_ACL:
  147. {
  148. struct tomoyo_path_acl *entry
  149. = container_of(acl, typeof(*entry), head);
  150. tomoyo_put_name_union(&entry->name);
  151. }
  152. break;
  153. case TOMOYO_TYPE_PATH2_ACL:
  154. {
  155. struct tomoyo_path2_acl *entry
  156. = container_of(acl, typeof(*entry), head);
  157. tomoyo_put_name_union(&entry->name1);
  158. tomoyo_put_name_union(&entry->name2);
  159. }
  160. break;
  161. case TOMOYO_TYPE_PATH_NUMBER_ACL:
  162. {
  163. struct tomoyo_path_number_acl *entry
  164. = container_of(acl, typeof(*entry), head);
  165. tomoyo_put_name_union(&entry->name);
  166. tomoyo_put_number_union(&entry->number);
  167. }
  168. break;
  169. case TOMOYO_TYPE_MKDEV_ACL:
  170. {
  171. struct tomoyo_mkdev_acl *entry
  172. = container_of(acl, typeof(*entry), head);
  173. tomoyo_put_name_union(&entry->name);
  174. tomoyo_put_number_union(&entry->mode);
  175. tomoyo_put_number_union(&entry->major);
  176. tomoyo_put_number_union(&entry->minor);
  177. }
  178. break;
  179. case TOMOYO_TYPE_MOUNT_ACL:
  180. {
  181. struct tomoyo_mount_acl *entry
  182. = container_of(acl, typeof(*entry), head);
  183. tomoyo_put_name_union(&entry->dev_name);
  184. tomoyo_put_name_union(&entry->dir_name);
  185. tomoyo_put_name_union(&entry->fs_type);
  186. tomoyo_put_number_union(&entry->flags);
  187. }
  188. break;
  189. case TOMOYO_TYPE_ENV_ACL:
  190. {
  191. struct tomoyo_env_acl *entry =
  192. container_of(acl, typeof(*entry), head);
  193. tomoyo_put_name(entry->env);
  194. }
  195. break;
  196. case TOMOYO_TYPE_INET_ACL:
  197. {
  198. struct tomoyo_inet_acl *entry =
  199. container_of(acl, typeof(*entry), head);
  200. tomoyo_put_group(entry->address.group);
  201. tomoyo_put_number_union(&entry->port);
  202. }
  203. break;
  204. case TOMOYO_TYPE_UNIX_ACL:
  205. {
  206. struct tomoyo_unix_acl *entry =
  207. container_of(acl, typeof(*entry), head);
  208. tomoyo_put_name_union(&entry->name);
  209. }
  210. break;
  211. case TOMOYO_TYPE_MANUAL_TASK_ACL:
  212. {
  213. struct tomoyo_task_acl *entry =
  214. container_of(acl, typeof(*entry), head);
  215. tomoyo_put_name(entry->domainname);
  216. }
  217. break;
  218. }
  219. }
  220. /**
  221. * tomoyo_del_domain - Delete members in "struct tomoyo_domain_info".
  222. *
  223. * @element: Pointer to "struct list_head".
  224. *
  225. * Returns nothing.
  226. *
  227. * Caller holds tomoyo_policy_lock mutex.
  228. */
  229. static inline void tomoyo_del_domain(struct list_head *element)
  230. {
  231. struct tomoyo_domain_info *domain =
  232. container_of(element, typeof(*domain), list);
  233. struct tomoyo_acl_info *acl;
  234. struct tomoyo_acl_info *tmp;
  235. /*
  236. * Since this domain is referenced from neither
  237. * "struct tomoyo_io_buffer" nor "struct cred"->security, we can delete
  238. * elements without checking for is_deleted flag.
  239. */
  240. list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
  241. tomoyo_del_acl(&acl->list);
  242. tomoyo_memory_free(acl);
  243. }
  244. tomoyo_put_name(domain->domainname);
  245. }
  246. /**
  247. * tomoyo_del_condition - Delete members in "struct tomoyo_condition".
  248. *
  249. * @element: Pointer to "struct list_head".
  250. *
  251. * Returns nothing.
  252. */
  253. void tomoyo_del_condition(struct list_head *element)
  254. {
  255. struct tomoyo_condition *cond = container_of(element, typeof(*cond),
  256. head.list);
  257. const u16 condc = cond->condc;
  258. const u16 numbers_count = cond->numbers_count;
  259. const u16 names_count = cond->names_count;
  260. const u16 argc = cond->argc;
  261. const u16 envc = cond->envc;
  262. unsigned int i;
  263. const struct tomoyo_condition_element *condp
  264. = (const struct tomoyo_condition_element *) (cond + 1);
  265. struct tomoyo_number_union *numbers_p
  266. = (struct tomoyo_number_union *) (condp + condc);
  267. struct tomoyo_name_union *names_p
  268. = (struct tomoyo_name_union *) (numbers_p + numbers_count);
  269. const struct tomoyo_argv *argv
  270. = (const struct tomoyo_argv *) (names_p + names_count);
  271. const struct tomoyo_envp *envp
  272. = (const struct tomoyo_envp *) (argv + argc);
  273. for (i = 0; i < numbers_count; i++)
  274. tomoyo_put_number_union(numbers_p++);
  275. for (i = 0; i < names_count; i++)
  276. tomoyo_put_name_union(names_p++);
  277. for (i = 0; i < argc; argv++, i++)
  278. tomoyo_put_name(argv->value);
  279. for (i = 0; i < envc; envp++, i++) {
  280. tomoyo_put_name(envp->name);
  281. tomoyo_put_name(envp->value);
  282. }
  283. }
  284. /**
  285. * tomoyo_del_name - Delete members in "struct tomoyo_name".
  286. *
  287. * @element: Pointer to "struct list_head".
  288. *
  289. * Returns nothing.
  290. */
  291. static inline void tomoyo_del_name(struct list_head *element)
  292. {
  293. /* Nothing to do. */
  294. }
  295. /**
  296. * tomoyo_del_path_group - Delete members in "struct tomoyo_path_group".
  297. *
  298. * @element: Pointer to "struct list_head".
  299. *
  300. * Returns nothing.
  301. */
  302. static inline void tomoyo_del_path_group(struct list_head *element)
  303. {
  304. struct tomoyo_path_group *member =
  305. container_of(element, typeof(*member), head.list);
  306. tomoyo_put_name(member->member_name);
  307. }
  308. /**
  309. * tomoyo_del_group - Delete "struct tomoyo_group".
  310. *
  311. * @element: Pointer to "struct list_head".
  312. *
  313. * Returns nothing.
  314. */
  315. static inline void tomoyo_del_group(struct list_head *element)
  316. {
  317. struct tomoyo_group *group =
  318. container_of(element, typeof(*group), head.list);
  319. tomoyo_put_name(group->group_name);
  320. }
  321. /**
  322. * tomoyo_del_address_group - Delete members in "struct tomoyo_address_group".
  323. *
  324. * @element: Pointer to "struct list_head".
  325. *
  326. * Returns nothing.
  327. */
  328. static inline void tomoyo_del_address_group(struct list_head *element)
  329. {
  330. /* Nothing to do. */
  331. }
  332. /**
  333. * tomoyo_del_number_group - Delete members in "struct tomoyo_number_group".
  334. *
  335. * @element: Pointer to "struct list_head".
  336. *
  337. * Returns nothing.
  338. */
  339. static inline void tomoyo_del_number_group(struct list_head *element)
  340. {
  341. /* Nothing to do. */
  342. }
  343. /**
  344. * tomoyo_try_to_gc - Try to kfree() an entry.
  345. *
  346. * @type: One of values in "enum tomoyo_policy_id".
  347. * @element: Pointer to "struct list_head".
  348. *
  349. * Returns nothing.
  350. *
  351. * Caller holds tomoyo_policy_lock mutex.
  352. */
  353. static void tomoyo_try_to_gc(const enum tomoyo_policy_id type,
  354. struct list_head *element)
  355. __must_hold(&tomoyo_policy_lock)
  356. {
  357. /*
  358. * __list_del_entry() guarantees that the list element became no longer
  359. * reachable from the list which the element was originally on (e.g.
  360. * tomoyo_domain_list). Also, synchronize_srcu() guarantees that the
  361. * list element became no longer referenced by syscall users.
  362. */
  363. __list_del_entry(element);
  364. mutex_unlock(&tomoyo_policy_lock);
  365. synchronize_srcu(&tomoyo_ss);
  366. /*
  367. * However, there are two users which may still be using the list
  368. * element. We need to defer until both users forget this element.
  369. *
  370. * Don't kfree() until "struct tomoyo_io_buffer"->r.{domain,group,acl}
  371. * and "struct tomoyo_io_buffer"->w.domain forget this element.
  372. */
  373. if (tomoyo_struct_used_by_io_buffer(element))
  374. goto reinject;
  375. switch (type) {
  376. case TOMOYO_ID_TRANSITION_CONTROL:
  377. tomoyo_del_transition_control(element);
  378. break;
  379. case TOMOYO_ID_MANAGER:
  380. tomoyo_del_manager(element);
  381. break;
  382. case TOMOYO_ID_AGGREGATOR:
  383. tomoyo_del_aggregator(element);
  384. break;
  385. case TOMOYO_ID_GROUP:
  386. tomoyo_del_group(element);
  387. break;
  388. case TOMOYO_ID_PATH_GROUP:
  389. tomoyo_del_path_group(element);
  390. break;
  391. case TOMOYO_ID_ADDRESS_GROUP:
  392. tomoyo_del_address_group(element);
  393. break;
  394. case TOMOYO_ID_NUMBER_GROUP:
  395. tomoyo_del_number_group(element);
  396. break;
  397. case TOMOYO_ID_CONDITION:
  398. tomoyo_del_condition(element);
  399. break;
  400. case TOMOYO_ID_NAME:
  401. /*
  402. * Don't kfree() until all "struct tomoyo_io_buffer"->r.w[]
  403. * forget this element.
  404. */
  405. if (tomoyo_name_used_by_io_buffer
  406. (container_of(element, typeof(struct tomoyo_name),
  407. head.list)->entry.name))
  408. goto reinject;
  409. tomoyo_del_name(element);
  410. break;
  411. case TOMOYO_ID_ACL:
  412. tomoyo_del_acl(element);
  413. break;
  414. case TOMOYO_ID_DOMAIN:
  415. /*
  416. * Don't kfree() until all "struct cred"->security forget this
  417. * element.
  418. */
  419. if (atomic_read(&container_of
  420. (element, typeof(struct tomoyo_domain_info),
  421. list)->users))
  422. goto reinject;
  423. break;
  424. case TOMOYO_MAX_POLICY:
  425. break;
  426. }
  427. mutex_lock(&tomoyo_policy_lock);
  428. if (type == TOMOYO_ID_DOMAIN)
  429. tomoyo_del_domain(element);
  430. tomoyo_memory_free(element);
  431. return;
  432. reinject:
  433. /*
  434. * We can safely reinject this element here because
  435. * (1) Appending list elements and removing list elements are protected
  436. * by tomoyo_policy_lock mutex.
  437. * (2) Only this function removes list elements and this function is
  438. * exclusively executed by tomoyo_gc_mutex mutex.
  439. * are true.
  440. */
  441. mutex_lock(&tomoyo_policy_lock);
  442. list_add_rcu(element, element->prev);
  443. }
  444. /**
  445. * tomoyo_collect_member - Delete elements with "struct tomoyo_acl_head".
  446. *
  447. * @id: One of values in "enum tomoyo_policy_id".
  448. * @member_list: Pointer to "struct list_head".
  449. *
  450. * Returns nothing.
  451. */
  452. static void tomoyo_collect_member(const enum tomoyo_policy_id id,
  453. struct list_head *member_list)
  454. __must_hold(&tomoyo_policy_lock)
  455. {
  456. struct tomoyo_acl_head *member;
  457. struct tomoyo_acl_head *tmp;
  458. list_for_each_entry_safe(member, tmp, member_list, list) {
  459. if (!member->is_deleted)
  460. continue;
  461. member->is_deleted = TOMOYO_GC_IN_PROGRESS;
  462. tomoyo_try_to_gc(id, &member->list);
  463. }
  464. }
  465. /**
  466. * tomoyo_collect_acl - Delete elements in "struct tomoyo_domain_info".
  467. *
  468. * @list: Pointer to "struct list_head".
  469. *
  470. * Returns nothing.
  471. */
  472. static void tomoyo_collect_acl(struct list_head *list)
  473. __must_hold(&tomoyo_policy_lock)
  474. {
  475. struct tomoyo_acl_info *acl;
  476. struct tomoyo_acl_info *tmp;
  477. list_for_each_entry_safe(acl, tmp, list, list) {
  478. if (!acl->is_deleted)
  479. continue;
  480. acl->is_deleted = TOMOYO_GC_IN_PROGRESS;
  481. tomoyo_try_to_gc(TOMOYO_ID_ACL, &acl->list);
  482. }
  483. }
  484. /**
  485. * tomoyo_collect_entry - Try to kfree() deleted elements.
  486. *
  487. * Returns nothing.
  488. */
  489. static void tomoyo_collect_entry(void)
  490. {
  491. int i;
  492. enum tomoyo_policy_id id;
  493. struct tomoyo_policy_namespace *ns;
  494. mutex_lock(&tomoyo_policy_lock);
  495. {
  496. struct tomoyo_domain_info *domain;
  497. struct tomoyo_domain_info *tmp;
  498. list_for_each_entry_safe(domain, tmp, &tomoyo_domain_list,
  499. list) {
  500. tomoyo_collect_acl(&domain->acl_info_list);
  501. if (!domain->is_deleted || atomic_read(&domain->users))
  502. continue;
  503. tomoyo_try_to_gc(TOMOYO_ID_DOMAIN, &domain->list);
  504. }
  505. }
  506. list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
  507. for (id = 0; id < TOMOYO_MAX_POLICY; id++)
  508. tomoyo_collect_member(id, &ns->policy_list[id]);
  509. for (i = 0; i < TOMOYO_MAX_ACL_GROUPS; i++)
  510. tomoyo_collect_acl(&ns->acl_group[i]);
  511. }
  512. {
  513. struct tomoyo_shared_acl_head *ptr;
  514. struct tomoyo_shared_acl_head *tmp;
  515. list_for_each_entry_safe(ptr, tmp, &tomoyo_condition_list,
  516. list) {
  517. if (atomic_read(&ptr->users) > 0)
  518. continue;
  519. atomic_set(&ptr->users, TOMOYO_GC_IN_PROGRESS);
  520. tomoyo_try_to_gc(TOMOYO_ID_CONDITION, &ptr->list);
  521. }
  522. }
  523. list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
  524. for (i = 0; i < TOMOYO_MAX_GROUP; i++) {
  525. struct list_head *list = &ns->group_list[i];
  526. struct tomoyo_group *group;
  527. struct tomoyo_group *tmp;
  528. switch (i) {
  529. case 0:
  530. id = TOMOYO_ID_PATH_GROUP;
  531. break;
  532. case 1:
  533. id = TOMOYO_ID_NUMBER_GROUP;
  534. break;
  535. default:
  536. id = TOMOYO_ID_ADDRESS_GROUP;
  537. break;
  538. }
  539. list_for_each_entry_safe(group, tmp, list, head.list) {
  540. tomoyo_collect_member(id, &group->member_list);
  541. if (!list_empty(&group->member_list) ||
  542. atomic_read(&group->head.users) > 0)
  543. continue;
  544. atomic_set(&group->head.users,
  545. TOMOYO_GC_IN_PROGRESS);
  546. tomoyo_try_to_gc(TOMOYO_ID_GROUP,
  547. &group->head.list);
  548. }
  549. }
  550. }
  551. for (i = 0; i < TOMOYO_MAX_HASH; i++) {
  552. struct list_head *list = &tomoyo_name_list[i];
  553. struct tomoyo_shared_acl_head *ptr;
  554. struct tomoyo_shared_acl_head *tmp;
  555. list_for_each_entry_safe(ptr, tmp, list, list) {
  556. if (atomic_read(&ptr->users) > 0)
  557. continue;
  558. atomic_set(&ptr->users, TOMOYO_GC_IN_PROGRESS);
  559. tomoyo_try_to_gc(TOMOYO_ID_NAME, &ptr->list);
  560. }
  561. }
  562. mutex_unlock(&tomoyo_policy_lock);
  563. }
  564. /**
  565. * tomoyo_gc_thread - Garbage collector thread function.
  566. *
  567. * @unused: Unused.
  568. *
  569. * Returns 0.
  570. */
  571. static int tomoyo_gc_thread(void *unused)
  572. {
  573. /* Garbage collector thread is exclusive. */
  574. static DEFINE_MUTEX(tomoyo_gc_mutex);
  575. if (!mutex_trylock(&tomoyo_gc_mutex))
  576. goto out;
  577. tomoyo_collect_entry();
  578. {
  579. struct tomoyo_io_buffer *head;
  580. struct tomoyo_io_buffer *tmp;
  581. spin_lock(&tomoyo_io_buffer_list_lock);
  582. list_for_each_entry_safe(head, tmp, &tomoyo_io_buffer_list,
  583. list) {
  584. if (head->users)
  585. continue;
  586. list_del(&head->list);
  587. /* Safe destruction because no users are left. */
  588. context_unsafe(
  589. kfree(head->read_buf);
  590. kfree(head->write_buf);
  591. );
  592. kfree(head);
  593. }
  594. spin_unlock(&tomoyo_io_buffer_list_lock);
  595. }
  596. mutex_unlock(&tomoyo_gc_mutex);
  597. out:
  598. /* This acts as do_exit(0). */
  599. return 0;
  600. }
  601. /**
  602. * tomoyo_notify_gc - Register/unregister /sys/kernel/security/tomoyo/ users.
  603. *
  604. * @head: Pointer to "struct tomoyo_io_buffer".
  605. * @is_register: True if register, false if unregister.
  606. *
  607. * Returns nothing.
  608. */
  609. void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register)
  610. {
  611. bool is_write = false;
  612. spin_lock(&tomoyo_io_buffer_list_lock);
  613. if (is_register) {
  614. head->users = 1;
  615. list_add(&head->list, &tomoyo_io_buffer_list);
  616. } else {
  617. /*
  618. * tomoyo_write_control() can concurrently update write_buf from
  619. * a non-NULL to new non-NULL pointer with io_sem held.
  620. */
  621. is_write = data_race(head->write_buf != NULL);
  622. if (!--head->users) {
  623. list_del(&head->list);
  624. /* Safe destruction because no users are left. */
  625. context_unsafe(
  626. kfree(head->read_buf);
  627. kfree(head->write_buf);
  628. );
  629. kfree(head);
  630. }
  631. }
  632. spin_unlock(&tomoyo_io_buffer_list_lock);
  633. if (is_write)
  634. kthread_run(tomoyo_gc_thread, NULL, "GC for TOMOYO");
  635. }