Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 31 Dec 2017 04:09:40 +0000 (UTC)
From:      Mateusz Guzik <mjg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r327412 - stable/11/sys/kern
Message-ID:  <201712310409.vBV49eGs012498@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mjg
Date: Sun Dec 31 04:09:40 2017
New Revision: 327412
URL: https://svnweb.freebsd.org/changeset/base/327412

Log:
  MFC r325725:
  
      sysctl: try to avoid malloc in name2oid
  
      name2oid is called all the time and passed names are almost always very short
      (< 16 characters).

Modified:
  stable/11/sys/kern/kern_sysctl.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/sys/kern/kern_sysctl.c
==============================================================================
--- stable/11/sys/kern/kern_sysctl.c	Sun Dec 31 04:06:11 2017	(r327411)
+++ stable/11/sys/kern/kern_sysctl.c	Sun Dec 31 04:09:40 2017	(r327412)
@@ -1117,17 +1117,21 @@ sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
 	int error, oid[CTL_MAXNAME], len = 0;
 	struct sysctl_oid *op = NULL;
 	struct rm_priotracker tracker;
+	char buf[32];
 
 	if (!req->newlen) 
 		return (ENOENT);
 	if (req->newlen >= MAXPATHLEN)	/* XXX arbitrary, undocumented */
 		return (ENAMETOOLONG);
 
-	p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
+	p = buf;
+	if (req->newlen >= sizeof(buf))
+		p = malloc(req->newlen+1, M_SYSCTL, M_WAITOK);
 
 	error = SYSCTL_IN(req, p, req->newlen);
 	if (error) {
-		free(p, M_SYSCTL);
+		if (p != buf)
+			free(p, M_SYSCTL);
 		return (error);
 	}
 
@@ -1137,7 +1141,8 @@ sysctl_sysctl_name2oid(SYSCTL_HANDLER_ARGS)
 	error = name2oid(p, oid, &len, &op);
 	SYSCTL_RUNLOCK(&tracker);
 
-	free(p, M_SYSCTL);
+	if (p != buf)
+		free(p, M_SYSCTL);
 
 	if (error)
 		return (error);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201712310409.vBV49eGs012498>