Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 4 Aug 2018 22:15:05 +0000 (UTC)
From:      Kyle Evans <kevans@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: r337336 - in stable/11/sys: kern sys
Message-ID:  <201808042215.w74MF5uj062762@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevans
Date: Sat Aug  4 22:15:05 2018
New Revision: 337336
URL: https://svnweb.freebsd.org/changeset/base/337336

Log:
  MFC r336152-r336154, r336157
  
  r336152:
  subr_hints: Use goto/label instead of series of conditionals
  
  r336153:
  subr_hints: Convert some bool-like ints to bools
  
  r336154:
  subr_hints: Skip static_env and static_hints if they don't contain hints
  
  This is possible because, well, they're static. Both the dynamic environment
  and the MD-environment (generally loader(8) environment) can potentially
  have room for new variables to be set, and thus do not receive this
  treatment.
  
  r336157:
  kern_environment: bool'itize dynamic_kenv; fix small style(9) nit

Modified:
  stable/11/sys/kern/kern_environment.c
  stable/11/sys/kern/subr_hints.c
  stable/11/sys/sys/systm.h
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/sys/kern/kern_environment.c
==============================================================================
--- stable/11/sys/kern/kern_environment.c	Sat Aug  4 22:12:12 2018	(r337335)
+++ stable/11/sys/kern/kern_environment.c	Sat Aug  4 22:15:05 2018	(r337336)
@@ -37,7 +37,6 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include <sys/types.h>
 #include <sys/param.h>
 #include <sys/proc.h>
 #include <sys/queue.h>
@@ -79,7 +78,7 @@ struct mtx	kenv_lock;
 /*
  * No need to protect this with a mutex since SYSINITS are single threaded.
  */
-int	dynamic_kenv = 0;
+bool	dynamic_kenv;
 
 #define KENV_CHECK	if (!dynamic_kenv) \
 			    panic("%s: called before SI_SUB_KMEM", __func__)
@@ -98,7 +97,7 @@ sys_kenv(td, uap)
 	size_t len, done, needed, buflen;
 	int error, i;
 
-	KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
+	KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = false"));
 
 	error = 0;
 	if (uap->what == KENV_DUMP) {
@@ -352,7 +351,7 @@ init_dynamic_kenv(void *data __unused)
 	kenvp[dynamic_envpos] = NULL;
 
 	mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
-	dynamic_kenv = 1;
+	dynamic_kenv = true;
 }
 SYSINIT(kenv, SI_SUB_KMEM + 1, SI_ORDER_FIRST, init_dynamic_kenv, NULL);
 
@@ -506,7 +505,7 @@ kern_setenv(const char *name, const char *value)
 	char *buf, *cp, *oldenv;
 	int namelen, vallen, i;
 
-	if (dynamic_kenv == 0 && md_env_len > 0)
+	if (!dynamic_kenv && md_env_len > 0)
 		return (setenv_static(name, value));
 
 	KENV_CHECK;

Modified: stable/11/sys/kern/subr_hints.c
==============================================================================
--- stable/11/sys/kern/subr_hints.c	Sat Aug  4 22:12:12 2018	(r337335)
+++ stable/11/sys/kern/subr_hints.c	Sat Aug  4 22:15:05 2018	(r337336)
@@ -46,8 +46,10 @@ __FBSDID("$FreeBSD$");
  * has already been setup (dynamic_kenv) and that we have added any supplied
  * static_hints to the dynamic environment.
  */
-static int	hintenv_merged;
-
+static bool	hintenv_merged;
+/* Static environment and static hints cannot change, so we'll skip known bad */
+static bool	stenv_skip;
+static bool	sthints_skip;
 /*
  * Access functions for device resources.
  */
@@ -82,7 +84,7 @@ static_hints_to_env(void *data __unused)
 		free(line, M_TEMP);
 		cp += i + 1;
 	}
-	hintenv_merged = 1;
+	hintenv_merged = true;
 }
 
 /* Any time after dynamic env is setup */
@@ -122,13 +124,14 @@ res_find(char **hintp_cookie, int *line, int *startln,
     const char **ret_name, int *ret_namelen, int *ret_unit,
     const char **ret_resname, int *ret_resnamelen, const char **ret_value)
 {
-	int dyn_used = 0, fbacklvl = FBACK_MDENV, hit, i = 0, n = 0;
+	int fbacklvl = FBACK_MDENV, i = 0, n = 0;
 	char r_name[32];
 	int r_unit;
 	char r_resname[32];
 	char r_value[128];
 	const char *s, *cp;
 	char *hintp, *p;
+	bool dyn_used = false;
 
 
 	/*
@@ -155,7 +158,7 @@ res_find(char **hintp_cookie, int *line, int *startln,
 				}
 			}
 			mtx_unlock(&kenv_lock);
-			dyn_used = 1;
+			dyn_used = true;
 		} else {
 			/*
 			 * We'll have a chance to keep coming back here until
@@ -176,17 +179,21 @@ fallback:
 			}
 			fbacklvl++;
 
-			if (fbacklvl <= FBACK_STENV &&
+			if (!stenv_skip && fbacklvl <= FBACK_STENV &&
 			    _res_checkenv(kern_envp)) {
 				hintp = kern_envp;
 				goto found;
-			}
+			} else
+				stenv_skip = true;
+
 			fbacklvl++;
 
 			/* We'll fallback to static_hints if needed/can */
-			if (fbacklvl <= FBACK_STATIC &&
+			if (!sthints_skip && fbacklvl <= FBACK_STATIC &&
 			    _res_checkenv(static_hints))
 				hintp = static_hints;
+			else
+				sthints_skip = true;
 found:
 			fbacklvl++;
 		}
@@ -197,7 +204,7 @@ found:
 	} else {
 		hintp = *hintp_cookie;
 		if (hintenv_merged && hintp == kenvp[0])
-			dyn_used = 1;
+			dyn_used = true;
 		else
 			/*
 			 * If we aren't using the dynamic environment, we need
@@ -215,34 +222,30 @@ found:
 
 	cp = hintp;
 	while (cp) {
-		hit = 1;
 		(*line)++;
 		if (strncmp(cp, "hint.", 5) != 0)
-			hit = 0;
-		else
-			n = sscanf(cp, "hint.%32[^.].%d.%32[^=]=%127s",
-			    r_name, &r_unit, r_resname, r_value);
-		/* We'll circumvent all of the checks if we already know */
-		if (hit) {
-			if (n != 4) {
-				printf("CONFIG: invalid hint '%s'\n", cp);
-				p = strchr(cp, 'h');
-				*p = 'H';
-				hit = 0;
-			}
-			if (hit && startln && *startln >= 0 && *line < *startln)
-				hit = 0;
-			if (hit && name && strcmp(name, r_name) != 0)
-				hit = 0;
-			if (hit && unit && *unit != r_unit)
-				hit = 0;
-			if (hit && resname && strcmp(resname, r_resname) != 0)
-				hit = 0;
-			if (hit && value && strcmp(value, r_value) != 0)
-				hit = 0;
-			if (hit)
-				break;
+			goto nexthint;
+		n = sscanf(cp, "hint.%32[^.].%d.%32[^=]=%127s", r_name, &r_unit,
+		    r_resname, r_value);
+		if (n != 4) {
+			printf("CONFIG: invalid hint '%s'\n", cp);
+			p = strchr(cp, 'h');
+			*p = 'H';
+			goto nexthint;
 		}
+		if (startln && *startln >= 0 && *line < *startln)
+			goto nexthint;
+		if (name && strcmp(name, r_name) != 0)
+			goto nexthint;
+		if (unit && *unit != r_unit)
+			goto nexthint;
+		if (resname && strcmp(resname, r_resname) != 0)
+			goto nexthint;
+		if (value && strcmp(value, r_value) != 0)
+			goto nexthint;
+		/* Successfully found a hint matching all criteria */
+		break;
+nexthint:
 		if (dyn_used) {
 			cp = kenvp[++i];
 			if (cp == NULL)

Modified: stable/11/sys/sys/systm.h
==============================================================================
--- stable/11/sys/sys/systm.h	Sat Aug  4 22:12:12 2018	(r337335)
+++ stable/11/sys/sys/systm.h	Sat Aug  4 22:15:05 2018	(r337336)
@@ -148,7 +148,7 @@ void	kassert_panic(const char *fmt, ...)  __printflike
  * XXX most of these variables should be const.
  */
 extern int osreldate;
-extern int dynamic_kenv;
+extern bool dynamic_kenv;
 extern struct mtx kenv_lock;
 extern char *kern_envp;
 extern char *md_envp;



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