2 * NET3: Garbage Collector For AF_UNIX sockets
5 * Copyright (C) Barak A. Pearlmutter.
6 * Released under the GPL version 2 or later.
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.
16 * Current optimizations:
18 * - explicit stack instead of recursion
19 * - tail recurse on first born instead of immediate push/pop
21 * Future optimizations:
23 * - don't just push entire root set; process in place
24 * - use linked list for internal stack
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * as published by the Free Software Foundation; either version
29 * 2 of the License, or (at your option) any later version.
32 * Alan Cox 07 Sept 1997 Vmalloc internal stack as needed.
33 * Cope with changing max_files.
37 #include <linux/kernel.h>
38 #include <linux/major.h>
39 #include <linux/signal.h>
40 #include <linux/sched.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/stat.h>
44 #include <linux/socket.h>
46 #include <linux/fcntl.h>
47 #include <linux/termios.h>
48 #include <linux/socket.h>
49 #include <linux/sockios.h>
50 #include <linux/net.h>
53 #include <linux/malloc.h>
54 #include <linux/vmalloc.h>
56 #include <asm/uaccess.h>
57 #include <linux/skbuff.h>
58 #include <linux/netdevice.h>
61 #include <net/af_unix.h>
62 #include <linux/proc_fs.h>
65 /* Internal data structures and random procedures: */
67 static unix_socket
**stack
; /* stack of objects to mark */
68 static int in_stack
= 0; /* first free entry in stack */
69 static int max_stack
; /* Top of stack */
71 extern inline unix_socket
*unix_get_socket(struct file
*filp
)
73 unix_socket
* u_sock
= NULL
;
74 struct inode
*inode
= filp
->f_dentry
->d_inode
;
79 if (inode
&& inode
->i_sock
) {
80 struct socket
* sock
= &inode
->u
.socket_i
;
81 struct sock
* s
= sock
->sk
;
86 if (s
&& sock
->ops
&& sock
->ops
->family
== AF_UNIX
)
93 * Keep the number of times in flight count for the file
94 * descriptor if it is for an AF_UNIX socket.
97 void unix_inflight(struct file
*fp
)
99 unix_socket
*s
=unix_get_socket(fp
);
101 s
->protinfo
.af_unix
.inflight
++;
104 void unix_notinflight(struct file
*fp
)
106 unix_socket
*s
=unix_get_socket(fp
);
108 s
->protinfo
.af_unix
.inflight
--;
113 * Garbage Collector Support Functions
116 extern inline void push_stack(unix_socket
*x
)
118 if (in_stack
== max_stack
)
119 panic("can't push onto full stack");
120 stack
[in_stack
++] = x
;
123 extern inline unix_socket
*pop_stack(void)
126 panic("can't pop empty gc stack");
127 return stack
[--in_stack
];
130 extern inline int empty_stack(void)
132 return in_stack
== 0;
135 extern inline void maybe_mark_and_push(unix_socket
*x
)
137 if (x
->protinfo
.af_unix
.marksweep
&MARKED
)
139 x
->protinfo
.af_unix
.marksweep
|=MARKED
;
144 /* The external entry point: unix_gc() */
148 static int in_unix_gc
=0;
154 * Avoid a recursive GC.
161 if(stack
==NULL
|| max_files
>max_stack
)
165 stack
=(unix_socket
**)vmalloc(max_files
*sizeof(struct unix_socket
*));
168 printk(KERN_NOTICE
"unix_gc: deferred due to low memory.\n");
176 * Assume everything is now unmarked
179 /* Invariant to be maintained:
180 - everything marked is either:
181 -- (a) on the stack, or
182 -- (b) has all of its children marked
183 - everything on the stack is always marked
184 - nothing is ever pushed onto the stack twice, because:
185 -- nothing previously marked is ever pushed on the stack
192 forall_unix_sockets(i
, s
)
195 * If all instances of the descriptor are not
196 * in flight we are in use.
198 if(s
->socket
&& s
->socket
->file
&& s
->socket
->file
->f_count
> s
->protinfo
.af_unix
.inflight
)
199 maybe_mark_and_push(s
);
206 while (!empty_stack())
208 unix_socket
*x
= pop_stack();
209 unix_socket
*f
=NULL
,*sk
;
212 skb
=skb_peek(&x
->receive_queue
);
215 * Loop through all but first born
218 while(skb
&& skb
!= (struct sk_buff
*)&x
->receive_queue
)
221 * Do we have file descriptors ?
226 * Process the descriptors of this socket
228 int nfd
=UNIXCB(skb
).fp
->count
;
229 struct file
**fp
= UNIXCB(skb
).fp
->fp
;
233 * Get the socket the fd matches if
236 if((sk
=unix_get_socket(*fp
++))!=NULL
)
239 * Remember the first, mark the
245 maybe_mark_and_push(sk
);
252 * Handle first born specially
257 if (!(f
->protinfo
.af_unix
.marksweep
&MARKED
))
259 f
->protinfo
.af_unix
.marksweep
|=MARKED
;
268 * Sweep phase. NOTE: this part dominates the time complexity
271 forall_unix_sockets(i
, s
)
274 if (!(s
->protinfo
.af_unix
.marksweep
&MARKED
))
277 * We exist only in the passing tree of sockets
278 * that is no longer connected to active descriptors
281 * Subtle item: We will correctly sweep out the
282 * socket that has just been closed by the user.
283 * We must not close this as we are in the middle
284 * of its close at this moment. Skip that file
285 * using f_count==0 to spot it.
288 if(s
->socket
&& s
->socket
->file
&& s
->socket
->file
->f_count
)
289 close_fp(s
->socket
->file
);
292 s
->protinfo
.af_unix
.marksweep
&=~MARKED
; /* unmark everything for next collection */
297 free_page((long)stack
);