Skip to content

Commit 3df3212

Browse files
Alex ShiH. Peter Anvin
authored andcommitted
x86/tlb: add tlb_flushall_shift knob into debugfs
kernel will replace cr3 rewrite with invlpg when tlb_flush_entries <= active_tlb_entries / 2^tlb_flushall_factor if tlb_flushall_factor is -1, kernel won't do this replacement. User can modify its value according to specific CPU/applications. Thanks for Borislav providing the help message of CONFIG_DEBUG_TLBFLUSH. Signed-off-by: Alex Shi <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: H. Peter Anvin <[email protected]>
1 parent c4211f4 commit 3df3212

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

arch/x86/Kconfig.debug

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,25 @@ config DOUBLEFAULT
129129
option saves about 4k and might cause you much additional grey
130130
hair.
131131

132+
config DEBUG_TLBFLUSH
133+
bool "Set upper limit of TLB entries to flush one-by-one"
134+
depends on DEBUG_KERNEL && (X86_64 || X86_INVLPG)
135+
---help---
136+
137+
X86-only for now.
138+
139+
This option allows the user to tune the amount of TLB entries the
140+
kernel flushes one-by-one instead of doing a full TLB flush. In
141+
certain situations, the former is cheaper. This is controlled by the
142+
tlb_flushall_shift knob under /sys/kernel/debug/x86. If you set it
143+
to -1, the code flushes the whole TLB unconditionally. Otherwise,
144+
for positive values of it, the kernel will use single TLB entry
145+
invalidating instructions according to the following formula:
146+
147+
flush_entries <= active_tlb_entries / 2^tlb_flushall_shift
148+
149+
If in doubt, say "N".
150+
132151
config IOMMU_DEBUG
133152
bool "Enable IOMMU debugging"
134153
depends on GART_IOMMU && DEBUG_KERNEL

arch/x86/mm/tlb.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <asm/cache.h>
1313
#include <asm/apic.h>
1414
#include <asm/uv/uv.h>
15+
#include <linux/debugfs.h>
1516

1617
DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate)
1718
= { &init_mm, 0, };
@@ -430,3 +431,53 @@ void flush_tlb_all(void)
430431
{
431432
on_each_cpu(do_flush_tlb_all, NULL, 1);
432433
}
434+
435+
#ifdef CONFIG_DEBUG_TLBFLUSH
436+
static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
437+
size_t count, loff_t *ppos)
438+
{
439+
char buf[32];
440+
unsigned int len;
441+
442+
len = sprintf(buf, "%hd\n", tlb_flushall_shift);
443+
return simple_read_from_buffer(user_buf, count, ppos, buf, len);
444+
}
445+
446+
static ssize_t tlbflush_write_file(struct file *file,
447+
const char __user *user_buf, size_t count, loff_t *ppos)
448+
{
449+
char buf[32];
450+
ssize_t len;
451+
s8 shift;
452+
453+
len = min(count, sizeof(buf) - 1);
454+
if (copy_from_user(buf, user_buf, len))
455+
return -EFAULT;
456+
457+
buf[len] = '\0';
458+
if (kstrtos8(buf, 0, &shift))
459+
return -EINVAL;
460+
461+
if (shift > 64)
462+
return -EINVAL;
463+
464+
tlb_flushall_shift = shift;
465+
return count;
466+
}
467+
468+
static const struct file_operations fops_tlbflush = {
469+
.read = tlbflush_read_file,
470+
.write = tlbflush_write_file,
471+
.llseek = default_llseek,
472+
};
473+
474+
static int __cpuinit create_tlb_flushall_shift(void)
475+
{
476+
if (cpu_has_invlpg) {
477+
debugfs_create_file("tlb_flushall_shift", S_IRUSR | S_IWUSR,
478+
arch_debugfs_dir, NULL, &fops_tlbflush);
479+
}
480+
return 0;
481+
}
482+
late_initcall(create_tlb_flushall_shift);
483+
#endif

0 commit comments

Comments
 (0)