svn commit: r368324 - stable/12/sys/kern

Kyle Evans kevans at FreeBSD.org
Fri Dec 4 02:28:46 UTC 2020


Author: kevans
Date: Fri Dec  4 02:28:45 2020
New Revision: 368324
URL: https://svnweb.freebsd.org/changeset/base/368324

Log:
  MFC r368009-r368010: kern: cpuset: minor improvements
  
  r368009:
  kern: cpuset: allow cpuset_create() to take an allocated *setp
  
  Currently, it must always allocate a new set to be used for passing to
  _cpuset_create, but it doesn't have to. This is purely kern_cpuset.c
  internal and it's sparsely used, so just change it to use *setp if it's
  not-NULL and modify the two consumers to pass in the address of a NULL
  cpuset.
  
  This paves the way for consumers that want the unr allocation without the
  possibility of sleeping as long as they've done their due diligence to
  ensure that the mask will properly apply atop the supplied parent
  (i.e. avoiding the free_unr() in the last failure path).
  
  r368010:
  kern: cpuset: rename _cpuset_create() to cpuset_init()
  
  cpuset_init() is better descriptor for what the function actually does. The
  name was previously taken by a sysinit that setup cpuset_zero's mask
  from all_cpus, it was removed in r331698 before stable/12 branched.
  
  A comment referencing the removed sysinit has now also been removed, since
  the setup previously done was moved into cpuset_thread0().

Modified:
  stable/12/sys/kern/kern_cpuset.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sys/kern/kern_cpuset.c
==============================================================================
--- stable/12/sys/kern/kern_cpuset.c	Fri Dec  4 02:20:41 2020	(r368323)
+++ stable/12/sys/kern/kern_cpuset.c	Fri Dec  4 02:28:45 2020	(r368324)
@@ -287,12 +287,12 @@ cpuset_lookup(cpusetid_t setid, struct thread *td)
 }
 
 /*
- * Create a set in the space provided in 'set' with the provided parameters.
+ * Initialize a set in the space provided in 'set' with the provided parameters.
  * The set is returned with a single ref.  May return EDEADLK if the set
  * will have no valid cpu based on restrictions from the parent.
  */
 static int
-_cpuset_create(struct cpuset *set, struct cpuset *parent,
+cpuset_init(struct cpuset *set, struct cpuset *parent,
     const cpuset_t *mask, struct domainset *domain, cpusetid_t id)
 {
 
@@ -326,6 +326,10 @@ _cpuset_create(struct cpuset *set, struct cpuset *pare
  * Create a new non-anonymous set with the requested parent and mask.  May
  * return failures if the mask is invalid or a new number can not be
  * allocated.
+ *
+ * If *setp is not NULL, then it will be used as-is.  The caller must take
+ * into account that *setp will be inserted at the head of cpuset_ids and
+ * plan any potentially conflicting cs_link usage accordingly.
  */
 static int
 cpuset_create(struct cpuset **setp, struct cpuset *parent, const cpuset_t *mask)
@@ -333,16 +337,22 @@ cpuset_create(struct cpuset **setp, struct cpuset *par
 	struct cpuset *set;
 	cpusetid_t id;
 	int error;
+	bool dofree;
 
 	id = alloc_unr(cpuset_unr);
 	if (id == -1)
 		return (ENFILE);
-	*setp = set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
-	error = _cpuset_create(set, parent, mask, NULL, id);
+	dofree = (*setp == NULL);
+	if (*setp != NULL)
+		set = *setp;
+	else
+		*setp = set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
+	error = cpuset_init(set, parent, mask, NULL, id);
 	if (error == 0)
 		return (0);
 	free_unr(cpuset_unr, id);
-	uma_zfree(cpuset_zone, set);
+	if (dofree)
+		uma_zfree(cpuset_zone, set);
 
 	return (error);
 }
@@ -978,7 +988,7 @@ cpuset_shadow(struct cpuset *set, struct cpuset **nset
 	else
 		d = set->cs_domain;
 	nset = LIST_FIRST(cpusets);
-	error = _cpuset_create(nset, parent, mask, d, CPUSET_INVALID);
+	error = cpuset_init(nset, parent, mask, d, CPUSET_INVALID);
 	if (error == 0) {
 		LIST_REMOVE(nset, cs_link);
 		*nsetp = nset;
@@ -1458,9 +1468,7 @@ domainset_zero(void)
  * sets:
  * 
  * 0 - The root set which should represent all valid processors in the
- *     system.  It is initially created with a mask of all processors
- *     because we don't know what processors are valid until cpuset_init()
- *     runs.  This set is immutable.
+ *     system.  This set is immutable.
  * 1 - The default set which all processes are a member of until changed.
  *     This allows an administrator to move all threads off of given cpus to
  *     dedicate them to high priority tasks or save power etc.
@@ -1497,14 +1505,14 @@ cpuset_thread0(void)
 	 * Now derive a default (1), modifiable set from that to give out.
 	 */
 	set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
-	error = _cpuset_create(set, cpuset_zero, NULL, NULL, 1);
+	error = cpuset_init(set, cpuset_zero, NULL, NULL, 1);
 	KASSERT(error == 0, ("Error creating default set: %d\n", error));
 	cpuset_default = set;
 	/*
 	 * Create the kernel set (2).
 	 */
 	set = uma_zalloc(cpuset_zone, M_WAITOK | M_ZERO);
-	error = _cpuset_create(set, cpuset_zero, NULL, NULL, 2);
+	error = cpuset_init(set, cpuset_zero, NULL, NULL, 2);
 	KASSERT(error == 0, ("Error creating kernel set: %d\n", error));
 	set->cs_domain = &domainset2;
 	cpuset_kernel = set;
@@ -1557,16 +1565,17 @@ cpuset_create_root(struct prison *pr, struct cpuset **
 	KASSERT(pr != NULL, ("[%s:%d] invalid pr", __func__, __LINE__));
 	KASSERT(setp != NULL, ("[%s:%d] invalid setp", __func__, __LINE__));
 
-	error = cpuset_create(setp, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
+	set = NULL;
+	error = cpuset_create(&set, pr->pr_cpuset, &pr->pr_cpuset->cs_mask);
 	if (error)
 		return (error);
 
-	KASSERT(*setp != NULL, ("[%s:%d] cpuset_create returned invalid data",
+	KASSERT(set != NULL, ("[%s:%d] cpuset_create returned invalid data",
 	    __func__, __LINE__));
 
 	/* Mark the set as root. */
-	set = *setp;
 	set->cs_flags |= CPU_SET_ROOT;
+	*setp = set;
 
 	return (0);
 }
@@ -1623,6 +1632,7 @@ sys_cpuset(struct thread *td, struct cpuset_args *uap)
 	thread_lock(td);
 	root = cpuset_refroot(td->td_cpuset);
 	thread_unlock(td);
+	set = NULL;
 	error = cpuset_create(&set, root, &root->cs_mask);
 	cpuset_rel(root);
 	if (error)


More information about the svn-src-all mailing list