subr_hash.c revision 112631
1/*
2 * Copyright (c) 1982, 1986, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)kern_subr.c	8.3 (Berkeley) 1/21/94
39 * $FreeBSD: head/sys/kern/kern_subr.c 112631 2003-03-25 20:13:24Z jhb $
40 */
41
42#include "opt_zero.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/ktr.h>
48#include <sys/lock.h>
49#include <sys/mutex.h>
50#include <sys/proc.h>
51#include <sys/malloc.h>
52#include <sys/resourcevar.h>
53#include <sys/sched.h>
54#include <sys/sysctl.h>
55#include <sys/vnode.h>
56
57#include <vm/vm.h>
58#include <vm/vm_page.h>
59#include <vm/vm_map.h>
60#ifdef ZERO_COPY_SOCKETS
61#include <vm/vm_param.h>
62#include <vm/vm_object.h>
63#endif
64
65SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
66	"Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
67
68#ifdef ZERO_COPY_SOCKETS
69/* Declared in uipc_socket.c */
70extern int so_zero_copy_receive;
71
72static int
73vm_pgmoveco(vm_map_t mapa, vm_object_t srcobj, vm_offset_t kaddr,
74    vm_offset_t uaddr)
75{
76	vm_map_t map = mapa;
77	vm_page_t kern_pg, user_pg;
78	vm_object_t uobject;
79	vm_map_entry_t entry;
80	vm_pindex_t upindex, kpindex;
81	vm_prot_t prot;
82	boolean_t wired;
83
84	/*
85	 * First lookup the kernel page.
86	 */
87	kern_pg = PHYS_TO_VM_PAGE(vtophys(kaddr));
88
89	if ((vm_map_lookup(&map, uaddr,
90			   VM_PROT_WRITE, &entry, &uobject,
91			   &upindex, &prot, &wired)) != KERN_SUCCESS) {
92		return(EFAULT);
93	}
94	if ((user_pg = vm_page_lookup(uobject, upindex)) != NULL) {
95		do
96			vm_page_lock_queues();
97		while (vm_page_sleep_if_busy(user_pg, 1, "vm_pgmoveco"));
98		vm_page_busy(user_pg);
99		pmap_remove_all(user_pg);
100		vm_page_free(user_pg);
101	} else
102		vm_page_lock_queues();
103	if (kern_pg->busy || ((kern_pg->queue - kern_pg->pc) == PQ_FREE) ||
104	    (kern_pg->hold_count != 0)|| (kern_pg->flags & PG_BUSY)) {
105		printf("vm_pgmoveco: pindex(%lu), busy(%d), PG_BUSY(%d), "
106		       "hold(%d) paddr(0x%lx)\n", (u_long)kern_pg->pindex,
107			kern_pg->busy, (kern_pg->flags & PG_BUSY) ? 1 : 0,
108			kern_pg->hold_count, (u_long)kern_pg->phys_addr);
109		if ((kern_pg->queue - kern_pg->pc) == PQ_FREE)
110			panic("vm_pgmoveco: renaming free page");
111		else
112			panic("vm_pgmoveco: renaming busy page");
113	}
114	kpindex = kern_pg->pindex;
115	vm_page_busy(kern_pg);
116	vm_page_rename(kern_pg, uobject, upindex);
117	vm_page_flag_clear(kern_pg, PG_BUSY);
118	kern_pg->valid = VM_PAGE_BITS_ALL;
119	vm_page_unlock_queues();
120
121	vm_map_lookup_done(map, entry);
122	return(KERN_SUCCESS);
123}
124#endif /* ZERO_COPY_SOCKETS */
125
126int
127uiomove(void *cp, int n, struct uio *uio)
128{
129	struct thread *td = curthread;
130	struct iovec *iov;
131	u_int cnt;
132	int error = 0;
133	int save = 0;
134
135	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
136	    ("uiomove: mode"));
137	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
138	    ("uiomove proc"));
139
140	if (td) {
141		mtx_lock_spin(&sched_lock);
142		save = td->td_flags & TDF_DEADLKTREAT;
143		td->td_flags |= TDF_DEADLKTREAT;
144		mtx_unlock_spin(&sched_lock);
145	}
146
147	while (n > 0 && uio->uio_resid) {
148		iov = uio->uio_iov;
149		cnt = iov->iov_len;
150		if (cnt == 0) {
151			uio->uio_iov++;
152			uio->uio_iovcnt--;
153			continue;
154		}
155		if (cnt > n)
156			cnt = n;
157
158		switch (uio->uio_segflg) {
159
160		case UIO_USERSPACE:
161			if (ticks - PCPU_GET(switchticks) >= hogticks)
162				uio_yield();
163			if (uio->uio_rw == UIO_READ)
164				error = copyout(cp, iov->iov_base, cnt);
165			else
166				error = copyin(iov->iov_base, cp, cnt);
167			if (error)
168				goto out;
169			break;
170
171		case UIO_SYSSPACE:
172			if (uio->uio_rw == UIO_READ)
173				bcopy(cp, iov->iov_base, cnt);
174			else
175				bcopy(iov->iov_base, cp, cnt);
176			break;
177		case UIO_NOCOPY:
178			break;
179		}
180		iov->iov_base = (char *)iov->iov_base + cnt;
181		iov->iov_len -= cnt;
182		uio->uio_resid -= cnt;
183		uio->uio_offset += cnt;
184		cp = (char *)cp + cnt;
185		n -= cnt;
186	}
187out:
188	if (td) {
189		mtx_lock_spin(&sched_lock);
190		td->td_flags = (td->td_flags & ~TDF_DEADLKTREAT) | save;
191		mtx_unlock_spin(&sched_lock);
192	}
193	return (error);
194}
195
196#ifdef ZERO_COPY_SOCKETS
197/*
198 * Experimental support for zero-copy I/O
199 */
200static int
201userspaceco(void *cp, u_int cnt, struct uio *uio, struct vm_object *obj,
202    int disposable)
203{
204	struct iovec *iov;
205	int error;
206
207	iov = uio->uio_iov;
208	if (uio->uio_rw == UIO_READ) {
209		if ((so_zero_copy_receive != 0)
210		 && (obj != NULL)
211		 && ((cnt & PAGE_MASK) == 0)
212		 && ((((intptr_t) iov->iov_base) & PAGE_MASK) == 0)
213		 && ((uio->uio_offset & PAGE_MASK) == 0)
214		 && ((((intptr_t) cp) & PAGE_MASK) == 0)
215		 && (obj->type == OBJT_DEFAULT)
216		 && (disposable != 0)) {
217			/* SOCKET: use page-trading */
218			/*
219			 * We only want to call vm_pgmoveco() on
220			 * disposeable pages, since it gives the
221			 * kernel page to the userland process.
222			 */
223			error =	vm_pgmoveco(&curproc->p_vmspace->vm_map,
224					    obj, (vm_offset_t)cp,
225					    (vm_offset_t)iov->iov_base);
226
227			/*
228			 * If we get an error back, attempt
229			 * to use copyout() instead.  The
230			 * disposable page should be freed
231			 * automatically if we weren't able to move
232			 * it into userland.
233			 */
234			if (error != 0)
235				error = copyout(cp, iov->iov_base, cnt);
236		} else {
237			error = copyout(cp, iov->iov_base, cnt);
238		}
239	} else {
240		error = copyin(iov->iov_base, cp, cnt);
241	}
242	return (error);
243}
244
245int
246uiomoveco(void *cp, int n, struct uio *uio, struct vm_object *obj,
247    int disposable)
248{
249	struct iovec *iov;
250	u_int cnt;
251	int error;
252
253	KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
254	    ("uiomoveco: mode"));
255	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
256	    ("uiomoveco proc"));
257
258	while (n > 0 && uio->uio_resid) {
259		iov = uio->uio_iov;
260		cnt = iov->iov_len;
261		if (cnt == 0) {
262			uio->uio_iov++;
263			uio->uio_iovcnt--;
264			continue;
265		}
266		if (cnt > n)
267			cnt = n;
268
269		switch (uio->uio_segflg) {
270
271		case UIO_USERSPACE:
272			if (ticks - PCPU_GET(switchticks) >= hogticks)
273				uio_yield();
274
275			error = userspaceco(cp, cnt, uio, obj, disposable);
276
277			if (error)
278				return (error);
279			break;
280
281		case UIO_SYSSPACE:
282			if (uio->uio_rw == UIO_READ)
283				bcopy(cp, iov->iov_base, cnt);
284			else
285				bcopy(iov->iov_base, cp, cnt);
286			break;
287		case UIO_NOCOPY:
288			break;
289		}
290		iov->iov_base = (char *)iov->iov_base + cnt;
291		iov->iov_len -= cnt;
292		uio->uio_resid -= cnt;
293		uio->uio_offset += cnt;
294		cp = (char *)cp + cnt;
295		n -= cnt;
296	}
297	return (0);
298}
299#endif /* ZERO_COPY_SOCKETS */
300
301/*
302 * Give next character to user as result of read.
303 */
304int
305ureadc(int c, struct uio *uio)
306{
307	struct iovec *iov;
308	char *iov_base;
309
310again:
311	if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
312		panic("ureadc");
313	iov = uio->uio_iov;
314	if (iov->iov_len == 0) {
315		uio->uio_iovcnt--;
316		uio->uio_iov++;
317		goto again;
318	}
319	switch (uio->uio_segflg) {
320
321	case UIO_USERSPACE:
322		if (subyte(iov->iov_base, c) < 0)
323			return (EFAULT);
324		break;
325
326	case UIO_SYSSPACE:
327		iov_base = iov->iov_base;
328		*iov_base = c;
329		iov->iov_base = iov_base;
330		break;
331
332	case UIO_NOCOPY:
333		break;
334	}
335	iov->iov_base = (char *)iov->iov_base + 1;
336	iov->iov_len--;
337	uio->uio_resid--;
338	uio->uio_offset++;
339	return (0);
340}
341
342/*
343 * General routine to allocate a hash table.
344 */
345void *
346hashinit(int elements, struct malloc_type *type, u_long *hashmask)
347{
348	long hashsize;
349	LIST_HEAD(generic, generic) *hashtbl;
350	int i;
351
352	if (elements <= 0)
353		panic("hashinit: bad elements");
354	for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
355		continue;
356	hashsize >>= 1;
357	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
358	for (i = 0; i < hashsize; i++)
359		LIST_INIT(&hashtbl[i]);
360	*hashmask = hashsize - 1;
361	return (hashtbl);
362}
363
364void
365hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
366{
367	LIST_HEAD(generic, generic) *hashtbl, *hp;
368
369	hashtbl = vhashtbl;
370	for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++)
371		if (!LIST_EMPTY(hp))
372			panic("hashdestroy: hash not empty");
373	free(hashtbl, type);
374}
375
376static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
377			2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
378			7159, 7673, 8191, 12281, 16381, 24571, 32749 };
379#define NPRIMES (sizeof(primes) / sizeof(primes[0]))
380
381/*
382 * General routine to allocate a prime number sized hash table.
383 */
384void *
385phashinit(int elements, struct malloc_type *type, u_long *nentries)
386{
387	long hashsize;
388	LIST_HEAD(generic, generic) *hashtbl;
389	int i;
390
391	if (elements <= 0)
392		panic("phashinit: bad elements");
393	for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
394		i++;
395		if (i == NPRIMES)
396			break;
397		hashsize = primes[i];
398	}
399	hashsize = primes[i - 1];
400	hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
401	for (i = 0; i < hashsize; i++)
402		LIST_INIT(&hashtbl[i]);
403	*nentries = hashsize;
404	return (hashtbl);
405}
406
407void
408uio_yield(void)
409{
410	struct thread *td;
411
412	td = curthread;
413	mtx_lock_spin(&sched_lock);
414	DROP_GIANT();
415	sched_prio(td, td->td_ksegrp->kg_user_pri); /* XXXKSE */
416	td->td_proc->p_stats->p_ru.ru_nivcsw++;
417	mi_switch();
418	mtx_unlock_spin(&sched_lock);
419	PICKUP_GIANT();
420}
421
422int
423copyinfrom(const void *src, void *dst, size_t len, int seg)
424{
425	int error = 0;
426
427	switch (seg) {
428	case UIO_USERSPACE:
429		error = copyin(src, dst, len);
430		break;
431	case UIO_SYSSPACE:
432		bcopy(src, dst, len);
433		break;
434	default:
435		panic("copyinfrom: bad seg %d\n", seg);
436	}
437	return (error);
438}
439
440int
441copyinstrfrom(const void *src, void *dst, size_t len, size_t *copied, int seg)
442{
443	int error = 0;
444
445	switch (seg) {
446	case UIO_USERSPACE:
447		error = copyinstr(src, dst, len, copied);
448		break;
449	case UIO_SYSSPACE:
450		error = copystr(src, dst, len, copied);
451		break;
452	default:
453		panic("copyinstrfrom: bad seg %d\n", seg);
454	}
455	return (error);
456}
457