From owner-svn-src-all@freebsd.org Tue May 3 07:17:15 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 236D3B2BA73; Tue, 3 May 2016 07:17:15 +0000 (UTC) (envelope-from sephe@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id D77F31371; Tue, 3 May 2016 07:17:14 +0000 (UTC) (envelope-from sephe@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u437HE4i029198; Tue, 3 May 2016 07:17:14 GMT (envelope-from sephe@FreeBSD.org) Received: (from sephe@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u437HDtc029195; Tue, 3 May 2016 07:17:13 GMT (envelope-from sephe@FreeBSD.org) Message-Id: <201605030717.u437HDtc029195@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: sephe set sender to sephe@FreeBSD.org using -f From: Sepherosa Ziehau Date: Tue, 3 May 2016 07:17:13 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r298956 - in head: share/man/man9 sys/kern sys/sys X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 May 2016 07:17:15 -0000 Author: sephe Date: Tue May 3 07:17:13 2016 New Revision: 298956 URL: https://svnweb.freebsd.org/changeset/base/298956 Log: kern: Add phashinit_flags(), which allows malloc(M_NOWAIT) It will be used for the upcoming LRO hash table initialization. And probably will be useful in other cases, when M_WAITOK can't be used. Reviewed by: jhb, kib Sponsored by: Microsoft OSTC Differential Revision: https://reviews.freebsd.org/D6138 Modified: head/share/man/man9/hashinit.9 head/sys/kern/subr_hash.c head/sys/sys/systm.h Modified: head/share/man/man9/hashinit.9 ============================================================================== --- head/share/man/man9/hashinit.9 Tue May 3 03:41:25 2016 (r298955) +++ head/share/man/man9/hashinit.9 Tue May 3 07:17:13 2016 (r298956) @@ -25,11 +25,11 @@ .\" .\" $FreeBSD$ .\" -.Dd January 23, 2016 +.Dd April 29, 2016 .Dt HASHINIT 9 .Os .Sh NAME -.Nm hashinit , hashinit_flags , hashdestroy , phashinit +.Nm hashinit , hashinit_flags , hashdestroy , phashinit , phashinit_flags .Nd manage kernel hash tables .Sh SYNOPSIS .In sys/malloc.h @@ -45,12 +45,14 @@ .Fn hashdestroy "void *hashtbl" "struct malloc_type *type" "u_long hashmask" .Ft "void *" .Fn phashinit "int nelements" "struct malloc_type *type" "u_long *nentries" +.Fn phashinit_flags "int nelements" "struct malloc_type *type" "u_long *nentries" "int flags" .Sh DESCRIPTION The .Fn hashinit , -.Fn hashinit_flags -and +.Fn hashinit_flags , .Fn phashinit +and +.Fn phashinit_flags functions allocate space for hash tables of size given by the argument .Fa nelements . .Pp @@ -71,6 +73,12 @@ function operates like but also accepts an additional argument .Fa flags which control various options during allocation. +.Fn phashinit_flags +function operates like +.Fn phashinit +but also accepts an additional argument +.Fa flags +which control various options during allocation. Allocated hash tables are contiguous arrays of .Xr LIST_HEAD 3 entries, allocated using @@ -100,13 +108,19 @@ must be used with one of the following v .It Dv HASH_NOWAIT Any malloc performed by the .Fn hashinit_flags +and +.Fn phashinit_flags function will not be allowed to wait, and therefore may fail. .It Dv HASH_WAITOK Any malloc performed by .Fn hashinit_flags +and +.Fn phashinit_flags function is allowed to wait for memory. This is also the behavior of -.Fn hashinit . +.Fn hashinit +and +.Fn phashinit . .El .Sh IMPLEMENTATION NOTES The largest prime hash value chosen by Modified: head/sys/kern/subr_hash.c ============================================================================== --- head/sys/kern/subr_hash.c Tue May 3 03:41:25 2016 (r298955) +++ head/sys/kern/subr_hash.c Tue May 3 07:17:13 2016 (r298956) @@ -104,16 +104,21 @@ static const int primes[] = { 1, 13, 31, #define NPRIMES nitems(primes) /* - * General routine to allocate a prime number sized hash table. + * General routine to allocate a prime number sized hash table with control of + * memory flags. */ void * -phashinit(int elements, struct malloc_type *type, u_long *nentries) +phashinit_flags(int elements, struct malloc_type *type, u_long *nentries, int flags) { long hashsize; LIST_HEAD(generic, generic) *hashtbl; - int i; + int i, m_flags; KASSERT(elements > 0, ("%s: bad elements", __func__)); + /* Exactly one of HASH_WAITOK and HASH_NOWAIT must be set. */ + KASSERT((flags & HASH_WAITOK) ^ (flags & HASH_NOWAIT), + ("Bad flags (0x%x) passed to phashinit_flags", flags)); + for (i = 1, hashsize = primes[1]; hashsize <= elements;) { i++; if (i == NPRIMES) @@ -121,9 +126,25 @@ phashinit(int elements, struct malloc_ty hashsize = primes[i]; } hashsize = primes[i - 1]; - hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK); + + m_flags = (flags & HASH_NOWAIT) ? M_NOWAIT : M_WAITOK; + hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, m_flags); + if (hashtbl == NULL) + return (NULL); + for (i = 0; i < hashsize; i++) LIST_INIT(&hashtbl[i]); *nentries = hashsize; return (hashtbl); } + +/* + * Allocate and initialize a prime number sized hash table with default flag: + * may sleep. + */ +void * +phashinit(int elements, struct malloc_type *type, u_long *nentries) +{ + + return (phashinit_flags(elements, type, nentries, HASH_WAITOK)); +} Modified: head/sys/sys/systm.h ============================================================================== --- head/sys/sys/systm.h Tue May 3 03:41:25 2016 (r298955) +++ head/sys/sys/systm.h Tue May 3 07:17:13 2016 (r298956) @@ -189,6 +189,8 @@ void *hashinit_flags(int count, struct m #define HASH_WAITOK 0x00000002 void *phashinit(int count, struct malloc_type *type, u_long *nentries); +void *phashinit_flags(int count, struct malloc_type *type, u_long *nentries, + int flags); void g_waitidle(void); void panic(const char *, ...) __dead2 __printflike(1, 2);