garbage.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <linux/mutex.h>
  79. #include <net/sock.h>
  80. #include <net/af_unix.h>
  81. #include <net/scm.h>
  82. #include <net/tcp_states.h>
  83. /* Internal data structures and random procedures: */
  84. #define GC_HEAD ((struct sock *)(-1))
  85. #define GC_ORPHAN ((struct sock *)(-3))
  86. static struct sock *gc_current = GC_HEAD; /* stack of objects to mark */
  87. atomic_t unix_tot_inflight = ATOMIC_INIT(0);
  88. static struct sock *unix_get_socket(struct file *filp)
  89. {
  90. struct sock *u_sock = NULL;
  91. struct inode *inode = filp->f_dentry->d_inode;
  92. /*
  93. * Socket ?
  94. */
  95. if (S_ISSOCK(inode->i_mode)) {
  96. struct socket * sock = SOCKET_I(inode);
  97. struct sock * s = sock->sk;
  98. /*
  99. * PF_UNIX ?
  100. */
  101. if (s && sock->ops && sock->ops->family == PF_UNIX)
  102. u_sock = s;
  103. }
  104. return u_sock;
  105. }
  106. /*
  107. * Keep the number of times in flight count for the file
  108. * descriptor if it is for an AF_UNIX socket.
  109. */
  110. void unix_inflight(struct file *fp)
  111. {
  112. struct sock *s = unix_get_socket(fp);
  113. if(s) {
  114. atomic_inc(&unix_sk(s)->inflight);
  115. atomic_inc(&unix_tot_inflight);
  116. }
  117. }
  118. void unix_notinflight(struct file *fp)
  119. {
  120. struct sock *s = unix_get_socket(fp);
  121. if(s) {
  122. atomic_dec(&unix_sk(s)->inflight);
  123. atomic_dec(&unix_tot_inflight);
  124. }
  125. }
  126. /*
  127. * Garbage Collector Support Functions
  128. */
  129. static inline struct sock *pop_stack(void)
  130. {
  131. struct sock *p = gc_current;
  132. gc_current = unix_sk(p)->gc_tree;
  133. return p;
  134. }
  135. static inline int empty_stack(void)
  136. {
  137. return gc_current == GC_HEAD;
  138. }
  139. static void maybe_unmark_and_push(struct sock *x)
  140. {
  141. struct unix_sock *u = unix_sk(x);
  142. if (u->gc_tree != GC_ORPHAN)
  143. return;
  144. sock_hold(x);
  145. u->gc_tree = gc_current;
  146. gc_current = x;
  147. }
  148. /* The external entry point: unix_gc() */
  149. void unix_gc(void)
  150. {
  151. static DEFINE_MUTEX(unix_gc_sem);
  152. int i;
  153. struct sock *s;
  154. struct sk_buff_head hitlist;
  155. struct sk_buff *skb;
  156. /*
  157. * Avoid a recursive GC.
  158. */
  159. if (!mutex_trylock(&unix_gc_sem))
  160. return;
  161. spin_lock(&unix_table_lock);
  162. forall_unix_sockets(i, s)
  163. {
  164. unix_sk(s)->gc_tree = GC_ORPHAN;
  165. }
  166. /*
  167. * Everything is now marked
  168. */
  169. /* Invariant to be maintained:
  170. - everything unmarked is either:
  171. -- (a) on the stack, or
  172. -- (b) has all of its children unmarked
  173. - everything on the stack is always unmarked
  174. - nothing is ever pushed onto the stack twice, because:
  175. -- nothing previously unmarked is ever pushed on the stack
  176. */
  177. /*
  178. * Push root set
  179. */
  180. forall_unix_sockets(i, s)
  181. {
  182. int open_count = 0;
  183. /*
  184. * If all instances of the descriptor are not
  185. * in flight we are in use.
  186. *
  187. * Special case: when socket s is embrion, it may be
  188. * hashed but still not in queue of listening socket.
  189. * In this case (see unix_create1()) we set artificial
  190. * negative inflight counter to close race window.
  191. * It is trick of course and dirty one.
  192. */
  193. if (s->sk_socket && s->sk_socket->file)
  194. open_count = file_count(s->sk_socket->file);
  195. if (open_count > atomic_read(&unix_sk(s)->inflight))
  196. maybe_unmark_and_push(s);
  197. }
  198. /*
  199. * Mark phase
  200. */
  201. while (!empty_stack())
  202. {
  203. struct sock *x = pop_stack();
  204. struct sock *sk;
  205. spin_lock(&x->sk_receive_queue.lock);
  206. skb = skb_peek(&x->sk_receive_queue);
  207. /*
  208. * Loop through all but first born
  209. */
  210. while (skb && skb != (struct sk_buff *)&x->sk_receive_queue) {
  211. /*
  212. * Do we have file descriptors ?
  213. */
  214. if(UNIXCB(skb).fp)
  215. {
  216. /*
  217. * Process the descriptors of this socket
  218. */
  219. int nfd=UNIXCB(skb).fp->count;
  220. struct file **fp = UNIXCB(skb).fp->fp;
  221. while(nfd--)
  222. {
  223. /*
  224. * Get the socket the fd matches if
  225. * it indeed does so
  226. */
  227. if((sk=unix_get_socket(*fp++))!=NULL)
  228. {
  229. maybe_unmark_and_push(sk);
  230. }
  231. }
  232. }
  233. /* We have to scan not-yet-accepted ones too */
  234. if (x->sk_state == TCP_LISTEN)
  235. maybe_unmark_and_push(skb->sk);
  236. skb=skb->next;
  237. }
  238. spin_unlock(&x->sk_receive_queue.lock);
  239. sock_put(x);
  240. }
  241. skb_queue_head_init(&hitlist);
  242. forall_unix_sockets(i, s)
  243. {
  244. struct unix_sock *u = unix_sk(s);
  245. if (u->gc_tree == GC_ORPHAN) {
  246. struct sk_buff *nextsk;
  247. spin_lock(&s->sk_receive_queue.lock);
  248. skb = skb_peek(&s->sk_receive_queue);
  249. while (skb &&
  250. skb != (struct sk_buff *)&s->sk_receive_queue) {
  251. nextsk = skb->next;
  252. /*
  253. * Do we have file descriptors ?
  254. */
  255. if (UNIXCB(skb).fp) {
  256. __skb_unlink(skb,
  257. &s->sk_receive_queue);
  258. __skb_queue_tail(&hitlist, skb);
  259. }
  260. skb = nextsk;
  261. }
  262. spin_unlock(&s->sk_receive_queue.lock);
  263. }
  264. u->gc_tree = GC_ORPHAN;
  265. }
  266. spin_unlock(&unix_table_lock);
  267. /*
  268. * Here we are. Hitlist is filled. Die.
  269. */
  270. __skb_queue_purge(&hitlist);
  271. mutex_unlock(&unix_gc_sem);
  272. }