openmp: Fix up handling of OMP_PLACES=threads(1)

When writing the places-*.c tests, I've noticed that we mishandle threads
abstract name with specified num-places if num-places isn't a multiple of
number of hw threads in a core.  It then happily ignores the maximum count
and overwrites for the remaining hw threads in a core further places that
haven't been allocated.

2021-10-15  Jakub Jelinek  <jakub@redhat.com>

	* config/linux/affinity.c (gomp_affinity_init_level_1): For level 1
	after creating count places clean up and return immediately.
	* testsuite/libgomp.c/places-6.c: New test.
	* testsuite/libgomp.c/places-7.c: New test.
	* testsuite/libgomp.c/places-8.c: New test.
	* testsuite/libgomp.c/places-9.c: New test.
	* testsuite/libgomp.c/places-10.c: New test.
diff --git a/libgomp/config/linux/affinity.c b/libgomp/config/linux/affinity.c
index 82981b6..69e72a8 100644
--- a/libgomp/config/linux/affinity.c
+++ b/libgomp/config/linux/affinity.c
@@ -338,8 +338,13 @@
 		      if (gomp_affinity_add_cpus (pl, first, 1, 0, true))
 			{
 			  CPU_CLR_S (first, gomp_cpuset_size, copy);
-			  if (level == 1)
-			    gomp_places_list_len++;
+			  if (level == 1
+			      && ++gomp_places_list_len >= count)
+			    {
+			      fclose (f);
+			      free (line);
+			      return;
+			    }
 			}
 		    }
 		if (*p == ',')
diff --git a/libgomp/testsuite/libgomp.c/places-10.c b/libgomp/testsuite/libgomp.c/places-10.c
new file mode 100644
index 0000000..7746b15
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/places-10.c
@@ -0,0 +1,10 @@
+/* { dg-set-target-env-var OMP_PLACES "numa_domains(1)" } */
+
+#include <omp.h>
+
+int
+main ()
+{
+  omp_display_env (0);
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c/places-6.c b/libgomp/testsuite/libgomp.c/places-6.c
new file mode 100644
index 0000000..bf552fb
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/places-6.c
@@ -0,0 +1,10 @@
+/* { dg-set-target-env-var OMP_PLACES "threads(1)" } */
+
+#include <omp.h>
+
+int
+main ()
+{
+  omp_display_env (0);
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c/places-7.c b/libgomp/testsuite/libgomp.c/places-7.c
new file mode 100644
index 0000000..07f3d97
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/places-7.c
@@ -0,0 +1,10 @@
+/* { dg-set-target-env-var OMP_PLACES "cores(1)" } */
+
+#include <omp.h>
+
+int
+main ()
+{
+  omp_display_env (0);
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c/places-8.c b/libgomp/testsuite/libgomp.c/places-8.c
new file mode 100644
index 0000000..ca7d55b
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/places-8.c
@@ -0,0 +1,10 @@
+/* { dg-set-target-env-var OMP_PLACES "sockets(1)" } */
+
+#include <omp.h>
+
+int
+main ()
+{
+  omp_display_env (0);
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c/places-9.c b/libgomp/testsuite/libgomp.c/places-9.c
new file mode 100644
index 0000000..627cdce
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/places-9.c
@@ -0,0 +1,10 @@
+/* { dg-set-target-env-var OMP_PLACES "ll_caches(1)" } */
+
+#include <omp.h>
+
+int
+main ()
+{
+  omp_display_env (0);
+  return 0;
+}