unix: fix TestSchedSetaffinity for smt settings

If a system has the ability to disable some cores, as on
ppc64 with the ppc64_cpu command, then TestSchedSetaffinity will
fail if the CPUset passed to Setaffinity includes one that has
been disabled.

This adds a check to use values from the oldMask, which are the valid
cores returned from Getaffinity, to pass to Setaffinity.

Fixes golang/go#27875

Change-Id: I9656f41867afc18e0eaedc4bdef5f75e137a1fcd
Reviewed-on: https://go-review.googlesource.com/137675
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/unix/syscall_linux_test.go b/unix/syscall_linux_test.go
index b278493..758efa6 100644
--- a/unix/syscall_linux_test.go
+++ b/unix/syscall_linux_test.go
@@ -273,6 +273,23 @@
 		t.Skip("skipping setaffinity tests on android")
 	}
 
+	// On a system like ppc64x where some cores can be disabled using ppc64_cpu,
+	// setaffinity should only be called with enabled cores. The valid cores
+	// are found from the oldMask, but if none are found then the setaffinity
+	// tests are skipped. Issue #27875.
+	if !oldMask.IsSet(cpu) {
+		newMask.Zero()
+		for i := 0; i < len(oldMask); i++ {
+			if oldMask.IsSet(i) {
+				newMask.Set(i)
+				break
+			}
+		}
+		if newMask.Count() == 0 {
+			t.Skip("skipping setaffinity tests if CPU not available")
+		}
+	}
+
 	err = unix.SchedSetaffinity(0, &newMask)
 	if err != nil {
 		t.Fatalf("SchedSetaffinity: %v", err)