garbage.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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/kernel.h>
  66. #include <linux/string.h>
  67. #include <linux/socket.h>
  68. #include <linux/un.h>
  69. #include <linux/net.h>
  70. #include <linux/fs.h>
  71. #include <linux/skbuff.h>
  72. #include <linux/netdevice.h>
  73. #include <linux/file.h>
  74. #include <linux/proc_fs.h>
  75. #include <linux/mutex.h>
  76. #include <linux/wait.h>
  77. #include <net/sock.h>
  78. #include <net/af_unix.h>
  79. #include <net/scm.h>
  80. #include <net/tcp_states.h>
  81. struct unix_sock *unix_get_socket(struct file *filp)
  82. {
  83. struct inode *inode = file_inode(filp);
  84. /* Socket ? */
  85. if (S_ISSOCK(inode->i_mode) && !(filp->f_mode & FMODE_PATH)) {
  86. struct socket *sock = SOCKET_I(inode);
  87. const struct proto_ops *ops;
  88. struct sock *sk = sock->sk;
  89. ops = READ_ONCE(sock->ops);
  90. /* PF_UNIX ? */
  91. if (sk && ops && ops->family == PF_UNIX)
  92. return unix_sk(sk);
  93. }
  94. return NULL;
  95. }
  96. static struct unix_vertex *unix_edge_successor(struct unix_edge *edge)
  97. {
  98. /* If an embryo socket has a fd,
  99. * the listener indirectly holds the fd's refcnt.
  100. */
  101. if (edge->successor->listener)
  102. return unix_sk(edge->successor->listener)->vertex;
  103. return edge->successor->vertex;
  104. }
  105. static bool unix_graph_maybe_cyclic;
  106. static bool unix_graph_grouped;
  107. static void unix_update_graph(struct unix_vertex *vertex)
  108. {
  109. /* If the receiver socket is not inflight, no cyclic
  110. * reference could be formed.
  111. */
  112. if (!vertex)
  113. return;
  114. unix_graph_maybe_cyclic = true;
  115. unix_graph_grouped = false;
  116. }
  117. static LIST_HEAD(unix_unvisited_vertices);
  118. enum unix_vertex_index {
  119. UNIX_VERTEX_INDEX_MARK1,
  120. UNIX_VERTEX_INDEX_MARK2,
  121. UNIX_VERTEX_INDEX_START,
  122. };
  123. static unsigned long unix_vertex_unvisited_index = UNIX_VERTEX_INDEX_MARK1;
  124. static void unix_add_edge(struct scm_fp_list *fpl, struct unix_edge *edge)
  125. {
  126. struct unix_vertex *vertex = edge->predecessor->vertex;
  127. if (!vertex) {
  128. vertex = list_first_entry(&fpl->vertices, typeof(*vertex), entry);
  129. vertex->index = unix_vertex_unvisited_index;
  130. vertex->out_degree = 0;
  131. INIT_LIST_HEAD(&vertex->edges);
  132. INIT_LIST_HEAD(&vertex->scc_entry);
  133. list_move_tail(&vertex->entry, &unix_unvisited_vertices);
  134. edge->predecessor->vertex = vertex;
  135. }
  136. vertex->out_degree++;
  137. list_add_tail(&edge->vertex_entry, &vertex->edges);
  138. unix_update_graph(unix_edge_successor(edge));
  139. }
  140. static void unix_del_edge(struct scm_fp_list *fpl, struct unix_edge *edge)
  141. {
  142. struct unix_vertex *vertex = edge->predecessor->vertex;
  143. if (!fpl->dead)
  144. unix_update_graph(unix_edge_successor(edge));
  145. list_del(&edge->vertex_entry);
  146. vertex->out_degree--;
  147. if (!vertex->out_degree) {
  148. edge->predecessor->vertex = NULL;
  149. list_move_tail(&vertex->entry, &fpl->vertices);
  150. }
  151. }
  152. static void unix_free_vertices(struct scm_fp_list *fpl)
  153. {
  154. struct unix_vertex *vertex, *next_vertex;
  155. list_for_each_entry_safe(vertex, next_vertex, &fpl->vertices, entry) {
  156. list_del(&vertex->entry);
  157. kfree(vertex);
  158. }
  159. }
  160. static DEFINE_SPINLOCK(unix_gc_lock);
  161. unsigned int unix_tot_inflight;
  162. void unix_add_edges(struct scm_fp_list *fpl, struct unix_sock *receiver)
  163. {
  164. int i = 0, j = 0;
  165. spin_lock(&unix_gc_lock);
  166. if (!fpl->count_unix)
  167. goto out;
  168. do {
  169. struct unix_sock *inflight = unix_get_socket(fpl->fp[j++]);
  170. struct unix_edge *edge;
  171. if (!inflight)
  172. continue;
  173. edge = fpl->edges + i++;
  174. edge->predecessor = inflight;
  175. edge->successor = receiver;
  176. unix_add_edge(fpl, edge);
  177. } while (i < fpl->count_unix);
  178. receiver->scm_stat.nr_unix_fds += fpl->count_unix;
  179. WRITE_ONCE(unix_tot_inflight, unix_tot_inflight + fpl->count_unix);
  180. out:
  181. WRITE_ONCE(fpl->user->unix_inflight, fpl->user->unix_inflight + fpl->count);
  182. spin_unlock(&unix_gc_lock);
  183. fpl->inflight = true;
  184. unix_free_vertices(fpl);
  185. }
  186. void unix_del_edges(struct scm_fp_list *fpl)
  187. {
  188. struct unix_sock *receiver;
  189. int i = 0;
  190. spin_lock(&unix_gc_lock);
  191. if (!fpl->count_unix)
  192. goto out;
  193. do {
  194. struct unix_edge *edge = fpl->edges + i++;
  195. unix_del_edge(fpl, edge);
  196. } while (i < fpl->count_unix);
  197. if (!fpl->dead) {
  198. receiver = fpl->edges[0].successor;
  199. receiver->scm_stat.nr_unix_fds -= fpl->count_unix;
  200. }
  201. WRITE_ONCE(unix_tot_inflight, unix_tot_inflight - fpl->count_unix);
  202. out:
  203. WRITE_ONCE(fpl->user->unix_inflight, fpl->user->unix_inflight - fpl->count);
  204. spin_unlock(&unix_gc_lock);
  205. fpl->inflight = false;
  206. }
  207. void unix_update_edges(struct unix_sock *receiver)
  208. {
  209. /* nr_unix_fds is only updated under unix_state_lock().
  210. * If it's 0 here, the embryo socket is not part of the
  211. * inflight graph, and GC will not see it, so no lock needed.
  212. */
  213. if (!receiver->scm_stat.nr_unix_fds) {
  214. receiver->listener = NULL;
  215. } else {
  216. spin_lock(&unix_gc_lock);
  217. unix_update_graph(unix_sk(receiver->listener)->vertex);
  218. receiver->listener = NULL;
  219. spin_unlock(&unix_gc_lock);
  220. }
  221. }
  222. int unix_prepare_fpl(struct scm_fp_list *fpl)
  223. {
  224. struct unix_vertex *vertex;
  225. int i;
  226. if (!fpl->count_unix)
  227. return 0;
  228. for (i = 0; i < fpl->count_unix; i++) {
  229. vertex = kmalloc(sizeof(*vertex), GFP_KERNEL);
  230. if (!vertex)
  231. goto err;
  232. list_add(&vertex->entry, &fpl->vertices);
  233. }
  234. fpl->edges = kvmalloc_array(fpl->count_unix, sizeof(*fpl->edges),
  235. GFP_KERNEL_ACCOUNT);
  236. if (!fpl->edges)
  237. goto err;
  238. return 0;
  239. err:
  240. unix_free_vertices(fpl);
  241. return -ENOMEM;
  242. }
  243. void unix_destroy_fpl(struct scm_fp_list *fpl)
  244. {
  245. if (fpl->inflight)
  246. unix_del_edges(fpl);
  247. kvfree(fpl->edges);
  248. unix_free_vertices(fpl);
  249. }
  250. static bool unix_vertex_dead(struct unix_vertex *vertex)
  251. {
  252. struct unix_edge *edge;
  253. struct unix_sock *u;
  254. long total_ref;
  255. list_for_each_entry(edge, &vertex->edges, vertex_entry) {
  256. struct unix_vertex *next_vertex = unix_edge_successor(edge);
  257. /* The vertex's fd can be received by a non-inflight socket. */
  258. if (!next_vertex)
  259. return false;
  260. /* The vertex's fd can be received by an inflight socket in
  261. * another SCC.
  262. */
  263. if (next_vertex->scc_index != vertex->scc_index)
  264. return false;
  265. }
  266. /* No receiver exists out of the same SCC. */
  267. edge = list_first_entry(&vertex->edges, typeof(*edge), vertex_entry);
  268. u = edge->predecessor;
  269. total_ref = file_count(u->sk.sk_socket->file);
  270. /* If not close()d, total_ref > out_degree. */
  271. if (total_ref != vertex->out_degree)
  272. return false;
  273. return true;
  274. }
  275. static void unix_collect_skb(struct list_head *scc, struct sk_buff_head *hitlist)
  276. {
  277. struct unix_vertex *vertex;
  278. list_for_each_entry_reverse(vertex, scc, scc_entry) {
  279. struct sk_buff_head *queue;
  280. struct unix_edge *edge;
  281. struct unix_sock *u;
  282. edge = list_first_entry(&vertex->edges, typeof(*edge), vertex_entry);
  283. u = edge->predecessor;
  284. queue = &u->sk.sk_receive_queue;
  285. spin_lock(&queue->lock);
  286. if (u->sk.sk_state == TCP_LISTEN) {
  287. struct sk_buff *skb;
  288. skb_queue_walk(queue, skb) {
  289. struct sk_buff_head *embryo_queue = &skb->sk->sk_receive_queue;
  290. spin_lock(&embryo_queue->lock);
  291. skb_queue_splice_init(embryo_queue, hitlist);
  292. spin_unlock(&embryo_queue->lock);
  293. }
  294. } else {
  295. skb_queue_splice_init(queue, hitlist);
  296. }
  297. spin_unlock(&queue->lock);
  298. }
  299. }
  300. static bool unix_scc_cyclic(struct list_head *scc)
  301. {
  302. struct unix_vertex *vertex;
  303. struct unix_edge *edge;
  304. /* SCC containing multiple vertices ? */
  305. if (!list_is_singular(scc))
  306. return true;
  307. vertex = list_first_entry(scc, typeof(*vertex), scc_entry);
  308. /* Self-reference or a embryo-listener circle ? */
  309. list_for_each_entry(edge, &vertex->edges, vertex_entry) {
  310. if (unix_edge_successor(edge) == vertex)
  311. return true;
  312. }
  313. return false;
  314. }
  315. static LIST_HEAD(unix_visited_vertices);
  316. static unsigned long unix_vertex_grouped_index = UNIX_VERTEX_INDEX_MARK2;
  317. static void __unix_walk_scc(struct unix_vertex *vertex, unsigned long *last_index,
  318. struct sk_buff_head *hitlist)
  319. {
  320. LIST_HEAD(vertex_stack);
  321. struct unix_edge *edge;
  322. LIST_HEAD(edge_stack);
  323. next_vertex:
  324. /* Push vertex to vertex_stack and mark it as on-stack
  325. * (index >= UNIX_VERTEX_INDEX_START).
  326. * The vertex will be popped when finalising SCC later.
  327. */
  328. list_add(&vertex->scc_entry, &vertex_stack);
  329. vertex->index = *last_index;
  330. vertex->scc_index = *last_index;
  331. (*last_index)++;
  332. /* Explore neighbour vertices (receivers of the current vertex's fd). */
  333. list_for_each_entry(edge, &vertex->edges, vertex_entry) {
  334. struct unix_vertex *next_vertex = unix_edge_successor(edge);
  335. if (!next_vertex)
  336. continue;
  337. if (next_vertex->index == unix_vertex_unvisited_index) {
  338. /* Iterative deepening depth first search
  339. *
  340. * 1. Push a forward edge to edge_stack and set
  341. * the successor to vertex for the next iteration.
  342. */
  343. list_add(&edge->stack_entry, &edge_stack);
  344. vertex = next_vertex;
  345. goto next_vertex;
  346. /* 2. Pop the edge directed to the current vertex
  347. * and restore the ancestor for backtracking.
  348. */
  349. prev_vertex:
  350. edge = list_first_entry(&edge_stack, typeof(*edge), stack_entry);
  351. list_del_init(&edge->stack_entry);
  352. next_vertex = vertex;
  353. vertex = edge->predecessor->vertex;
  354. /* If the successor has a smaller scc_index, two vertices
  355. * are in the same SCC, so propagate the smaller scc_index
  356. * to skip SCC finalisation.
  357. */
  358. vertex->scc_index = min(vertex->scc_index, next_vertex->scc_index);
  359. } else if (next_vertex->index != unix_vertex_grouped_index) {
  360. /* Loop detected by a back/cross edge.
  361. *
  362. * The successor is on vertex_stack, so two vertices are in
  363. * the same SCC. If the successor has a smaller *scc_index*,
  364. * propagate it to skip SCC finalisation.
  365. */
  366. vertex->scc_index = min(vertex->scc_index, next_vertex->scc_index);
  367. } else {
  368. /* The successor was already grouped as another SCC */
  369. }
  370. }
  371. if (vertex->index == vertex->scc_index) {
  372. struct unix_vertex *v;
  373. struct list_head scc;
  374. bool scc_dead = true;
  375. /* SCC finalised.
  376. *
  377. * If the scc_index was not updated, all the vertices above on
  378. * vertex_stack are in the same SCC. Group them using scc_entry.
  379. */
  380. __list_cut_position(&scc, &vertex_stack, &vertex->scc_entry);
  381. list_for_each_entry_reverse(v, &scc, scc_entry) {
  382. /* Don't restart DFS from this vertex in unix_walk_scc(). */
  383. list_move_tail(&v->entry, &unix_visited_vertices);
  384. /* Mark vertex as off-stack. */
  385. v->index = unix_vertex_grouped_index;
  386. if (scc_dead)
  387. scc_dead = unix_vertex_dead(v);
  388. }
  389. if (scc_dead)
  390. unix_collect_skb(&scc, hitlist);
  391. else if (!unix_graph_maybe_cyclic)
  392. unix_graph_maybe_cyclic = unix_scc_cyclic(&scc);
  393. list_del(&scc);
  394. }
  395. /* Need backtracking ? */
  396. if (!list_empty(&edge_stack))
  397. goto prev_vertex;
  398. }
  399. static void unix_walk_scc(struct sk_buff_head *hitlist)
  400. {
  401. unsigned long last_index = UNIX_VERTEX_INDEX_START;
  402. unix_graph_maybe_cyclic = false;
  403. /* Visit every vertex exactly once.
  404. * __unix_walk_scc() moves visited vertices to unix_visited_vertices.
  405. */
  406. while (!list_empty(&unix_unvisited_vertices)) {
  407. struct unix_vertex *vertex;
  408. vertex = list_first_entry(&unix_unvisited_vertices, typeof(*vertex), entry);
  409. __unix_walk_scc(vertex, &last_index, hitlist);
  410. }
  411. list_replace_init(&unix_visited_vertices, &unix_unvisited_vertices);
  412. swap(unix_vertex_unvisited_index, unix_vertex_grouped_index);
  413. unix_graph_grouped = true;
  414. }
  415. static void unix_walk_scc_fast(struct sk_buff_head *hitlist)
  416. {
  417. unix_graph_maybe_cyclic = false;
  418. while (!list_empty(&unix_unvisited_vertices)) {
  419. struct unix_vertex *vertex;
  420. struct list_head scc;
  421. bool scc_dead = true;
  422. vertex = list_first_entry(&unix_unvisited_vertices, typeof(*vertex), entry);
  423. list_add(&scc, &vertex->scc_entry);
  424. list_for_each_entry_reverse(vertex, &scc, scc_entry) {
  425. list_move_tail(&vertex->entry, &unix_visited_vertices);
  426. if (scc_dead)
  427. scc_dead = unix_vertex_dead(vertex);
  428. }
  429. if (scc_dead)
  430. unix_collect_skb(&scc, hitlist);
  431. else if (!unix_graph_maybe_cyclic)
  432. unix_graph_maybe_cyclic = unix_scc_cyclic(&scc);
  433. list_del(&scc);
  434. }
  435. list_replace_init(&unix_visited_vertices, &unix_unvisited_vertices);
  436. }
  437. static bool gc_in_progress;
  438. static void __unix_gc(struct work_struct *work)
  439. {
  440. struct sk_buff_head hitlist;
  441. struct sk_buff *skb;
  442. spin_lock(&unix_gc_lock);
  443. if (!unix_graph_maybe_cyclic) {
  444. spin_unlock(&unix_gc_lock);
  445. goto skip_gc;
  446. }
  447. __skb_queue_head_init(&hitlist);
  448. if (unix_graph_grouped)
  449. unix_walk_scc_fast(&hitlist);
  450. else
  451. unix_walk_scc(&hitlist);
  452. spin_unlock(&unix_gc_lock);
  453. skb_queue_walk(&hitlist, skb) {
  454. if (UNIXCB(skb).fp)
  455. UNIXCB(skb).fp->dead = true;
  456. }
  457. __skb_queue_purge(&hitlist);
  458. skip_gc:
  459. WRITE_ONCE(gc_in_progress, false);
  460. }
  461. static DECLARE_WORK(unix_gc_work, __unix_gc);
  462. void unix_gc(void)
  463. {
  464. WRITE_ONCE(gc_in_progress, true);
  465. queue_work(system_unbound_wq, &unix_gc_work);
  466. }
  467. #define UNIX_INFLIGHT_TRIGGER_GC 16000
  468. #define UNIX_INFLIGHT_SANE_USER (SCM_MAX_FD * 8)
  469. void wait_for_unix_gc(struct scm_fp_list *fpl)
  470. {
  471. /* If number of inflight sockets is insane,
  472. * force a garbage collect right now.
  473. *
  474. * Paired with the WRITE_ONCE() in unix_inflight(),
  475. * unix_notinflight(), and __unix_gc().
  476. */
  477. if (READ_ONCE(unix_tot_inflight) > UNIX_INFLIGHT_TRIGGER_GC &&
  478. !READ_ONCE(gc_in_progress))
  479. unix_gc();
  480. /* Penalise users who want to send AF_UNIX sockets
  481. * but whose sockets have not been received yet.
  482. */
  483. if (!fpl || !fpl->count_unix ||
  484. READ_ONCE(fpl->user->unix_inflight) < UNIX_INFLIGHT_SANE_USER)
  485. return;
  486. if (READ_ONCE(gc_in_progress))
  487. flush_work(&unix_gc_work);
  488. }