Skip to content

Commit ae1ee11

Browse files
rustyrussellAndi Kleen
authored andcommitted
[PATCH] i386: Use per-cpu variables for GDT, PDA
Allocating PDA and GDT at boot is a pain. Using simple per-cpu variables adds happiness (although we need the GDT page-aligned for Xen, which we do in a followup patch). [akpm@linux-foundation.org: build fix] Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Signed-off-by: Andi Kleen <ak@suse.de> Cc: Andi Kleen <ak@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 8f9aeca commit ae1ee11

7 files changed

Lines changed: 21 additions & 115 deletions

File tree

‎arch/i386/kernel/cpu/common.c‎

Lines changed: 13 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
DEFINE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
2626
EXPORT_PER_CPU_SYMBOL(cpu_gdt_descr);
2727

28-
struct i386_pda *_cpu_pda[NR_CPUS] __read_mostly;
29-
EXPORT_SYMBOL(_cpu_pda);
28+
DEFINE_PER_CPU(struct desc_struct, cpu_gdt[GDT_ENTRIES]);
29+
30+
DEFINE_PER_CPU(struct i386_pda, _cpu_pda);
31+
EXPORT_PER_CPU_SYMBOL(_cpu_pda);
3032

3133
static int cachesize_override __cpuinitdata = -1;
3234
static int disable_x86_fxsr __cpuinitdata;
@@ -609,52 +611,6 @@ struct pt_regs * __devinit idle_regs(struct pt_regs *regs)
609611
return regs;
610612
}
611613

612-
static __cpuinit int alloc_gdt(int cpu)
613-
{
614-
struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
615-
struct desc_struct *gdt;
616-
struct i386_pda *pda;
617-
618-
gdt = (struct desc_struct *)cpu_gdt_descr->address;
619-
pda = cpu_pda(cpu);
620-
621-
/*
622-
* This is a horrible hack to allocate the GDT. The problem
623-
* is that cpu_init() is called really early for the boot CPU
624-
* (and hence needs bootmem) but much later for the secondary
625-
* CPUs, when bootmem will have gone away
626-
*/
627-
if (NODE_DATA(0)->bdata->node_bootmem_map) {
628-
BUG_ON(gdt != NULL || pda != NULL);
629-
630-
gdt = alloc_bootmem_pages(PAGE_SIZE);
631-
pda = alloc_bootmem(sizeof(*pda));
632-
/* alloc_bootmem(_pages) panics on failure, so no check */
633-
634-
memset(gdt, 0, PAGE_SIZE);
635-
memset(pda, 0, sizeof(*pda));
636-
} else {
637-
/* GDT and PDA might already have been allocated if
638-
this is a CPU hotplug re-insertion. */
639-
if (gdt == NULL)
640-
gdt = (struct desc_struct *)get_zeroed_page(GFP_KERNEL);
641-
642-
if (pda == NULL)
643-
pda = kmalloc_node(sizeof(*pda), GFP_KERNEL, cpu_to_node(cpu));
644-
645-
if (unlikely(!gdt || !pda)) {
646-
free_pages((unsigned long)gdt, 0);
647-
kfree(pda);
648-
return 0;
649-
}
650-
}
651-
652-
cpu_gdt_descr->address = (unsigned long)gdt;
653-
cpu_pda(cpu) = pda;
654-
655-
return 1;
656-
}
657-
658614
/* Initial PDA used by boot CPU */
659615
struct i386_pda boot_pda = {
660616
._pda = &boot_pda,
@@ -670,31 +626,17 @@ static inline void set_kernel_fs(void)
670626
asm volatile ("mov %0, %%fs" : : "r" (__KERNEL_PDA) : "memory");
671627
}
672628

673-
/* Initialize the CPU's GDT and PDA. The boot CPU does this for
674-
itself, but secondaries find this done for them. */
675-
__cpuinit int init_gdt(int cpu, struct task_struct *idle)
629+
/* Initialize the CPU's GDT and PDA. This is either the boot CPU doing itself
630+
(still using cpu_gdt_table), or a CPU doing it for a secondary which
631+
will soon come up. */
632+
__cpuinit void init_gdt(int cpu, struct task_struct *idle)
676633
{
677634
struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
678-
struct desc_struct *gdt;
679-
struct i386_pda *pda;
680-
681-
/* For non-boot CPUs, the GDT and PDA should already have been
682-
allocated. */
683-
if (!alloc_gdt(cpu)) {
684-
printk(KERN_CRIT "CPU%d failed to allocate GDT or PDA\n", cpu);
685-
return 0;
686-
}
635+
struct desc_struct *gdt = per_cpu(cpu_gdt, cpu);
636+
struct i386_pda *pda = &per_cpu(_cpu_pda, cpu);
687637

688-
gdt = (struct desc_struct *)cpu_gdt_descr->address;
689-
pda = cpu_pda(cpu);
690-
691-
BUG_ON(gdt == NULL || pda == NULL);
692-
693-
/*
694-
* Initialize the per-CPU GDT with the boot GDT,
695-
* and set up the GDT descriptor:
696-
*/
697638
memcpy(gdt, cpu_gdt_table, GDT_SIZE);
639+
cpu_gdt_descr->address = (unsigned long)gdt;
698640
cpu_gdt_descr->size = GDT_SIZE - 1;
699641

700642
pack_descriptor((u32 *)&gdt[GDT_ENTRY_PDA].a,
@@ -706,17 +648,12 @@ __cpuinit int init_gdt(int cpu, struct task_struct *idle)
706648
pda->_pda = pda;
707649
pda->cpu_number = cpu;
708650
pda->pcurrent = idle;
709-
710-
return 1;
711651
}
712652

713653
void __cpuinit cpu_set_gdt(int cpu)
714654
{
715655
struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
716656

717-
/* Reinit these anyway, even if they've already been done (on
718-
the boot CPU, this will transition from the boot gdt+pda to
719-
the real ones). */
720657
load_gdt(cpu_gdt_descr);
721658
set_kernel_fs();
722659
}
@@ -804,13 +741,8 @@ void __cpuinit cpu_init(void)
804741
struct task_struct *curr = current;
805742

806743
/* Set up the real GDT and PDA, so we can transition from the
807-
boot versions. */
808-
if (!init_gdt(cpu, curr)) {
809-
/* failed to allocate something; not much we can do... */
810-
for (;;)
811-
local_irq_enable();
812-
}
813-
744+
boot_gdt_table & boot_pda. */
745+
init_gdt(cpu, curr);
814746
cpu_set_gdt(cpu);
815747
_cpu_init(cpu, curr);
816748
}

‎arch/i386/kernel/smpboot.c‎

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -808,13 +808,7 @@ static int __cpuinit do_boot_cpu(int apicid, int cpu)
808808
if (IS_ERR(idle))
809809
panic("failed fork for CPU %d", cpu);
810810

811-
/* Pre-allocate and initialize the CPU's GDT and PDA so it
812-
doesn't have to do any memory allocation during the
813-
delicate CPU-bringup phase. */
814-
if (!init_gdt(cpu, idle)) {
815-
printk(KERN_INFO "Couldn't allocate GDT/PDA for CPU %d\n", cpu);
816-
return -1; /* ? */
817-
}
811+
init_gdt(cpu, idle);
818812

819813
idle->thread.eip = (unsigned long) start_secondary;
820814
/* start_eip had better be page-aligned! */
@@ -940,26 +934,13 @@ static int __cpuinit __smp_prepare_cpu(int cpu)
940934
DECLARE_COMPLETION_ONSTACK(done);
941935
struct warm_boot_cpu_info info;
942936
int apicid, ret;
943-
struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
944937

945938
apicid = x86_cpu_to_apicid[cpu];
946939
if (apicid == BAD_APICID) {
947940
ret = -ENODEV;
948941
goto exit;
949942
}
950943

951-
/*
952-
* the CPU isn't initialized at boot time, allocate gdt table here.
953-
* cpu_init will initialize it
954-
*/
955-
if (!cpu_gdt_descr->address) {
956-
cpu_gdt_descr->address = get_zeroed_page(GFP_KERNEL);
957-
if (!cpu_gdt_descr->address)
958-
printk(KERN_CRIT "CPU%d failed to allocate GDT\n", cpu);
959-
ret = -ENOMEM;
960-
goto exit;
961-
}
962-
963944
info.complete = &done;
964945
info.apicid = apicid;
965946
info.cpu = cpu;

‎arch/i386/mach-voyager/voyager_smp.c‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -580,15 +580,7 @@ do_boot_cpu(__u8 cpu)
580580
/* init_tasks (in sched.c) is indexed logically */
581581
stack_start.esp = (void *) idle->thread.esp;
582582

583-
/* Pre-allocate and initialize the CPU's GDT and PDA so it
584-
doesn't have to do any memory allocation during the
585-
delicate CPU-bringup phase. */
586-
if (!init_gdt(cpu, idle)) {
587-
printk(KERN_INFO "Couldn't allocate GDT/PDA for CPU %d\n", cpu);
588-
cpucount--;
589-
return;
590-
}
591-
583+
init_gdt(cpu, idle);
592584
irq_ctx_init(cpu);
593585

594586
/* Note: Don't modify initial ss override */

‎include/asm-generic/percpu.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _ASM_GENERIC_PERCPU_H_
22
#define _ASM_GENERIC_PERCPU_H_
33
#include <linux/compiler.h>
4+
#include <linux/threads.h>
45

56
#define __GENERIC_PER_CPU
67
#ifdef CONFIG_SMP

‎include/asm-i386/desc.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct Xgt_desc_struct {
2222

2323
extern struct Xgt_desc_struct idt_descr;
2424
DECLARE_PER_CPU(struct Xgt_desc_struct, cpu_gdt_descr);
25+
DECLARE_PER_CPU(struct desc_struct, cpu_gdt[GDT_ENTRIES]);
2526
extern struct Xgt_desc_struct early_gdt_descr;
2627

2728
static inline struct desc_struct *get_cpu_gdt_table(unsigned int cpu)

‎include/asm-i386/pda.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <linux/stddef.h>
1010
#include <linux/types.h>
11+
#include <asm/percpu.h>
1112

1213
struct i386_pda
1314
{
@@ -18,10 +19,8 @@ struct i386_pda
1819
struct pt_regs *irq_regs;
1920
};
2021

21-
extern struct i386_pda *_cpu_pda[];
22-
23-
#define cpu_pda(i) (_cpu_pda[i])
24-
22+
DECLARE_PER_CPU(struct i386_pda, _cpu_pda);
23+
#define cpu_pda(i) (&per_cpu(_cpu_pda, (i)))
2524
#define pda_offset(field) offsetof(struct i386_pda, field)
2625

2726
extern void __bad_pda_field(void);

‎include/asm-i386/processor.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ extern unsigned long boot_option_idle_override;
743743
extern void enable_sep_cpu(void);
744744
extern int sysenter_setup(void);
745745

746-
extern int init_gdt(int cpu, struct task_struct *idle);
746+
extern void init_gdt(int cpu, struct task_struct *idle);
747747
extern void cpu_set_gdt(int);
748748
extern void secondary_cpu_init(void);
749749

0 commit comments

Comments
 (0)