Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/net Use kmem(9) instead of malloc/free



details:   https://anonhg.NetBSD.org/src/rev/c5a8fe3d3b5a
branches:  trunk
changeset: 351061:c5a8fe3d3b5a
user:      ozaki-r <ozaki-r%NetBSD.org@localhost>
date:      Wed Feb 01 08:07:27 2017 +0000

description:
Use kmem(9) instead of malloc/free

diffstat:

 sys/net/bpf.c     |  43 +++++++++++++++++++++++++------------------
 sys/net/bpfdesc.h |   3 ++-
 2 files changed, 27 insertions(+), 19 deletions(-)

diffs (162 lines):

diff -r e368807c5e8b -r c5a8fe3d3b5a sys/net/bpf.c
--- a/sys/net/bpf.c     Wed Feb 01 08:06:01 2017 +0000
+++ b/sys/net/bpf.c     Wed Feb 01 08:07:27 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bpf.c,v 1.207 2017/02/01 08:06:01 ozaki-r Exp $        */
+/*     $NetBSD: bpf.c,v 1.208 2017/02/01 08:07:27 ozaki-r Exp $        */
 
 /*
  * Copyright (c) 1990, 1991, 1993
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.207 2017/02/01 08:06:01 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.208 2017/02/01 08:07:27 ozaki-r Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_bpf.h"
@@ -446,7 +446,7 @@
        if ((error = fd_allocfile(&fp, &fd)) != 0)
                return error;
 
-       d = malloc(sizeof(*d), M_DEVBUF, M_WAITOK|M_ZERO);
+       d = kmem_zalloc(sizeof(*d), KM_SLEEP);
        d->bd_bufsize = bpf_bufsize;
        d->bd_seesent = 1;
        d->bd_feedback = 0;
@@ -511,7 +511,7 @@
        callout_destroy(&d->bd_callout);
        seldestroy(&d->bd_sel);
        softint_disestablish(d->bd_sih);
-       free(d, M_DEVBUF);
+       kmem_free(d, sizeof(*d));
 
        return (0);
 }
@@ -840,7 +840,11 @@
         * Set buffer length.
         */
        case BIOCSBLEN:
-               if (d->bd_bif != NULL)
+               /*
+                * Forbid to change the buffer length if buffers are already
+                * allocated.
+                */
+               if (d->bd_bif != NULL || d->bd_sbuf != NULL)
                        error = EINVAL;
                else {
                        u_int size = *(u_int *)addr;
@@ -1110,7 +1114,7 @@
 {
        struct bpf_insn *fcode, *old;
        bpfjit_func_t jcode, oldj;
-       size_t flen, size;
+       size_t flen, size = 0, old_size;
        int s;
 
        jcode = NULL;
@@ -1126,10 +1130,10 @@
                 * userspace and validate it.
                 */
                size = flen * sizeof(*fp->bf_insns);
-               fcode = malloc(size, M_DEVBUF, M_WAITOK);
+               fcode = kmem_alloc(size, KM_SLEEP);
                if (copyin(fp->bf_insns, fcode, size) != 0 ||
                    !bpf_validate(fcode, (int)flen)) {
-                       free(fcode, M_DEVBUF);
+                       kmem_free(fcode, size);
                        return EINVAL;
                }
                membar_consumer();
@@ -1139,16 +1143,19 @@
                fcode = NULL;
        }
 
+       old_size = d->bd_filter_size;
+
        s = splnet();
        old = d->bd_filter;
        d->bd_filter = fcode;
+       d->bd_filter_size = size;
        oldj = d->bd_jitcode;
        d->bd_jitcode = jcode;
        reset_d(d);
        splx(s);
 
        if (old) {
-               free(old, M_DEVBUF);
+               kmem_free(old, old_size);
        }
        if (oldj) {
                bpf_jit_freecode(oldj);
@@ -1822,12 +1829,12 @@
 bpf_allocbufs(struct bpf_d *d)
 {
 
-       d->bd_fbuf = malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
+       d->bd_fbuf = kmem_alloc(d->bd_bufsize, KM_NOSLEEP);
        if (!d->bd_fbuf)
                return (ENOBUFS);
-       d->bd_sbuf = malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
+       d->bd_sbuf = kmem_alloc(d->bd_bufsize, KM_NOSLEEP);
        if (!d->bd_sbuf) {
-               free(d->bd_fbuf, M_DEVBUF);
+               kmem_free(d->bd_fbuf, d->bd_bufsize);
                return (ENOBUFS);
        }
        d->bd_slen = 0;
@@ -1848,14 +1855,14 @@
         * free.
         */
        if (d->bd_sbuf != NULL) {
-               free(d->bd_sbuf, M_DEVBUF);
+               kmem_free(d->bd_sbuf, d->bd_bufsize);
                if (d->bd_hbuf != NULL)
-                       free(d->bd_hbuf, M_DEVBUF);
+                       kmem_free(d->bd_hbuf, d->bd_bufsize);
                if (d->bd_fbuf != NULL)
-                       free(d->bd_fbuf, M_DEVBUF);
+                       kmem_free(d->bd_fbuf, d->bd_bufsize);
        }
        if (d->bd_filter)
-               free(d->bd_filter, M_DEVBUF);
+               kmem_free(d->bd_filter, d->bd_filter_size);
 
        if (d->bd_jitcode != NULL) {
                bpf_jit_freecode(d->bd_jitcode);
@@ -1871,7 +1878,7 @@
 _bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
 {
        struct bpf_if *bp;
-       bp = malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
+       bp = kmem_alloc(sizeof(*bp), KM_NOSLEEP);
        if (bp == NULL)
                panic("bpfattach");
 
@@ -1957,7 +1964,7 @@
                                splx(s);
                                softint_disestablish(bp->bif_si);
                        }
-                       free(bp, M_DEVBUF);
+                       kmem_free(bp, sizeof(*bp));
                        goto again;
                }
        }
diff -r e368807c5e8b -r c5a8fe3d3b5a sys/net/bpfdesc.h
--- a/sys/net/bpfdesc.h Wed Feb 01 08:06:01 2017 +0000
+++ b/sys/net/bpfdesc.h Wed Feb 01 08:07:27 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bpfdesc.h,v 1.40 2017/01/24 09:05:28 ozaki-r Exp $     */
+/*     $NetBSD: bpfdesc.h,v 1.41 2017/02/01 08:07:27 ozaki-r Exp $     */
 
 /*
  * Copyright (c) 1990, 1991, 1993
@@ -102,6 +102,7 @@
        int             bd_compat32;    /* 32-bit stream on LP64 system */
 #endif
        bpfjit_func_t   bd_jitcode;     /* compiled filter program */
+       size_t          bd_filter_size;
 };
 
 



Home | Main Index | Thread Index | Old Index