garbage.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * NET3: Garbage Collector For AF_UNIX sockets
  3. *
  4. * Garbage Collector:
  5. * Copyright (C) Barak A. Pearlmutter.
  6. * Released under the GPL version 2 or later.
  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. * This program is free software; you can redistribute it and/or
  28. * modify it under the terms of the GNU General Public License
  29. * as published by the Free Software Foundation; either version
  30. * 2 of the License, or (at your option) any later version.
  31. *
  32. * Fixes:
  33. * Alan Cox 07 Sept 1997 Vmalloc internal stack as needed.
  34. * Cope with changing max_files.
  35. * Al Viro 11 Oct 1998
  36. * Graph may have cycles. That is, we can send the descriptor
  37. * of foo to bar and vice versa. Current code chokes on that.
  38. * Fix: move SCM_RIGHTS ones into the separate list and then
  39. * skb_free() them all instead of doing explicit fput's.
  40. * Another problem: since fput() may block somebody may
  41. * create a new unix_socket when we are in the middle of sweep
  42. * phase. Fix: revert the logic wrt MARKED. Mark everything
  43. * upon the beginning and unmark non-junk ones.
  44. *
  45. * [12 Oct 1998] AAARGH! New code purges all SCM_RIGHTS
  46. * sent to connect()'ed but still not accept()'ed sockets.
  47. * Fixed. Old code had slightly different problem here:
  48. * extra fput() in situation when we passed the descriptor via
  49. * such socket and closed it (descriptor). That would happen on
  50. * each unix_gc() until the accept(). Since the struct file in
  51. * question would go to the free list and might be reused...
  52. * That might be the reason of random oopses on filp_close()
  53. * in unrelated processes.
  54. *
  55. * AV 28 Feb 1999
  56. * Kill the explicit allocation of stack. Now we keep the tree
  57. * with root in dummy + pointer (gc_current) to one of the nodes.
  58. * Stack is represented as path from gc_current to dummy. Unmark
  59. * now means "add to tree". Push == "make it a son of gc_current".
  60. * Pop == "move gc_current to parent". We keep only pointers to
  61. * parents (->gc_tree).
  62. * AV 1 Mar 1999
  63. * Damn. Added missing check for ->dead in listen queues scanning.
  64. *
  65. */
  66. #include <linux/kernel.h>
  67. #include <linux/sched.h>
  68. #include <linux/string.h>
  69. #include <linux/socket.h>
  70. #include <linux/un.h>
  71. #include <linux/net.h>
  72. #include <linux/fs.h>
  73. #include <linux/slab.h>
  74. #include <linux/skbuff.h>
  75. #include <linux/netdevice.h>
  76. #include <linux/file.h>
  77. #include <linux/proc_fs.h>
  78. #include <net/sock.h>
  79. #include <net/af_unix.h>
  80. #include <net/scm.h>
  81. #include <net/tcp_states.h>
  82. /* Internal data structures and random procedures: */
  83. #define GC_HEAD ((struct sock *)(-1))
  84. #define GC_ORPHAN ((struct sock *)(-3))
  85. static struct sock *gc_current = GC_HEAD; /* stack of objects to mark */
  86. atomic_t unix_tot_inflight = ATOMIC_INIT(0);
  87. static struct sock *unix_get_socket(struct file *filp)
  88. {
  89. struct sock *u_sock = NULL;
  90. struct inode *inode = filp->f_dentry->d_inode;
  91. /*
  92. * Socket ?
  93. */
  94. if (S_ISSOCK(inode->i_mode)) {
  95. struct socket * sock = SOCKET_I(inode);
  96. struct sock * s = sock->sk;
  97. /*
  98. * PF_UNIX ?
  99. */
  100. if (s && sock->ops && sock->ops->family == PF_UNIX)
  101. u_sock = s;
  102. }
  103. return u_sock;
  104. }
  105. /*
  106. * Keep the number of times in flight count for the file
  107. * descriptor if it is for an AF_UNIX socket.
  108. */
  109. void unix_inflight(struct file *fp)
  110. {
  111. struct sock *s = unix_get_socket(fp);
  112. if(s) {
  113. atomic_inc(&unix_sk(s)->inflight);
  114. atomic_inc(&unix_tot_inflight);
  115. }
  116. }
  117. void unix_notinflight(struct file *fp)
  118. {
  119. struct sock *s = unix_get_socket(fp);
  120. if(s) {
  121. atomic_dec(&unix_sk(s)->inflight);
  122. atomic_dec(&unix_tot_inflight);
  123. }
  124. }
  125. /*
  126. * Garbage Collector Support Functions
  127. */
  128. static inline struct sock *pop_stack(void)
  129. {
  130. struct sock *p = gc_current;
  131. gc_current = unix_sk(p)->gc_tree;
  132. return p;
  133. }
  134. static inline int empty_stack(void)
  135. {
  136. return gc_current == GC_HEAD;
  137. }
  138. static void maybe_unmark_and_push(struct sock *x)
  139. {
  140. struct unix_sock *u = unix_sk(x);
  141. if (u->gc_tree != GC_ORPHAN)
  142. return;
  143. sock_hold(x);
  144. u->gc_tree = gc_current;
  145. gc_current = x;
  146. }
  147. /* The external entry point: unix_gc() */
  148. void unix_gc(void)
  149. {
  150. static DECLARE_MUTEX(unix_gc_sem);
  151. int i;
  152. struct sock *s;
  153. struct sk_buff_head hitlist;
  154. struct sk_buff *skb;
  155. /*
  156. * Avoid a recursive GC.
  157. */
  158. if (down_trylock(&unix_gc_sem))
  159. return;
  160. spin_lock(&unix_table_lock);
  161. forall_unix_sockets(i, s)
  162. {
  163. unix_sk(s)->gc_tree = GC_ORPHAN;
  164. }
  165. /*
  166. * Everything is now marked
  167. */
  168. /* Invariant to be maintained:
  169. - everything unmarked is either:
  170. -- (a) on the stack, or
  171. -- (b) has all of its children unmarked
  172. - everything on the stack is always unmarked
  173. - nothing is ever pushed onto the stack twice, because:
  174. -- nothing previously unmarked is ever pushed on the stack
  175. */
  176. /*
  177. * Push root set
  178. */
  179. forall_unix_sockets(i, s)
  180. {
  181. int open_count = 0;
  182. /*
  183. * If all instances of the descriptor are not
  184. * in flight we are in use.
  185. *
  186. * Special case: when socket s is embrion, it may be
  187. * hashed but still not in queue of listening socket.
  188. * In this case (see unix_create1()) we set artificial
  189. * negative inflight counter to close race window.
  190. * It is trick of course and dirty one.
  191. */
  192. if (s->sk_socket && s->sk_socket->file)
  193. open_count = file_count(s->sk_socket->file);
  194. if (open_count > atomic_read(&unix_sk(s)->inflight))
  195. maybe_unmark_and_push(s);
  196. }
  197. /*
  198. * Mark phase
  199. */
  200. while (!empty_stack())
  201. {
  202. struct sock *x = pop_stack();
  203. struct sock *sk;
  204. spin_lock(&x->sk_receive_queue.lock);
  205. skb = skb_peek(&x->sk_receive_queue);
  206. /*
  207. * Loop through all but first born
  208. */
  209. while (skb && skb != (struct sk_buff *)&x->sk_receive_queue) {
  210. /*
  211. * Do we have file descriptors ?
  212. */
  213. if(UNIXCB(skb).fp)
  214. {
  215. /*
  216. * Process the descriptors of this socket
  217. */
  218. int nfd=UNIXCB(skb).fp->count;
  219. struct file **fp = UNIXCB(skb).fp->fp;
  220. while(nfd--)
  221. {
  222. /*
  223. * Get the socket the fd matches if
  224. * it indeed does so
  225. */
  226. if((sk=unix_get_socket(*fp++))!=NULL)
  227. {
  228. maybe_unmark_and_push(sk);
  229. }
  230. }
  231. }
  232. /* We have to scan not-yet-accepted ones too */
  233. if (x->sk_state == TCP_LISTEN)
  234. maybe_unmark_and_push(skb->sk);
  235. skb=skb->next;
  236. }
  237. spin_unlock(&x->sk_receive_queue.lock);
  238. sock_put(x);
  239. }
  240. skb_queue_head_init(&hitlist);
  241. forall_unix_sockets(i, s)
  242. {
  243. struct unix_sock *u = unix_sk(s);
  244. if (u->gc_tree == GC_ORPHAN) {
  245. struct sk_buff *nextsk;
  246. spin_lock(&s->sk_receive_queue.lock);
  247. skb = skb_peek(&s->sk_receive_queue);
  248. while (skb &&
  249. skb != (struct sk_buff *)&s->sk_receive_queue) {
  250. nextsk = skb->next;
  251. /*
  252. * Do we have file descriptors ?
  253. */
  254. if (UNIXCB(skb).fp) {
  255. __skb_unlink(skb,
  256. &s->sk_receive_queue);
  257. __skb_queue_tail(&hitlist, skb);
  258. }
  259. skb = nextsk;
  260. }
  261. spin_unlock(&s->sk_receive_queue.lock);
  262. }
  263. u->gc_tree = GC_ORPHAN;
  264. }
  265. spin_unlock(&unix_table_lock);
  266. /*
  267. * Here we are. Hitlist is filled. Die.
  268. */
  269. __skb_queue_purge(&hitlist);
  270. up(&unix_gc_sem);
  271. }