mirror of
https://github.com/rd-stuffs/msm-4.14.git
synced 2025-02-20 11:45:48 +08:00
In commit 4b53a3412d66 ("sched/core: Remove the tsk_nr_cpus_allowed() wrapper") the tsk_nr_cpus_allowed() wrapper was removed. There was not much difference in !RT but in RT we used this to implement migrate_disable(). Within a migrate_disable() section the CPU mask is restricted to single CPU while the "normal" CPU mask remains untouched. As an alternative implementation Ingo suggested to use struct task_struct { const cpumask_t *cpus_ptr; cpumask_t cpus_mask; }; with t->cpus_allowed_ptr = &t->cpus_allowed; In -RT we then can switch the cpus_ptr to t->cpus_allowed_ptr = &cpumask_of(task_cpu(p)); in a migration disabled region. The rules are simple: - Code that 'uses' ->cpus_allowed would use the pointer. - Code that 'modifies' ->cpus_allowed would use the direct mask. While converting the existing users I tried to stick with the rules above however… well mostly CPUFREQ tries to temporary switch the CPU mask to do something on a certain CPU and then switches the mask back it its original value. So in theory `cpus_ptr' could or should be used. However if this is invoked in a migration disabled region (which is not the case because it would require something like preempt_disable() and set_cpus_allowed_ptr() might sleep so it can't be) then the "restore" part would restore the wrong mask. So it only looks strange and I go for the pointer… Some drivers copy the cpumask without cpumask_copy() and others use cpumask_copy but without alloc_cpumask_var(). I did not fix those as part of this, could do this as a follow up… So is this the way we want it? Is the usage of `cpus_ptr' vs `cpus_mask' for the set + restore part (see cpufreq users) what we want? At some point it looks like they should use a different interface for their doing. I am not sure why switching to certain CPU is important but maybe it could be done via a workqueue from the CPUFREQ core (so we have a comment desribing why are doing this and a get_online_cpus() to ensure that the CPU does not go offline too early). Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Mike Galbraith <efault@gmx.de> Cc: Ingo Molnar <mingo@elte.hu> Cc: Rafael J. Wysocki <rjw@rjwysocki.net> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> [Sultan Alsawaf: adapt to floral] Signed-off-by: Sultan Alsawaf <sultan@kerneltoast.com> Signed-off-by: Zlatan Radovanovic <zlatan.radovanovic@fet.ba> Signed-off-by: azrim <mirzaspc@gmail.com>
278 lines
6.0 KiB
C
278 lines
6.0 KiB
C
/*
|
|
* kernel/sched/cpudl.c
|
|
*
|
|
* Global CPU deadline management
|
|
*
|
|
* Author: Juri Lelli <j.lelli@sssup.it>
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; version 2
|
|
* of the License.
|
|
*/
|
|
|
|
#include <linux/gfp.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/slab.h>
|
|
#include "cpudeadline.h"
|
|
|
|
static inline int parent(int i)
|
|
{
|
|
return (i - 1) >> 1;
|
|
}
|
|
|
|
static inline int left_child(int i)
|
|
{
|
|
return (i << 1) + 1;
|
|
}
|
|
|
|
static inline int right_child(int i)
|
|
{
|
|
return (i << 1) + 2;
|
|
}
|
|
|
|
static void cpudl_heapify_down(struct cpudl *cp, int idx)
|
|
{
|
|
int l, r, largest;
|
|
|
|
int orig_cpu = cp->elements[idx].cpu;
|
|
u64 orig_dl = cp->elements[idx].dl;
|
|
|
|
if (left_child(idx) >= cp->size)
|
|
return;
|
|
|
|
/* adapted from lib/prio_heap.c */
|
|
while(1) {
|
|
u64 largest_dl;
|
|
l = left_child(idx);
|
|
r = right_child(idx);
|
|
largest = idx;
|
|
largest_dl = orig_dl;
|
|
|
|
if ((l < cp->size) && dl_time_before(orig_dl,
|
|
cp->elements[l].dl)) {
|
|
largest = l;
|
|
largest_dl = cp->elements[l].dl;
|
|
}
|
|
if ((r < cp->size) && dl_time_before(largest_dl,
|
|
cp->elements[r].dl))
|
|
largest = r;
|
|
|
|
if (largest == idx)
|
|
break;
|
|
|
|
/* pull largest child onto idx */
|
|
cp->elements[idx].cpu = cp->elements[largest].cpu;
|
|
cp->elements[idx].dl = cp->elements[largest].dl;
|
|
cp->elements[cp->elements[idx].cpu].idx = idx;
|
|
idx = largest;
|
|
}
|
|
/* actual push down of saved original values orig_* */
|
|
cp->elements[idx].cpu = orig_cpu;
|
|
cp->elements[idx].dl = orig_dl;
|
|
cp->elements[cp->elements[idx].cpu].idx = idx;
|
|
}
|
|
|
|
static void cpudl_heapify_up(struct cpudl *cp, int idx)
|
|
{
|
|
int p;
|
|
|
|
int orig_cpu = cp->elements[idx].cpu;
|
|
u64 orig_dl = cp->elements[idx].dl;
|
|
|
|
if (idx == 0)
|
|
return;
|
|
|
|
do {
|
|
p = parent(idx);
|
|
if (dl_time_before(orig_dl, cp->elements[p].dl))
|
|
break;
|
|
/* pull parent onto idx */
|
|
cp->elements[idx].cpu = cp->elements[p].cpu;
|
|
cp->elements[idx].dl = cp->elements[p].dl;
|
|
cp->elements[cp->elements[idx].cpu].idx = idx;
|
|
idx = p;
|
|
} while (idx != 0);
|
|
/* actual push up of saved original values orig_* */
|
|
cp->elements[idx].cpu = orig_cpu;
|
|
cp->elements[idx].dl = orig_dl;
|
|
cp->elements[cp->elements[idx].cpu].idx = idx;
|
|
}
|
|
|
|
static void cpudl_heapify(struct cpudl *cp, int idx)
|
|
{
|
|
if (idx > 0 && dl_time_before(cp->elements[parent(idx)].dl,
|
|
cp->elements[idx].dl))
|
|
cpudl_heapify_up(cp, idx);
|
|
else
|
|
cpudl_heapify_down(cp, idx);
|
|
}
|
|
|
|
static inline int cpudl_maximum(struct cpudl *cp)
|
|
{
|
|
return cp->elements[0].cpu;
|
|
}
|
|
|
|
/*
|
|
* cpudl_find - find the best (later-dl) CPU in the system
|
|
* @cp: the cpudl max-heap context
|
|
* @p: the task
|
|
* @later_mask: a mask to fill in with the selected CPUs (or NULL)
|
|
*
|
|
* Returns: int - CPUs were found
|
|
*/
|
|
int cpudl_find(struct cpudl *cp, struct task_struct *p,
|
|
struct cpumask *later_mask)
|
|
{
|
|
const struct sched_dl_entity *dl_se = &p->dl;
|
|
|
|
if (later_mask &&
|
|
cpumask_and(later_mask, cp->free_cpus, p->cpus_ptr)) {
|
|
return 1;
|
|
} else {
|
|
int best_cpu = cpudl_maximum(cp);
|
|
WARN_ON(best_cpu != -1 && !cpu_present(best_cpu));
|
|
|
|
if (cpumask_test_cpu(best_cpu, p->cpus_ptr) &&
|
|
dl_time_before(dl_se->deadline, cp->elements[0].dl)) {
|
|
if (later_mask)
|
|
cpumask_set_cpu(best_cpu, later_mask);
|
|
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* cpudl_clear - remove a cpu from the cpudl max-heap
|
|
* @cp: the cpudl max-heap context
|
|
* @cpu: the target cpu
|
|
*
|
|
* Notes: assumes cpu_rq(cpu)->lock is locked
|
|
*
|
|
* Returns: (void)
|
|
*/
|
|
void cpudl_clear(struct cpudl *cp, int cpu)
|
|
{
|
|
int old_idx, new_cpu;
|
|
unsigned long flags;
|
|
|
|
WARN_ON(!cpu_present(cpu));
|
|
|
|
raw_spin_lock_irqsave(&cp->lock, flags);
|
|
|
|
old_idx = cp->elements[cpu].idx;
|
|
if (old_idx == IDX_INVALID) {
|
|
/*
|
|
* Nothing to remove if old_idx was invalid.
|
|
* This could happen if a rq_offline_dl is
|
|
* called for a CPU without -dl tasks running.
|
|
*/
|
|
} else {
|
|
new_cpu = cp->elements[cp->size - 1].cpu;
|
|
cp->elements[old_idx].dl = cp->elements[cp->size - 1].dl;
|
|
cp->elements[old_idx].cpu = new_cpu;
|
|
cp->size--;
|
|
cp->elements[new_cpu].idx = old_idx;
|
|
cp->elements[cpu].idx = IDX_INVALID;
|
|
cpudl_heapify(cp, old_idx);
|
|
|
|
cpumask_set_cpu(cpu, cp->free_cpus);
|
|
}
|
|
raw_spin_unlock_irqrestore(&cp->lock, flags);
|
|
}
|
|
|
|
/*
|
|
* cpudl_set - update the cpudl max-heap
|
|
* @cp: the cpudl max-heap context
|
|
* @cpu: the target cpu
|
|
* @dl: the new earliest deadline for this cpu
|
|
*
|
|
* Notes: assumes cpu_rq(cpu)->lock is locked
|
|
*
|
|
* Returns: (void)
|
|
*/
|
|
void cpudl_set(struct cpudl *cp, int cpu, u64 dl)
|
|
{
|
|
int old_idx;
|
|
unsigned long flags;
|
|
|
|
WARN_ON(!cpu_present(cpu));
|
|
|
|
raw_spin_lock_irqsave(&cp->lock, flags);
|
|
|
|
old_idx = cp->elements[cpu].idx;
|
|
if (old_idx == IDX_INVALID) {
|
|
int new_idx = cp->size++;
|
|
cp->elements[new_idx].dl = dl;
|
|
cp->elements[new_idx].cpu = cpu;
|
|
cp->elements[cpu].idx = new_idx;
|
|
cpudl_heapify_up(cp, new_idx);
|
|
cpumask_clear_cpu(cpu, cp->free_cpus);
|
|
} else {
|
|
cp->elements[old_idx].dl = dl;
|
|
cpudl_heapify(cp, old_idx);
|
|
}
|
|
|
|
raw_spin_unlock_irqrestore(&cp->lock, flags);
|
|
}
|
|
|
|
/*
|
|
* cpudl_set_freecpu - Set the cpudl.free_cpus
|
|
* @cp: the cpudl max-heap context
|
|
* @cpu: rd attached cpu
|
|
*/
|
|
void cpudl_set_freecpu(struct cpudl *cp, int cpu)
|
|
{
|
|
cpumask_set_cpu(cpu, cp->free_cpus);
|
|
}
|
|
|
|
/*
|
|
* cpudl_clear_freecpu - Clear the cpudl.free_cpus
|
|
* @cp: the cpudl max-heap context
|
|
* @cpu: rd attached cpu
|
|
*/
|
|
void cpudl_clear_freecpu(struct cpudl *cp, int cpu)
|
|
{
|
|
cpumask_clear_cpu(cpu, cp->free_cpus);
|
|
}
|
|
|
|
/*
|
|
* cpudl_init - initialize the cpudl structure
|
|
* @cp: the cpudl max-heap context
|
|
*/
|
|
int cpudl_init(struct cpudl *cp)
|
|
{
|
|
int i;
|
|
|
|
raw_spin_lock_init(&cp->lock);
|
|
cp->size = 0;
|
|
|
|
cp->elements = kcalloc(nr_cpu_ids,
|
|
sizeof(struct cpudl_item),
|
|
GFP_KERNEL);
|
|
if (!cp->elements)
|
|
return -ENOMEM;
|
|
|
|
if (!zalloc_cpumask_var(&cp->free_cpus, GFP_KERNEL)) {
|
|
kfree(cp->elements);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
for_each_possible_cpu(i)
|
|
cp->elements[i].idx = IDX_INVALID;
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* cpudl_cleanup - clean up the cpudl structure
|
|
* @cp: the cpudl max-heap context
|
|
*/
|
|
void cpudl_cleanup(struct cpudl *cp)
|
|
{
|
|
free_cpumask_var(cp->free_cpus);
|
|
kfree(cp->elements);
|
|
}
|