1 /*
2  * Copyright (c) 2018, Google Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <kernel/mp.h>
25 #include <kernel/thread.h>
26 #include <lib/unittest/unittest.h>
27 #include <lk/init.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 
31 #define THREAD_DELAY_MS 1
32 
33 #define SMPTEST_CYCLES 16
34 
35 static struct smptest_thread {
36     thread_t* thread;
37 
38     volatile bool started;
39     volatile uint unblock_count;
40     volatile uint error_count;
41     volatile uint done_count;
42 
43 } smptest_thread[SMP_MAX_CPUS];
44 
45 /* Check if a thread is blocked, using volatile to ensure re-read */
thread_is_blocked(volatile thread_t * thread)46 static bool thread_is_blocked(volatile thread_t* thread) {
47     return thread->state == THREAD_BLOCKED;
48 }
49 
smptest_thread_func(void * arg)50 static int smptest_thread_func(void* arg) {
51     const uint i = (uintptr_t)arg;
52     const uint expected_cpu = i;
53     struct smptest_thread* const smpt = &smptest_thread[i];
54 
55     /* Note thread as started so main thread sees which CPUs are available */
56     smpt->started = true;
57 
58     uint cpu = arch_curr_cpu_num();
59     if (cpu != expected_cpu) {
60         /* Warn if the thread starts on another CPU than it was pinned to */
61         printf("%s: thread %d started on wrong cpu: %d\n", __func__, i, cpu);
62         smpt->error_count++;
63     }
64 
65     while (true) {
66         THREAD_LOCK(state1);
67         get_current_thread()->state = THREAD_BLOCKED;
68         thread_block();
69 
70         cpu = arch_curr_cpu_num();
71         if (cpu != expected_cpu) {
72             /* Don't update any state if the thread runs on the wrong CPU. */
73             printf("%s: thread %d ran on wrong cpu: %d\n", __func__, i, cpu);
74             smpt->error_count++;
75             continue;
76         }
77 
78         /*
79          * Update unblock count for this cpu so the main test thread can see
80          * that it ran.
81          */
82         smpt->unblock_count++;
83         THREAD_UNLOCK(state1);
84 
85         /* Sleep to allow other threads to block */
86         thread_sleep(THREAD_DELAY_MS);
87 
88         THREAD_LOCK(state2);
89 
90         /* Find and unblock the next started cpu */
91         for (uint next_cpu = i + 1; next_cpu < SMP_MAX_CPUS; next_cpu++) {
92             if (smptest_thread[next_cpu].started) {
93                 thread_t* next = smptest_thread[next_cpu].thread;
94 
95                 /* Next CPU should be blocked; wake it up */
96                 if (thread_is_blocked(next)) {
97                     thread_unblock(next, false);
98                 } else {
99                     printf("%s: thread %d not blocked\n", __func__, i + 1);
100                     smpt->error_count++;
101                 }
102 
103                 break;
104             }
105         }
106 
107         /*
108          * Update unblock count for this cpu so the main test thread can see
109          * that it completed.
110          */
111         smpt->done_count++;
112         THREAD_UNLOCK(state2);
113     }
114     return 0;
115 }
116 
TEST(smptest,run)117 TEST(smptest, run) {
118     bool wait_for_cpus = false;
119 
120     for (uint i = 0; i < SMP_MAX_CPUS; i++) {
121         if (!thread_is_blocked(smptest_thread[i].thread)) {
122             unittest_printf("[   INFO   ] thread %d not ready\n", i);
123             wait_for_cpus = true;
124         }
125     }
126 
127     /*
128      * test-runner can start the test before all CPUs have finished booting.
129      * Wait another second for all the CPUs we need to be ready if needed.
130      */
131     if (wait_for_cpus) {
132         unittest_printf("[   INFO   ] waiting for threads to be ready\n");
133         thread_sleep(1000);
134     }
135 
136     for (uint i = 0; i < SMP_MAX_CPUS; i++) {
137         ASSERT_EQ(!mp_is_cpu_active(i) ||
138                           thread_is_blocked(smptest_thread[i].thread),
139                   true, "thread %d not ready\n", i);
140     }
141 
142     for (uint i = 0; i < SMP_MAX_CPUS; i++) {
143         smptest_thread[i].unblock_count = 0;
144         smptest_thread[i].error_count = 0;
145         smptest_thread[i].done_count = 0;
146     }
147 
148     /*
149      * Repeat the test, in case the CPUs don't go back to the same state
150      * after the first wake-up
151      */
152     for (uint j = 1; j < SMPTEST_CYCLES; j++) {
153         THREAD_LOCK(state);
154         /*
155          * Wake up thread on CPU 0 to start a test run. Each thread 'n' should
156          * wake-up thread 'n+1' until the last thread stops.
157          * Check thread is blocked before unblocking to avoid asserts.
158          */
159         if (thread_is_blocked(smptest_thread[0].thread)) {
160             thread_unblock(smptest_thread[0].thread, false);
161         }
162 
163         THREAD_UNLOCK(state);
164 
165         /* Sleep to allow all CPUs to run with some margin */
166         thread_sleep((THREAD_DELAY_MS + 5) * SMP_MAX_CPUS);
167 
168         /*
169          * Check that every CPU-thread ran exactly once each time we woke up the
170          * first thread.
171          */
172         for (uint cpu = 0; cpu < SMP_MAX_CPUS; cpu++) {
173             const struct smptest_thread* const smpt = &smptest_thread[cpu];
174 
175             /*
176              * Some cpus can still execute the thread body (e.g. if they are
177              * interrupted by some other jobs), let them time to finish
178              * (up to 1 sec, then think they got stuck).
179              */
180             for (int i = 0; i < 10; i++) {
181                 if (smpt->unblock_count != j || smpt->done_count != j) {
182                     thread_sleep(100);
183                 }
184             }
185 
186             const int unblock_count = smpt->unblock_count;
187             const int error_count = smpt->error_count;
188             const int done_count = smpt->done_count;
189 
190             if (smpt->started) {
191                 EXPECT_EQ(unblock_count, j, "cpu %d FAILED block count\n", cpu);
192                 EXPECT_EQ(error_count, 0, "cpu %d FAILED error count\n", cpu);
193                 EXPECT_EQ(done_count, j, "cpu %d FAILED done count\n", cpu);
194 
195                 if (j == SMPTEST_CYCLES - 1) {
196                     unittest_printf(
197                             "[   INFO   ] smptest cpu %d ran %d times\n", cpu,
198                             SMPTEST_CYCLES);
199                 }
200             } else {
201                 EXPECT_EQ(mp_is_cpu_active(cpu), false,
202                           "cpu %d active but not running", cpu);
203                 EXPECT_EQ(unblock_count, 0, "cpu %d FAILED block count\n", cpu);
204                 EXPECT_EQ(error_count, 0, "cpu %d FAILED error count\n", cpu);
205                 EXPECT_EQ(done_count, 0, "cpu %d FAILED done count\n", cpu);
206             }
207         }
208     }
209 
210 test_abort:;
211 }
212 
smptest_setup(uint level)213 static void smptest_setup(uint level) {
214     /* Create a thread for each possible CPU */
215     for (uint cpu = 0; cpu < SMP_MAX_CPUS; cpu++) {
216         struct smptest_thread* smpt = &smptest_thread[cpu];
217         char thread_name[32];
218 
219         snprintf(thread_name, sizeof(thread_name), "smptest-%u", cpu);
220         smpt->thread = thread_create(thread_name, smptest_thread_func,
221                                      (void*)(uintptr_t)cpu, HIGH_PRIORITY,
222                                      DEFAULT_STACK_SIZE);
223         thread_set_pinned_cpu(smpt->thread, cpu);
224     }
225 
226     /* Allow threads to run */
227     for (uint cpu = 0; cpu < SMP_MAX_CPUS; cpu++) {
228         thread_resume(smptest_thread[cpu].thread);
229     }
230 }
231 
232 LK_INIT_HOOK(smptest_hook, smptest_setup, LK_INIT_LEVEL_APPS);
233 
234 PORT_TEST(smptest, "com.android.kernel.smp-unittest");
235