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(9).



details:   https://anonhg.NetBSD.org/src/rev/36b174705b48
branches:  trunk
changeset: 744366:36b174705b48
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Fri Jan 31 00:49:18 2020 +0000

description:
- Use kmem(9) instead of malloc(9).
- When handling SIOCGIFMEDIA, don't traverse the media list directly;
  refactor that out into a ifmedia_getwords() function.

diffstat:

 sys/net/if_media.c |  58 ++++++++++++++++++++++++++++-------------------------
 1 files changed, 31 insertions(+), 27 deletions(-)

diffs (127 lines):

diff -r fa6bf6157867 -r 36b174705b48 sys/net/if_media.c
--- a/sys/net/if_media.c        Fri Jan 31 00:24:51 2020 +0000
+++ b/sys/net/if_media.c        Fri Jan 31 00:49:18 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_media.c,v 1.49 2020/01/20 19:35:39 thorpej Exp $    */
+/*     $NetBSD: if_media.c,v 1.50 2020/01/31 00:49:18 thorpej Exp $    */
 
 /*-
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -76,14 +76,14 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_media.c,v 1.49 2020/01/20 19:35:39 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_media.c,v 1.50 2020/01/31 00:49:18 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/errno.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
-#include <sys/malloc.h>
+#include <sys/kmem.h>
 
 #include <net/if.h>
 #include <net/if_media.h>
@@ -106,8 +106,6 @@
 static void ifmedia_printword(int);
 #endif
 
-MALLOC_DEFINE(M_IFMEDIA, "ifmedia", "interface media state");
-
 /*
  * Initialize if_media struct for a specific interface instance.
  */
@@ -162,7 +160,7 @@
        }
 #endif
 
-       entry = malloc(sizeof(*entry), M_IFMEDIA, M_WAITOK);
+       entry = kmem_zalloc(sizeof(*entry), KM_SLEEP);
        entry->ifm_media = mword;
        entry->ifm_data = data;
        entry->ifm_aux = aux;
@@ -235,6 +233,22 @@
 #endif
 }
 
+static int
+ifmedia_getwords(struct ifmedia * const ifm, int *words, int maxwords)
+{
+       struct ifmedia_entry *ep;
+       int nwords = 0;
+
+       TAILQ_FOREACH(ep, &ifm->ifm_list, ifm_list) {
+               if (words != NULL && nwords < maxwords) {
+                       words[nwords] = ep->ifm_media;
+               }
+               nwords++;
+       }
+
+       return nwords;
+}
+
 /*
  * Device-independent media ioctl support function.
  */
@@ -304,8 +318,7 @@
        /* Get list of available media and current media on interface. */
        case SIOCGIFMEDIA:
        {
-               struct ifmedia_entry *ep;
-               size_t nwords;
+               int nwords1, nwords2;
 
                if (ifmr->ifm_count < 0)
                        return EINVAL;
@@ -320,31 +333,22 @@
                 * Count them so we know a-priori how much is the max we'll
                 * need.
                 */
-               ep = TAILQ_FIRST(&ifm->ifm_list);
-               for (nwords = 0; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list))
-                       nwords++;
+               nwords1 = nwords2 = ifmedia_getwords(ifm, NULL, 0);
 
                if (ifmr->ifm_count != 0) {
-                       size_t count;
-                       size_t minwords = nwords > (size_t)ifmr->ifm_count
-                           ? (size_t)ifmr->ifm_count : nwords;
-                       int *kptr = malloc(minwords * sizeof(int), M_TEMP,
-                           M_WAITOK);
+                       int maxwords = MIN(nwords1, ifmr->ifm_count);
+                       int *kptr = kmem_zalloc(maxwords * sizeof(int),
+                           KM_SLEEP);
 
-                       /* Get the media words from the interface's list. */
-                       ep = TAILQ_FIRST(&ifm->ifm_list);
-                       for (count = 0; ep != NULL && count < minwords;
-                           ep = TAILQ_NEXT(ep, ifm_list), count++)
-                               kptr[count] = ep->ifm_media;
-
+                       nwords2 = ifmedia_getwords(ifm, kptr, maxwords);
                        error = copyout(kptr, ifmr->ifm_ulist,
-                           minwords * sizeof(int));
-                       if (error == 0 && ep != NULL)
+                           maxwords * sizeof(int));
+                       if (error == 0 && nwords2 > nwords1)
                                error = E2BIG;  /* oops! */
-                       free(kptr, M_TEMP);
+                       kmem_free(kptr, maxwords * sizeof(int));
                }
                /* Update with the real number */
-               ifmr->ifm_count = nwords;
+               ifmr->ifm_count = nwords2;
                break;
        }
 
@@ -420,7 +424,7 @@
                if (inst == IFM_INST_ANY ||
                    inst == IFM_INST(ife->ifm_media)) {
                        TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
-                       free(ife, M_IFMEDIA);
+                       kmem_free(ife, sizeof(*ife));
                }
        }
        if (inst == IFM_INST_ANY) {



Home | Main Index | Thread Index | Old Index