Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | /* * NET3: Garbage Collector For AF_UNIX sockets * * Garbage Collector: * Copyright (C) Barak A. Pearlmutter. * Released under the GPL version 2 or later. * * 12/3/97 -- Flood * Internal stack is only allocated one page. On systems with NR_FILE * > 1024, this makes it quite easy for a user-space program to open * a large number of AF_UNIX domain sockets, causing the garbage * collection routines to run up against the wall (and panic). * Changed the MAX_STACK to be associated to the system-wide open file * maximum, and use vmalloc() instead of get_free_page() [as more than * one page may be necessary]. As noted below, this should ideally be * done with a linked list. * * Chopped about by Alan Cox 22/3/96 to make it fit the AF_UNIX socket problem. * If it doesn't work blame me, it worked when Barak sent it. * * Assumptions: * * - object w/ a bit * - free list * * Current optimizations: * * - explicit stack instead of recursion * - tail recurse on first born instead of immediate push/pop * * Future optimizations: * * - don't just push entire root set; process in place * - use linked list for internal stack * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * Fixes: * */ #include <linux/kernel.h> #include <linux/major.h> #include <linux/signal.h> #include <linux/sched.h> #include <linux/errno.h> #include <linux/string.h> #include <linux/stat.h> #include <linux/socket.h> #include <linux/un.h> #include <linux/fcntl.h> #include <linux/termios.h> #include <linux/socket.h> #include <linux/sockios.h> #include <linux/net.h> #include <linux/in.h> #include <linux/fs.h> #include <linux/malloc.h> #include <asm/segment.h> #include <linux/skbuff.h> #include <linux/netdevice.h> #include <net/sock.h> #include <net/tcp.h> #include <net/af_unix.h> #include <linux/proc_fs.h> /* Internal data structures and random procedures: */ static unix_socket **stack; /* stack of objects to mark */ static int in_stack = 0; /* first free entry in stack */ static int max_stack; /* Calculated in unix_gc() */ extern inline unix_socket *unix_get_socket(struct file *filp) { unix_socket * u_sock = NULL; struct inode *inode = filp->f_inode; /* * Socket ? */ if (inode && inode->i_sock) { struct socket * s = &inode->u.socket_i; /* * AF_UNIX ? */ if (s->ops == &unix_proto_ops) u_sock = s->data; } return u_sock; } /* * Keep the number of times in flight count for the file * descriptor if it is for an AF_UNIX socket. */ void unix_inflight(struct file *fp) { unix_socket *s=unix_get_socket(fp); if(s) s->protinfo.af_unix.inflight++; } void unix_notinflight(struct file *fp) { unix_socket *s=unix_get_socket(fp); if(s) s->protinfo.af_unix.inflight--; } /* * Garbage Collector Support Functions */ extern inline void push_stack(unix_socket *x) { if (in_stack == max_stack) panic("can't push onto full stack"); stack[in_stack++] = x; } extern inline unix_socket *pop_stack(void) { if (in_stack == 0) panic("can't pop empty gc stack"); return stack[--in_stack]; } extern inline int empty_stack(void) { return in_stack == 0; } extern inline void maybe_mark_and_push(unix_socket *x) { if (x->protinfo.af_unix.marksweep&MARKED) return; x->protinfo.af_unix.marksweep|=MARKED; push_stack(x); } /* The external entry point: unix_gc() */ void unix_gc(void) { static int in_unix_gc=0; unix_socket *s; unix_socket *next; /* * Avoid a recursive GC. */ if(in_unix_gc) return; in_unix_gc=1; max_stack = max_files; stack=(unix_socket **)vmalloc(max_stack * sizeof(unix_socket **)); if (!stack) { in_unix_gc=0; return; } /* * Assume everything is now unmarked */ /* Invariant to be maintained: - everything marked is either: -- (a) on the stack, or -- (b) has all of its children marked - everything on the stack is always marked - nothing is ever pushed onto the stack twice, because: -- nothing previously marked is ever pushed on the stack */ /* * Push root set */ for(s=unix_socket_list;s!=NULL;s=s->next) { /* * If all instances of the descriptor are not * in flight we are in use. */ if(s->socket && s->socket->file && s->socket->file->f_count > s->protinfo.af_unix.inflight) maybe_mark_and_push(s); } /* * Mark phase */ while (!empty_stack()) { unix_socket *x = pop_stack(); unix_socket *f=NULL,*sk; struct sk_buff *skb; tail: skb=skb_peek(&x->receive_queue); /* * Loop through all but first born */ while(skb && skb != (struct sk_buff *)&x->receive_queue) { /* * Do we have file descriptors ? */ if(skb->h.filp) { /* * Process the descriptors of this socket */ int nfd=*(int *)skb->h.filp; struct file **fp=(struct file **)(skb->h.filp+sizeof(int)); while(nfd--) { /* * Get the socket the fd matches if * it indeed does so */ if((sk=unix_get_socket(*fp++))!=NULL) { /* * Remember the first, mark the * rest. */ if(f==NULL) f=sk; else maybe_mark_and_push(sk); } } } skb=skb->next; } /* * Handle first born specially */ if (f) { if (!(f->protinfo.af_unix.marksweep&MARKED)) { f->protinfo.af_unix.marksweep|=MARKED; x=f; f=NULL; goto tail; } } } /* * Sweep phase. NOTE: this part dominates the time complexity */ for(s=unix_socket_list;s!=NULL;s=next) { next=s->next; if (!(s->protinfo.af_unix.marksweep&MARKED)) { /* * We exist only in the passing tree of sockets * that is no longer connected to active descriptors * Time to die.. * * Subtle item: We will correctly sweep out the * socket that has just been closed by the user. * We must not close this as we are in the middle * of its close at this moment. Skip that file * using f_count==0 to spot it. */ if(s->socket && s->socket->file && s->socket->file->f_count) close_fp(s->socket->file); } else s->protinfo.af_unix.marksweep&=~MARKED; /* unmark everything for next collection */ } in_unix_gc=0; vfree(stack); } |