On Sat, 08 Jul 2023 21:58:30 +0300 (EEST) YASUOKA Masahiko <yasu...@openbsd.org> wrote: > The diff makes the mbstat be the same size which is actually used. > Also revert the previous that the mbstat is located on the stack.
The userland program also needed to be changed. ok? Index: sys/kern/kern_sysctl.c =================================================================== RCS file: /cvs/src/sys/kern/kern_sysctl.c,v retrieving revision 1.417 diff -u -p -r1.417 kern_sysctl.c --- sys/kern/kern_sysctl.c 7 Jul 2023 16:27:46 -0000 1.417 +++ sys/kern/kern_sysctl.c 9 Jul 2023 07:22:58 -0000 @@ -515,22 +515,20 @@ 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; - mbs = malloc(sizeof(*mbs), M_TEMP, M_WAITOK | M_ZERO); + memset(&mbs, 0, sizeof(mbs)); 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]; - ret = sysctl_rdstruct(oldp, oldlenp, newp, mbs, sizeof(*mbs)); - free(mbs, M_TEMP, sizeof(*mbs)); - return (ret); + return (sysctl_rdstruct(oldp, oldlenp, newp, + &mbs, sizeof(mbs))); } case KERN_MSGBUFSIZE: case KERN_CONSBUFSIZE: { Index: sys/sys/mbuf.h =================================================================== RCS file: /cvs/src/sys/sys/mbuf.h,v retrieving revision 1.260 diff -u -p -r1.260 mbuf.h --- sys/sys/mbuf.h 7 Jul 2023 14:17:34 -0000 1.260 +++ sys/sys/mbuf.h 9 Jul 2023 07:22:58 -0000 @@ -363,6 +363,12 @@ u_int mextfree_register(void (*)(caddr_t /* length to m_copy to copy all */ #define M_COPYALL 1000000000 +#define MBSTAT_TYPES MT_NTYPES +#define MBSTAT_DROPS (MBSTAT_TYPES + 0) +#define MBSTAT_WAIT (MBSTAT_TYPES + 1) +#define MBSTAT_DRAIN (MBSTAT_TYPES + 2) +#define MBSTAT_COUNT (MBSTAT_TYPES + 3) + /* * Mbuf statistics. * For statistics related to mbuf and cluster allocations, see also the @@ -372,14 +378,9 @@ 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_long m_mtypes[256]; /* type specific mbuf allocations */ + u_long m_mtypes[MBSTAT_COUNT]; + /* type specific mbuf allocations */ }; - -#define MBSTAT_TYPES MT_NTYPES -#define MBSTAT_DROPS (MBSTAT_TYPES + 0) -#define MBSTAT_WAIT (MBSTAT_TYPES + 1) -#define MBSTAT_DRAIN (MBSTAT_TYPES + 2) -#define MBSTAT_COUNT (MBSTAT_TYPES + 3) #include <sys/mutex.h> Index: usr.bin/netstat/mbuf.c =================================================================== RCS file: /cvs/src/usr.bin/netstat/mbuf.c,v retrieving revision 1.44 diff -u -p -r1.44 mbuf.c --- usr.bin/netstat/mbuf.c 7 Jul 2023 14:17:35 -0000 1.44 +++ usr.bin/netstat/mbuf.c 9 Jul 2023 07:22:58 -0000 @@ -93,7 +93,7 @@ mbpr(void) struct mbtypes *mp; size_t size; - if (nmbtypes != 256) { + if (nmbtypes != MBSTAT_COUNT) { fprintf(stderr, "%s: unexpected change to mbstat; check source\n", __progname);