Skip to content

Commit 50de1a8

Browse files
committed
Merge branch 'for-linus' into for-next
Conflicts: mm/percpu.c
2 parents 390dfd9 + ee0a6ef commit 50de1a8

4 files changed

Lines changed: 132 additions & 60 deletions

File tree

‎drivers/base/cpu.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static ssize_t show_crash_notes(struct sys_device *dev, struct sysdev_attribute
9797
* boot up and this data does not change there after. Hence this
9898
* operation should be safe. No locking required.
9999
*/
100-
addr = __pa(per_cpu_ptr(crash_notes, cpunum));
100+
addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
101101
rc = sprintf(buf, "%Lx\n", addr);
102102
return rc;
103103
}

‎include/linux/percpu.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ extern int __init pcpu_page_first_chunk(size_t reserved_size,
130130
extern void *__alloc_reserved_percpu(size_t size, size_t align);
131131
extern void *__alloc_percpu(size_t size, size_t align);
132132
extern void free_percpu(void *__pdata);
133+
extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
133134

134135
#ifndef CONFIG_HAVE_SETUP_PER_CPU_AREA
135136
extern void __init setup_per_cpu_areas(void);
@@ -155,6 +156,11 @@ static inline void free_percpu(void *p)
155156
kfree(p);
156157
}
157158

159+
static inline phys_addr_t per_cpu_ptr_to_phys(void *addr)
160+
{
161+
return __pa(addr);
162+
}
163+
158164
static inline void __init setup_per_cpu_areas(void) { }
159165

160166
static inline void *pcpu_lpage_remapped(void *kaddr)

‎kernel/sched.c‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,11 +1563,7 @@ static unsigned long cpu_avg_load_per_task(int cpu)
15631563

15641564
#ifdef CONFIG_FAIR_GROUP_SCHED
15651565

1566-
struct update_shares_data {
1567-
unsigned long rq_weight[NR_CPUS];
1568-
};
1569-
1570-
static DEFINE_PER_CPU(struct update_shares_data, update_shares_data);
1566+
static __read_mostly unsigned long *update_shares_data;
15711567

15721568
static void __set_se_shares(struct sched_entity *se, unsigned long shares);
15731569

@@ -1577,12 +1573,12 @@ static void __set_se_shares(struct sched_entity *se, unsigned long shares);
15771573
static void update_group_shares_cpu(struct task_group *tg, int cpu,
15781574
unsigned long sd_shares,
15791575
unsigned long sd_rq_weight,
1580-
struct update_shares_data *usd)
1576+
unsigned long *usd_rq_weight)
15811577
{
15821578
unsigned long shares, rq_weight;
15831579
int boost = 0;
15841580

1585-
rq_weight = usd->rq_weight[cpu];
1581+
rq_weight = usd_rq_weight[cpu];
15861582
if (!rq_weight) {
15871583
boost = 1;
15881584
rq_weight = NICE_0_LOAD;
@@ -1617,7 +1613,7 @@ static void update_group_shares_cpu(struct task_group *tg, int cpu,
16171613
static int tg_shares_up(struct task_group *tg, void *data)
16181614
{
16191615
unsigned long weight, rq_weight = 0, shares = 0;
1620-
struct update_shares_data *usd;
1616+
unsigned long *usd_rq_weight;
16211617
struct sched_domain *sd = data;
16221618
unsigned long flags;
16231619
int i;
@@ -1626,11 +1622,11 @@ static int tg_shares_up(struct task_group *tg, void *data)
16261622
return 0;
16271623

16281624
local_irq_save(flags);
1629-
usd = &__get_cpu_var(update_shares_data);
1625+
usd_rq_weight = per_cpu_ptr(update_shares_data, smp_processor_id());
16301626

16311627
for_each_cpu(i, sched_domain_span(sd)) {
16321628
weight = tg->cfs_rq[i]->load.weight;
1633-
usd->rq_weight[i] = weight;
1629+
usd_rq_weight[i] = weight;
16341630

16351631
/*
16361632
* If there are currently no tasks on the cpu pretend there
@@ -1651,7 +1647,7 @@ static int tg_shares_up(struct task_group *tg, void *data)
16511647
shares = tg->shares;
16521648

16531649
for_each_cpu(i, sched_domain_span(sd))
1654-
update_group_shares_cpu(tg, i, shares, rq_weight, usd);
1650+
update_group_shares_cpu(tg, i, shares, rq_weight, usd_rq_weight);
16551651

16561652
local_irq_restore(flags);
16571653

@@ -9406,6 +9402,10 @@ void __init sched_init(void)
94069402
#endif /* CONFIG_USER_SCHED */
94079403
#endif /* CONFIG_GROUP_SCHED */
94089404

9405+
#if defined CONFIG_FAIR_GROUP_SCHED && defined CONFIG_SMP
9406+
update_shares_data = __alloc_percpu(nr_cpu_ids * sizeof(unsigned long),
9407+
__alignof__(unsigned long));
9408+
#endif
94099409
for_each_possible_cpu(i) {
94109410
struct rq *rq;
94119411

‎mm/percpu.c‎

Lines changed: 114 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include <asm/cacheflush.h>
7373
#include <asm/sections.h>
7474
#include <asm/tlbflush.h>
75+
#include <asm/io.h>
7576

7677
#define PCPU_SLOT_BASE_SHIFT 5 /* 1-31 shares the same slot */
7778
#define PCPU_DFL_MAP_ALLOC 16 /* start a map with 16 ents */
@@ -151,7 +152,10 @@ static int pcpu_reserved_chunk_limit;
151152
*
152153
* During allocation, pcpu_alloc_mutex is kept locked all the time and
153154
* pcpu_lock is grabbed and released as necessary. All actual memory
154-
* allocations are done using GFP_KERNEL with pcpu_lock released.
155+
* allocations are done using GFP_KERNEL with pcpu_lock released. In
156+
* general, percpu memory can't be allocated with irq off but
157+
* irqsave/restore are still used in alloc path so that it can be used
158+
* from early init path - sched_init() specifically.
155159
*
156160
* Free path accesses and alters only the index data structures, so it
157161
* can be safely called from atomic context. When memory needs to be
@@ -350,63 +354,86 @@ static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
350354
}
351355

352356
/**
353-
* pcpu_extend_area_map - extend area map for allocation
354-
* @chunk: target chunk
357+
* pcpu_need_to_extend - determine whether chunk area map needs to be extended
358+
* @chunk: chunk of interest
355359
*
356-
* Extend area map of @chunk so that it can accomodate an allocation.
357-
* A single allocation can split an area into three areas, so this
358-
* function makes sure that @chunk->map has at least two extra slots.
360+
* Determine whether area map of @chunk needs to be extended to
361+
* accomodate a new allocation.
359362
*
360363
* CONTEXT:
361-
* pcpu_alloc_mutex, pcpu_lock. pcpu_lock is released and reacquired
362-
* if area map is extended.
364+
* pcpu_lock.
363365
*
364366
* RETURNS:
365-
* 0 if noop, 1 if successfully extended, -errno on failure.
367+
* New target map allocation length if extension is necessary, 0
368+
* otherwise.
366369
*/
367-
static int pcpu_extend_area_map(struct pcpu_chunk *chunk)
368-
__releases(lock) __acquires(lock)
370+
static int pcpu_need_to_extend(struct pcpu_chunk *chunk)
369371
{
370372
int new_alloc;
371-
int *new;
372-
size_t size;
373373

374-
/* has enough? */
375374
if (chunk->map_alloc >= chunk->map_used + 2)
376375
return 0;
377376

378-
spin_unlock_irq(&pcpu_lock);
379-
380377
new_alloc = PCPU_DFL_MAP_ALLOC;
381378
while (new_alloc < chunk->map_used + 2)
382379
new_alloc *= 2;
383380

384-
new = pcpu_mem_alloc(new_alloc * sizeof(new[0]));
385-
if (!new) {
386-
spin_lock_irq(&pcpu_lock);
381+
return new_alloc;
382+
}
383+
384+
/**
385+
* pcpu_extend_area_map - extend area map of a chunk
386+
* @chunk: chunk of interest
387+
* @new_alloc: new target allocation length of the area map
388+
*
389+
* Extend area map of @chunk to have @new_alloc entries.
390+
*
391+
* CONTEXT:
392+
* Does GFP_KERNEL allocation. Grabs and releases pcpu_lock.
393+
*
394+
* RETURNS:
395+
* 0 on success, -errno on failure.
396+
*/
397+
static int pcpu_extend_area_map(struct pcpu_chunk *chunk, int new_alloc)
398+
{
399+
int *old = NULL, *new = NULL;
400+
size_t old_size = 0, new_size = new_alloc * sizeof(new[0]);
401+
unsigned long flags;
402+
403+
new = pcpu_mem_alloc(new_size);
404+
if (!new)
387405
return -ENOMEM;
388-
}
389406

390-
/*
391-
* Acquire pcpu_lock and switch to new area map. Only free
392-
* could have happened inbetween, so map_used couldn't have
393-
* grown.
394-
*/
395-
spin_lock_irq(&pcpu_lock);
396-
BUG_ON(new_alloc < chunk->map_used + 2);
407+
/* acquire pcpu_lock and switch to new area map */
408+
spin_lock_irqsave(&pcpu_lock, flags);
397409

398-
size = chunk->map_alloc * sizeof(chunk->map[0]);
399-
memcpy(new, chunk->map, size);
410+
if (new_alloc <= chunk->map_alloc)
411+
goto out_unlock;
412+
413+
old_size = chunk->map_alloc * sizeof(chunk->map[0]);
414+
memcpy(new, chunk->map, old_size);
400415

401416
/*
402417
* map_alloc < PCPU_DFL_MAP_ALLOC indicates that the chunk is
403418
* one of the first chunks and still using static map.
404419
*/
405420
if (chunk->map_alloc >= PCPU_DFL_MAP_ALLOC)
406-
pcpu_mem_free(chunk->map, size);
421+
old = chunk->map;
407422

408423
chunk->map_alloc = new_alloc;
409424
chunk->map = new;
425+
new = NULL;
426+
427+
out_unlock:
428+
spin_unlock_irqrestore(&pcpu_lock, flags);
429+
430+
/*
431+
* pcpu_mem_free() might end up calling vfree() which uses
432+
* IRQ-unsafe lock and thus can't be called under pcpu_lock.
433+
*/
434+
pcpu_mem_free(old, old_size);
435+
pcpu_mem_free(new, new_size);
436+
410437
return 0;
411438
}
412439

@@ -1045,7 +1072,8 @@ static void *pcpu_alloc(size_t size, size_t align, bool reserved)
10451072
static int warn_limit = 10;
10461073
struct pcpu_chunk *chunk;
10471074
const char *err;
1048-
int slot, off;
1075+
int slot, off, new_alloc;
1076+
unsigned long flags;
10491077

10501078
if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) {
10511079
WARN(true, "illegal size (%zu) or align (%zu) for "
@@ -1054,19 +1082,30 @@ static void *pcpu_alloc(size_t size, size_t align, bool reserved)
10541082
}
10551083

10561084
mutex_lock(&pcpu_alloc_mutex);
1057-
spin_lock_irq(&pcpu_lock);
1085+
spin_lock_irqsave(&pcpu_lock, flags);
10581086

10591087
/* serve reserved allocations from the reserved chunk if available */
10601088
if (reserved && pcpu_reserved_chunk) {
10611089
chunk = pcpu_reserved_chunk;
1062-
if (size > chunk->contig_hint ||
1063-
pcpu_extend_area_map(chunk) < 0) {
1064-
err = "failed to extend area map of reserved chunk";
1090+
1091+
if (size > chunk->contig_hint) {
1092+
err = "alloc from reserved chunk failed";
10651093
goto fail_unlock;
10661094
}
1095+
1096+
while ((new_alloc = pcpu_need_to_extend(chunk))) {
1097+
spin_unlock_irqrestore(&pcpu_lock, flags);
1098+
if (pcpu_extend_area_map(chunk, new_alloc) < 0) {
1099+
err = "failed to extend area map of reserved chunk";
1100+
goto fail_unlock_mutex;
1101+
}
1102+
spin_lock_irqsave(&pcpu_lock, flags);
1103+
}
1104+
10671105
off = pcpu_alloc_area(chunk, size, align);
10681106
if (off >= 0)
10691107
goto area_found;
1108+
10701109
err = "alloc from reserved chunk failed";
10711110
goto fail_unlock;
10721111
}
@@ -1078,14 +1117,20 @@ static void *pcpu_alloc(size_t size, size_t align, bool reserved)
10781117
if (size > chunk->contig_hint)
10791118
continue;
10801119

1081-
switch (pcpu_extend_area_map(chunk)) {
1082-
case 0:
1083-
break;
1084-
case 1:
1085-
goto restart; /* pcpu_lock dropped, restart */
1086-
default:
1087-
err = "failed to extend area map";
1088-
goto fail_unlock;
1120+
new_alloc = pcpu_need_to_extend(chunk);
1121+
if (new_alloc) {
1122+
spin_unlock_irqrestore(&pcpu_lock, flags);
1123+
if (pcpu_extend_area_map(chunk,
1124+
new_alloc) < 0) {
1125+
err = "failed to extend area map";
1126+
goto fail_unlock_mutex;
1127+
}
1128+
spin_lock_irqsave(&pcpu_lock, flags);
1129+
/*
1130+
* pcpu_lock has been dropped, need to
1131+
* restart cpu_slot list walking.
1132+
*/
1133+
goto restart;
10891134
}
10901135

10911136
off = pcpu_alloc_area(chunk, size, align);
@@ -1095,24 +1140,24 @@ static void *pcpu_alloc(size_t size, size_t align, bool reserved)
10951140
}
10961141

10971142
/* hmmm... no space left, create a new chunk */
1098-
spin_unlock_irq(&pcpu_lock);
1143+
spin_unlock_irqrestore(&pcpu_lock, flags);
10991144

11001145
chunk = alloc_pcpu_chunk();
11011146
if (!chunk) {
11021147
err = "failed to allocate new chunk";
11031148
goto fail_unlock_mutex;
11041149
}
11051150

1106-
spin_lock_irq(&pcpu_lock);
1151+
spin_lock_irqsave(&pcpu_lock, flags);
11071152
pcpu_chunk_relocate(chunk, -1);
11081153
goto restart;
11091154

11101155
area_found:
1111-
spin_unlock_irq(&pcpu_lock);
1156+
spin_unlock_irqrestore(&pcpu_lock, flags);
11121157

11131158
/* populate, map and clear the area */
11141159
if (pcpu_populate_chunk(chunk, off, size)) {
1115-
spin_lock_irq(&pcpu_lock);
1160+
spin_lock_irqsave(&pcpu_lock, flags);
11161161
pcpu_free_area(chunk, off);
11171162
err = "failed to populate";
11181163
goto fail_unlock;
@@ -1124,7 +1169,7 @@ static void *pcpu_alloc(size_t size, size_t align, bool reserved)
11241169
return __addr_to_pcpu_ptr(chunk->base_addr + off);
11251170

11261171
fail_unlock:
1127-
spin_unlock_irq(&pcpu_lock);
1172+
spin_unlock_irqrestore(&pcpu_lock, flags);
11281173
fail_unlock_mutex:
11291174
mutex_unlock(&pcpu_alloc_mutex);
11301175
if (warn_limit) {
@@ -1256,6 +1301,27 @@ void free_percpu(void *ptr)
12561301
}
12571302
EXPORT_SYMBOL_GPL(free_percpu);
12581303

1304+
/**
1305+
* per_cpu_ptr_to_phys - convert translated percpu address to physical address
1306+
* @addr: the address to be converted to physical address
1307+
*
1308+
* Given @addr which is dereferenceable address obtained via one of
1309+
* percpu access macros, this function translates it into its physical
1310+
* address. The caller is responsible for ensuring @addr stays valid
1311+
* until this function finishes.
1312+
*
1313+
* RETURNS:
1314+
* The physical address for @addr.
1315+
*/
1316+
phys_addr_t per_cpu_ptr_to_phys(void *addr)
1317+
{
1318+
if ((unsigned long)addr < VMALLOC_START ||
1319+
(unsigned long)addr >= VMALLOC_END)
1320+
return __pa(addr);
1321+
else
1322+
return page_to_phys(vmalloc_to_page(addr));
1323+
}
1324+
12591325
static inline size_t pcpu_calc_fc_sizes(size_t static_size,
12601326
size_t reserved_size,
12611327
ssize_t *dyn_sizep)

0 commit comments

Comments
 (0)