| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | Pip stress - Priority Inheritance with processes |
| 4 | |
| 5 | Copyright (C) 2009, John Kacur <[email protected]> |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * This program demonstrates the technique of using priority inheritance (PI) |
| 10 | * mutexes with processes instead of threads. |
| 11 | * The way to do this is to obtain some shared memory - in this case with |
| 12 | * mmap that backs a pthread_mutex_t since this will support PI. |
| 13 | * Pay particular attention to how this is intialized to support processes. |
| 14 | * Function init_shared_pthread_mutex() does this by setting the |
| 15 | * pthread_mutexattr to PTHREAD_PROCESS_SHARED and the mutex protocol to |
| 16 | * PTHREAD_PRIO_INHERIT. |
| 17 | * In this program we purposely try to invoke a classic priority inversion. |
| 18 | * A low priority process grabs the mutex and does some work. |
| 19 | * A high priority process comes a long and is blocked since the mutex is taken. |
| 20 | * A medium priority process that doesn't require the mutex then takes the |
| 21 | * processor. Because the processes are restricted to one cpu, the low priority |
| 22 | * processes never makes any progress because the medium priority process |
| 23 | * runs in an infinite loop. This is a priority inversion because the |
| 24 | * medium priority process is running at the expensive of the high priority |
| 25 | * process. However, since we have used PRIO_INHERIT and are running on a |
| 26 | * machine that supports preemption, the high priority process will lend it's |
| 27 | * priority to the low priority process which will preempt the medium priority |
| 28 | * process. The low priority process will then release the mutex which the |
| 29 | * high priority process can obtain. When the high priority process gets to run |
| 30 | * it kills the medium priority process. |
| 31 | * The state structure keeps track of the progress. Although this program |
| 32 | * is set up to likely trigger an inversion, there is no guarantee that |
| 33 | * scheduling will make that happen. After the program completes it reports |
| 34 | * whether a priority inversion occurred or not. In either case this program |
| 35 | * demonstrates how to use priority inheritance mutexes with processes. |
| 36 | * In fact, you would be better off to avoid scenarios in which a priority |
| 37 | * inversion occurs if possible - this program tries to trigger them just |
| 38 | * to show that it works. If you are having difficulty triggering an inversion, |
| 39 | * merely increase the time that the low priority process sleeps while |
| 40 | * holding the lock. (usleep); |
| 41 | * Also note that you have to run as a user with permission to change |
| 42 | * scheduling priorities. |
| 43 | */ |
| 44 | |
| 45 | #include "pip_stress.h" |
| 46 | |
| 47 | #include <unistd.h> |
| 48 | #include <getopt.h> |
| 49 | |
| 50 | /* default time for low priority thread usleep */ |
| 51 | useconds_t usleep_val = 500; |
| 52 | |
| 53 | pthread_mutex_t *resource; |
| 54 | |
| 55 | /* This records the state to determine whether a priority inversion occurred */ |
| 56 | struct State { |
| 57 | int low_owns_resource; |
| 58 | int high_started; |
| 59 | int high_owns_resource; |
| 60 | int medium_started; |
| 61 | int inversion; |
| 62 | pthread_mutex_t *mutex; |
| 63 | }; |
| 64 | |
| 65 | struct State *statep; |
| 66 | |
| 67 | const int policy = SCHED_FIFO; |
| 68 | static int prio_min; /* Initialized for the minimum priority of policy */ |
| 69 | |
| 70 | static void usage(int error) |
| 71 | { |
| 72 | printf("pip_stress V %1.2f\n", VERSION); |
| 73 | printf("Usage:\n" |
| 74 | "pip_stress <options>\n"\ |
| 75 | "-h --help Show this help menu.\n"\ |
| 76 | "-u TIME --usleep=TIME Specify the sleep time in usec of the low priority process.\n"\ |
| 77 | " Defaults to 500.\n" |
| 78 | ); |
| 79 | exit(error); |
| 80 | } |
| 81 | |
| 82 | int main(int argc, char *argv[]) |
| 83 | { |
| 84 | void *mptr; /* memory pointer */ |
| 85 | pid_t pid1, pid2; |
| 86 | cpu_set_t set, *setp = &set; |
| 87 | int res; |
| 88 | int *minimum_priority = (int*)&prio_min; |
| 89 | |
| 90 | for (;;) { |
| 91 | struct option long_options[] = { |
| 92 | { "help", no_argument, NULL, 'h' }, |
| 93 | { "usleep", required_argument, NULL, 'u' }, |
| 94 | { NULL, 0, NULL, 0 }, |
| 95 | }; |
| 96 | |
| 97 | int c = getopt_long(argc, argv, "hu:", long_options, NULL); |
| 98 | if (c == -1) |
| 99 | break; |
| 100 | switch (c) { |
| 101 | case 'h': |
| 102 | usage(0); |
| 103 | break; |
| 104 | case 'u': |
| 105 | usleep_val = strtoul(optarg, NULL, 10); |
| 106 | break; |
| 107 | default: |
| 108 | usage(1); |
| 109 | break; |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | *minimum_priority = sched_get_priority_min(policy); |
| 114 | |
| 115 | if (check_privs()) |
| 116 | exit(1); |
| 117 | |
| 118 | mptr = mmap_page(); /* Get a page of shared memory */ |
| 119 | resource = (pthread_mutex_t*)mptr; /* point our lock to it */ |
| 120 | mptr += sizeof(pthread_mutex_t); /* advance the memory pointer */ |
| 121 | |
| 122 | /* Initialize our mutex via the resource pointer */ |
| 123 | init_shared_pthread_mutex(resource, PTHREAD_PRIO_INHERIT); |
| 124 | |
| 125 | statep = (struct State*)mptr; |
| 126 | mptr += sizeof(struct State); |
| 127 | |
| 128 | init_state(); /* Initialize the state structure */ |
| 129 | |
| 130 | statep->mutex = (pthread_mutex_t*)mptr; /* point the next lock to it */ |
| 131 | mptr += sizeof(pthread_mutex_t); /* advance the memory pointer */ |
| 132 | |
| 133 | /* Initialize our State mutex */ |
| 134 | init_shared_pthread_mutex(statep->mutex, PTHREAD_PRIO_NONE); |
| 135 | |
| 136 | set_rt_prio(0, prio_min, policy); |
| 137 | |
| 138 | /* We restrict this program to the first cpu, inorder to increase |
| 139 | * the likelihood of a priority inversion */ |
| 140 | CPU_ZERO(setp); |
| 141 | CPU_SET(0, setp); |
| 142 | res = sched_setaffinity(0, sizeof(set), setp); |
| 143 | if (res == -1) { |
| 144 | int err = errno; |
| 145 | err_msg("sched_setaffinity: "); |
| 146 | err_exit(err, NULL); |
| 147 | } |
| 148 | |
| 149 | pid1 = fork(); |
| 150 | if (pid1 == -1) { |
| 151 | perror("fork"); |
| 152 | exit(1); |
| 153 | } else if (pid1 != 0) { /* parent code */ |
| 154 | low(pid1); |
| 155 | } else { /* child code */ |
| 156 | pid2 = fork(); /* parent code */ |
| 157 | if (pid2 == -1) { |
| 158 | perror("fork: "); |
| 159 | exit(1); |
| 160 | } else if (pid2 != 0) { /* parent code */ |
| 161 | high(pid2); |
| 162 | } else { /* child code */ |
| 163 | medium(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | exit(0); |
| 168 | } |
| 169 | |
| 170 | /* Initialize the structure that tracks when a priority inversion occurs */ |
| 171 | void init_state(void) |
| 172 | { |
| 173 | /* Init the State structure */ |
| 174 | statep->low_owns_resource = 0; |
| 175 | statep->high_started = 0; |
| 176 | statep->high_owns_resource = 0; |
| 177 | statep->medium_started = 0; |
| 178 | /* Assume an inversion will occur until proven false */ |
| 179 | statep->inversion = 1; |
| 180 | } |
| 181 | |
| 182 | /* @pid = high priority process pid */ |
| 183 | void low(pid_t pid) |
| 184 | { |
| 185 | int status; |
| 186 | Pthread_mutex_lock(resource); |
| 187 | Pthread_mutex_lock(statep->mutex); |
| 188 | statep->low_owns_resource = 1; |
| 189 | if (statep->high_owns_resource || |
| 190 | statep->medium_started) { |
| 191 | statep->inversion = 0; |
| 192 | } |
| 193 | Pthread_mutex_unlock(statep->mutex); |
| 194 | usleep(usleep_val); |
| 195 | Pthread_mutex_unlock(resource); |
| 196 | waitpid(pid, &status, 0); |
| 197 | } |
| 198 | |
| 199 | void medium(void) |
| 200 | { |
| 201 | set_rt_prio(0, prio_min+1, policy); |
| 202 | Pthread_mutex_lock(statep->mutex); |
| 203 | statep->medium_started = 1; |
| 204 | if (!statep->high_started) |
| 205 | statep->inversion = 0; |
| 206 | Pthread_mutex_unlock(statep->mutex); |
| 207 | |
| 208 | for(;;); /* infinite loop */ |
| 209 | } |
| 210 | |
| 211 | /* @pid = medium priority process pid */ |
| 212 | void high(pid_t pid) |
| 213 | { |
| 214 | int status; |
| 215 | set_rt_prio(0, prio_min+2, policy); |
| 216 | |
| 217 | /* Must come after raising the priority */ |
| 218 | Pthread_mutex_lock(statep->mutex); |
| 219 | statep->high_started = 1; |
| 220 | Pthread_mutex_unlock(statep->mutex); |
| 221 | |
| 222 | Pthread_mutex_lock(resource); |
| 223 | Pthread_mutex_lock(statep->mutex); |
| 224 | statep->high_owns_resource = 1; |
| 225 | if (!statep->low_owns_resource || !statep->medium_started) |
| 226 | statep->inversion = 0; |
| 227 | Pthread_mutex_unlock(statep->mutex); |
| 228 | Pthread_mutex_unlock(resource); |
| 229 | kill(pid, SIGKILL); /* kill the medium thread */ |
| 230 | waitpid(pid, &status, 0); |
| 231 | |
| 232 | Pthread_mutex_lock(statep->mutex); |
| 233 | |
| 234 | if (statep->inversion) |
| 235 | printf("Successfully used priority inheritance to handle an inversion\n"); |
| 236 | else |
| 237 | printf("No inversion incurred\n"); |
| 238 | Pthread_mutex_unlock(statep->mutex); |
| 239 | } |
| 240 | |
| 241 | /* mmap a page of anonymous shared memory */ |
| 242 | void *mmap_page(void) |
| 243 | { |
| 244 | void *mptr; |
| 245 | long pgsize = sysconf(_SC_PAGE_SIZE); |
| 246 | |
| 247 | mptr = mmap(NULL, pgsize, PROTRW, MMAP_FLAGS, 0, 0); |
| 248 | if (mptr == MAP_FAILED) { |
| 249 | perror("In function mmap_page - mmap"); |
| 250 | exit(1); |
| 251 | } |
| 252 | |
| 253 | return mptr; |
| 254 | } |
| 255 | |
| 256 | long process_shared_mutex_available(void) |
| 257 | { |
| 258 | long res = -1; /* undefined */ |
| 259 | #ifdef _POSIX_THREAD_PROCESS_SHARED |
| 260 | res = sysconf(_SC_THREAD_PROCESS_SHARED); |
| 261 | if (res == -1) { |
| 262 | int err = errno; /* save the error number */ |
| 263 | err_msg("%s: sysconf(_SC_THREAD_PROCESS_SHARED): ", __func__); |
| 264 | err_exit(err, NULL); |
| 265 | } |
| 266 | #else |
| 267 | #error _POSIX_THREAD_PROCESS_SHARED is not defined |
| 268 | #endif |
| 269 | return res; |
| 270 | } |
| 271 | |
| 272 | void Pthread_mutexattr_init(pthread_mutexattr_t *attr) |
| 273 | { |
| 274 | int err; |
| 275 | err = pthread_mutexattr_init(attr); |
| 276 | if (err) { |
| 277 | err_msg("%s: pthread_mutexattr_init(): ", __func__); |
| 278 | err_exit(err, NULL); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void Pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared) |
| 283 | { |
| 284 | int err; |
| 285 | err = pthread_mutexattr_setpshared(attr, pshared); |
| 286 | if (err) { |
| 287 | err_msg("%s: pthread_mutexattr_setpshared(): ", __func__); |
| 288 | err_exit(err, NULL); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | void Pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol) |
| 293 | { |
| 294 | int err; |
| 295 | err = pthread_mutexattr_setprotocol(attr, protocol); |
| 296 | if (err) { |
| 297 | err_msg("%s: pthread_mutexattr_setprotocol(): ", __func__); |
| 298 | err_exit(err, NULL); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void Pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr) |
| 303 | { |
| 304 | int err; |
| 305 | err = pthread_mutex_init(mutex, attr); |
| 306 | if (err) { |
| 307 | err_msg("%s: pthread_mutex_init(): ", __func__); |
| 308 | err_exit(err, NULL); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void Pthread_mutex_lock(pthread_mutex_t *mutex) |
| 313 | { |
| 314 | int err; |
| 315 | err = pthread_mutex_lock(mutex); |
| 316 | if (err) { |
| 317 | err_msg("%s: pthread_mutex_lock(): ", __func__); |
| 318 | err_exit(err, NULL); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void Pthread_mutex_unlock(pthread_mutex_t *mutex) |
| 323 | { |
| 324 | int err; |
| 325 | err = pthread_mutex_unlock(mutex); |
| 326 | if (err) { |
| 327 | err_msg("%s: pthread_mutex_unlock(): ", __func__); |
| 328 | err_exit(err, NULL); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void init_shared_pthread_mutex(pthread_mutex_t *mutex, int protocol) |
| 333 | { |
| 334 | pthread_mutexattr_t attr; |
| 335 | |
| 336 | process_shared_mutex_available(); |
| 337 | |
| 338 | Pthread_mutexattr_init(&attr); |
| 339 | Pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
| 340 | Pthread_mutexattr_setprotocol(&attr, protocol); |
| 341 | |
| 342 | Pthread_mutex_init(mutex, &attr); |
| 343 | } |
| 344 | |
| 345 | /* Set the priority and policy of a process */ |
| 346 | int set_rt_prio(pid_t pid, int prio, int policy) |
| 347 | { |
| 348 | int err; |
| 349 | struct sched_param param; |
| 350 | struct sched_param *pparam = ¶m; |
| 351 | pparam->sched_priority = prio; |
| 352 | err = sched_setscheduler(pid, policy, pparam); |
| 353 | if (err) { |
| 354 | err = errno; /* save the errno */ |
| 355 | err_msg_n(err, "%s: sched_setscheduler(): ", __func__); |
| 356 | err_msg("%s: prio = %d\n", __func__, prio); |
| 357 | err_msg("%s: pparam->sched_priority = %d\n", __func__, pparam->sched_priority); |
| 358 | err_msg("%s: policy = %d\n", __func__, policy); |
| 359 | } |
| 360 | return err; /* 0 on success */ |
| 361 | } |
| 362 | |
| 363 | int get_rt_prio(pid_t pid) |
| 364 | { |
| 365 | int err; |
| 366 | struct sched_param param; |
| 367 | err = sched_getparam(pid, ¶m); |
| 368 | if (err) { |
| 369 | err = errno; /* save the errno */ |
| 370 | err_msg_n(err, "%s: get_rt_prio(): ", __func__); |
| 371 | return -1; |
| 372 | } |
| 373 | return param.sched_priority; |
| 374 | } |