Skip to content

Commit 4998e7f

Browse files
Daniel Bristot de Oliveirarostedt
authored andcommitted
tracing/osnoise: Switch from PF_NO_SETAFFINITY to migrate_disable
Currently, osnoise/timerlat threads run with PF_NO_SETAFFINITY set. It works well, however, cgroups do not allow PF_NO_SETAFFINITY threads to be accepted, and this creates a limitation to osnoise/timerlat. To avoid this limitation, disable migration of the threads as soon as they start to run, and then clean the PF_NO_SETAFFINITY flag (still) used during thread creation. If for some reason a thread migration is requested, e.g., via sched_settafinity, the tracer thread will notice and exit. Link: https://lkml.kernel.org/r/8ba8bc9c15b3ea40cf73cf67a9bc061a264609f0.1686063934.git.bristot@kernel.org Cc: Juri Lelli <[email protected]> Cc: William White <[email protected]> Cc: Daniel Bristot de Oliveira <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Jonathan Corbet <[email protected]> Signed-off-by: Daniel Bristot de Oliveira <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 83f7444 commit 4998e7f

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

kernel/trace/trace_osnoise.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,39 @@ static void osnoise_sleep(void)
15451545
}
15461546
}
15471547

1548+
/*
1549+
* osnoise_migration_pending - checks if the task needs to migrate
1550+
*
1551+
* osnoise/timerlat threads are per-cpu. If there is a pending request to
1552+
* migrate the thread away from the current CPU, something bad has happened.
1553+
* Play the good citizen and leave.
1554+
*
1555+
* Returns 0 if it is safe to continue, 1 otherwise.
1556+
*/
1557+
static inline int osnoise_migration_pending(void)
1558+
{
1559+
if (!current->migration_pending)
1560+
return 0;
1561+
1562+
/*
1563+
* If migration is pending, there is a task waiting for the
1564+
* tracer to enable migration. The tracer does not allow migration,
1565+
* thus: taint and leave to unblock the blocked thread.
1566+
*/
1567+
osnoise_taint("migration requested to osnoise threads, leaving.");
1568+
1569+
/*
1570+
* Unset this thread from the threads managed by the interface.
1571+
* The tracers are responsible for cleaning their env before
1572+
* exiting.
1573+
*/
1574+
mutex_lock(&interface_lock);
1575+
this_cpu_osn_var()->kthread = NULL;
1576+
mutex_unlock(&interface_lock);
1577+
1578+
return 1;
1579+
}
1580+
15481581
/*
15491582
* osnoise_main - The osnoise detection kernel thread
15501583
*
@@ -1553,12 +1586,29 @@ static void osnoise_sleep(void)
15531586
*/
15541587
static int osnoise_main(void *data)
15551588
{
1589+
unsigned long flags;
1590+
1591+
/*
1592+
* This thread was created pinned to the CPU using PF_NO_SETAFFINITY.
1593+
* The problem is that cgroup does not allow PF_NO_SETAFFINITY thread.
1594+
*
1595+
* To work around this limitation, disable migration and remove the
1596+
* flag.
1597+
*/
1598+
migrate_disable();
1599+
raw_spin_lock_irqsave(&current->pi_lock, flags);
1600+
current->flags &= ~(PF_NO_SETAFFINITY);
1601+
raw_spin_unlock_irqrestore(&current->pi_lock, flags);
15561602

15571603
while (!kthread_should_stop()) {
1604+
if (osnoise_migration_pending())
1605+
break;
1606+
15581607
run_osnoise();
15591608
osnoise_sleep();
15601609
}
15611610

1611+
migrate_enable();
15621612
return 0;
15631613
}
15641614

@@ -1706,6 +1756,7 @@ static int timerlat_main(void *data)
17061756
struct timerlat_variables *tlat = this_cpu_tmr_var();
17071757
struct timerlat_sample s;
17081758
struct sched_param sp;
1759+
unsigned long flags;
17091760
u64 now, diff;
17101761

17111762
/*
@@ -1714,6 +1765,18 @@ static int timerlat_main(void *data)
17141765
sp.sched_priority = DEFAULT_TIMERLAT_PRIO;
17151766
sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
17161767

1768+
/*
1769+
* This thread was created pinned to the CPU using PF_NO_SETAFFINITY.
1770+
* The problem is that cgroup does not allow PF_NO_SETAFFINITY thread.
1771+
*
1772+
* To work around this limitation, disable migration and remove the
1773+
* flag.
1774+
*/
1775+
migrate_disable();
1776+
raw_spin_lock_irqsave(&current->pi_lock, flags);
1777+
current->flags &= ~(PF_NO_SETAFFINITY);
1778+
raw_spin_unlock_irqrestore(&current->pi_lock, flags);
1779+
17171780
tlat->count = 0;
17181781
tlat->tracing_thread = false;
17191782

@@ -1731,6 +1794,7 @@ static int timerlat_main(void *data)
17311794
osn_var->sampling = 1;
17321795

17331796
while (!kthread_should_stop()) {
1797+
17341798
now = ktime_to_ns(hrtimer_cb_get_time(&tlat->timer));
17351799
diff = now - tlat->abs_period;
17361800

@@ -1749,10 +1813,14 @@ static int timerlat_main(void *data)
17491813
if (time_to_us(diff) >= osnoise_data.stop_tracing_total)
17501814
osnoise_stop_tracing();
17511815

1816+
if (osnoise_migration_pending())
1817+
break;
1818+
17521819
wait_next_period(tlat);
17531820
}
17541821

17551822
hrtimer_cancel(&tlat->timer);
1823+
migrate_enable();
17561824
return 0;
17571825
}
17581826
#else /* CONFIG_TIMERLAT_TRACER */

0 commit comments

Comments
 (0)