Hi,

I'd like to expand the counters in struct mbstat from u_short to u_long.

When I was debugging a mbuf leak, I saw the result of "netstat -m"
---
28647 mbufs in use:
        28551 mbufs allocated to data
        4 mbufs allocated to packet headers
        92 mbufs allocated to socket names and addresses
159506/160736 mbuf 2048 byte clusters in use (current/peak)
0/30 mbuf 2112 byte clusters in use (current/peak)
0/24 mbuf 4096 byte clusters in use (current/peak)
0/24 mbuf 8192 byte clusters in use (current/peak)
0/0 mbuf 9216 byte clusters in use (current/peak)
0/0 mbuf 12288 byte clusters in use (current/peak)
0/16 mbuf 16384 byte clusters in use (current/peak)
0/0 mbuf 65536 byte clusters in use (current/peak)
360980/362484/2097152 Kbytes allocated to network (current/peak/max)
0 requests for memory denied
0 requests for memory delayed
0 calls to protocol drain routines
---

I couldn't figure out why mcl2k is leaked without leaking mbuf for few
days.  Actually it was shown in u_short (actual number % 65535).


ok? comments?

Index: sys/sys/mbuf.h
===================================================================
RCS file: /disk/cvs/openbsd/src/sys/sys/mbuf.h,v
retrieving revision 1.259
diff -u -p -r1.259 mbuf.h
--- sys/sys/mbuf.h      4 Jul 2023 09:47:51 -0000       1.259
+++ sys/sys/mbuf.h      6 Jul 2023 14:36:15 -0000
@@ -372,7 +372,7 @@ struct mbstat {
        u_long  m_drops;        /* times failed to find space */
        u_long  m_wait;         /* times waited for space */
        u_long  m_drain;        /* times drained protocols for space */
-       u_short m_mtypes[256];  /* type specific mbuf allocations */
+       u_long  m_mtypes[256];  /* type specific mbuf allocations */
 };
 
 #define MBSTAT_TYPES           MT_NTYPES
Index: sys/kern/kern_sysctl.c
===================================================================
RCS file: /disk/cvs/openbsd/src/sys/kern/kern_sysctl.c,v
retrieving revision 1.416
diff -u -p -r1.416 kern_sysctl.c
--- sys/kern/kern_sysctl.c      2 Jul 2023 19:02:27 -0000       1.416
+++ sys/kern/kern_sysctl.c      6 Jul 2023 14:36:15 -0000
@@ -515,20 +515,22 @@ kern_sysctl(int *name, u_int namelen, vo
        case KERN_MBSTAT: {
                extern struct cpumem *mbstat;
                uint64_t counters[MBSTAT_COUNT];
-               struct mbstat mbs;
+               struct mbstat *mbs;
                unsigned int i;
+               int ret;
 
-               memset(&mbs, 0, sizeof(mbs));
+               mbs = malloc(sizeof(*mbs), M_TEMP, M_WAITOK | M_ZERO);
                counters_read(mbstat, counters, MBSTAT_COUNT);
                for (i = 0; i < MBSTAT_TYPES; i++)
-                       mbs.m_mtypes[i] = counters[i];
+                       mbs->m_mtypes[i] = counters[i];
 
-               mbs.m_drops = counters[MBSTAT_DROPS];
-               mbs.m_wait = counters[MBSTAT_WAIT];
-               mbs.m_drain = counters[MBSTAT_DRAIN];
+               mbs->m_drops = counters[MBSTAT_DROPS];
+               mbs->m_wait = counters[MBSTAT_WAIT];
+               mbs->m_drain = counters[MBSTAT_DRAIN];
 
-               return (sysctl_rdstruct(oldp, oldlenp, newp,
-                   &mbs, sizeof(mbs)));
+               ret = sysctl_rdstruct(oldp, oldlenp, newp, mbs, sizeof(*mbs));
+               free(mbs, M_TEMP, sizeof(*mbs));
+               return (ret);
        }
        case KERN_MSGBUFSIZE:
        case KERN_CONSBUFSIZE: {
Index: usr.bin/netstat/mbuf.c
===================================================================
RCS file: /disk/cvs/openbsd/src/usr.bin/netstat/mbuf.c,v
retrieving revision 1.43
diff -u -p -r1.43 mbuf.c
--- usr.bin/netstat/mbuf.c      16 Jul 2019 17:39:02 -0000      1.43
+++ usr.bin/netstat/mbuf.c      6 Jul 2023 14:36:15 -0000
@@ -78,7 +78,7 @@ static struct mbtypes {
        { 0, 0 }
 };
 
-int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
+int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(u_long);
 bool seen[256];                        /* "have we seen this type yet?" */
 
 /*
@@ -172,7 +172,7 @@ mbpr(void)
        for (mp = mbtypes; mp->mt_name; mp++)
                if (mbstat.m_mtypes[mp->mt_type]) {
                        seen[mp->mt_type] = YES;
-                       printf("\t%u mbuf%s allocated to %s\n",
+                       printf("\t%lu mbuf%s allocated to %s\n",
                            mbstat.m_mtypes[mp->mt_type],
                            plural(mbstat.m_mtypes[mp->mt_type]),
                            mp->mt_name);
@@ -180,7 +180,7 @@ mbpr(void)
        seen[MT_FREE] = YES;
        for (i = 0; i < nmbtypes; i++)
                if (!seen[i] && mbstat.m_mtypes[i]) {
-                       printf("\t%u mbuf%s allocated to <mbuf type %d>\n",
+                       printf("\t%lu mbuf%s allocated to <mbuf type %d>\n",
                            mbstat.m_mtypes[i],
                            plural(mbstat.m_mtypes[i]), i);
                }

Reply via email to