diff --git a/kernel/cpuset.c b/kernel/cpuset.c
index f57854b08922..4c49188cc49b 100644
--- a/kernel/cpuset.c
+++ b/kernel/cpuset.c
@@ -1682,9 +1682,9 @@ static int pid_array_load(pid_t *pidarray, int npids, struct cpuset *cs)
 
 	do_each_thread(g, p) {
 		if (p->cpuset == cs) {
-			pidarray[n++] = p->pid;
 			if (unlikely(n == npids))
 				goto array_full;
+			pidarray[n++] = p->pid;
 		}
 	} while_each_thread(g, p);