Skip to content

Commit 642fa44

Browse files
Davidlohr Buesoingomolnar
authored andcommitted
sched/core: Remove set_task_state()
This is a nasty interface and setting the state of a foreign task must not be done. As of the following commit: be628be ("bcache: Make gc wakeup sane, remove set_task_state()") ... everyone in the kernel calls set_task_state() with current, allowing the helper to be removed. However, as the comment indicates, it is still around for those archs where computing current is more expensive than using a pointer, at least in theory. An important arch that is affected is arm64, however this has been addressed now [1] and performance is up to par making no difference with either calls. Of all the callers, if any, it's the locking bits that would care most about this -- ie: we end up passing a tsk pointer to a lot of the lock slowpath, and setting ->state on that. The following numbers are based on two tests: a custom ad-hoc microbenchmark that just measures latencies (for ~65 million calls) between get_task_state() vs get_current_state(). Secondly for a higher overview, an unlink microbenchmark was used, which pounds on a single file with open, close,unlink combos with increasing thread counts (up to 4x ncpus). While the workload is quite unrealistic, it does contend a lot on the inode mutex or now rwsem. [1] https://lkml.kernel.org/r/1483468021-8237-1-git-send-email-mark.rutland@arm.com == 1. x86-64 == Avg runtime set_task_state(): 601 msecs Avg runtime set_current_state(): 552 msecs vanilla dirty Hmean unlink1-processes-2 36089.26 ( 0.00%) 38977.33 ( 8.00%) Hmean unlink1-processes-5 28555.01 ( 0.00%) 29832.55 ( 4.28%) Hmean unlink1-processes-8 37323.75 ( 0.00%) 44974.57 ( 20.50%) Hmean unlink1-processes-12 43571.88 ( 0.00%) 44283.01 ( 1.63%) Hmean unlink1-processes-21 34431.52 ( 0.00%) 38284.45 ( 11.19%) Hmean unlink1-processes-30 34813.26 ( 0.00%) 37975.17 ( 9.08%) Hmean unlink1-processes-48 37048.90 ( 0.00%) 39862.78 ( 7.59%) Hmean unlink1-processes-79 35630.01 ( 0.00%) 36855.30 ( 3.44%) Hmean unlink1-processes-110 36115.85 ( 0.00%) 39843.91 ( 10.32%) Hmean unlink1-processes-141 32546.96 ( 0.00%) 35418.52 ( 8.82%) Hmean unlink1-processes-172 34674.79 ( 0.00%) 36899.21 ( 6.42%) Hmean unlink1-processes-203 37303.11 ( 0.00%) 36393.04 ( -2.44%) Hmean unlink1-processes-224 35712.13 ( 0.00%) 36685.96 ( 2.73%) == 2. ppc64le == Avg runtime set_task_state(): 938 msecs Avg runtime set_current_state: 940 msecs vanilla dirty Hmean unlink1-processes-2 19269.19 ( 0.00%) 30704.50 ( 59.35%) Hmean unlink1-processes-5 20106.15 ( 0.00%) 21804.15 ( 8.45%) Hmean unlink1-processes-8 17496.97 ( 0.00%) 17243.28 ( -1.45%) Hmean unlink1-processes-12 14224.15 ( 0.00%) 17240.21 ( 21.20%) Hmean unlink1-processes-21 14155.66 ( 0.00%) 15681.23 ( 10.78%) Hmean unlink1-processes-30 14450.70 ( 0.00%) 15995.83 ( 10.69%) Hmean unlink1-processes-48 16945.57 ( 0.00%) 16370.42 ( -3.39%) Hmean unlink1-processes-79 15788.39 ( 0.00%) 14639.27 ( -7.28%) Hmean unlink1-processes-110 14268.48 ( 0.00%) 14377.40 ( 0.76%) Hmean unlink1-processes-141 14023.65 ( 0.00%) 16271.69 ( 16.03%) Hmean unlink1-processes-172 13417.62 ( 0.00%) 16067.55 ( 19.75%) Hmean unlink1-processes-203 15293.08 ( 0.00%) 15440.40 ( 0.96%) Hmean unlink1-processes-234 13719.32 ( 0.00%) 16190.74 ( 18.01%) Hmean unlink1-processes-265 16400.97 ( 0.00%) 16115.22 ( -1.74%) Hmean unlink1-processes-296 14388.60 ( 0.00%) 16216.13 ( 12.70%) Hmean unlink1-processes-320 15771.85 ( 0.00%) 15905.96 ( 0.85%) x86-64 (known to be fast for get_current()/this_cpu_read_stable() caching) and ppc64 (with paca) show similar improvements in the unlink microbenches. The small delta for ppc64 (2ms), does not represent the gains on the unlink runs. In the case of x86, there was a decent amount of variation in the latency runs, but always within a 20 to 50ms increase), ppc was more constant. Signed-off-by: Davidlohr Bueso <dbueso@suse.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: dave@stgolabs.net Cc: mark.rutland@arm.com Link: http://lkml.kernel.org/r/1483479794-14013-5-git-send-email-dave@stgolabs.net Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent d269a8b commit 642fa44

12 files changed

Lines changed: 26 additions & 51 deletions

File tree

‎arch/um/drivers/random.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
7676
add_sigio_fd(random_fd);
7777

7878
add_wait_queue(&host_read_wait, &wait);
79-
set_task_state(current, TASK_INTERRUPTIBLE);
79+
set_current_state(TASK_INTERRUPTIBLE);
8080

8181
schedule();
8282
remove_wait_queue(&host_read_wait, &wait);

‎drivers/md/dm-bufio.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ static void __wait_for_free_buffer(struct dm_bufio_client *c)
794794
DECLARE_WAITQUEUE(wait, current);
795795

796796
add_wait_queue(&c->free_buffer_wait, &wait);
797-
set_task_state(current, TASK_UNINTERRUPTIBLE);
797+
set_current_state(TASK_UNINTERRUPTIBLE);
798798
dm_bufio_unlock(c);
799799

800800
io_schedule();

‎drivers/md/dm-crypt.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,14 +1210,14 @@ static int dmcrypt_write(void *data)
12101210
spin_unlock_irq(&cc->write_thread_wait.lock);
12111211

12121212
if (unlikely(kthread_should_stop())) {
1213-
set_task_state(current, TASK_RUNNING);
1213+
set_current_state(TASK_RUNNING);
12141214
remove_wait_queue(&cc->write_thread_wait, &wait);
12151215
break;
12161216
}
12171217

12181218
schedule();
12191219

1220-
set_task_state(current, TASK_RUNNING);
1220+
set_current_state(TASK_RUNNING);
12211221
spin_lock_irq(&cc->write_thread_wait.lock);
12221222
__remove_wait_queue(&cc->write_thread_wait, &wait);
12231223
goto continue_locked;

‎drivers/md/persistent-data/dm-block-manager.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ static int __check_holder(struct block_lock *lock)
120120
static void __wait(struct waiter *w)
121121
{
122122
for (;;) {
123-
set_task_state(current, TASK_UNINTERRUPTIBLE);
123+
set_current_state(TASK_UNINTERRUPTIBLE);
124124

125125
if (!w->task)
126126
break;
127127

128128
schedule();
129129
}
130130

131-
set_task_state(current, TASK_RUNNING);
131+
set_current_state(TASK_RUNNING);
132132
}
133133

134134
static void __wake_waiter(struct waiter *w)

‎drivers/staging/lustre/lnet/libcfs/linux/linux-debug.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
107107
libcfs_debug_dumplog();
108108
if (libcfs_panic_on_lbug)
109109
panic("LBUG");
110-
set_task_state(current, TASK_UNINTERRUPTIBLE);
110+
set_current_state(TASK_UNINTERRUPTIBLE);
111111
while (1)
112112
schedule();
113113
}

‎drivers/tty/tty_ldsem.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ down_read_failed(struct ld_semaphore *sem, long count, long timeout)
231231

232232
/* wait to be given the lock */
233233
for (;;) {
234-
set_task_state(current, TASK_UNINTERRUPTIBLE);
234+
set_current_state(TASK_UNINTERRUPTIBLE);
235235

236236
if (!waiter.task)
237237
break;
@@ -240,7 +240,7 @@ down_read_failed(struct ld_semaphore *sem, long count, long timeout)
240240
timeout = schedule_timeout(timeout);
241241
}
242242

243-
__set_task_state(current, TASK_RUNNING);
243+
__set_current_state(TASK_RUNNING);
244244

245245
if (!timeout) {
246246
/* lock timed out but check if this task was just
@@ -289,14 +289,14 @@ down_write_failed(struct ld_semaphore *sem, long count, long timeout)
289289

290290
waiter.task = current;
291291

292-
set_task_state(current, TASK_UNINTERRUPTIBLE);
292+
set_current_state(TASK_UNINTERRUPTIBLE);
293293
for (;;) {
294294
if (!timeout)
295295
break;
296296
raw_spin_unlock_irq(&sem->wait_lock);
297297
timeout = schedule_timeout(timeout);
298298
raw_spin_lock_irq(&sem->wait_lock);
299-
set_task_state(current, TASK_UNINTERRUPTIBLE);
299+
set_current_state(TASK_UNINTERRUPTIBLE);
300300
locked = writer_trylock(sem);
301301
if (locked)
302302
break;
@@ -307,7 +307,7 @@ down_write_failed(struct ld_semaphore *sem, long count, long timeout)
307307
list_del(&waiter.list);
308308
raw_spin_unlock_irq(&sem->wait_lock);
309309

310-
__set_task_state(current, TASK_RUNNING);
310+
__set_current_state(TASK_RUNNING);
311311

312312
/* lock wait may have timed out */
313313
if (!locked)

‎include/linux/sched.h‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ extern void proc_sched_set_task(struct task_struct *p);
227227
extern char ___assert_task_state[1 - 2*!!(
228228
sizeof(TASK_STATE_TO_CHAR_STR)-1 != ilog2(TASK_STATE_MAX)+1)];
229229

230-
/* Convenience macros for the sake of set_task_state */
230+
/* Convenience macros for the sake of set_current_state */
231231
#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE)
232232
#define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED)
233233
#define TASK_TRACED (TASK_WAKEKILL | __TASK_TRACED)
@@ -254,17 +254,6 @@ extern char ___assert_task_state[1 - 2*!!(
254254

255255
#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
256256

257-
#define __set_task_state(tsk, state_value) \
258-
do { \
259-
(tsk)->task_state_change = _THIS_IP_; \
260-
(tsk)->state = (state_value); \
261-
} while (0)
262-
#define set_task_state(tsk, state_value) \
263-
do { \
264-
(tsk)->task_state_change = _THIS_IP_; \
265-
smp_store_mb((tsk)->state, (state_value)); \
266-
} while (0)
267-
268257
#define __set_current_state(state_value) \
269258
do { \
270259
current->task_state_change = _THIS_IP_; \
@@ -277,20 +266,6 @@ extern char ___assert_task_state[1 - 2*!!(
277266
} while (0)
278267

279268
#else
280-
281-
/*
282-
* @tsk had better be current, or you get to keep the pieces.
283-
*
284-
* The only reason is that computing current can be more expensive than
285-
* using a pointer that's already available.
286-
*
287-
* Therefore, see set_current_state().
288-
*/
289-
#define __set_task_state(tsk, state_value) \
290-
do { (tsk)->state = (state_value); } while (0)
291-
#define set_task_state(tsk, state_value) \
292-
smp_store_mb((tsk)->state, (state_value))
293-
294269
/*
295270
* set_current_state() includes a barrier so that the write of current->state
296271
* is correctly serialised wrt the caller's subsequent test of whether to

‎kernel/exit.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,12 @@ static void exit_mm(void)
501501
complete(&core_state->startup);
502502

503503
for (;;) {
504-
set_task_state(current, TASK_UNINTERRUPTIBLE);
504+
set_current_state(TASK_UNINTERRUPTIBLE);
505505
if (!self.task) /* see coredump_finish() */
506506
break;
507507
freezable_schedule();
508508
}
509-
__set_task_state(current, TASK_RUNNING);
509+
__set_current_state(TASK_RUNNING);
510510
down_read(&mm->mmap_sem);
511511
}
512512
atomic_inc(&mm->mm_count);

‎kernel/locking/mutex.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
666666

667667
lock_contended(&lock->dep_map, ip);
668668

669-
set_task_state(current, state);
669+
set_current_state(state);
670670
for (;;) {
671671
/*
672672
* Once we hold wait_lock, we're serialized against
@@ -701,7 +701,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
701701
__mutex_set_flag(lock, MUTEX_FLAG_HANDOFF);
702702
}
703703

704-
set_task_state(current, state);
704+
set_current_state(state);
705705
/*
706706
* Here we order against unlock; we must either see it change
707707
* state back to RUNNING and fall through the next schedule(),
@@ -715,7 +715,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
715715
}
716716
spin_lock_mutex(&lock->wait_lock, flags);
717717
acquired:
718-
__set_task_state(current, TASK_RUNNING);
718+
__set_current_state(TASK_RUNNING);
719719

720720
mutex_remove_waiter(lock, &waiter, current);
721721
if (likely(list_empty(&lock->wait_list)))
@@ -735,7 +735,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
735735
return 0;
736736

737737
err:
738-
__set_task_state(current, TASK_RUNNING);
738+
__set_current_state(TASK_RUNNING);
739739
mutex_remove_waiter(lock, &waiter, current);
740740
spin_unlock_mutex(&lock->wait_lock, flags);
741741
debug_mutex_free_waiter(&waiter);

‎kernel/locking/rwsem-spinlock.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void __sched __down_read(struct rw_semaphore *sem)
139139
goto out;
140140
}
141141

142-
set_task_state(current, TASK_UNINTERRUPTIBLE);
142+
set_current_state(TASK_UNINTERRUPTIBLE);
143143

144144
/* set up my own style of waitqueue */
145145
waiter.task = current;
@@ -156,10 +156,10 @@ void __sched __down_read(struct rw_semaphore *sem)
156156
if (!waiter.task)
157157
break;
158158
schedule();
159-
set_task_state(current, TASK_UNINTERRUPTIBLE);
159+
set_current_state(TASK_UNINTERRUPTIBLE);
160160
}
161161

162-
__set_task_state(current, TASK_RUNNING);
162+
__set_current_state(TASK_RUNNING);
163163
out:
164164
;
165165
}
@@ -216,7 +216,7 @@ int __sched __down_write_common(struct rw_semaphore *sem, int state)
216216
ret = -EINTR;
217217
goto out;
218218
}
219-
set_task_state(current, state);
219+
set_current_state(state);
220220
raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
221221
schedule();
222222
raw_spin_lock_irqsave(&sem->wait_lock, flags);

0 commit comments

Comments
 (0)