gc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. /* The list for "struct tomoyo_io_buffer". */
  25. static LIST_HEAD(tomoyo_io_buffer_list);
  26. /* Lock for protecting tomoyo_io_buffer_list. */
  27. static DEFINE_SPINLOCK(tomoyo_io_buffer_list_lock);
  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. {
  356. /*
  357. * __list_del_entry() guarantees that the list element became no longer
  358. * reachable from the list which the element was originally on (e.g.
  359. * tomoyo_domain_list). Also, synchronize_srcu() guarantees that the
  360. * list element became no longer referenced by syscall users.
  361. */
  362. __list_del_entry(element);
  363. mutex_unlock(&tomoyo_policy_lock);
  364. synchronize_srcu(&tomoyo_ss);
  365. /*
  366. * However, there are two users which may still be using the list
  367. * element. We need to defer until both users forget this element.
  368. *
  369. * Don't kfree() until "struct tomoyo_io_buffer"->r.{domain,group,acl}
  370. * and "struct tomoyo_io_buffer"->w.domain forget this element.
  371. */
  372. if (tomoyo_struct_used_by_io_buffer(element))
  373. goto reinject;
  374. switch (type) {
  375. case TOMOYO_ID_TRANSITION_CONTROL:
  376. tomoyo_del_transition_control(element);
  377. break;
  378. case TOMOYO_ID_MANAGER:
  379. tomoyo_del_manager(element);
  380. break;
  381. case TOMOYO_ID_AGGREGATOR:
  382. tomoyo_del_aggregator(element);
  383. break;
  384. case TOMOYO_ID_GROUP:
  385. tomoyo_del_group(element);
  386. break;
  387. case TOMOYO_ID_PATH_GROUP:
  388. tomoyo_del_path_group(element);
  389. break;
  390. case TOMOYO_ID_ADDRESS_GROUP:
  391. tomoyo_del_address_group(element);
  392. break;
  393. case TOMOYO_ID_NUMBER_GROUP:
  394. tomoyo_del_number_group(element);
  395. break;
  396. case TOMOYO_ID_CONDITION:
  397. tomoyo_del_condition(element);
  398. break;
  399. case TOMOYO_ID_NAME:
  400. /*
  401. * Don't kfree() until all "struct tomoyo_io_buffer"->r.w[]
  402. * forget this element.
  403. */
  404. if (tomoyo_name_used_by_io_buffer
  405. (container_of(element, typeof(struct tomoyo_name),
  406. head.list)->entry.name))
  407. goto reinject;
  408. tomoyo_del_name(element);
  409. break;
  410. case TOMOYO_ID_ACL:
  411. tomoyo_del_acl(element);
  412. break;
  413. case TOMOYO_ID_DOMAIN:
  414. /*
  415. * Don't kfree() until all "struct cred"->security forget this
  416. * element.
  417. */
  418. if (atomic_read(&container_of
  419. (element, typeof(struct tomoyo_domain_info),
  420. list)->users))
  421. goto reinject;
  422. break;
  423. case TOMOYO_MAX_POLICY:
  424. break;
  425. }
  426. mutex_lock(&tomoyo_policy_lock);
  427. if (type == TOMOYO_ID_DOMAIN)
  428. tomoyo_del_domain(element);
  429. tomoyo_memory_free(element);
  430. return;
  431. reinject:
  432. /*
  433. * We can safely reinject this element here bacause
  434. * (1) Appending list elements and removing list elements are protected
  435. * by tomoyo_policy_lock mutex.
  436. * (2) Only this function removes list elements and this function is
  437. * exclusively executed by tomoyo_gc_mutex mutex.
  438. * are true.
  439. */
  440. mutex_lock(&tomoyo_policy_lock);
  441. list_add_rcu(element, element->prev);
  442. }
  443. /**
  444. * tomoyo_collect_member - Delete elements with "struct tomoyo_acl_head".
  445. *
  446. * @id: One of values in "enum tomoyo_policy_id".
  447. * @member_list: Pointer to "struct list_head".
  448. *
  449. * Returns nothing.
  450. */
  451. static void tomoyo_collect_member(const enum tomoyo_policy_id id,
  452. struct list_head *member_list)
  453. {
  454. struct tomoyo_acl_head *member;
  455. struct tomoyo_acl_head *tmp;
  456. list_for_each_entry_safe(member, tmp, member_list, list) {
  457. if (!member->is_deleted)
  458. continue;
  459. member->is_deleted = TOMOYO_GC_IN_PROGRESS;
  460. tomoyo_try_to_gc(id, &member->list);
  461. }
  462. }
  463. /**
  464. * tomoyo_collect_acl - Delete elements in "struct tomoyo_domain_info".
  465. *
  466. * @list: Pointer to "struct list_head".
  467. *
  468. * Returns nothing.
  469. */
  470. static void tomoyo_collect_acl(struct list_head *list)
  471. {
  472. struct tomoyo_acl_info *acl;
  473. struct tomoyo_acl_info *tmp;
  474. list_for_each_entry_safe(acl, tmp, list, list) {
  475. if (!acl->is_deleted)
  476. continue;
  477. acl->is_deleted = TOMOYO_GC_IN_PROGRESS;
  478. tomoyo_try_to_gc(TOMOYO_ID_ACL, &acl->list);
  479. }
  480. }
  481. /**
  482. * tomoyo_collect_entry - Try to kfree() deleted elements.
  483. *
  484. * Returns nothing.
  485. */
  486. static void tomoyo_collect_entry(void)
  487. {
  488. int i;
  489. enum tomoyo_policy_id id;
  490. struct tomoyo_policy_namespace *ns;
  491. mutex_lock(&tomoyo_policy_lock);
  492. {
  493. struct tomoyo_domain_info *domain;
  494. struct tomoyo_domain_info *tmp;
  495. list_for_each_entry_safe(domain, tmp, &tomoyo_domain_list,
  496. list) {
  497. tomoyo_collect_acl(&domain->acl_info_list);
  498. if (!domain->is_deleted || atomic_read(&domain->users))
  499. continue;
  500. tomoyo_try_to_gc(TOMOYO_ID_DOMAIN, &domain->list);
  501. }
  502. }
  503. list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
  504. for (id = 0; id < TOMOYO_MAX_POLICY; id++)
  505. tomoyo_collect_member(id, &ns->policy_list[id]);
  506. for (i = 0; i < TOMOYO_MAX_ACL_GROUPS; i++)
  507. tomoyo_collect_acl(&ns->acl_group[i]);
  508. }
  509. {
  510. struct tomoyo_shared_acl_head *ptr;
  511. struct tomoyo_shared_acl_head *tmp;
  512. list_for_each_entry_safe(ptr, tmp, &tomoyo_condition_list,
  513. list) {
  514. if (atomic_read(&ptr->users) > 0)
  515. continue;
  516. atomic_set(&ptr->users, TOMOYO_GC_IN_PROGRESS);
  517. tomoyo_try_to_gc(TOMOYO_ID_CONDITION, &ptr->list);
  518. }
  519. }
  520. list_for_each_entry(ns, &tomoyo_namespace_list, namespace_list) {
  521. for (i = 0; i < TOMOYO_MAX_GROUP; i++) {
  522. struct list_head *list = &ns->group_list[i];
  523. struct tomoyo_group *group;
  524. struct tomoyo_group *tmp;
  525. switch (i) {
  526. case 0:
  527. id = TOMOYO_ID_PATH_GROUP;
  528. break;
  529. case 1:
  530. id = TOMOYO_ID_NUMBER_GROUP;
  531. break;
  532. default:
  533. id = TOMOYO_ID_ADDRESS_GROUP;
  534. break;
  535. }
  536. list_for_each_entry_safe(group, tmp, list, head.list) {
  537. tomoyo_collect_member(id, &group->member_list);
  538. if (!list_empty(&group->member_list) ||
  539. atomic_read(&group->head.users) > 0)
  540. continue;
  541. atomic_set(&group->head.users,
  542. TOMOYO_GC_IN_PROGRESS);
  543. tomoyo_try_to_gc(TOMOYO_ID_GROUP,
  544. &group->head.list);
  545. }
  546. }
  547. }
  548. for (i = 0; i < TOMOYO_MAX_HASH; i++) {
  549. struct list_head *list = &tomoyo_name_list[i];
  550. struct tomoyo_shared_acl_head *ptr;
  551. struct tomoyo_shared_acl_head *tmp;
  552. list_for_each_entry_safe(ptr, tmp, list, list) {
  553. if (atomic_read(&ptr->users) > 0)
  554. continue;
  555. atomic_set(&ptr->users, TOMOYO_GC_IN_PROGRESS);
  556. tomoyo_try_to_gc(TOMOYO_ID_NAME, &ptr->list);
  557. }
  558. }
  559. mutex_unlock(&tomoyo_policy_lock);
  560. }
  561. /**
  562. * tomoyo_gc_thread - Garbage collector thread function.
  563. *
  564. * @unused: Unused.
  565. *
  566. * Returns 0.
  567. */
  568. static int tomoyo_gc_thread(void *unused)
  569. {
  570. /* Garbage collector thread is exclusive. */
  571. static DEFINE_MUTEX(tomoyo_gc_mutex);
  572. if (!mutex_trylock(&tomoyo_gc_mutex))
  573. goto out;
  574. tomoyo_collect_entry();
  575. {
  576. struct tomoyo_io_buffer *head;
  577. struct tomoyo_io_buffer *tmp;
  578. spin_lock(&tomoyo_io_buffer_list_lock);
  579. list_for_each_entry_safe(head, tmp, &tomoyo_io_buffer_list,
  580. list) {
  581. if (head->users)
  582. continue;
  583. list_del(&head->list);
  584. kfree(head->read_buf);
  585. kfree(head->write_buf);
  586. kfree(head);
  587. }
  588. spin_unlock(&tomoyo_io_buffer_list_lock);
  589. }
  590. mutex_unlock(&tomoyo_gc_mutex);
  591. out:
  592. /* This acts as do_exit(0). */
  593. return 0;
  594. }
  595. /**
  596. * tomoyo_notify_gc - Register/unregister /sys/kernel/security/tomoyo/ users.
  597. *
  598. * @head: Pointer to "struct tomoyo_io_buffer".
  599. * @is_register: True if register, false if unregister.
  600. *
  601. * Returns nothing.
  602. */
  603. void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register)
  604. {
  605. bool is_write = false;
  606. spin_lock(&tomoyo_io_buffer_list_lock);
  607. if (is_register) {
  608. head->users = 1;
  609. list_add(&head->list, &tomoyo_io_buffer_list);
  610. } else {
  611. is_write = head->write_buf != NULL;
  612. if (!--head->users) {
  613. list_del(&head->list);
  614. kfree(head->read_buf);
  615. kfree(head->write_buf);
  616. kfree(head);
  617. }
  618. }
  619. spin_unlock(&tomoyo_io_buffer_list_lock);
  620. if (is_write)
  621. kthread_run(tomoyo_gc_thread, NULL, "GC for TOMOYO");
  622. }