| 1 | /* Thread Priority Protect helpers. | 
|---|
| 2 | Copyright (C) 2006-2020 Free Software Foundation, Inc. | 
|---|
| 3 | This file is part of the GNU C Library. | 
|---|
| 4 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2006. | 
|---|
| 5 |  | 
|---|
| 6 | The GNU C Library is free software; you can redistribute it and/or | 
|---|
| 7 | modify it under the terms of the GNU Lesser General Public | 
|---|
| 8 | License as published by the Free Software Foundation; either | 
|---|
| 9 | version 2.1 of the License, or (at your option) any later version. | 
|---|
| 10 |  | 
|---|
| 11 | The GNU C Library is distributed in the hope that it will be useful, | 
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 14 | Lesser General Public License for more details. | 
|---|
| 15 |  | 
|---|
| 16 | You should have received a copy of the GNU Lesser General Public | 
|---|
| 17 | License along with the GNU C Library; if not, see | 
|---|
| 18 | <https://www.gnu.org/licenses/>.  */ | 
|---|
| 19 |  | 
|---|
| 20 | #include <assert.h> | 
|---|
| 21 | #include <atomic.h> | 
|---|
| 22 | #include <errno.h> | 
|---|
| 23 | #include <pthreadP.h> | 
|---|
| 24 | #include <sched.h> | 
|---|
| 25 | #include <stdlib.h> | 
|---|
| 26 | #include <atomic.h> | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | int __sched_fifo_min_prio = -1; | 
|---|
| 30 | int __sched_fifo_max_prio = -1; | 
|---|
| 31 |  | 
|---|
| 32 | /* We only want to initialize __sched_fifo_min_prio and __sched_fifo_max_prio | 
|---|
| 33 | once.  The standard solution would be similar to pthread_once, but then | 
|---|
| 34 | readers would need to use an acquire fence.  In this specific case, | 
|---|
| 35 | initialization is comprised of just idempotent writes to two variables | 
|---|
| 36 | that have an initial value of -1.  Therefore, we can treat each variable as | 
|---|
| 37 | a separate, at-least-once initialized value.  This enables using just | 
|---|
| 38 | relaxed MO loads and stores, but requires that consumers check for | 
|---|
| 39 | initialization of each value that is to be used; see | 
|---|
| 40 | __pthread_tpp_change_priority for an example. | 
|---|
| 41 | */ | 
|---|
| 42 | void | 
|---|
| 43 | __init_sched_fifo_prio (void) | 
|---|
| 44 | { | 
|---|
| 45 | atomic_store_relaxed (&__sched_fifo_max_prio, | 
|---|
| 46 | __sched_get_priority_max (SCHED_FIFO)); | 
|---|
| 47 | atomic_store_relaxed (&__sched_fifo_min_prio, | 
|---|
| 48 | __sched_get_priority_min (SCHED_FIFO)); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | int | 
|---|
| 52 | __pthread_tpp_change_priority (int previous_prio, int new_prio) | 
|---|
| 53 | { | 
|---|
| 54 | struct pthread *self = THREAD_SELF; | 
|---|
| 55 | struct priority_protection_data *tpp = THREAD_GETMEM (self, tpp); | 
|---|
| 56 | int fifo_min_prio = atomic_load_relaxed (&__sched_fifo_min_prio); | 
|---|
| 57 | int fifo_max_prio = atomic_load_relaxed (&__sched_fifo_max_prio); | 
|---|
| 58 |  | 
|---|
| 59 | if (tpp == NULL) | 
|---|
| 60 | { | 
|---|
| 61 | /* See __init_sched_fifo_prio.  We need both the min and max prio, | 
|---|
| 62 | so need to check both, and run initialization if either one is | 
|---|
| 63 | not initialized.  The memory model's write-read coherence rule | 
|---|
| 64 | makes this work.  */ | 
|---|
| 65 | if (fifo_min_prio == -1 || fifo_max_prio == -1) | 
|---|
| 66 | { | 
|---|
| 67 | __init_sched_fifo_prio (); | 
|---|
| 68 | fifo_min_prio = atomic_load_relaxed (&__sched_fifo_min_prio); | 
|---|
| 69 | fifo_max_prio = atomic_load_relaxed (&__sched_fifo_max_prio); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | size_t size = sizeof *tpp; | 
|---|
| 73 | size += (fifo_max_prio - fifo_min_prio + 1) | 
|---|
| 74 | * sizeof (tpp->priomap[0]); | 
|---|
| 75 | tpp = calloc (size, 1); | 
|---|
| 76 | if (tpp == NULL) | 
|---|
| 77 | return ENOMEM; | 
|---|
| 78 | tpp->priomax = fifo_min_prio - 1; | 
|---|
| 79 | THREAD_SETMEM (self, tpp, tpp); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | assert (new_prio == -1 | 
|---|
| 83 | || (new_prio >= fifo_min_prio | 
|---|
| 84 | && new_prio <= fifo_max_prio)); | 
|---|
| 85 | assert (previous_prio == -1 | 
|---|
| 86 | || (previous_prio >= fifo_min_prio | 
|---|
| 87 | && previous_prio <= fifo_max_prio)); | 
|---|
| 88 |  | 
|---|
| 89 | int priomax = tpp->priomax; | 
|---|
| 90 | int newpriomax = priomax; | 
|---|
| 91 | if (new_prio != -1) | 
|---|
| 92 | { | 
|---|
| 93 | if (tpp->priomap[new_prio - fifo_min_prio] + 1 == 0) | 
|---|
| 94 | return EAGAIN; | 
|---|
| 95 | ++tpp->priomap[new_prio - fifo_min_prio]; | 
|---|
| 96 | if (new_prio > priomax) | 
|---|
| 97 | newpriomax = new_prio; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | if (previous_prio != -1) | 
|---|
| 101 | { | 
|---|
| 102 | if (--tpp->priomap[previous_prio - fifo_min_prio] == 0 | 
|---|
| 103 | && priomax == previous_prio | 
|---|
| 104 | && previous_prio > new_prio) | 
|---|
| 105 | { | 
|---|
| 106 | int i; | 
|---|
| 107 | for (i = previous_prio - 1; i >= fifo_min_prio; --i) | 
|---|
| 108 | if (tpp->priomap[i - fifo_min_prio]) | 
|---|
| 109 | break; | 
|---|
| 110 | newpriomax = i; | 
|---|
| 111 | } | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | if (priomax == newpriomax) | 
|---|
| 115 | return 0; | 
|---|
| 116 |  | 
|---|
| 117 | /* See CREATE THREAD NOTES in nptl/pthread_create.c.  */ | 
|---|
| 118 | lll_lock (self->lock, LLL_PRIVATE); | 
|---|
| 119 |  | 
|---|
| 120 | tpp->priomax = newpriomax; | 
|---|
| 121 |  | 
|---|
| 122 | int result = 0; | 
|---|
| 123 |  | 
|---|
| 124 | if ((self->flags & ATTR_FLAG_SCHED_SET) == 0) | 
|---|
| 125 | { | 
|---|
| 126 | if (__sched_getparam (self->tid, &self->schedparam) != 0) | 
|---|
| 127 | result = errno; | 
|---|
| 128 | else | 
|---|
| 129 | self->flags |= ATTR_FLAG_SCHED_SET; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | if ((self->flags & ATTR_FLAG_POLICY_SET) == 0) | 
|---|
| 133 | { | 
|---|
| 134 | self->schedpolicy = __sched_getscheduler (self->tid); | 
|---|
| 135 | if (self->schedpolicy == -1) | 
|---|
| 136 | result = errno; | 
|---|
| 137 | else | 
|---|
| 138 | self->flags |= ATTR_FLAG_POLICY_SET; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | if (result == 0) | 
|---|
| 142 | { | 
|---|
| 143 | struct sched_param sp = self->schedparam; | 
|---|
| 144 | if (sp.sched_priority < newpriomax || sp.sched_priority < priomax) | 
|---|
| 145 | { | 
|---|
| 146 | if (sp.sched_priority < newpriomax) | 
|---|
| 147 | sp.sched_priority = newpriomax; | 
|---|
| 148 |  | 
|---|
| 149 | if (__sched_setscheduler (self->tid, self->schedpolicy, &sp) < 0) | 
|---|
| 150 | result = errno; | 
|---|
| 151 | } | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | lll_unlock (self->lock, LLL_PRIVATE); | 
|---|
| 155 |  | 
|---|
| 156 | return result; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | int | 
|---|
| 160 | __pthread_current_priority (void) | 
|---|
| 161 | { | 
|---|
| 162 | struct pthread *self = THREAD_SELF; | 
|---|
| 163 | if ((self->flags & (ATTR_FLAG_POLICY_SET | ATTR_FLAG_SCHED_SET)) | 
|---|
| 164 | == (ATTR_FLAG_POLICY_SET | ATTR_FLAG_SCHED_SET)) | 
|---|
| 165 | return self->schedparam.sched_priority; | 
|---|
| 166 |  | 
|---|
| 167 | int result = 0; | 
|---|
| 168 |  | 
|---|
| 169 | /* See CREATE THREAD NOTES in nptl/pthread_create.c.  */ | 
|---|
| 170 | lll_lock (self->lock, LLL_PRIVATE); | 
|---|
| 171 |  | 
|---|
| 172 | if ((self->flags & ATTR_FLAG_SCHED_SET) == 0) | 
|---|
| 173 | { | 
|---|
| 174 | if (__sched_getparam (self->tid, &self->schedparam) != 0) | 
|---|
| 175 | result = -1; | 
|---|
| 176 | else | 
|---|
| 177 | self->flags |= ATTR_FLAG_SCHED_SET; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | if ((self->flags & ATTR_FLAG_POLICY_SET) == 0) | 
|---|
| 181 | { | 
|---|
| 182 | self->schedpolicy = __sched_getscheduler (self->tid); | 
|---|
| 183 | if (self->schedpolicy == -1) | 
|---|
| 184 | result = -1; | 
|---|
| 185 | else | 
|---|
| 186 | self->flags |= ATTR_FLAG_POLICY_SET; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | if (result != -1) | 
|---|
| 190 | result = self->schedparam.sched_priority; | 
|---|
| 191 |  | 
|---|
| 192 | lll_unlock (self->lock, LLL_PRIVATE); | 
|---|
| 193 |  | 
|---|
| 194 | return result; | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|