garbage.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * NET3: Garbage Collector For AF_UNIX sockets
  4. *
  5. * Garbage Collector:
  6. * Copyright (C) Barak A. Pearlmutter.
  7. *
  8. * Chopped about by Alan Cox 22/3/96 to make it fit the AF_UNIX socket problem.
  9. * If it doesn't work blame me, it worked when Barak sent it.
  10. *
  11. * Assumptions:
  12. *
  13. * - object w/ a bit
  14. * - free list
  15. *
  16. * Current optimizations:
  17. *
  18. * - explicit stack instead of recursion
  19. * - tail recurse on first born instead of immediate push/pop
  20. * - we gather the stuff that should not be killed into tree
  21. * and stack is just a path from root to the current pointer.
  22. *
  23. * Future optimizations:
  24. *
  25. * - don't just push entire root set; process in place
  26. *
  27. * Fixes:
  28. * Alan Cox 07 Sept 1997 Vmalloc internal stack as needed.
  29. * Cope with changing max_files.
  30. * Al Viro 11 Oct 1998
  31. * Graph may have cycles. That is, we can send the descriptor
  32. * of foo to bar and vice versa. Current code chokes on that.
  33. * Fix: move SCM_RIGHTS ones into the separate list and then
  34. * skb_free() them all instead of doing explicit fput's.
  35. * Another problem: since fput() may block somebody may
  36. * create a new unix_socket when we are in the middle of sweep
  37. * phase. Fix: revert the logic wrt MARKED. Mark everything
  38. * upon the beginning and unmark non-junk ones.
  39. *
  40. * [12 Oct 1998] AAARGH! New code purges all SCM_RIGHTS
  41. * sent to connect()'ed but still not accept()'ed sockets.
  42. * Fixed. Old code had slightly different problem here:
  43. * extra fput() in situation when we passed the descriptor via
  44. * such socket and closed it (descriptor). That would happen on
  45. * each unix_gc() until the accept(). Since the struct file in
  46. * question would go to the free list and might be reused...
  47. * That might be the reason of random oopses on filp_close()
  48. * in unrelated processes.
  49. *
  50. * AV 28 Feb 1999
  51. * Kill the explicit allocation of stack. Now we keep the tree
  52. * with root in dummy + pointer (gc_current) to one of the nodes.
  53. * Stack is represented as path from gc_current to dummy. Unmark
  54. * now means "add to tree". Push == "make it a son of gc_current".
  55. * Pop == "move gc_current to parent". We keep only pointers to
  56. * parents (->gc_tree).
  57. * AV 1 Mar 1999
  58. * Damn. Added missing check for ->dead in listen queues scanning.
  59. *
  60. * Miklos Szeredi 25 Jun 2007
  61. * Reimplement with a cycle collecting algorithm. This should
  62. * solve several problems with the previous code, like being racy
  63. * wrt receive and holding up unrelated socket operations.
  64. */
  65. #include <linux/fs.h>
  66. #include <linux/list.h>
  67. #include <linux/skbuff.h>
  68. #include <linux/socket.h>
  69. #include <linux/workqueue.h>
  70. #include <net/af_unix.h>
  71. #include <net/scm.h>
  72. #include <net/tcp_states.h>
  73. #include "af_unix.h"
  74. struct unix_vertex {
  75. struct list_head edges;
  76. struct list_head entry;
  77. struct list_head scc_entry;
  78. unsigned long out_degree;
  79. unsigned long index;
  80. unsigned long scc_index;
  81. };
  82. struct unix_edge {
  83. struct unix_sock *predecessor;
  84. struct unix_sock *successor;
  85. struct list_head vertex_entry;
  86. struct list_head stack_entry;
  87. };
  88. struct unix_sock *unix_get_socket(struct file *filp)
  89. {
  90. struct inode *inode = file_inode(filp);
  91. /* Socket ? */
  92. if (S_ISSOCK(inode->i_mode) && !(filp->f_mode & FMODE_PATH)) {
  93. struct socket *sock = SOCKET_I(inode);
  94. const struct proto_ops *ops;
  95. struct sock *sk = sock->sk;
  96. ops = READ_ONCE(sock->ops);
  97. /* PF_UNIX ? */
  98. if (sk && ops && ops->family == PF_UNIX)
  99. return unix_sk(sk);
  100. }
  101. return NULL;
  102. }
  103. static struct unix_vertex *unix_edge_successor(struct unix_edge *edge)
  104. {
  105. /* If an embryo socket has a fd,
  106. * the listener indirectly holds the fd's refcnt.
  107. */
  108. if (edge->successor->listener)
  109. return unix_sk(edge->successor->listener)->vertex;
  110. return edge->successor->vertex;
  111. }
  112. enum {
  113. UNIX_GRAPH_NOT_CYCLIC,
  114. UNIX_GRAPH_MAYBE_CYCLIC,
  115. UNIX_GRAPH_CYCLIC,
  116. };
  117. static unsigned char unix_graph_state;
  118. static void unix_update_graph(struct unix_vertex *vertex)
  119. {
  120. /* If the receiver socket is not inflight, no cyclic
  121. * reference could be formed.
  122. */
  123. if (!vertex)
  124. return;
  125. WRITE_ONCE(unix_graph_state, UNIX_GRAPH_MAYBE_CYCLIC);
  126. }
  127. static LIST_HEAD(unix_unvisited_vertices);
  128. enum unix_vertex_index {
  129. UNIX_VERTEX_INDEX_MARK1,
  130. UNIX_VERTEX_INDEX_MARK2,
  131. UNIX_VERTEX_INDEX_START,
  132. };
  133. static unsigned long unix_vertex_unvisited_index = UNIX_VERTEX_INDEX_MARK1;
  134. static unsigned long unix_vertex_max_scc_index = UNIX_VERTEX_INDEX_START;
  135. static void unix_add_edge(struct scm_fp_list *fpl, struct unix_edge *edge)
  136. {
  137. struct unix_vertex *vertex = edge->predecessor->vertex;
  138. if (!vertex) {
  139. vertex = list_first_entry(&fpl->vertices, typeof(*vertex), entry);
  140. vertex->index = unix_vertex_unvisited_index;
  141. vertex->scc_index = ++unix_vertex_max_scc_index;
  142. vertex->out_degree = 0;
  143. INIT_LIST_HEAD(&vertex->edges);
  144. INIT_LIST_HEAD(&vertex->scc_entry);
  145. list_move_tail(&vertex->entry, &unix_unvisited_vertices);
  146. edge->predecessor->vertex = vertex;
  147. }
  148. vertex->out_degree++;
  149. list_add_tail(&edge->vertex_entry, &vertex->edges);
  150. unix_update_graph(unix_edge_successor(edge));
  151. }
  152. static void unix_del_edge(struct scm_fp_list *fpl, struct unix_edge *edge)
  153. {
  154. struct unix_vertex *vertex = edge->predecessor->vertex;
  155. if (!fpl->dead)
  156. unix_update_graph(unix_edge_successor(edge));
  157. list_del(&edge->vertex_entry);
  158. vertex->out_degree--;
  159. if (!vertex->out_degree) {
  160. edge->predecessor->vertex = NULL;
  161. list_move_tail(&vertex->entry, &fpl->vertices);
  162. }
  163. }
  164. static void unix_free_vertices(struct scm_fp_list *fpl)
  165. {
  166. struct unix_vertex *vertex, *next_vertex;
  167. list_for_each_entry_safe(vertex, next_vertex, &fpl->vertices, entry) {
  168. list_del(&vertex->entry);
  169. kfree(vertex);
  170. }
  171. }
  172. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(unix_gc_lock);
  173. void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver)
  174. {
  175. int i = 0, j = 0;
  176. spin_lock(&unix_gc_lock);
  177. if (!fpl->count_unix)
  178. goto out;
  179. do {
  180. struct unix_sock *inflight = unix_get_socket(fpl->fp[j++]);
  181. struct unix_edge *edge;
  182. if (!inflight)
  183. continue;
  184. edge = fpl->edges + i++;
  185. edge->predecessor = inflight;
  186. edge->successor = receiver;
  187. unix_add_edge(fpl, edge);
  188. } while (i < fpl->count_unix);
  189. receiver->scm_stat.nr_unix_fds += fpl->count_unix;
  190. out:
  191. WRITE_ONCE(fpl->user->unix_inflight, fpl->user->unix_inflight + fpl->count);
  192. spin_unlock(&unix_gc_lock);
  193. fpl->inflight = true;
  194. unix_free_vertices(fpl);
  195. }
  196. void unix_del_edges(struct scm_fp_list *fpl)
  197. {
  198. struct unix_sock *receiver;
  199. int i = 0;
  200. spin_lock(&unix_gc_lock);
  201. if (!fpl->count_unix)
  202. goto out;
  203. do {
  204. struct unix_edge *edge = fpl->edges + i++;
  205. unix_del_edge(fpl, edge);
  206. } while (i < fpl->count_unix);
  207. if (!fpl->dead) {
  208. receiver = fpl->edges[0].successor;
  209. receiver->scm_stat.nr_unix_fds -= fpl->count_unix;
  210. }
  211. out:
  212. WRITE_ONCE(fpl->user->unix_inflight, fpl->user->unix_inflight - fpl->count);
  213. spin_unlock(&unix_gc_lock);
  214. fpl->inflight = false;
  215. }
  216. void unix_update_edges(struct unix_sock *receiver)
  217. {
  218. /* nr_unix_fds is only updated under unix_state_lock().
  219. * If it's 0 here, the embryo socket is not part of the
  220. * inflight graph, and GC will not see it, so no lock needed.
  221. */
  222. if (!receiver->scm_stat.nr_unix_fds) {
  223. receiver->listener = NULL;
  224. } else {
  225. spin_lock(&unix_gc_lock);
  226. unix_update_graph(unix_sk(receiver->listener)->vertex);
  227. receiver->listener = NULL;
  228. spin_unlock(&unix_gc_lock);
  229. }
  230. }
  231. int unix_prepare_fpl(struct scm_fp_list *fpl)
  232. {
  233. struct unix_vertex *vertex;
  234. int i;
  235. if (!fpl->count_unix)
  236. return 0;
  237. for (i = 0; i < fpl->count_unix; i++) {
  238. vertex = kmalloc_obj(*vertex);
  239. if (!vertex)
  240. goto err;
  241. list_add(&vertex->entry, &fpl->vertices);
  242. }
  243. fpl->edges = kvmalloc_objs(*fpl->edges, fpl->count_unix,
  244. GFP_KERNEL_ACCOUNT);
  245. if (!fpl->edges)
  246. goto err;
  247. unix_schedule_gc(fpl->user);
  248. return 0;
  249. err:
  250. unix_free_vertices(fpl);
  251. return -ENOMEM;
  252. }
  253. void unix_destroy_fpl(struct scm_fp_list *fpl)
  254. {
  255. if (fpl->inflight)
  256. unix_del_edges(fpl);
  257. kvfree(fpl->edges);
  258. unix_free_vertices(fpl);
  259. }
  260. static bool gc_in_progress;
  261. static seqcount_t unix_peek_seq = SEQCNT_ZERO(unix_peek_seq);
  262. void unix_peek_fpl(struct scm_fp_list *fpl)
  263. {
  264. static DEFINE_SPINLOCK(unix_peek_lock);
  265. if (!fpl || !fpl->count_unix)
  266. return;
  267. if (!READ_ONCE(gc_in_progress))
  268. return;
  269. /* Invalidate the final refcnt check in unix_vertex_dead(). */
  270. spin_lock(&unix_peek_lock);
  271. raw_write_seqcount_barrier(&unix_peek_seq);
  272. spin_unlock(&unix_peek_lock);
  273. }
  274. static bool unix_vertex_dead(struct unix_vertex *vertex)
  275. {
  276. struct unix_edge *edge;
  277. struct unix_sock *u;
  278. long total_ref;
  279. list_for_each_entry(edge, &vertex->edges, vertex_entry) {
  280. struct unix_vertex *next_vertex = unix_edge_successor(edge);
  281. /* The vertex's fd can be received by a non-inflight socket. */
  282. if (!next_vertex)
  283. return false;
  284. /* The vertex's fd can be received by an inflight socket in
  285. * another SCC.
  286. */
  287. if (next_vertex->scc_index != vertex->scc_index)
  288. return false;
  289. }
  290. /* No receiver exists out of the same SCC. */
  291. edge = list_first_entry(&vertex->edges, typeof(*edge), vertex_entry);
  292. u = edge->predecessor;
  293. total_ref = file_count(u->sk.sk_socket->file);
  294. /* If not close()d, total_ref > out_degree. */
  295. if (total_ref != vertex->out_degree)
  296. return false;
  297. return true;
  298. }
  299. static LIST_HEAD(unix_visited_vertices);
  300. static unsigned long unix_vertex_grouped_index = UNIX_VERTEX_INDEX_MARK2;
  301. static bool unix_scc_dead(struct list_head *scc, bool fast)
  302. {
  303. struct unix_vertex *vertex;
  304. bool scc_dead = true;
  305. unsigned int seq;
  306. seq = read_seqcount_begin(&unix_peek_seq);
  307. list_for_each_entry_reverse(vertex, scc, scc_entry) {
  308. /* Don't restart DFS from this vertex. */
  309. list_move_tail(&vertex->entry, &unix_visited_vertices);
  310. /* Mark vertex as off-stack for __unix_walk_scc(). */
  311. if (!fast)
  312. vertex->index = unix_vertex_grouped_index;
  313. if (scc_dead)
  314. scc_dead = unix_vertex_dead(vertex);
  315. }
  316. /* If MSG_PEEK intervened, defer this SCC to the next round. */
  317. if (read_seqcount_retry(&unix_peek_seq, seq))
  318. return false;
  319. return scc_dead;
  320. }
  321. static void unix_collect_skb(struct list_head *scc, struct sk_buff_head *hitlist)
  322. {
  323. struct unix_vertex *vertex;
  324. list_for_each_entry_reverse(vertex, scc, scc_entry) {
  325. struct sk_buff_head *queue;
  326. struct unix_edge *edge;
  327. struct unix_sock *u;
  328. edge = list_first_entry(&vertex->edges, typeof(*edge), vertex_entry);
  329. u = edge->predecessor;
  330. queue = &u->sk.sk_receive_queue;
  331. spin_lock(&queue->lock);
  332. if (u->sk.sk_state == TCP_LISTEN) {
  333. struct sk_buff *skb;
  334. skb_queue_walk(queue, skb) {
  335. struct sk_buff_head *embryo_queue = &skb->sk->sk_receive_queue;
  336. spin_lock(&embryo_queue->lock);
  337. skb_queue_splice_init(embryo_queue, hitlist);
  338. spin_unlock(&embryo_queue->lock);
  339. }
  340. } else {
  341. skb_queue_splice_init(queue, hitlist);
  342. }
  343. spin_unlock(&queue->lock);
  344. }
  345. }
  346. static bool unix_scc_cyclic(struct list_head *scc)
  347. {
  348. struct unix_vertex *vertex;
  349. struct unix_edge *edge;
  350. /* SCC containing multiple vertices ? */
  351. if (!list_is_singular(scc))
  352. return true;
  353. vertex = list_first_entry(scc, typeof(*vertex), scc_entry);
  354. /* Self-reference or a embryo-listener circle ? */
  355. list_for_each_entry(edge, &vertex->edges, vertex_entry) {
  356. if (unix_edge_successor(edge) == vertex)
  357. return true;
  358. }
  359. return false;
  360. }
  361. static unsigned long __unix_walk_scc(struct unix_vertex *vertex,
  362. unsigned long *last_index,
  363. struct sk_buff_head *hitlist)
  364. {
  365. unsigned long cyclic_sccs = 0;
  366. LIST_HEAD(vertex_stack);
  367. struct unix_edge *edge;
  368. LIST_HEAD(edge_stack);
  369. next_vertex:
  370. /* Push vertex to vertex_stack and mark it as on-stack
  371. * (index >= UNIX_VERTEX_INDEX_START).
  372. * The vertex will be popped when finalising SCC later.
  373. */
  374. list_add(&vertex->scc_entry, &vertex_stack);
  375. vertex->index = *last_index;
  376. vertex->scc_index = *last_index;
  377. (*last_index)++;
  378. /* Explore neighbour vertices (receivers of the current vertex's fd). */
  379. list_for_each_entry(edge, &vertex->edges, vertex_entry) {
  380. struct unix_vertex *next_vertex = unix_edge_successor(edge);
  381. if (!next_vertex)
  382. continue;
  383. if (next_vertex->index == unix_vertex_unvisited_index) {
  384. /* Iterative deepening depth first search
  385. *
  386. * 1. Push a forward edge to edge_stack and set
  387. * the successor to vertex for the next iteration.
  388. */
  389. list_add(&edge->stack_entry, &edge_stack);
  390. vertex = next_vertex;
  391. goto next_vertex;
  392. /* 2. Pop the edge directed to the current vertex
  393. * and restore the ancestor for backtracking.
  394. */
  395. prev_vertex:
  396. edge = list_first_entry(&edge_stack, typeof(*edge), stack_entry);
  397. list_del_init(&edge->stack_entry);
  398. next_vertex = vertex;
  399. vertex = edge->predecessor->vertex;
  400. /* If the successor has a smaller scc_index, two vertices
  401. * are in the same SCC, so propagate the smaller scc_index
  402. * to skip SCC finalisation.
  403. */
  404. vertex->scc_index = min(vertex->scc_index, next_vertex->scc_index);
  405. } else if (next_vertex->index != unix_vertex_grouped_index) {
  406. /* Loop detected by a back/cross edge.
  407. *
  408. * The successor is on vertex_stack, so two vertices are in
  409. * the same SCC. If the successor has a smaller *scc_index*,
  410. * propagate it to skip SCC finalisation.
  411. */
  412. vertex->scc_index = min(vertex->scc_index, next_vertex->scc_index);
  413. } else {
  414. /* The successor was already grouped as another SCC */
  415. }
  416. }
  417. if (vertex->index == vertex->scc_index) {
  418. struct list_head scc;
  419. /* SCC finalised.
  420. *
  421. * If the scc_index was not updated, all the vertices above on
  422. * vertex_stack are in the same SCC. Group them using scc_entry.
  423. */
  424. __list_cut_position(&scc, &vertex_stack, &vertex->scc_entry);
  425. if (unix_scc_dead(&scc, false)) {
  426. unix_collect_skb(&scc, hitlist);
  427. } else {
  428. if (unix_vertex_max_scc_index < vertex->scc_index)
  429. unix_vertex_max_scc_index = vertex->scc_index;
  430. if (unix_scc_cyclic(&scc))
  431. cyclic_sccs++;
  432. }
  433. list_del(&scc);
  434. }
  435. /* Need backtracking ? */
  436. if (!list_empty(&edge_stack))
  437. goto prev_vertex;
  438. return cyclic_sccs;
  439. }
  440. static unsigned long unix_graph_cyclic_sccs;
  441. static void unix_walk_scc(struct sk_buff_head *hitlist)
  442. {
  443. unsigned long last_index = UNIX_VERTEX_INDEX_START;
  444. unsigned long cyclic_sccs = 0;
  445. unix_vertex_max_scc_index = UNIX_VERTEX_INDEX_START;
  446. /* Visit every vertex exactly once.
  447. * __unix_walk_scc() moves visited vertices to unix_visited_vertices.
  448. */
  449. while (!list_empty(&unix_unvisited_vertices)) {
  450. struct unix_vertex *vertex;
  451. vertex = list_first_entry(&unix_unvisited_vertices, typeof(*vertex), entry);
  452. cyclic_sccs += __unix_walk_scc(vertex, &last_index, hitlist);
  453. }
  454. list_replace_init(&unix_visited_vertices, &unix_unvisited_vertices);
  455. swap(unix_vertex_unvisited_index, unix_vertex_grouped_index);
  456. WRITE_ONCE(unix_graph_cyclic_sccs, cyclic_sccs);
  457. WRITE_ONCE(unix_graph_state,
  458. cyclic_sccs ? UNIX_GRAPH_CYCLIC : UNIX_GRAPH_NOT_CYCLIC);
  459. }
  460. static void unix_walk_scc_fast(struct sk_buff_head *hitlist)
  461. {
  462. unsigned long cyclic_sccs = unix_graph_cyclic_sccs;
  463. while (!list_empty(&unix_unvisited_vertices)) {
  464. struct unix_vertex *vertex;
  465. struct list_head scc;
  466. vertex = list_first_entry(&unix_unvisited_vertices, typeof(*vertex), entry);
  467. list_add(&scc, &vertex->scc_entry);
  468. if (unix_scc_dead(&scc, true)) {
  469. cyclic_sccs--;
  470. unix_collect_skb(&scc, hitlist);
  471. }
  472. list_del(&scc);
  473. }
  474. list_replace_init(&unix_visited_vertices, &unix_unvisited_vertices);
  475. WRITE_ONCE(unix_graph_cyclic_sccs, cyclic_sccs);
  476. WRITE_ONCE(unix_graph_state,
  477. cyclic_sccs ? UNIX_GRAPH_CYCLIC : UNIX_GRAPH_NOT_CYCLIC);
  478. }
  479. static void unix_gc(struct work_struct *work)
  480. {
  481. struct sk_buff_head hitlist;
  482. struct sk_buff *skb;
  483. spin_lock(&unix_gc_lock);
  484. if (unix_graph_state == UNIX_GRAPH_NOT_CYCLIC) {
  485. spin_unlock(&unix_gc_lock);
  486. goto skip_gc;
  487. }
  488. __skb_queue_head_init(&hitlist);
  489. if (unix_graph_state == UNIX_GRAPH_CYCLIC)
  490. unix_walk_scc_fast(&hitlist);
  491. else
  492. unix_walk_scc(&hitlist);
  493. spin_unlock(&unix_gc_lock);
  494. skb_queue_walk(&hitlist, skb) {
  495. if (UNIXCB(skb).fp)
  496. UNIXCB(skb).fp->dead = true;
  497. }
  498. __skb_queue_purge_reason(&hitlist, SKB_DROP_REASON_SOCKET_CLOSE);
  499. skip_gc:
  500. WRITE_ONCE(gc_in_progress, false);
  501. }
  502. static DECLARE_WORK(unix_gc_work, unix_gc);
  503. #define UNIX_INFLIGHT_SANE_USER (SCM_MAX_FD * 8)
  504. void unix_schedule_gc(struct user_struct *user)
  505. {
  506. if (READ_ONCE(unix_graph_state) == UNIX_GRAPH_NOT_CYCLIC)
  507. return;
  508. /* Penalise users who want to send AF_UNIX sockets
  509. * but whose sockets have not been received yet.
  510. */
  511. if (user &&
  512. READ_ONCE(user->unix_inflight) < UNIX_INFLIGHT_SANE_USER)
  513. return;
  514. if (!READ_ONCE(gc_in_progress)) {
  515. WRITE_ONCE(gc_in_progress, true);
  516. queue_work(system_dfl_wq, &unix_gc_work);
  517. }
  518. if (user && READ_ONCE(unix_graph_cyclic_sccs))
  519. flush_work(&unix_gc_work);
  520. }