Address Space Isolation for KVM
Subsystems (57)
cover Address Space Isolation for KVM
This patch series is a proof-of-concept RFC for an end-to-end implementation of Address Space Isolation for KVM. It has similar goals and a somewhat similar high-level design as the original ASI patches from Alexandre Chartre ([1],[2],[3],[4]), but with a different underlying implementation. This also includes several memory management changes to help with differentiating between sensitive and non-sensitive memory and mapping of non-sensitive memory into the ASI restricted address spaces.
This RFC is intended as a demonstration of what a full ASI implementation for KVM could look like, not necessarily as a direct proposal for what might eventually be merged. In particular, these patches do not yet implement KPTI on top of ASI, although the framework is generic enough to be able to support it. Similarly, these patches do not include non-sensitive annotations for data structures that did not get frequently accessed during execution of our test workloads, but the framework is designed such that new non-sensitive memory annotations can be added trivially.
The patches apply on top of Linux v5.16. These patches are also available via gerrit at https://linux-review.googlesource.com/q/topic:asi-rfc.
Background
==========
Address Space Isolation is a comprehensive security mitigation for several types of speculative execution attacks. Even though the kernel already has several speculative execution vulnerability mitigations, some of them can be quite expensive if enabled fully e.g. to fully mitigate L1TF using the existing mechanisms requires doing an L1 cache flush on every single VM entry as well as disabling hyperthreading altogether. (Although core scheduling can provide some protection when hyperthreading is enabled, it is not sufficient by itself to protect against all leaks unless sibling hyperthread stunning is also performed on every VM exit.) ASI provides a much less expensive mitigation for such vulnerabilities while still providing an almost similar level of protection.
There are a couple of basic insights/assumptions behind ASI:
1. Most execution paths in the kernel (especially during virtual machine execution) access only memory that is not particularly sensitive even if it were to get leaked to the executing process/VM (setting aside for a moment what exactly should be considered sensitive or non-sensitive).
2. Even when executing speculatively, the CPU can generally only bring memory that is mapped in the current page tables into its various caches and internal buffers.
Given these, the idea of using ASI to thwart speculative attacks is that we can execute the kernel using a restricted set of page tables most of the time and switch to the full unrestricted kernel address space only when the kernel needs to access something that is not mapped in the restricted address space. And we keep track of when a switch to the full kernel address space is done, so that before returning back to the process/VM, we can switch back to the restricted address space. In the paths where the kernel is able to execute entirely while remaining in the restricted address space, we can skip other mitigations for speculative execution attacks (such as L1 cache / micro-arch buffer flushes, sibling hyperthread stunning etc.). Only in the cases where we do end up switching the page tables, we perform these more expensive mitigations. Assuming that happens relatively infrequently, the performance can be significantly better compared to performing these mitigations all the time.
Please note that although we do have a sibling hyperthread stunning implementation internally, which is fully integrated with KVM-ASI, it is not included in this RFC for the time being. The earlier upstream proposal for sibling stunning [6] could potentially be integrated into an upstream ASI implementation.
Basic concepts
==============
Different types of restricted address spaces are represented by different ASI classes. For instance, KVM-ASI is an ASI class used during VM execution. KPTI would be another ASI class. An ASI instance (struct asi) represents a single restricted address space. There is a separate ASI instance for each untrusted context (e.g. a userspace process, a VM, or even a single VCPU etc.) Note that there can be multiple untrusted security contexts (and thus multiple restricted address spaces) within a single process e.g. in the case of VMs, the userspace process is a different security context than the guest VM, and in principle, even each VCPU could be considered a separate security context (That would be primarily useful for securing nested virtualization).
In this RFC, a process can have at most one ASI instance of each class, though this is not an inherent limitation and multiple instances of the same class should eventually be supported. (A process can still have ASI instances of different classes e.g. KVM-ASI and KPTI.) In fact, in principle, it is not even entirely necessary to tie an ASI instance to a process. That is just a simplification for the initial implementation.
An asi_enter operation switches into the restricted address space represented by the given ASI instance. An asi_exit operation switches to the full unrestricted kernel address space. Each ASI class can provide hooks to be executed during these operations, which can be used to perform speculative attack mitigations relevant to that class. For instance, the KVM-ASI hooks would perform a sibling-hyperthread-stun operation in the asi_exit hook, and L1-flush/MDS-clear and sibling-hyperthread-unstun operations in the asi_enter hook. On the other hand, the hooks for the KPTI class would be NO-OP, since the switching of the page tables is enough mitigation in that case.
If the kernel attempts to access memory that is not mapped in the currently active ASI instance, the page fault handler automatically performs an asi_exit operation. This means that except for a few critical pieces of memory, leaving something out of an unrestricted address space will result in only a performance hit, rather than a catastrophic failure. The kernel can also perform explicit asi_exit operations in some paths as needed.
Apart from the page fault handler, other exceptions and interrupts (even NMIs) do not automatically cause an asi_exit and could potentially be executed completely within a restricted address space if they don't end up accessing any sensitive piece of memory.
The mappings within a restricted address space are always a subset of the full kernel address space and each mapping is always the same as the corresponding mapping in the full kernel address space. This is necessary because we could potentially end up performing an asi_exit at any point.
Although this RFC only includes an implementation of the KVM-ASI class, a KPTI class could also be implemented on top of the same infrastructure. Furthermore, in the future we could also implement a KPTI-Next class that actually uses the ASI model for userspace processes i.e. mapping non-sensitive kernel memory in the restricted address space and trying to execute most syscalls/interrupts without switching to the full kernel address space, as opposed to the current KPTI which requires an address space switch on every kernel/user mode transition.
Memory classification
=====================
We divide memory into three categories.
1. Sensitive memory
This is memory that should never get leaked to any process or VM. Sensitive memory is only mapped in the unrestricted kernel page tables. By default, all memory is considered sensitive unless specifically categorized otherwise.
2. Globally non-sensitive memory
This is memory that does not present a substantial security threat even if it were to get leaked to any process or VM in the system. Globally non-sensitive memory is mapped in the restricted address spaces for all processes.
3. Locally non-sensitive memory
This is memory that does not present a substantial security threat if it were to get leaked to the currently running process or VM, but would present a security issue if it were to get leaked to any other process or VM in the system. Examples include userspace memory (or guest memory in the case of VMs) or kernel structures containing userspace/guest register context etc. Locally non-sensitive memory is mapped only in the restricted address space of a single process.
Various mechanisms are provided to annotate different types of memory (static, buddy allocator, slab, vmalloc etc.) as globally or locally non-sensitive. In addition, the ASI infrastructure takes care to ensure that different classes of memory do not share the same physical page. This includes separation of sensitive, globally non-sensitive and locally non-sensitive memory into different pages and also separation of locally non-sensitive memory for different processes into different pages as well.
What exactly should be considered non-sensitive (either globally or locally) is somewhat open-ended. Some things are clearly sensitive or non-sensitive, but many things also fall into a gray area, depending on how paranoid one wants to be. For this proof of concept, we have generally treated such things as non-sensitive, though that may not necessarily be the ideal classification in each case. Similarly, there is also a gray area between globally and locally non-sensitive classifications in some cases, and in those cases this RFC has mostly erred on the side of marking them as locally non-sensitive, even though many of those cases could likely be safely classified as globally non-sensitive.
Although this implementation includes fairly extensive support for marking most types of dynamically allocated memory as locally non-sensitive, it is possibly feasible, at least for KVM-ASI, to get away with a simpler implementation (such as [5]), if we are very selective about what memory we treat as locally non-sensitive (as opposed to globally non-sensitive). Nevertheless, the more general mechanism is included in this proof of concept as an illustration for what could be done if we really needed to treat any arbitrary kernel memory as locally non-sensitive.
It is also possible to have ASI classes that do not utilize the above described infrastructure and instead manage all the memory mappings inside the restricted address space on their own.
References
==========
[1] https://lore.kernel.org/lkml/[email protected]
[2] https://lore.kernel.org/lkml/[email protected]
[3] https://lore.kernel.org/lkml/[email protected]
[4] https://lore.kernel.org/lkml/[email protected]
[5] https://lore.kernel.org/lkml/[email protected]
[6] https://lore.kernel.org/lkml/[email protected]
Junaid Shahid (32):
mm: asi: Introduce ASI core API
mm: asi: Add command-line parameter to enable/disable ASI
mm: asi: Switch to unrestricted address space when entering scheduler
mm: asi: ASI support in interrupts/exceptions
mm: asi: Make __get_current_cr3_fast() ASI-aware
mm: asi: ASI page table allocation and free functions
mm: asi: Functions to map/unmap a memory range into ASI page tables
mm: asi: Add basic infrastructure for global non-sensitive mappings
mm: Add __PAGEFLAG_FALSE
mm: asi: Support for global non-sensitive direct map allocations
mm: asi: Global non-sensitive vmalloc/vmap support
mm: asi: Support for global non-sensitive slab caches
mm: asi: Disable ASI API when ASI is not enabled for a process
kvm: asi: Restricted address space for VM execution
mm: asi: Support for mapping non-sensitive pcpu chunks
mm: asi: Aliased direct map for local non-sensitive allocations
mm: asi: Support for pre-ASI-init local non-sensitive allocations
mm: asi: Support for locally nonsensitive page allocations
mm: asi: Support for locally non-sensitive vmalloc allocations
mm: asi: Add support for locally non-sensitive VM_USERMAP pages
mm: asi: Add support for mapping all userspace memory into ASI
mm: asi: Support for local non-sensitive slab caches
mm: asi: Avoid warning from NMI userspace accesses in ASI context
mm: asi: Use separate PCIDs for restricted address spaces
mm: asi: Avoid TLB flushes during ASI CR3 switches when possible
mm: asi: Avoid TLB flush IPIs to CPUs not in ASI context
mm: asi: Reduce TLB flushes when freeing pages asynchronously
mm: asi: Add API for mapping userspace address ranges
mm: asi: Support for non-sensitive SLUB caches
x86: asi: Allocate FPU state separately when ASI is enabled.
kvm: asi: Map guest memory into restricted ASI address space
kvm: asi: Unmap guest memory from ASI address space when using nested
virt
Ofir Weisse (15):
asi: Added ASI memory cgroup flag
mm: asi: Added refcounting when initilizing an asi
mm: asi: asi_exit() on PF, skip handling if address is accessible
mm: asi: Adding support for dynamic percpu ASI allocations
mm: asi: ASI annotation support for static variables.
mm: asi: ASI annotation support for dynamic modules.
mm: asi: Skip conventional L1TF/MDS mitigations
mm: asi: support for static percpu DEFINE_PER_CPU*_ASI
mm: asi: Annotation of static variables to be nonsensitive
mm: asi: Annotation of PERCPU variables to be nonsensitive
mm: asi: Annotation of dynamic variables to be nonsensitive
kvm: asi: Splitting kvm_vcpu_arch into non/sensitive parts
mm: asi: Mapping global nonsensitive areas in asi_global_init
kvm: asi: Do asi_exit() in vcpu_run loop before returning to userspace
mm: asi: Properly un/mapping task stack from ASI + tlb flush
arch/alpha/include/asm/Kbuild | 1 + arch/arc/include/asm/Kbuild | 1 + arch/arm/include/asm/Kbuild | 1 + arch/arm64/include/asm/Kbuild | 1 + arch/csky/include/asm/Kbuild | 1 + arch/h8300/include/asm/Kbuild | 1 + arch/hexagon/include/asm/Kbuild | 1 + arch/ia64/include/asm/Kbuild | 1 + arch/m68k/include/asm/Kbuild | 1 + arch/microblaze/include/asm/Kbuild | 1 + arch/mips/include/asm/Kbuild | 1 + arch/nds32/include/asm/Kbuild | 1 + arch/nios2/include/asm/Kbuild | 1 + arch/openrisc/include/asm/Kbuild | 1 + arch/parisc/include/asm/Kbuild | 1 + arch/powerpc/include/asm/Kbuild | 1 + arch/riscv/include/asm/Kbuild | 1 + arch/s390/include/asm/Kbuild | 1 + arch/sh/include/asm/Kbuild | 1 + arch/sparc/include/asm/Kbuild | 1 + arch/um/include/asm/Kbuild | 1 + arch/x86/events/core.c | 6 +- arch/x86/events/intel/bts.c | 2 +- arch/x86/events/intel/core.c | 2 +- arch/x86/events/msr.c | 2 +- arch/x86/events/perf_event.h | 4 +- arch/x86/include/asm/asi.h | 215 ++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/current.h | 2 +- arch/x86/include/asm/debugreg.h | 2 +- arch/x86/include/asm/desc.h | 2 +- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/fpu/api.h | 3 +- arch/x86/include/asm/hardirq.h | 2 +- arch/x86/include/asm/hw_irq.h | 2 +- arch/x86/include/asm/idtentry.h | 25 +- arch/x86/include/asm/kvm_host.h | 124 +- arch/x86/include/asm/page.h | 19 +- arch/x86/include/asm/page_64.h | 27 +- arch/x86/include/asm/page_64_types.h | 20 + arch/x86/include/asm/percpu.h | 2 +- arch/x86/include/asm/pgtable_64_types.h | 10 + arch/x86/include/asm/preempt.h | 2 +- arch/x86/include/asm/processor.h | 17 +- arch/x86/include/asm/smp.h | 2 +- arch/x86/include/asm/tlbflush.h | 49 +- arch/x86/include/asm/topology.h | 2 +- arch/x86/kernel/alternative.c | 2 +- arch/x86/kernel/apic/apic.c | 2 +- arch/x86/kernel/apic/x2apic_cluster.c | 8 +- arch/x86/kernel/cpu/bugs.c | 2 +- arch/x86/kernel/cpu/common.c | 12 +- arch/x86/kernel/e820.c | 7 +- arch/x86/kernel/fpu/core.c | 47 +- arch/x86/kernel/fpu/init.c | 7 +- arch/x86/kernel/fpu/internal.h | 1 + arch/x86/kernel/fpu/xstate.c | 21 +- arch/x86/kernel/head_64.S | 12 + arch/x86/kernel/hw_breakpoint.c | 2 +- arch/x86/kernel/irq.c | 2 +- arch/x86/kernel/irqinit.c | 2 +- arch/x86/kernel/nmi.c | 6 +- arch/x86/kernel/process.c | 13 +- arch/x86/kernel/setup.c | 4 +- arch/x86/kernel/setup_percpu.c | 4 +- arch/x86/kernel/smp.c | 2 +- arch/x86/kernel/smpboot.c | 3 +- arch/x86/kernel/traps.c | 2 + arch/x86/kernel/tsc.c | 10 +- arch/x86/kernel/vmlinux.lds.S | 2 +- arch/x86/kvm/cpuid.c | 18 +- arch/x86/kvm/kvm_cache_regs.h | 22 +- arch/x86/kvm/lapic.c | 11 +- arch/x86/kvm/mmu.h | 16 +- arch/x86/kvm/mmu/mmu.c | 209 ++-- arch/x86/kvm/mmu/mmu_internal.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 40 +- arch/x86/kvm/mmu/spte.c | 6 +- arch/x86/kvm/mmu/spte.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 14 +- arch/x86/kvm/mtrr.c | 2 +- arch/x86/kvm/svm/nested.c | 34 +- arch/x86/kvm/svm/sev.c | 70 +- arch/x86/kvm/svm/svm.c | 52 +- arch/x86/kvm/trace.h | 10 +- arch/x86/kvm/vmx/capabilities.h | 14 +- arch/x86/kvm/vmx/nested.c | 90 +- arch/x86/kvm/vmx/vmx.c | 152 ++- arch/x86/kvm/x86.c | 315 +++-- arch/x86/kvm/x86.h | 4 +- arch/x86/mm/Makefile | 1 + arch/x86/mm/asi.c | 1397 ++++++++++++++++++++++ arch/x86/mm/fault.c | 67 +- arch/x86/mm/init.c | 7 +- arch/x86/mm/init_64.c | 26 +- arch/x86/mm/kaslr.c | 34 +- arch/x86/mm/mm_internal.h | 5 + arch/x86/mm/physaddr.c | 8 + arch/x86/mm/tlb.c | 419 ++++++- arch/xtensa/include/asm/Kbuild | 1 + fs/binfmt_elf.c | 2 +- fs/eventfd.c | 2 +- fs/eventpoll.c | 10 +- fs/exec.c | 7 + fs/file.c | 3 +- fs/timerfd.c | 2 +- include/asm-generic/asi.h | 149 +++ include/asm-generic/irq_regs.h | 2 +- include/asm-generic/percpu.h | 6 + include/asm-generic/vmlinux.lds.h | 36 +- include/linux/arch_topology.h | 2 +- include/linux/debug_locks.h | 4 +- include/linux/gfp.h | 13 +- include/linux/hrtimer.h | 2 +- include/linux/interrupt.h | 2 +- include/linux/jiffies.h | 4 +- include/linux/kernel_stat.h | 4 +- include/linux/kvm_host.h | 7 +- include/linux/kvm_types.h | 3 + include/linux/memcontrol.h | 3 + include/linux/mm_types.h | 59 + include/linux/module.h | 15 + include/linux/notifier.h | 2 +- include/linux/page-flags.h | 19 + include/linux/percpu-defs.h | 39 + include/linux/percpu.h | 8 +- include/linux/pgtable.h | 3 + include/linux/prandom.h | 2 +- include/linux/profile.h | 2 +- include/linux/rcupdate.h | 4 +- include/linux/rcutree.h | 2 +- include/linux/sched.h | 5 + include/linux/sched/mm.h | 12 + include/linux/sched/sysctl.h | 1 + include/linux/slab.h | 68 +- include/linux/slab_def.h | 4 + include/linux/slub_def.h | 6 + include/linux/vmalloc.h | 16 +- include/trace/events/mmflags.h | 14 +- init/main.c | 2 +- kernel/cgroup/cgroup.c | 9 +- kernel/cpu.c | 14 +- kernel/entry/common.c | 6 + kernel/events/core.c | 25 +- kernel/exit.c | 2 + kernel/fork.c | 69 +- kernel/freezer.c | 2 +- kernel/irq_work.c | 6 +- kernel/locking/lockdep.c | 14 +- kernel/module-internal.h | 1 + kernel/module.c | 210 +++- kernel/panic.c | 2 +- kernel/printk/printk.c | 4 +- kernel/profile.c | 4 +- kernel/rcu/srcutree.c | 3 +- kernel/rcu/tree.c | 12 +- kernel/rcu/update.c | 4 +- kernel/sched/clock.c | 2 +- kernel/sched/core.c | 23 +- kernel/sched/cpuacct.c | 10 +- kernel/sched/cpufreq.c | 3 +- kernel/sched/cputime.c | 4 +- kernel/sched/fair.c | 7 +- kernel/sched/loadavg.c | 2 +- kernel/sched/rt.c | 2 +- kernel/sched/sched.h | 25 +- kernel/sched/topology.c | 28 +- kernel/smp.c | 26 +- kernel/softirq.c | 5 +- kernel/time/hrtimer.c | 4 +- kernel/time/jiffies.c | 8 +- kernel/time/ntp.c | 30 +- kernel/time/tick-common.c | 6 +- kernel/time/tick-internal.h | 6 +- kernel/time/tick-sched.c | 4 +- kernel/time/timekeeping.c | 10 +- kernel/time/timekeeping.h | 2 +- kernel/time/timer.c | 4 +- kernel/trace/ring_buffer.c | 5 +- kernel/trace/trace.c | 4 +- kernel/trace/trace_preemptirq.c | 2 +- kernel/trace/trace_sched_switch.c | 4 +- kernel/tracepoint.c | 2 +- kernel/watchdog.c | 12 +- lib/debug_locks.c | 5 +- lib/irq_regs.c | 2 +- lib/radix-tree.c | 6 +- lib/random32.c | 3 +- mm/init-mm.c | 2 + mm/internal.h | 3 + mm/memcontrol.c | 37 +- mm/memory.c | 4 +- mm/page_alloc.c | 204 +++- mm/percpu-internal.h | 23 +- mm/percpu-km.c | 5 +- mm/percpu-vm.c | 57 +- mm/percpu.c | 273 ++++- mm/slab.c | 42 +- mm/slab.h | 166 ++- mm/slab_common.c | 461 ++++++- mm/slub.c | 140 ++- mm/sparse.c | 4 +- mm/util.c | 3 +- mm/vmalloc.c | 193 ++- net/core/skbuff.c | 2 +- net/core/sock.c | 2 +- security/Kconfig | 12 + tools/perf/builtin-kmem.c | 2 + virt/kvm/coalesced_mmio.c | 2 +- virt/kvm/eventfd.c | 5 +- virt/kvm/kvm_main.c | 61 +- 211 files changed, 5727 insertions(+), 959 deletions(-) create mode 100644 arch/x86/include/asm/asi.h create mode 100644 arch/x86/mm/asi.c create mode 100644 include/asm-generic/asi.h
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 1/47 mm: asi: Introduce ASI core API
Introduce core API for Address Space Isolation (ASI). Kernel address space isolation provides the ability to run some kernel code with a reduced kernel address space.
There can be multiple classes of such restricted kernel address spaces (e.g. KPTI, KVM-PTI etc.). Each ASI class is identified by an index. The ASI class can register some hooks to be called when entering/exiting the restricted address space.
Currently, there is a fixed maximum number of ASI classes supported. In addition, each process can have at most one restricted address space from each ASI class. Neither of these are inherent limitations and are merely simplifying assumptions for the time being.
(The Kconfig and the high-level ASI API are derived from the original ASI RFC by Alexandre Chartre).
--- arch/alpha/include/asm/Kbuild | 1 + arch/arc/include/asm/Kbuild | 1 + arch/arm/include/asm/Kbuild | 1 + arch/arm64/include/asm/Kbuild | 1 + arch/csky/include/asm/Kbuild | 1 + arch/h8300/include/asm/Kbuild | 1 + arch/hexagon/include/asm/Kbuild | 1 + arch/ia64/include/asm/Kbuild | 1 + arch/m68k/include/asm/Kbuild | 1 + arch/microblaze/include/asm/Kbuild | 1 + arch/mips/include/asm/Kbuild | 1 + arch/nds32/include/asm/Kbuild | 1 + arch/nios2/include/asm/Kbuild | 1 + arch/openrisc/include/asm/Kbuild | 1 + arch/parisc/include/asm/Kbuild | 1 + arch/powerpc/include/asm/Kbuild | 1 + arch/riscv/include/asm/Kbuild | 1 + arch/s390/include/asm/Kbuild | 1 + arch/sh/include/asm/Kbuild | 1 + arch/sparc/include/asm/Kbuild | 1 + arch/um/include/asm/Kbuild | 1 + arch/x86/include/asm/asi.h | 81 +++++++++++++++ arch/x86/include/asm/tlbflush.h | 2 + arch/x86/mm/Makefile | 1 + arch/x86/mm/asi.c | 152 +++++++++++++++++++++++++++++ arch/x86/mm/init.c | 5 +- arch/x86/mm/tlb.c | 2 +- arch/xtensa/include/asm/Kbuild | 1 + include/asm-generic/asi.h | 51 ++++++++++ include/linux/mm_types.h | 3 + kernel/fork.c | 3 + security/Kconfig | 10 ++ 32 files changed, 329 insertions(+), 3 deletions(-) create mode 100644 arch/x86/include/asm/asi.h create mode 100644 arch/x86/mm/asi.c create mode 100644 include/asm-generic/asi.h
arch/alpha/include/asm/Kbuild +1 -0
@@ -4,3 +4,4 @@ generated-y += syscall_table.h generic-y += export.h generic-y += kvm_para.h generic-y += mcs_spinlock.h +generic-y += asi.h
arch/arc/include/asm/Kbuild +1 -0
@@ -4,3 +4,4 @@ generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += parport.h generic-y += user.h +generic-y += asi.h
arch/arm/include/asm/Kbuild +1 -0
@@ -6,3 +6,4 @@ generic-y += parport.h generated-y += mach-types.h generated-y += unistd-nr.h +generic-y += asi.h
arch/arm64/include/asm/Kbuild +1 -0
@@ -4,5 +4,6 @@ generic-y += mcs_spinlock.h generic-y += qrwlock.h generic-y += qspinlock.h generic-y += user.h +generic-y += asi.h generated-y += cpucaps.h
arch/csky/include/asm/Kbuild +1 -0
@@ -6,3 +6,4 @@ generic-y += kvm_para.h generic-y += qrwlock.h generic-y += user.h generic-y += vmlinux.lds.h +generic-y += asi.h
arch/h8300/include/asm/Kbuild +1 -0
@@ -6,3 +6,4 @@ generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += parport.h generic-y += spinlock.h +generic-y += asi.h
arch/hexagon/include/asm/Kbuild +1 -0
@@ -3,3 +3,4 @@ generic-y += extable.h generic-y += iomap.h generic-y += kvm_para.h generic-y += mcs_spinlock.h +generic-y += asi.h
arch/ia64/include/asm/Kbuild +1 -0
@@ -3,3 +3,4 @@ generated-y += syscall_table.h generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += vtime.h +generic-y += asi.h
arch/m68k/include/asm/Kbuild +1 -0
@@ -4,3 +4,4 @@ generic-y += extable.h generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += spinlock.h +generic-y += asi.h
arch/microblaze/include/asm/Kbuild +1 -0
@@ -8,3 +8,4 @@ generic-y += parport.h generic-y += syscalls.h generic-y += tlb.h generic-y += user.h +generic-y += asi.h
arch/mips/include/asm/Kbuild +1 -0
@@ -14,3 +14,4 @@ generic-y += parport.h generic-y += qrwlock.h generic-y += qspinlock.h generic-y += user.h +generic-y += asi.h
arch/nds32/include/asm/Kbuild +1 -0
@@ -6,3 +6,4 @@ generic-y += gpio.h generic-y += kvm_para.h generic-y += parport.h generic-y += user.h +generic-y += asi.h
arch/nios2/include/asm/Kbuild +1 -0
@@ -5,3 +5,4 @@ generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += spinlock.h generic-y += user.h +generic-y += asi.h
arch/openrisc/include/asm/Kbuild +1 -0
@@ -7,3 +7,4 @@ generic-y += qspinlock.h generic-y += qrwlock_types.h generic-y += qrwlock.h generic-y += user.h +generic-y += asi.h
arch/parisc/include/asm/Kbuild +1 -0
@@ -4,3 +4,4 @@ generated-y += syscall_table_64.h generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += user.h +generic-y += asi.h
arch/powerpc/include/asm/Kbuild +1 -0
@@ -8,3 +8,4 @@ generic-y += mcs_spinlock.h generic-y += qrwlock.h generic-y += vtime.h generic-y += early_ioremap.h +generic-y += asi.h
arch/riscv/include/asm/Kbuild +1 -0
@@ -5,3 +5,4 @@ generic-y += flat.h generic-y += kvm_para.h generic-y += user.h generic-y += vmlinux.lds.h +generic-y += asi.h
arch/s390/include/asm/Kbuild +1 -0
@@ -8,3 +8,4 @@ generic-y += asm-offsets.h generic-y += export.h generic-y += kvm_types.h generic-y += mcs_spinlock.h +generic-y += asi.h
arch/sh/include/asm/Kbuild +1 -0
@@ -3,3 +3,4 @@ generated-y += syscall_table.h generic-y += kvm_para.h generic-y += mcs_spinlock.h generic-y += parport.h +generic-y += asi.h
arch/sparc/include/asm/Kbuild +1 -0
@@ -4,3 +4,4 @@ generated-y += syscall_table_64.h generic-y += export.h generic-y += kvm_para.h generic-y += mcs_spinlock.h +generic-y += asi.h
arch/um/include/asm/Kbuild +1 -0
@@ -27,3 +27,4 @@ generic-y += word-at-a-time.h generic-y += kprobes.h generic-y += mm_hooks.h generic-y += vga.h +generic-y += asi.h
arch/x86/include/asm/asi.h new file +81 -0
81 lines not on this page · raw message
arch/x86/include/asm/tlbflush.h +2 -0
@@ -259,6 +259,8 @@ static inline void arch_tlbbatch_add_mm(struct arch_tlbflush_unmap_batch *batch,extern void arch_tlbbatch_flush (struct arch_tlbflush_unmap_batch *batch); +unsigned long build_cr3 (pgd_t *pgd,u16 asid); +#endif /* !MODULE */ #endif /* _ASM_X86_TLBFLUSH_H */
arch/x86/mm/Makefile +1 -0
@@ -51,6 +51,7 @@ obj-$(CONFIG_NUMA_EMU) += numa_emulation.o obj-$(CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS) += pkeys.o obj-$(CONFIG_RANDOMIZE_MEMORY) += kaslr.o obj-$(CONFIG_PAGE_TABLE_ISOLATION) += pti.o +obj-$(CONFIG_ADDRESS_SPACE_ISOLATION) += asi.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_identity.o
arch/x86/mm/asi.c new file +152 -0
152 lines not on this page · raw message
arch/x86/mm/init.c +3 -2
@@ -238,8 +238,9 @@ static void __init probe_page_size_mask(void)/* By the default is everything supported: */ __default_kernel_pte_mask = __supported_pte_mask; -/* Except when with PTI where the kernel is mostly non-Global: */ -if (cpu_feature_enabled (X86_FEATURE_PTI )) +/* Except when with PTI or ASI where the kernel is mostly non-Global: */ +if (cpu_feature_enabled (X86_FEATURE_PTI ) || +IS_ENABLED (CONFIG_ADDRESS_SPACE_ISOLATION )) __default_kernel_pte_mask &= ~_PAGE_GLOBAL;/* Enable 1 GB linear kernel mappings if available: */
arch/x86/mm/tlb.c +1 -1
@@ -153,7 +153,7 @@ static inline u16 user_pcid(u16 asid)return ret; } -static inline unsigned long build_cr3 (pgd_t *pgd,u16 asid) +inline unsigned long build_cr3 (pgd_t *pgd,u16 asid) {if (static_cpu_has (X86_FEATURE_PCID )) {return __sme_pa (pgd) |kern_pcid (asid);
arch/xtensa/include/asm/Kbuild +1 -0
@@ -7,3 +7,4 @@ generic-y += param.h generic-y += qrwlock.h generic-y += qspinlock.h generic-y += user.h +generic-y += asi.h
include/asm-generic/asi.h new file +51 -0
51 lines not on this page · raw message
include/linux/mm_types.h +3 -0
@@ -18,6 +18,7 @@#include <linux/seqlock.h> #include <asm/mmu.h> +#include <asm/asi.h> #ifndef AT_VECTOR_SIZE_ARCH #define AT_VECTOR_SIZE_ARCH 0
@@ -495,6 +496,8 @@ struct mm_struct {atomic_t membarrier_state;#endif +struct asi asi[ASI_MAX_NUM ]; +/** * @mm_users: The number of users including userspace. *
kernel/fork.c +3 -0
@@ -102,6 +102,7 @@#include <asm/mmu_context.h> #include <asm/cacheflush.h> #include <asm/tlbflush.h> +#include <asm/asi.h> #include <trace/events/sched.h>
@@ -1071,6 +1072,8 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, mm->def_flags =0 ; } +asi_init_mm_state (mm); +if (mm_alloc_pgd (mm)) goto fail_nopgd;
security/Kconfig +10 -0
@@ -65,6 +65,16 @@ config PAGE_TABLE_ISOLATION See Documentation/x86/pti.rst for more details. +config ADDRESS_SPACE_ISOLATION + bool "Allow code to run with a reduced kernel address space" + default n + depends on X86_64 && !UML + depends on !PARAVIRT + help + This feature provides the ability to run some kernel code + with a reduced kernel address space. This can be used to + mitigate some speculative execution attacks. + config SECURITY_INFINIBAND bool "Infiniband Security Hooks" depends on SECURITY && INFINIBAND
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 2/47 mm: asi: Add command-line parameter to enable/disable ASI
In reply to Junaid ShahidA parameter named "asi" is added, disabled by default. A feature flag X86_FEATURE_ASI is set if ASI is enabled.
--- arch/x86/include/asm/asi.h | 17 ++++++++++---- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 ++++++- arch/x86/mm/asi.c | 29 ++++++++++++++++++++++++ arch/x86/mm/init.c | 2 +- include/asm-generic/asi.h | 2 ++ 6 files changed, 53 insertions(+), 6 deletions(-)
arch/x86/include/asm/asi.h +13 -4
@@ -6,6 +6,7 @@#include <asm/pgtable_types.h> #include <asm/percpu.h> +#include <asm/cpufeature.h> #ifdef CONFIG_ADDRESS_SPACE_ISOLATION
@@ -52,18 +53,24 @@ void asi_exit(void);static inline void asi_set_target_unrestricted (void ) { -barrier (); -this_cpu_write (asi_cpu_state.target_asi,NULL ); +if (static_cpu_has (X86_FEATURE_ASI )) { +barrier (); +this_cpu_write (asi_cpu_state.target_asi,NULL ); + } }static inline struct asi *asi_get_current (void ) { -return this_cpu_read (asi_cpu_state.curr_asi); +return static_cpu_has (X86_FEATURE_ASI ) + ?this_cpu_read (asi_cpu_state.curr_asi) + :NULL ; }static inline struct asi *asi_get_target (void ) { -return this_cpu_read (asi_cpu_state.target_asi); +return static_cpu_has (X86_FEATURE_ASI ) + ?this_cpu_read (asi_cpu_state.target_asi) + :NULL ; }static inline bool is_asi_active (void )
@@ -76,6 +83,8 @@ static inline bool asi_is_target_unrestricted(void)return !asi_get_target (); } +#define static_asi_enabled () cpu_feature_enabled(X86_FEATURE_ASI) +#endif /* CONFIG_ADDRESS_SPACE_ISOLATION */ #endif
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -295,6 +295,7 @@#define X86_FEATURE_PER_THREAD_MBA (11*32+ 7)/* "" Per-thread Memory Bandwidth Allocation */ #define X86_FEATURE_SGX1 (11*32+ 8)/* "" Basic SGX */ #define X86_FEATURE_SGX2 (11*32+ 9)/* "" SGX Enclave Dynamic Memory Management (EDMM) */ +#define X86_FEATURE_ASI (11*32+10)/* Kernel Address Space Isolation */ /* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */ #define X86_FEATURE_AVX_VNNI (12*32+ 4)/* AVX VNNI instructions */
arch/x86/include/asm/disabled-features.h +7 -1
@@ -56,6 +56,12 @@# define DISABLE_PTI (1 << (X86_FEATURE_PTI & 31))#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +# define DISABLE_ASI 0 +#else +# define DISABLE_ASI (1 << (X86_FEATURE_ASI & 31)) +#endif +/* Force disable because it's broken beyond repair */ #define DISABLE_ENQCMD (1 << (X86_FEATURE_ENQCMD & 31))
@@ -79,7 +85,7 @@#define DISABLED_MASK8 0#define DISABLED_MASK9 (DISABLE_SMAP|DISABLE_SGX)#define DISABLED_MASK10 0 -#define DISABLED_MASK11 0 +#define DISABLED_MASK11 (DISABLE_ASI)#define DISABLED_MASK12 0#define DISABLED_MASK13 0#define DISABLED_MASK14 0
arch/x86/mm/asi.c +29 -0
@@ -1,5 +1,7 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/init.h> +#include <asm/asi.h> #include <asm/pgalloc.h> #include <asm/mmu_context.h>
@@ -18,6 +20,9 @@ int asi_register_class(const char *name, uint flags, {int i; +if (!boot_cpu_has (X86_FEATURE_ASI )) +return 0 ; +VM_BUG_ON (name ==NULL );spin_lock (&asi_class_lock);
@@ -43,6 +48,9 @@ EXPORT_SYMBOL_GPL(asi_register_class);void asi_unregister_class (int index) { +if (!boot_cpu_has (X86_FEATURE_ASI )) +return ; +spin_lock (&asi_class_lock);WARN_ON (asi_class[index].name ==NULL );
@@ -52,10 +60,22 @@ void asi_unregister_class(int index) }EXPORT_SYMBOL_GPL (asi_unregister_class); +static int __init set_asi_param (char *str) +{ +if (strcmp (str,"on" ) ==0 ) +setup_force_cpu_cap (X86_FEATURE_ASI ); + +return 0 ; +} +early_param ("asi" , set_asi_param); +int asi_init (struct mm_struct *mm,int asi_index) {struct asi *asi = &mm->asi[asi_index]; +if (!boot_cpu_has (X86_FEATURE_ASI )) +return 0 ; +/* Index 0 is reserved for special purposes. */ WARN_ON (asi_index ==0 || asi_index >=ASI_MAX_NUM );WARN_ON (asi->pgd !=NULL );
@@ -79,6 +99,9 @@ EXPORT_SYMBOL_GPL(asi_init);void asi_destroy (struct asi *asi) { +if (!boot_cpu_has (X86_FEATURE_ASI )) +return ; +free_pages ((ulong )asi->pgd,PGD_ALLOCATION_ORDER );memset (asi,0 ,sizeof (struct asi )); }
@@ -109,6 +132,9 @@ static void __asi_enter(void)void asi_enter (struct asi *asi) { +if (!static_cpu_has (X86_FEATURE_ASI )) +return ; +VM_WARN_ON_ONCE (!asi);this_cpu_write (asi_cpu_state.target_asi, asi);
@@ -123,6 +149,9 @@ void asi_exit(void)u64 unrestricted_cr3;struct asi *asi; +if (!static_cpu_has (X86_FEATURE_ASI )) +return ; +preempt_disable (); VM_BUG_ON(this_cpu_read(cpu_tlbstate.loaded_mm) ==
arch/x86/mm/init.c +1 -1
@@ -240,7 +240,7 @@ static void __init probe_page_size_mask(void) __default_kernel_pte_mask = __supported_pte_mask;/* Except when with PTI or ASI where the kernel is mostly non-Global: */ if (cpu_feature_enabled (X86_FEATURE_PTI ) || -IS_ENABLED (CONFIG_ADDRESS_SPACE_ISOLATION )) +cpu_feature_enabled (X86_FEATURE_ASI )) __default_kernel_pte_mask &= ~_PAGE_GLOBAL;/* Enable 1 GB linear kernel mappings if available: */
include/asm-generic/asi.h +2 -0
@@ -44,6 +44,8 @@ static inline struct asi *asi_get_target(void) { return NULL; }static inline struct asi *asi_get_current (void ) {return NULL ; } +#define static_asi_enabled () false +#endif /* !_ASSEMBLY_ */ #endif /* !CONFIG_ADDRESS_SPACE_ISOLATION */
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 3/47 mm: asi: Switch to unrestricted address space when entering scheduler
In reply to Junaid ShahidTo keep things simpler, we run the scheduler only in the full unrestricted address space for the time being.
--- kernel/sched/core.c | 5 +++++ 1 file changed, 5 insertions(+)
kernel/sched/core.c +5 -0
@@ -19,6 +19,7 @@#include <asm/switch_to.h> #include <asm/tlb.h> +#include <asm/asi.h> #include "../workqueue_internal.h" #include "../../fs/io-wq.h"
@@ -6141,6 +6142,10 @@ static void __sched notrace __schedule(unsigned int sched_mode) rq =cpu_rq (cpu); prev = rq->curr; +/* This could possibly be delayed to just before the context switch. */ +VM_WARN_ON (!asi_is_target_unrestricted ()); +asi_exit (); +schedule_debug (prev, !!sched_mode);if (sched_feat (HRTICK ) ||sched_feat (HRTICK_DL ))
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 4/47 mm: asi: ASI support in interrupts/exceptions
In reply to Junaid ShahidAdd support for potentially switching address spaces from within interrupts/exceptions/NMIs etc. An interrupt does not automatically switch to the unrestricted address space. It can switch if needed to access some memory not available in the restricted address space, using the normal asi_exit call.
On return from the outermost interrupt, if the target address space was the restricted address space (e.g. we were in the critical code path between ASI Enter and VM Enter), the restricted address space will be automatically restored. Otherwise, execution will continue in the unrestricted address space until the next explicit ASI Enter.
In order to keep track of when to restore the restricted address space, an interrupt/exception nesting depth counter is maintained per-task. An alternative implementation without needing this counter is also possible, but the counter unlocks an additional nice-to-have benefit by allowing detection of whether or not we are currently executing inside an exception context, which would be useful in a later patch.
--- arch/x86/include/asm/asi.h | 35 ++++++++++++++++++++++++++++++++ arch/x86/include/asm/idtentry.h | 25 +++++++++++++++++++++-- arch/x86/include/asm/processor.h | 5 +++++ arch/x86/kernel/process.c | 2 ++ arch/x86/kernel/traps.c | 2 ++ arch/x86/mm/asi.c | 3 ++- kernel/entry/common.c | 6 ++++++ 7 files changed, 75 insertions(+), 3 deletions(-)
arch/x86/include/asm/asi.h +35 -0
@@ -4,6 +4,8 @@#include <asm-generic/asi.h> +#include <linux/sched.h> +#include <asm/pgtable_types.h> #include <asm/percpu.h> #include <asm/cpufeature.h>
@@ -51,6 +53,11 @@ void asi_destroy(struct asi *asi);void asi_enter (struct asi *asi);void asi_exit (void ); +static inline void asi_init_thread_state (struct thread_struct *thread) +{ + thread->intr_nest_depth =0 ; +} +static inline void asi_set_target_unrestricted (void ) {if (static_cpu_has (X86_FEATURE_ASI )) {
@@ -85,6 +92,34 @@ static inline bool asi_is_target_unrestricted(void)#define static_asi_enabled () cpu_feature_enabled(X86_FEATURE_ASI) +static inline void asi_intr_enter (void ) +{ +if (static_cpu_has (X86_FEATURE_ASI )) { + current->thread.intr_nest_depth++; +barrier (); + } +} + +static inline void asi_intr_exit (void ) +{ +void __asi_enter (void ); + +if (static_cpu_has (X86_FEATURE_ASI )) { +barrier (); + +if (--current->thread.intr_nest_depth ==0 ) +__asi_enter (); + } +} + +#else /* CONFIG_ADDRESS_SPACE_ISOLATION */ + +static inline void asi_intr_enter (void ) { } + +static inline void asi_intr_exit (void ) { } + +static inline void asi_init_thread_state (struct thread_struct *thread) { } +#endif /* CONFIG_ADDRESS_SPACE_ISOLATION */ #endif
arch/x86/include/asm/idtentry.h +23 -2
@@ -10,6 +10,7 @@#include <linux/hardirq.h> #include <asm/irq_stack.h> +#include <asm/asi.h> /** * DECLARE_IDTENTRY - Declare functions for simple IDT entry points
@@ -133,7 +134,16 @@ static __always_inline void __##func(struct pt_regs *regs, \* is required before the enter/exit() helpers are invoked. */ #define DEFINE_IDTENTRY_RAW (func) \ -__visible noinstr void func(struct pt_regs *regs) +static __always_inline void __##func(struct pt_regs *regs); \ + \ +__visible noinstr void func(struct pt_regs *regs) \ +{ \ + asi_intr_enter(); \ + __##func (regs); \ + asi_intr_exit(); \ +} \ + \ +static __always_inline void __##func(struct pt_regs *regs)/** * DECLARE_IDTENTRY_RAW_ERRORCODE - Declare functions for raw IDT entry points
@@ -161,7 +171,18 @@ __visible noinstr void func(struct pt_regs *regs)* is required before the enter/exit() helpers are invoked. */ #define DEFINE_IDTENTRY_RAW_ERRORCODE (func) \ -__visible noinstr void func(struct pt_regs *regs, unsigned long error_code) +static __always_inline void __##func(struct pt_regs *regs, \ + unsigned long error_code); \ + \ +__visible noinstr void func(struct pt_regs *regs, unsigned long error_code)\ +{ \ + asi_intr_enter(); \ + __##func (regs, error_code); \ + asi_intr_exit(); \ +} \ + \ +static __always_inline void __##func(struct pt_regs *regs, \ + unsigned long error_code)/** * DECLARE_IDTENTRY_IRQ - Declare functions for device interrupt IDT entry
arch/x86/include/asm/processor.h +5 -0
@@ -519,6 +519,11 @@ struct thread_struct { unsigned int iopl_warn:1; unsigned int sig_on_uaccess_err:1; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +/* The nesting depth of exceptions/interrupts */ +int intr_nest_depth; +#endif +/* * Protection Keys Register for Userspace. Loaded immediately on * context switch. Store it in thread_struct to avoid a lookup in
arch/x86/kernel/process.c +2 -0
@@ -90,6 +90,8 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)#ifdef CONFIG_VM86 dst->thread.vm86 =NULL ;#endif +asi_init_thread_state (&dst->thread); +/* Drop the copied pointer to current's fpstate */ dst->thread.fpu.fpstate =NULL ;
arch/x86/kernel/traps.c +2 -0
@@ -61,6 +61,7 @@#include <asm/insn.h> #include <asm/insn-eval.h> #include <asm/vdso.h> +#include <asm/asi.h> #ifdef CONFIG_X86_64 #include <asm/x86_init.h>
@@ -413,6 +414,7 @@ DEFINE_IDTENTRY_DF(exc_double_fault) }#endif +asi_exit ();irqentry_nmi_enter (regs);instrumentation_begin ();notify_die (DIE_TRAP , str, regs, error_code,X86_TRAP_DF ,SIGSEGV );
arch/x86/mm/asi.c +2 -1
@@ -107,12 +107,13 @@ void asi_destroy(struct asi *asi) }EXPORT_SYMBOL_GPL (asi_destroy); -static void __asi_enter (void ) +void __asi_enter (void ) {u64 asi_cr3;struct asi *target =this_cpu_read (asi_cpu_state.target_asi);VM_BUG_ON (preemptible ()); +VM_BUG_ON (current->thread.intr_nest_depth !=0 );if (!target || target ==this_cpu_read (asi_cpu_state.curr_asi))return ;
kernel/entry/common.c +6 -0
@@ -9,6 +9,8 @@#include "common.h" +#include <asm/asi.h> +#define CREATE_TRACE_POINTS #include <trace/events/syscalls.h>
@@ -321,6 +323,8 @@ noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs) .exit_rcu = false, }; +asi_intr_enter (); +if (user_mode (regs)) {irqentry_enter_from_user_mode (regs);return ret;
@@ -416,6 +420,7 @@ noinstr void irqentry_exit(struct pt_regs *regs, irqentry_state_t state)instrumentation_end ();rcu_irq_exit ();lockdep_hardirqs_on (CALLER_ADDR0 ); +asi_intr_exit ();return ; }
@@ -438,6 +443,7 @@ noinstr void irqentry_exit(struct pt_regs *regs, irqentry_state_t state)if (state.exit_rcu)rcu_irq_exit (); } +asi_intr_exit (); } irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs)
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 5/47 mm: asi: Make __get_current_cr3_fast() ASI-aware
In reply to Junaid ShahidWhen ASI is active, __get_current_cr3_fast() adjusts the returned CR3 value accordingly to reflect the actual ASI CR3.
--- arch/x86/include/asm/asi.h | 7 +++++++ arch/x86/mm/tlb.c | 20 ++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-)
arch/x86/include/asm/asi.h +7 -0
@@ -112,6 +112,11 @@ static inline void asi_intr_exit(void) } } +static inline pgd_t *asi_pgd (struct asi *asi) +{ +return asi->pgd; +} +#else /* CONFIG_ADDRESS_SPACE_ISOLATION */ static inline void asi_intr_enter (void ) { }
@@ -120,6 +125,8 @@ static inline void asi_intr_exit(void) { }static inline void asi_init_thread_state (struct thread_struct *thread) { } +static inline pgd_t *asi_pgd (struct asi *asi) {return NULL ; } +#endif /* CONFIG_ADDRESS_SPACE_ISOLATION */ #endif
arch/x86/mm/tlb.c +18 -2
@@ -17,6 +17,7 @@#include <asm/cacheflush.h> #include <asm/apic.h> #include <asm/perf_event.h> +#include <asm/asi.h> #include "mm_internal.h"
@@ -1073,12 +1074,27 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end)*/ unsigned long __get_current_cr3_fast (void ) { -unsigned long cr3 =build_cr3 (this_cpu_read (cpu_tlbstate.loaded_mm)->pgd, -this_cpu_read (cpu_tlbstate.loaded_mm_asid)); +unsigned long cr3; +pgd_t *pgd; +u16 asid =this_cpu_read (cpu_tlbstate.loaded_mm_asid); +struct asi *asi =asi_get_current (); + +if (asi) + pgd =asi_pgd (asi); +else + pgd =this_cpu_read (cpu_tlbstate.loaded_mm)->pgd; + + cr3 =build_cr3 (pgd, asid);/* For now, be very restrictive about when this can be called. */ VM_WARN_ON (in_nmi () ||preemptible ()); +/* +* CR3 is unstable if the target ASI is unrestricted +* and a restricted ASI is currently loaded. +*/ +VM_WARN_ON_ONCE (asi &&asi_is_target_unrestricted ()); +VM_BUG_ON (cr3 !=__read_cr3 ());return cr3; }
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 6/47 mm: asi: ASI page table allocation and free functions
In reply to Junaid ShahidThis adds custom allocation and free functions for ASI page tables.
The alloc functions support allocating memory using different GFP reclaim flags, in order to be able to support non-sensitive allocations from both standard and atomic contexts. They also install the page tables locklessly, which makes it slightly simpler to handle non-sensitive allocations from interrupts/exceptions.
The free functions recursively free the page tables when the ASI instance is being torn down.
--- arch/x86/mm/asi.c | 109 +++++++++++++++++++++++++++++++++++++++- include/linux/pgtable.h | 3 ++ 2 files changed, 111 insertions(+), 1 deletion(-)
arch/x86/mm/asi.c +108 -1
@@ -60,6 +60,113 @@ void asi_unregister_class(int index) }EXPORT_SYMBOL_GPL (asi_unregister_class); +#ifndef mm_inc_nr_p4ds +#define mm_inc_nr_p4ds (mm) do {} while (false) +#endif + +#ifndef mm_dec_nr_p4ds +#define mm_dec_nr_p4ds (mm) do {} while (false) +#endif + +#define pte_offset pte_offset_kernel + +#define DEFINE_ASI_PGTBL_ALLOC(base, level) \ +static level##_t * asi_##level##_alloc(struct asi *asi, \ + base##_t *base, ulong addr, \ + gfp_t flags) \ +{ \ + if (unlikely(base##_none(*base))) { \ + ulong pgtbl = get_zeroed_page(flags); \ + phys_addr_t pgtbl_pa; \ + \ + if (pgtbl == 0) \ + return NULL; \ + \ + pgtbl_pa = __pa(pgtbl); \ + paravirt_alloc_##level(asi->mm, PHYS_PFN(pgtbl_pa)); \ + \ + if (cmpxchg((ulong *)base, 0, \ + pgtbl_pa | _PAGE_TABLE) == 0) { \ + mm_inc_nr_##level##s(asi->mm); \ + } else { \ + paravirt_release_##level(PHYS_PFN(pgtbl_pa)); \ + free_page(pgtbl); \ + } \ + \ + /* NOP on native. PV call on Xen. */ \ + set_##base(base, *base); \ + } \ + VM_BUG_ON(base##_large(*base)); \ + return level##_offset(base, addr); \ +} + +DEFINE_ASI_PGTBL_ALLOC(pgd, p4d) +DEFINE_ASI_PGTBL_ALLOC(p4d, pud) +DEFINE_ASI_PGTBL_ALLOC(pud, pmd) +DEFINE_ASI_PGTBL_ALLOC(pmd, pte) + +#define asi_free_dummy (asi, addr) +#define __pmd_free (mm, pmd) free_page((ulong)(pmd)) +#define pud_page_vaddr (pud) ((ulong)pud_pgtable(pud)) +#define p4d_page_vaddr (p4d) ((ulong)p4d_pgtable(p4d)) + +static inline unsigned long pte_page_vaddr (pte_t pte) +{ +return (unsigned long )__va (pte_val (pte) &PTE_PFN_MASK ); +} + +#define DEFINE_ASI_PGTBL_FREE (level,LEVEL , next, free) \ +static void asi_free_##level(struct asi *asi, ulong pgtbl_addr) \ +{ \ + uint i; \ + level##_t *level = (level##_t *)pgtbl_addr; \ + \ + for (i = 0; i < PTRS_PER_##LEVEL; i++) { \ + ulong vaddr; \ + \ + if (level##_none(level[i])) \ + continue; \ + \ + vaddr = level##_page_vaddr(level[i]); \ + \ + if (!level##_leaf(level[i])) \ + asi_free_##next(asi, vaddr); \ + else \ + VM_WARN(true, "Lingering mapping in ASI %p at %lx",\ + asi, vaddr); \ + } \ + paravirt_release_##level(PHYS_PFN(__pa(pgtbl_addr))); \ + free(asi->mm, level); \ + mm_dec_nr_##level##s(asi->mm); \ +} + +DEFINE_ASI_PGTBL_FREE(pte, PTE, dummy, pte_free_kernel) +DEFINE_ASI_PGTBL_FREE(pmd, PMD, pte, __pmd_free) +DEFINE_ASI_PGTBL_FREE(pud, PUD, pmd, pud_free) +DEFINE_ASI_PGTBL_FREE(p4d, P4D, pud, p4d_free) + +static void asi_free_pgd_range (struct asi *asi,uint start,uint end) +{ +uint i; + +for (i = start; i < end; i++) +if (pgd_present (asi->pgd[i])) +asi_free_p4d (asi, (ulong )p4d_offset (asi->pgd + i,0 )); +} + +/* +* Free the page tables allocated for the given ASI instance. +* The caller must ensure that all the mappings have already been cleared +* and appropriate TLB flushes have been issued before calling this function. +*/ +static void asi_free_pgd (struct asi *asi) +{ +VM_BUG_ON (asi->mm == &init_mm); + +asi_free_pgd_range (asi,KERNEL_PGD_BOUNDARY ,PTRS_PER_PGD ); +free_pages ((ulong )asi->pgd,PGD_ALLOCATION_ORDER ); +} +static int __init set_asi_param (char *str) {if (strcmp (str,"on" ) ==0 )
@@ -102,7 +209,7 @@ void asi_destroy(struct asi *asi)if (!boot_cpu_has (X86_FEATURE_ASI ))return ; -free_pages ((ulong )asi->pgd,PGD_ALLOCATION_ORDER ); +asi_free_pgd (asi);memset (asi,0 ,sizeof (struct asi )); }EXPORT_SYMBOL_GPL (asi_destroy);
include/linux/pgtable.h +3 -0
@@ -1593,6 +1593,9 @@ typedef unsigned int pgtbl_mod_mask;#ifndef pmd_leaf#define pmd_leaf (x) 0#endif +#ifndef pte_leaf +#define pte_leaf (x) 1 +#endif #ifndef pgd_leaf_size#define pgd_leaf_size (x) (1ULL << PGDIR_SHIFT)
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 7/47 mm: asi: Functions to map/unmap a memory range into ASI page tables
In reply to Junaid ShahidTwo functions, asi_map() and asi_map_gfp(), are added to allow mapping memory into ASI page tables. The mapping will be identical to the one for the same virtual address in the unrestricted page tables. This is necessary to allow switching between the page tables at any arbitrary point in the kernel.
Another function, asi_unmap() is added to allow unmapping memory mapped via asi_map*
--- arch/x86/include/asm/asi.h | 5 + arch/x86/mm/asi.c | 196 +++++++++++++++++++++++++++++++++++++ include/asm-generic/asi.h | 19 ++++ mm/internal.h | 3 + mm/vmalloc.c | 60 +++++++----- 5 files changed, 261 insertions(+), 22 deletions(-)
arch/x86/include/asm/asi.h +5 -0
@@ -53,6 +53,11 @@ void asi_destroy(struct asi *asi);void asi_enter (struct asi *asi);void asi_exit (void ); +int asi_map_gfp (struct asi *asi,void *addr,size_t len,gfp_t gfp_flags); +int asi_map (struct asi *asi,void *addr,size_t len); +void asi_unmap (struct asi *asi,void *addr,size_t len,bool flush_tlb); +void asi_flush_tlb_range (struct asi *asi,void *addr,size_t len); +static inline void asi_init_thread_state (struct thread_struct *thread) { thread->intr_nest_depth =0 ;
arch/x86/mm/asi.c +196 -0
205 lines not on this page · raw message
include/asm-generic/asi.h +19 -0
@@ -2,6 +2,8 @@#ifndef __ASM_GENERIC_ASI_H#define __ASM_GENERIC_ASI_H +#include <linux/types.h> +/* ASI class flags */ #define ASI_MAP_STANDARD_NONSENSITIVE 1
@@ -44,6 +46,23 @@ static inline struct asi *asi_get_target(void) { return NULL; }static inline struct asi *asi_get_current (void ) {return NULL ; } +static inline +int asi_map_gfp (struct asi *asi,void *addr,size_t len,gfp_t gfp_flags) +{ +return 0 ; +} + +static inline int asi_map (struct asi *asi,void *addr,size_t len) +{ +return 0 ; +} + +static inline +void asi_unmap (struct asi *asi,void *addr,size_t len,bool flush_tlb) { } + +static inline +void asi_flush_tlb_range (struct asi *asi,void *addr,size_t len) { } +#define static_asi_enabled () false#endif /* !_ASSEMBLY_ */
mm/internal.h +3 -0
@@ -79,6 +79,9 @@ void unmap_page_range(struct mmu_gather *tlb, unsigned long addr, unsigned long end, struct zap_details *details); +void vunmap_pgd_range (pgd_t *pgd_table,unsigned long addr,unsigned long end, +pgtbl_mod_mask *mask,bool sleepable); +void do_page_cache_ra (struct readahead_control *,unsigned long nr_to_read,unsigned long lookahead_size);void force_page_cache_ra (struct readahead_control *,unsigned long nr);
mm/vmalloc.c +38 -22
114 lines not on this page · raw message
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 8/47 mm: asi: Add basic infrastructure for global non-sensitive mappings
In reply to Junaid ShahidA pseudo-PGD is added to store global non-sensitive ASI mappings. Actual ASI PGDs copy entries from this pseudo-PGD during asi_init().
Memory can be mapped as globally non-sensitive by calling asi_map() with ASI_GLOBAL_NONSENSITIVE.
Page tables allocated for global non-sensitive mappings are never freed.
--- arch/x86/include/asm/asi.h | 12 ++++++++++++ arch/x86/mm/asi.c | 36 +++++++++++++++++++++++++++++++++++- arch/x86/mm/init_64.c | 26 +++++++++++++++++--------- arch/x86/mm/mm_internal.h | 3 +++ include/asm-generic/asi.h | 5 +++++ mm/init-mm.c | 2 ++ 6 files changed, 74 insertions(+), 10 deletions(-)
arch/x86/include/asm/asi.h +12 -0
@@ -15,6 +15,8 @@#define ASI_MAX_NUM_ORDER 2#define ASI_MAX_NUM (1 << ASI_MAX_NUM_ORDER) +#define ASI_GLOBAL_NONSENSITIVE (&init_mm.asi[0]) +struct asi_state {struct asi *curr_asi;struct asi *target_asi;
@@ -41,6 +43,8 @@ struct asi { DECLARE_PER_CPU_ALIGNED(struct asi_state, asi_cpu_state); +extern pgd_t asi_global_nonsensitive_pgd[]; +void asi_init_mm_state (struct mm_struct *mm); int asi_register_class(const char *name, uint flags,
@@ -117,6 +121,14 @@ static inline void asi_intr_exit(void) } } +#define INIT_MM_ASI (init_mm) \ + .asi = { \ + [0] = { \ + .pgd = asi_global_nonsensitive_pgd, \ + .mm = &init_mm \ + } \ + }, +static inline pgd_t *asi_pgd (struct asi *asi) {return asi->pgd;
arch/x86/mm/asi.c +35 -1
@@ -1,11 +1,13 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/init.h> +#include <linux/memblock.h> #include <asm/asi.h> #include <asm/pgalloc.h> #include <asm/mmu_context.h> +#include "mm_internal.h" #include "../../../mm/internal.h" #undef pr_fmt
@@ -17,6 +19,8 @@ static DEFINE_SPINLOCK(asi_class_lock); DEFINE_PER_CPU_ALIGNED(struct asi_state, asi_cpu_state);EXPORT_PER_CPU_SYMBOL_GPL (asi_cpu_state); +__aligned(PAGE_SIZE) pgd_t asi_global_nonsensitive_pgd[PTRS_PER_PGD ]; +int asi_register_class (const char *name,uint flags,const struct asi_hooks *ops) {
@@ -160,12 +164,17 @@ static void asi_free_pgd_range(struct asi *asi, uint start, uint end)* Free the page tables allocated for the given ASI instance. * The caller must ensure that all the mappings have already been cleared * and appropriate TLB flushes have been issued before calling this function. +* +* For standard non-sensitive ASI classes, the page tables shared with the +* master pseudo-PGD are not freed. */ static void asi_free_pgd (struct asi *asi) {VM_BUG_ON (asi->mm == &init_mm); -asi_free_pgd_range (asi,KERNEL_PGD_BOUNDARY ,PTRS_PER_PGD ); +if (!(asi->class->flags &ASI_MAP_STANDARD_NONSENSITIVE )) +asi_free_pgd_range (asi,KERNEL_PGD_BOUNDARY ,PTRS_PER_PGD ); +free_pages ((ulong )asi->pgd,PGD_ALLOCATION_ORDER ); }
@@ -178,6 +187,24 @@ static int __init set_asi_param(char *str) }early_param ("asi" , set_asi_param); +static int __init asi_global_init (void ) +{ +if (!boot_cpu_has (X86_FEATURE_ASI )) +return 0 ; + +preallocate_toplevel_pgtbls (asi_global_nonsensitive_pgd, +PAGE_OFFSET , +PAGE_OFFSET +PFN_PHYS (max_possible_pfn) -1 , +"ASI Global Non-sensitive direct map" ); + +preallocate_toplevel_pgtbls (asi_global_nonsensitive_pgd, +VMALLOC_START ,VMALLOC_END , +"ASI Global Non-sensitive vmalloc" ); + +return 0 ; +} +subsys_initcall (asi_global_init) + int asi_init(struct mm_struct *mm, int asi_index) {struct asi *asi = &mm->asi[asi_index];
@@ -202,6 +229,13 @@ int asi_init(struct mm_struct *mm, int asi_index) asi->class = &asi_class[asi_index]; asi->mm = mm; +if (asi->class->flags &ASI_MAP_STANDARD_NONSENSITIVE ) { +uint i; + +for (i =KERNEL_PGD_BOUNDARY ; i <PTRS_PER_PGD ; i++) +set_pgd (asi->pgd + i, asi_global_nonsensitive_pgd[i]); + } +return 0 ; }EXPORT_SYMBOL_GPL (asi_init);
arch/x86/mm/init_64.c +17 -9
@@ -1277,18 +1277,15 @@ static void __init register_page_bootmem_info(void)#endif } -/* -* Pre-allocates page-table pages for the vmalloc area in the kernel page-table. -* Only the level which needs to be synchronized between all page-tables is -* allocated because the synchronization can be expensive. -*/ -static void __init preallocate_vmalloc_pages (void ) +void __init preallocate_toplevel_pgtbls (pgd_t *pgd_table, +ulong start,ulong end, +const char *name) {unsigned long addr;const char *lvl; -for (addr =VMALLOC_START ; addr <=VMALLOC_END ; addr =ALIGN (addr +1 ,PGDIR_SIZE )) { -pgd_t *pgd =pgd_offset_k (addr); +for (addr = start; addr <= end; addr =ALIGN (addr +1 ,PGDIR_SIZE )) { +pgd_t *pgd =pgd_offset_pgd (pgd_table, addr);p4d_t *p4d;pud_t *pud;
@@ -1324,7 +1321,18 @@ static void __init preallocate_vmalloc_pages(void)* The pages have to be there now or they will be missing in * process page-tables later. */ -panic ("Failed to pre-allocate %s pages for vmalloc area\n" , lvl); +panic ("Failed to pre-allocate %s pages for %s area\n" , lvl, name); +} + +/* +* Pre-allocates page-table pages for the vmalloc area in the kernel page-table. +* Only the level which needs to be synchronized between all page-tables is +* allocated because the synchronization can be expensive. +*/ +static void __init preallocate_vmalloc_pages (void ) +{ +preallocate_toplevel_pgtbls (init_mm.pgd,VMALLOC_START ,VMALLOC_END , +"vmalloc" ); }void __init mem_init (void )
arch/x86/mm/mm_internal.h +3 -0
@@ -19,6 +19,9 @@ unsigned long kernel_physical_mapping_change(unsigned long start, unsigned long page_size_mask);void zone_sizes_init (void ); +void preallocate_toplevel_pgtbls (pgd_t *pgd_table,ulong start,ulong end, +const char *name); +extern int after_bootmem;void update_cache_mode_entry (unsigned entry,enum page_cache_mode cache);
include/asm-generic/asi.h +5 -0
@@ -12,6 +12,8 @@#define ASI_MAX_NUM_ORDER 0#define ASI_MAX_NUM 0 +#define ASI_GLOBAL_NONSENSITIVE NULL +#ifndef _ASSEMBLY_struct asi_hooks {};
@@ -63,8 +65,11 @@ void asi_unmap(struct asi *asi, void *addr, size_t len, bool flush_tlb) { }static inline void asi_flush_tlb_range (struct asi *asi,void *addr,size_t len) { } +#define INIT_MM_ASI (init_mm) +#define static_asi_enabled () false +#endif /* !_ASSEMBLY_ */ #endif /* !CONFIG_ADDRESS_SPACE_ISOLATION */
mm/init-mm.c +2 -0
@@ -11,6 +11,7 @@#include <linux/atomic.h> #include <linux/user_namespace.h> #include <asm/mmu.h> +#include <asm/asi.h> #ifndef INIT_MM_CONTEXT #define INIT_MM_CONTEXT (name)
@@ -38,6 +39,7 @@ struct mm_struct init_mm = { .mmlist = LIST_HEAD_INIT(init_mm.mmlist), .user_ns = &init_user_ns, .cpu_bitmap = CPU_BITS_NONE, + INIT_MM_ASI(init_mm) INIT_MM_CONTEXT(init_mm) };
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 9/47 mm: Add __PAGEFLAG_FALSE
In reply to Junaid Shahid__PAGEFLAG_FALSE is a non-atomic equivalent of PAGEFLAG_FALSE.
--- include/linux/page-flags.h | 7 +++++++ 1 file changed, 7 insertions(+)
include/linux/page-flags.h +7 -0
@@ -390,6 +390,10 @@ static inline int Page##uname(const struct page *page) { return 0; } static inline void folio_set_##lname(struct folio *folio) { } \ static inline void SetPage##uname(struct page *page) { } +#define __SETPAGEFLAG_NOOP (uname, lname) \ +static inline void __folio_set_##lname(struct folio *folio) { } \ +static inline void __SetPage##uname(struct page *page) { } +#define CLEARPAGEFLAG_NOOP (uname, lname) \ static inline void folio_clear_##lname(struct folio *folio) { } \ static inline void ClearPage##uname(struct page *page) { }
@@ -411,6 +415,9 @@ static inline int TestClearPage##uname(struct page *page) { return 0; }#define PAGEFLAG_FALSE (uname, lname) TESTPAGEFLAG_FALSE(uname, lname) \ SETPAGEFLAG_NOOP(uname, lname) CLEARPAGEFLAG_NOOP(uname, lname) +#define __PAGEFLAG_FALSE (uname, lname) TESTPAGEFLAG_FALSE(uname, lname) \ + __SETPAGEFLAG_NOOP(uname, lname) __CLEARPAGEFLAG_NOOP(uname, lname) +#define TESTSCFLAG_FALSE (uname, lname) \ TESTSETFLAG_FALSE(uname, lname) TESTCLEARFLAG_FALSE(uname, lname)
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 10/47 mm: asi: Support for global non-sensitive direct map allocations
In reply to Junaid ShahidA new GFP flag is added to specify that an allocation should be considered globally non-sensitive. The pages will be mapped into the ASI global non-sensitive pseudo-PGD, which is shared between all standard ASI instances. A new page flag is also added so that when these pages are freed, they can also be unmapped from the ASI page tables.
--- include/linux/gfp.h | 10 ++- include/linux/mm_types.h | 5 ++ include/linux/page-flags.h | 9 ++ include/trace/events/mmflags.h | 12 ++- mm/page_alloc.c | 145 ++++++++++++++++++++++++++++++++- tools/perf/builtin-kmem.c | 1 + 6 files changed, 178 insertions(+), 4 deletions(-)
include/linux/gfp.h +9 -1
@@ -60,6 +60,11 @@ struct vm_area_struct;#else #define ___GFP_NOLOCKDEP 0#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +#define ___GFP_GLOBAL_NONSENSITIVE 0x4000000u +#else +#define ___GFP_GLOBAL_NONSENSITIVE 0 +#endif /* If the above are modified, __GFP_BITS_SHIFT may need updating */ /*
@@ -248,8 +253,11 @@ struct vm_area_struct;/* Disable lockdep for GFP context tracking */ #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP) +/* Allocate non-sensitive memory */ +#define __GFP_GLOBAL_NONSENSITIVE ((__force gfp_t)___GFP_GLOBAL_NONSENSITIVE) +/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (25 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT 27#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/**
include/linux/mm_types.h +5 -0
@@ -191,6 +191,11 @@ struct page {/** @rcu_head: You can use this to free a page by RCU. */ struct rcu_head rcu_head; + +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +/* Links the pages_to_free_async list */ +struct llist_node async_free_node; +#endif };union {/* This union is 4 bytes in size. */
include/linux/page-flags.h +9 -0
@@ -140,6 +140,9 @@ enum pageflags {#endif #ifdef CONFIG_KASAN_HW_TAGS PG_skip_kasan_poison, +#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION + PG_global_nonsensitive, #endif __NR_PAGEFLAGS,
@@ -542,6 +545,12 @@ TESTCLEARFLAG(Young, young, PF_ANY) PAGEFLAG(Idle, idle, PF_ANY)#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +__PAGEFLAG (GlobalNonSensitive, global_nonsensitive,PF_ANY ); +#else +__PAGEFLAG_FALSE (GlobalNonSensitive, global_nonsensitive); +#endif +#ifdef CONFIG_KASAN_HW_TAGS PAGEFLAG (SkipKASanPoison, skip_kasan_poison,PF_HEAD ) #else
include/trace/events/mmflags.h +10 -2
@@ -50,7 +50,8 @@ {(unsigned long)__GFP_DIRECT_RECLAIM, "__GFP_DIRECT_RECLAIM"},\ {(unsigned long)__GFP_KSWAPD_RECLAIM, "__GFP_KSWAPD_RECLAIM"},\ {(unsigned long)__GFP_ZEROTAGS, "__GFP_ZEROTAGS"}, \ - {(unsigned long)__GFP_SKIP_KASAN_POISON,"__GFP_SKIP_KASAN_POISON"}\ + {(unsigned long)__GFP_SKIP_KASAN_POISON,"__GFP_SKIP_KASAN_POISON"},\ + {(unsigned long)__GFP_GLOBAL_NONSENSITIVE, "__GFP_GLOBAL_NONSENSITIVE"}\ #define show_gfp_flags(flags) \ (flags) ? __print_flags(flags, "|", \
@@ -93,6 +94,12 @@#define IF_HAVE_PG_SKIP_KASAN_POISON (flag,string)#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +#define IF_HAVE_ASI (flag, string) ,{1UL << flag, string} +#else +#define IF_HAVE_ASI (flag, string) +#endif +#define __def_pageflag_names \ {1UL << PG_locked, "locked" }, \ {1UL << PG_waiters, "waiters" }, \
@@ -121,7 +128,8 @@ IF_HAVE_PG_HWPOISON(PG_hwpoison, "hwpoison" ) \ IF_HAVE_PG_IDLE(PG_young, "young" ) \ IF_HAVE_PG_IDLE(PG_idle, "idle" ) \ IF_HAVE_PG_ARCH_2(PG_arch_2, "arch_2" ) \ -IF_HAVE_PG_SKIP_KASAN_POISON(PG_skip_kasan_poison, "skip_kasan_poison") +IF_HAVE_PG_SKIP_KASAN_POISON(PG_skip_kasan_poison, "skip_kasan_poison") \ +IF_HAVE_ASI(PG_global_nonsensitive, "global_nonsensitive")#define show_page_flags (flags) \ (flags) ? __print_flags(flags, "|", \
mm/page_alloc.c +144 -1
175 lines not on this page · raw message
tools/perf/builtin-kmem.c +1 -0
@@ -660,6 +660,7 @@ static const struct { { "__GFP_RECLAIM", "R" }, { "__GFP_DIRECT_RECLAIM", "DR" }, { "__GFP_KSWAPD_RECLAIM", "KR" }, + { "__GFP_GLOBAL_NONSENSITIVE", "GNS" }, };static size_t max_gfp_len;
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 11/47 mm: asi: Global non-sensitive vmalloc/vmap support
In reply to Junaid ShahidA new flag, VM_GLOBAL_NONSENSITIVE is added to designate globally non-sensitive vmalloc/vmap areas. When using the __vmalloc / __vmalloc_node APIs, if the corresponding GFP flag is specified, the VM flag is automatically added. When using the __vmalloc_node_range API, either flag can be specified independently. The VM flag will only map the vmalloc area as non-sensitive, while the GFP flag will only map the underlying direct map area as non-sensitive.
When using the __vmalloc_node_range API, instead of VMALLOC_START/END, VMALLOC_GLOBAL_NONSENSITIVE_START/END should be used. This is to keep these mappings separate from locally non-sensitive vmalloc areas, which will be added later. Areas outside of the standard vmalloc range can specify the range as before.
--- arch/x86/include/asm/pgtable_64_types.h | 5 +++ arch/x86/mm/asi.c | 3 +- include/asm-generic/asi.h | 3 ++ include/linux/vmalloc.h | 6 +++ mm/vmalloc.c | 53 ++++++++++++++++++++++--- 5 files changed, 64 insertions(+), 6 deletions(-)
arch/x86/include/asm/pgtable_64_types.h +5 -0
@@ -141,6 +141,11 @@ extern unsigned int ptrs_per_p4d;#define VMALLOC_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1) +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +#define VMALLOC_GLOBAL_NONSENSITIVE_START VMALLOC_START +#define VMALLOC_GLOBAL_NONSENSITIVE_END VMALLOC_END +#endif +#define MODULES_VADDR (__START_KERNEL_map + KERNEL_IMAGE_SIZE)/* The module sections ends with the start of the fixmap */ #ifndef CONFIG_DEBUG_KMAP_LOCAL_FORCE_MAP
arch/x86/mm/asi.c +2 -1
@@ -198,7 +198,8 @@ static int __init asi_global_init(void) "ASI Global Non-sensitive direct map");preallocate_toplevel_pgtbls (asi_global_nonsensitive_pgd, -VMALLOC_START ,VMALLOC_END , +VMALLOC_GLOBAL_NONSENSITIVE_START , +VMALLOC_GLOBAL_NONSENSITIVE_END ,"ASI Global Non-sensitive vmalloc" );return 0 ;
include/asm-generic/asi.h +3 -0
@@ -14,6 +14,9 @@#define ASI_GLOBAL_NONSENSITIVE NULL +#define VMALLOC_GLOBAL_NONSENSITIVE_START VMALLOC_START +#define VMALLOC_GLOBAL_NONSENSITIVE_END VMALLOC_END +#ifndef _ASSEMBLY_struct asi_hooks {};
include/linux/vmalloc.h +6 -0
@@ -39,6 +39,12 @@ struct notifier_block; /* in notifier.h */* determine which allocations need the module shadow freed. */ +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +#define VM_GLOBAL_NONSENSITIVE 0x00000800/* Similar to __GFP_GLOBAL_NONSENSITIVE */ +#else +#define VM_GLOBAL_NONSENSITIVE 0 +#endif +/* bits [20..32] reserved for arch specific ioremap internals */ /*
mm/vmalloc.c +48 -5
96 lines not on this page · raw message
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 12/47 mm: asi: Support for global non-sensitive slab caches
In reply to Junaid ShahidA new flag SLAB_GLOBAL_NONSENSITIVE is added, which would designate all objects within that slab cache to be globally non-sensitive.
Another flag SLAB_NONSENSITIVE is also added, which is currently just an alias for SLAB_GLOBAL_NONSENSITIVE, but will eventually be used to designate slab caches which can allocate either global or local non-sensitive objects.
In addition, new kmalloc caches have been added that can be used to allocate non-sensitive objects.
--- include/linux/slab.h | 32 +++++++++++++++---- mm/slab.c | 5 +++ mm/slab.h | 14 ++++++++- mm/slab_common.c | 73 +++++++++++++++++++++++++++++++++----------- security/Kconfig | 2 +- 5 files changed, 101 insertions(+), 25 deletions(-)
include/linux/slab.h +26 -6
@@ -120,6 +120,12 @@/* Slab deactivation flag */ #define SLAB_DEACTIVATED ((slab_flags_t __force)0x10000000U) +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +#define SLAB_GLOBAL_NONSENSITIVE ((slab_flags_t __force)0x20000000U) +#else +#define SLAB_GLOBAL_NONSENSITIVE 0 +#endif +/* * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. *
@@ -329,6 +335,11 @@ enum kmalloc_cache_type {extern struct kmem_cache * kmalloc_caches[NR_KMALLOC_TYPES ][KMALLOC_SHIFT_HIGH +1 ]; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +extern struct kmem_cache * +nonsensitive_kmalloc_caches[NR_KMALLOC_TYPES ][KMALLOC_SHIFT_HIGH +1 ]; +#endif +/* * Define gfp bits that should not be set for KMALLOC_NORMAL. */
@@ -361,6 +372,17 @@ static __always_inline enum kmalloc_cache_type kmalloc_type(gfp_t flags)return KMALLOC_CGROUP ; } +static __always_inline struct kmem_cache *get_kmalloc_cache(gfp_t flags, +uint index) +{ +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION + +if (static_asi_enabled () && (flags & __GFP_GLOBAL_NONSENSITIVE)) +return nonsensitive_kmalloc_caches[kmalloc_type (flags)][index]; +#endif +return kmalloc_caches[kmalloc_type (flags)][index]; +} +/* * Figure out which kmalloc slab an allocation of a certain size * belongs to.
@@ -587,9 +609,8 @@ static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags)if (!index)return ZERO_SIZE_PTR ; -return kmem_cache_alloc_trace ( - kmalloc_caches[kmalloc_type (flags)][index], - flags, size); +return kmem_cache_alloc_trace (get_kmalloc_cache (flags, index), + flags, size);#endif }return __kmalloc (size, flags);
@@ -605,9 +626,8 @@ static __always_inline __alloc_size(1) void *kmalloc_node(size_t size, gfp_t flaif (!i)return ZERO_SIZE_PTR ; -return kmem_cache_alloc_node_trace ( - kmalloc_caches[kmalloc_type (flags)][i], - flags, node, size); +return kmem_cache_alloc_node_trace (get_kmalloc_cache (flags, i), + flags, node, size); }#endif return __kmalloc_node (size, flags, node);
mm/slab.c +5 -0
@@ -1956,6 +1956,9 @@ int __kmem_cache_create(struct kmem_cache *cachep, slab_flags_t flags) size =ALIGN (size,REDZONE_ALIGN ); } +if (!static_asi_enabled ()) + flags &= ~SLAB_NONSENSITIVE ; +/* 3) caller mandated alignment */ if (ralign < cachep->align) { ralign = cachep->align;
@@ -2058,6 +2061,8 @@ int __kmem_cache_create(struct kmem_cache *cachep, slab_flags_t flags) cachep->allocflags |=GFP_DMA32 ;if (flags &SLAB_RECLAIM_ACCOUNT ) cachep->allocflags |= __GFP_RECLAIMABLE; +if (flags &SLAB_GLOBAL_NONSENSITIVE ) + cachep->allocflags |= __GFP_GLOBAL_NONSENSITIVE; cachep->size = size; cachep->reciprocal_buffer_size =reciprocal_value (size);
mm/slab.h +13 -1
@@ -77,6 +77,10 @@ extern struct kmem_cache *kmem_cache;/* A table of kmalloc cache names and sizes */ extern const struct kmalloc_info_struct {const char *name[NR_KMALLOC_TYPES ]; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +const char *nonsensitive_name[NR_KMALLOC_TYPES ]; +#endif +slab_flags_t flags[NR_KMALLOC_TYPES ];unsigned int size; } kmalloc_info[];
@@ -124,11 +128,14 @@ static inline slab_flags_t kmem_cache_flags(unsigned int object_size, }#endif +/* This will also include SLAB_LOCAL_NONSENSITIVE in a later patch. */ +#define SLAB_NONSENSITIVE SLAB_GLOBAL_NONSENSITIVE/* Legal flag mask for kmem_cache_create(), for various configurations */ #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \ SLAB_CACHE_DMA32 | SLAB_PANIC | \ - SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS ) + SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS | \ + SLAB_NONSENSITIVE)#if defined(CONFIG_DEBUG_SLAB )#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
@@ -491,6 +498,11 @@ static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,might_alloc (flags); +if (static_asi_enabled ()) { +VM_BUG_ON (!(s->flags &SLAB_GLOBAL_NONSENSITIVE ) && + (flags & __GFP_GLOBAL_NONSENSITIVE)); + } +if (should_failslab (s, flags))return NULL ;
mm/slab_common.c +56 -17
134 lines not on this page · raw message
security/Kconfig +1 -1
@@ -68,7 +68,7 @@ config PAGE_TABLE_ISOLATION config ADDRESS_SPACE_ISOLATION bool "Allow code to run with a reduced kernel address space" default n - depends on X86_64 && !UML + depends on X86_64 && !UML && SLAB depends on !PARAVIRT help This feature provides the ability to run some kernel code
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 13/47 asi: Added ASI memory cgroup flag
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
Adds a cgroup flag to control if ASI is enabled for processes in that cgroup.
Can be set or cleared by writing to the memory.use_asi file in the memory cgroup. The flag only affects new processes created after the flag was set.
In addition to the cgroup flag, we may also want to add a per-process flag, though it will have to be something that can be set at process creation time.
--- arch/x86/mm/asi.c | 14 ++++++++++++++ include/linux/memcontrol.h | 3 +++ include/linux/mm_types.h | 17 +++++++++++++++++ mm/memcontrol.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+)
arch/x86/mm/asi.c +14 -0
@@ -2,6 +2,7 @@#include <linux/init.h> #include <linux/memblock.h> +#include <linux/memcontrol.h> #include <asm/asi.h> #include <asm/pgalloc.h>
@@ -322,7 +323,20 @@ EXPORT_SYMBOL_GPL(asi_exit);void asi_init_mm_state (struct mm_struct *mm) { +struct mem_cgroup *memcg =get_mem_cgroup_from_mm (mm); +memset (mm->asi,0 ,sizeof (mm->asi)); + mm->asi_enabled = false; + +/* +* TODO: In addition to a cgroup flag, we may also want a per-process +* flag. +*/ +if (memcg) { + mm->asi_enabled =boot_cpu_has (X86_FEATURE_ASI ) && + memcg->use_asi; +css_put (&memcg->css); + } } static bool is_page_within_range(size_t addr, size_t page_size,
include/linux/memcontrol.h +3 -0
@@ -259,6 +259,9 @@ struct mem_cgroup {*/ bool oom_group; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +bool use_asi; +#endif /* protected by memcg_oom_lock */ bool oom_lock;int under_oom;
include/linux/mm_types.h +17 -0
@@ -607,6 +607,14 @@ struct mm_struct {* new_owner->alloc_lock is held */ struct task_struct __rcu *owner; + +#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +/* Is ASI enabled for this mm? ASI requires allocating extra +* resources, such as ASI page tables. To prevent allocationg +* these resources for every mm in the system, we expect that +* only VM mm's will have this flag set. */ +bool asi_enabled;#endif struct user_namespace *user_ns;
@@ -665,6 +673,15 @@ struct mm_struct {extern struct mm_struct init_mm; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +static inline bool mm_asi_enabled (struct mm_struct *mm) +{ +return mm->asi_enabled; +} +#else +static inline bool mm_asi_enabled (struct mm_struct *mm) {return false; } +#endif +/* Pointer magic because the dynamic array size confuses some compilers. */ static inline void mm_init_cpumask (struct mm_struct *mm) {
mm/memcontrol.c +30 -0
@@ -3539,6 +3539,29 @@ static int mem_cgroup_hierarchy_write(struct cgroup_subsys_state *css,return -EINVAL ; } +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION + +static u64 mem_cgroup_asi_read (struct cgroup_subsys_state *css, +struct cftype *cft) +{ +return mem_cgroup_from_css (css)->use_asi; +} + +static int mem_cgroup_asi_write (struct cgroup_subsys_state *css, +struct cftype *cft,u64 val) +{ +struct mem_cgroup *memcg =mem_cgroup_from_css (css); + +if (val ==1 || val ==0 ) + memcg->use_asi = val; +else +return -EINVAL ; + +return 0 ; +} + +#endif +static unsigned long mem_cgroup_usage (struct mem_cgroup *memcg,bool swap) {unsigned long val;
@@ -4888,6 +4911,13 @@ static struct cftype mem_cgroup_legacy_files[] = { .write_u64 = mem_cgroup_hierarchy_write, .read_u64 = mem_cgroup_hierarchy_read, }, +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION + { + .name = "use_asi", + .write_u64 = mem_cgroup_asi_write, + .read_u64 = mem_cgroup_asi_read, + }, +#endif { .name = "cgroup.event_control", /* XXX: for compat */ .write = memcg_write_event_control,
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 14/47 mm: asi: Disable ASI API when ASI is not enabled for a process
In reply to Junaid ShahidIf ASI is not enabled for a process, then asi_init() will return a NULL ASI pointer as output, though it will return a 0 error code. All other ASI API functions will return without an error when they get a NULL ASI pointer.
--- arch/x86/include/asm/asi.h | 2 +- arch/x86/mm/asi.c | 18 ++++++++++-------- include/asm-generic/asi.h | 7 ++++++- 3 files changed, 17 insertions(+), 10 deletions(-)
arch/x86/include/asm/asi.h +1 -1
@@ -51,7 +51,7 @@ int asi_register_class(const char *name, uint flags, const struct asi_hooks *ops);void asi_unregister_class (int index); -int asi_init (struct mm_struct *mm,int asi_index); +int asi_init (struct mm_struct *mm,int asi_index,struct asi **out_asi);void asi_destroy (struct asi *asi);void asi_enter (struct asi *asi);
arch/x86/mm/asi.c +10 -8
@@ -207,11 +207,13 @@ static int __init asi_global_init(void) }subsys_initcall (asi_global_init) -int asi_init(struct mm_struct *mm, int asi_index) +int asi_init(struct mm_struct *mm, int asi_index, struct asi **out_asi) {struct asi *asi = &mm->asi[asi_index]; -if (!boot_cpu_has (X86_FEATURE_ASI )) + *out_asi =NULL ; + +if (!boot_cpu_has (X86_FEATURE_ASI ) || !mm->asi_enabled)return 0 ;/* Index 0 is reserved for special purposes. */
@@ -238,13 +240,15 @@ int asi_init(struct mm_struct *mm, int asi_index)set_pgd (asi->pgd + i, asi_global_nonsensitive_pgd[i]); } + *out_asi = asi; +return 0 ; }EXPORT_SYMBOL_GPL (asi_init);void asi_destroy (struct asi *asi) { -if (!boot_cpu_has (X86_FEATURE_ASI )) +if (!boot_cpu_has (X86_FEATURE_ASI ) || !asi)return ;asi_free_pgd (asi);
@@ -278,11 +282,9 @@ void __asi_enter(void)void asi_enter (struct asi *asi) { -if (!static_cpu_has (X86_FEATURE_ASI )) +if (!static_cpu_has (X86_FEATURE_ASI ) || !asi)return ; -VM_WARN_ON_ONCE (!asi); -this_cpu_write (asi_cpu_state.target_asi, asi);barrier ();
@@ -423,7 +425,7 @@ int asi_map_gfp(struct asi *asi, void *addr, size_t len, gfp_t gfp_flags)size_t end = start + len;size_t page_size; -if (!static_cpu_has (X86_FEATURE_ASI )) +if (!static_cpu_has (X86_FEATURE_ASI ) || !asi)return 0 ;VM_BUG_ON (start & ~PAGE_MASK );
@@ -514,7 +516,7 @@ void asi_unmap(struct asi *asi, void *addr, size_t len, bool flush_tlb)size_t end = start + len;pgtbl_mod_mask mask =0 ; -if (!static_cpu_has (X86_FEATURE_ASI ) || !len) +if (!static_cpu_has (X86_FEATURE_ASI ) || !asi || !len)return ;VM_BUG_ON (start & ~PAGE_MASK );
include/asm-generic/asi.h +6 -1
@@ -33,7 +33,12 @@ static inline void asi_unregister_class(int asi_index) { }static inline void asi_init_mm_state (struct mm_struct *mm) { } -static inline int asi_init (struct mm_struct *mm,int asi_index) {return 0 ; } +static inline +int asi_init (struct mm_struct *mm,int asi_index,struct asi **out_asi) +{ + *out_asi =NULL ; +return 0 ; +}static inline void asi_destroy (struct asi *asi) { }
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 15/47 kvm: asi: Restricted address space for VM execution
In reply to Junaid ShahidAn ASI restricted address space is added for KVM. It is currently only enabled for Intel CPUs. The ASI hooks have been setup to do an L1D cache flush and MDS clear when entering the restricted address space.
The hooks are also meant to stun and unstun the sibling hyperthread when exiting and entering the restricted address space. Internally, we do have a full stunning implementation available, but it hasn't yet been determined whether it is fully compatible with the upstream core scheduling implementation, so it is not included in this patch series and instead this patch just includes corresponding stub functions to demonstrate where the stun/unstun would happen.
--- arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/vmx/vmx.c | 41 ++++++++++++----- arch/x86/kvm/x86.c | 81 ++++++++++++++++++++++++++++++++- include/linux/kvm_host.h | 2 + 4 files changed, 113 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1494,6 +1494,8 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); + +void (*flush_sensitive_cpu_state)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {
arch/x86/kvm/vmx/vmx.c +29 -12
95 lines not on this page · raw message
arch/x86/kvm/x86.c +80 -1
133 lines not on this page · raw message
include/linux/kvm_host.h +2 -0
@@ -38,6 +38,7 @@#include <asm/kvm_host.h> #include <linux/kvm_dirty_ring.h> +#include <asm/asi.h> #ifndef KVM_MAX_VCPU_IDS #define KVM_MAX_VCPU_IDS KVM_MAX_VCPUS
@@ -551,6 +552,7 @@ struct kvm {*/ struct mutex slots_arch_lock;struct mm_struct *mm;/* userspace tied to this vm */ +struct asi *asi;struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM ];struct kvm_vcpu *vcpus[KVM_MAX_VCPUS ];
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 16/47 mm: asi: Support for mapping non-sensitive pcpu chunks
In reply to Junaid ShahidThis adds support for mapping and unmapping dynamic percpu chunks as globally non-sensitive. A later patch will modify the percpu allocator to use this for dynamically allocating non-sensitive percpu memory.
--- include/linux/vmalloc.h | 4 ++-- mm/percpu-vm.c | 51 +++++++++++++++++++++++++++++++++-------- mm/vmalloc.c | 17 ++++++++++---- security/Kconfig | 2 +- 4 files changed, 58 insertions(+), 16 deletions(-)
include/linux/vmalloc.h +2 -2
@@ -260,14 +260,14 @@ extern __init void vm_area_register_early(struct vm_struct *vm, size_t align);# ifdef CONFIG_MMU struct vm_struct **pcpu_get_vm_areas (const unsigned long *offsets,const size_t *sizes,int nr_vms, -size_t align); +size_t align,ulong flags);void pcpu_free_vm_areas (struct vm_struct **vms,int nr_vms);# else static inline struct vm_struct **pcpu_get_vm_areas (const unsigned long *offsets,const size_t *sizes,int nr_vms, -size_t align) +size_t align,ulong flags) {return NULL ; }
mm/percpu-vm.c +42 -9
106 lines not on this page · raw message
mm/vmalloc.c +13 -4
@@ -3664,10 +3664,10 @@ pvm_determine_end_from_reverse(struct vmap_area **va, unsigned long align)*/ struct vm_struct **pcpu_get_vm_areas (const unsigned long *offsets,const size_t *sizes,int nr_vms, -size_t align) +size_t align,ulong flags) { -const unsigned long vmalloc_start =ALIGN (VMALLOC_START , align); -const unsigned long vmalloc_end =VMALLOC_END & ~(align -1 ); +unsigned long vmalloc_start =VMALLOC_START ; +unsigned long vmalloc_end =VMALLOC_END ;struct vmap_area **vas, *va;struct vm_struct **vms;int area, area2, last_area, term_area;
@@ -3677,6 +3677,15 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,/* verify parameters and allocate data structures */ BUG_ON (offset_in_page (align) || !is_power_of_2 (align)); + +if (static_asi_enabled () && (flags &VM_GLOBAL_NONSENSITIVE )) { + vmalloc_start =VMALLOC_GLOBAL_NONSENSITIVE_START ; + vmalloc_end =VMALLOC_GLOBAL_NONSENSITIVE_END ; + } + + vmalloc_start =ALIGN (vmalloc_start, align); + vmalloc_end = vmalloc_end & ~(align -1 ); +for (last_area =0 , area =0 ; area < nr_vms; area++) { start = offsets[area]; end = start + sizes[area];
@@ -3815,7 +3824,7 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,for (area =0 ; area < nr_vms; area++) {insert_vmap_area (vas[area], &vmap_area_root, &vmap_area_list); -setup_vmalloc_vm_locked (vms[area], vas[area],VM_ALLOC , +setup_vmalloc_vm_locked (vms[area], vas[area], flags |VM_ALLOC , pcpu_get_vm_areas); }spin_unlock (&vmap_area_lock);
security/Kconfig +1 -1
@@ -68,7 +68,7 @@ config PAGE_TABLE_ISOLATION config ADDRESS_SPACE_ISOLATION bool "Allow code to run with a reduced kernel address space" default n - depends on X86_64 && !UML && SLAB + depends on X86_64 && !UML && SLAB && !NEED_PER_CPU_KM depends on !PARAVIRT help This feature provides the ability to run some kernel code
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 17/47 mm: asi: Aliased direct map for local non-sensitive allocations
In reply to Junaid ShahidThis creates a second copy of the direct map, which mirrors the normal direct map in the regular unrestricted kernel page tables. But in the ASI restricted address spaces, the page tables for this aliased direct map would be local to each process. So this aliased map can be used for locally non-sensitive page allocations.
Because of the lack of available kernel virtual address space, we have to reduce the max possible direct map size by half. That should be fine with 5 level page tables but could be an issue with 4 level page tables (as max 32 TB RAM could be supported instead of 64 TB).
An alternative vmap-style implementation of an aliased local region is possible without this limitation, but that has some other compromises and would be usable only if we trim down the types of structures marked as local non-sensitive by limiting the designation to only those that really are locally non-sensitive but globally sensitive. That is certainly ideal and likely feasible, and would also allow removal of some other relatively complex infrastructure introduced in later patches. But we are including this implementation here just for demonstration of a fully general mechanism.
An altogether different alternative to a separate aliased region is also possible by just partitioning the regular direct map (either statically or dynamically via additional page-block types), which is certainly feasible but would require more effort to implement properly.
--- arch/x86/include/asm/page.h | 19 +++++++- arch/x86/include/asm/page_64.h | 25 +++++++++- arch/x86/include/asm/page_64_types.h | 20 ++++++++ arch/x86/kernel/e820.c | 7 ++- arch/x86/mm/asi.c | 69 +++++++++++++++++++++++++++- arch/x86/mm/kaslr.c | 34 +++++++++++++- arch/x86/mm/mm_internal.h | 2 + arch/x86/mm/physaddr.c | 8 ++++ include/linux/page-flags.h | 3 ++ include/trace/events/mmflags.h | 3 +- security/Kconfig | 1 + 11 files changed, 183 insertions(+), 8 deletions(-)
arch/x86/include/asm/page.h +18 -1
@@ -18,6 +18,7 @@struct page ; +#include <linux/jump_label.h> #include <linux/range.h> extern struct range pfn_mapped[];extern int nr_pfn_mapped;
@@ -56,8 +57,24 @@ static inline void copy_user_page(void *to, void *from, unsigned long vaddr, __phys_addr_symbol(__phys_reloc_hide((unsigned long)(x)))#ifndef __va -#define __va (x) ((void *)((unsigned long)(x)+PAGE_OFFSET)) + +#define ___va (x) ((void *)((unsigned long)(x)+PAGE_OFFSET)) + +#ifndef CONFIG_ADDRESS_SPACE_ISOLATION +#define __va (x) ___va(x) +#else + +DECLARE_STATIC_KEY_FALSE (asi_local_map_initialized); +void *asi_va (unsigned long pa); + +/* +* This might significantly increase the size of the jump table. +* If that turns out to be a problem, we should use a non-static branch. +*/ +#define __va (x) (static_branch_likely(&asi_local_map_initialized) \ + ? asi_va((unsigned long)(x)) : ___va(x))#endif +#endif /* __va */ #define __boot_va (x) __va(x)#define __boot_pa (x) __pa(x)
arch/x86/include/asm/page_64.h +24 -1
@@ -5,6 +5,7 @@#include <asm/page_64_types.h> #ifndef __ASSEMBLY__ +#include <linux/jump_label.h> #include <asm/alternative.h> /* duplicated to the one in bootmem.h */
@@ -15,12 +16,34 @@ extern unsigned long page_offset_base;extern unsigned long vmalloc_base;extern unsigned long vmemmap_base; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION + +extern unsigned long asi_local_map_base; +DECLARE_STATIC_KEY_FALSE (asi_local_map_initialized); + +#else + +/* Should never be used if ASI is not enabled */ +#define asi_local_map_base (*(ulong *)NULL) + +#endif +static inline unsigned long __phys_addr_nodebug (unsigned long x) {unsigned long y = x - __START_KERNEL_map; +unsigned long map_start =PAGE_OFFSET ; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +/* +* This might significantly increase the size of the jump table. +* If that turns out to be a problem, we should use a non-static branch. +*/ +if (static_branch_likely (&asi_local_map_initialized) && + x >ASI_LOCAL_MAP ) + map_start =ASI_LOCAL_MAP ; +#endif /* use the carry flag to determine if x was < __START_KERNEL_map */ - x = y + ((x > y) ? phys_base : (__START_KERNEL_map -PAGE_OFFSET )); + x = y + ((x > y) ? phys_base : (__START_KERNEL_map - map_start));return x; }
arch/x86/include/asm/page_64_types.h +20 -0
@@ -2,6 +2,8 @@#ifndef _ASM_X86_PAGE_64_DEFS_H#define _ASM_X86_PAGE_64_DEFS_H +#include <asm/sparsemem.h> +#ifndef __ASSEMBLY__#include <asm/kaslr.h> #endif
@@ -47,6 +49,24 @@#define __PAGE_OFFSET __PAGE_OFFSET_BASE_L4#endif /* CONFIG_DYNAMIC_MEMORY_LAYOUT */ +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION + +#define __ASI_LOCAL_MAP_BASE (__PAGE_OFFSET + \ + ALIGN(_BITUL(MAX_PHYSMEM_BITS - 1), PGDIR_SIZE)) + +#ifdef CONFIG_DYNAMIC_MEMORY_LAYOUT +#define ASI_LOCAL_MAP asi_local_map_base +#else +#define ASI_LOCAL_MAP __ASI_LOCAL_MAP_BASE +#endif + +#else /* CONFIG_ADDRESS_SPACE_ISOLATION */ + +/* Should never be used if ASI is not enabled */ +#define ASI_LOCAL_MAP (*(ulong *)NULL) + +#endif +#define __START_KERNEL_map _AC(0xffffffff80000000, UL)/* See Documentation/x86/x86_64/mm.rst for a description of the memory map. */
arch/x86/kernel/e820.c +6 -1
@@ -880,6 +880,11 @@ static void __init early_panic(char *msg)static int userdef__initdata ; +u64 __init set_phys_mem_limit (u64 size) +{ +return e820__range_remove (size,ULLONG_MAX - size,E820_TYPE_RAM ,1 ); +} +/* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ static int __init parse_memopt (char *p) {
@@ -905,7 +910,7 @@ static int __init parse_memopt(char *p)if (mem_size ==0 )return -EINVAL ; -e820__range_remove (mem_size,ULLONG_MAX - mem_size,E820_TYPE_RAM ,1 ); +set_phys_mem_limit (mem_size);#ifdef CONFIG_MEMORY_HOTPLUG max_mem_size = mem_size;
arch/x86/mm/asi.c +67 -2
103 lines not on this page · raw message
arch/x86/mm/kaslr.c +32 -2
@@ -48,6 +48,7 @@ static const unsigned long vaddr_end = CPU_ENTRY_AREA_BASE;static __initdata struct kaslr_memory_region {unsigned long *base;unsigned long size_tb; +unsigned long extra_bytes; } kaslr_regions[] = { { &page_offset_base,0 }, { &vmalloc_base,0 },
@@ -57,7 +58,7 @@ static __initdata struct kaslr_memory_region {/* Get size in bytes used by the memory region */ static inline unsigned long get_padding (struct kaslr_memory_region *region) { -return (region->size_tb <<TB_SHIFT ); +return (region->size_tb <<TB_SHIFT ) + region->extra_bytes; }/* Initialize base and padding for each memory region randomized with KASLR */
@@ -69,6 +70,8 @@ void __init kernel_randomize_memory(void)struct rnd_state rand_state;unsigned long remain_entropy;unsigned long vmemmap_size; +unsigned int max_physmem_bits =MAX_PHYSMEM_BITS - + !!boot_cpu_has (X86_FEATURE_ASI ); vaddr_start =pgtable_l5_enabled () ? __PAGE_OFFSET_BASE_L5 : __PAGE_OFFSET_BASE_L4; vaddr = vaddr_start;
@@ -85,7 +88,7 @@ void __init kernel_randomize_memory(void)if (!kaslr_memory_enabled ())return ; - kaslr_regions[0 ].size_tb =1 << (MAX_PHYSMEM_BITS -TB_SHIFT ); + kaslr_regions[0 ].size_tb =1 << (max_physmem_bits -TB_SHIFT ); kaslr_regions[1 ].size_tb =VMALLOC_SIZE_TB ;/*
@@ -100,6 +103,18 @@ void __init kernel_randomize_memory(void)if (memory_tb < kaslr_regions[0 ].size_tb) kaslr_regions[0 ].size_tb = memory_tb; +if (boot_cpu_has (X86_FEATURE_ASI )) { +ulong direct_map_size = kaslr_regions[0 ].size_tb <<TB_SHIFT ; + +/* Reserve additional space for the ASI Local Map */ + direct_map_size =round_up (direct_map_size,PGDIR_SIZE ); + direct_map_size *=2 ; +VM_BUG_ON (direct_map_size % (1UL <<TB_SHIFT )); + + kaslr_regions[0 ].size_tb = direct_map_size >>TB_SHIFT ; + kaslr_regions[0 ].extra_bytes =PGDIR_SIZE ; + } +/* * Calculate the vmemmap region size in TBs, aligned to a TB * boundary.
@@ -136,6 +151,21 @@ void __init kernel_randomize_memory(void) vaddr =round_up (vaddr +1 ,PUD_SIZE ); remain_entropy -= entropy; } + +/* +* This ensures that the ASI Local Map does not share a PGD entry with +* the regular direct map, and also that the alignment of the two +* regions is the same. +* +* We are relying on the fact that the region following the ASI Local +* Map will be the local non-sensitive portion of the VMALLOC region. +* If that were not the case and the next region was a global one, +* then we would need extra padding after the ASI Local Map to ensure +* that it doesn't share a PGD entry with that global region. +*/ +if (cpu_feature_enabled (X86_FEATURE_ASI )) + asi_local_map_base = page_offset_base +PGDIR_SIZE + + ((kaslr_regions[0 ].size_tb /2 ) <<TB_SHIFT ); }void __meminit init_trampoline_kaslr (void )
arch/x86/mm/mm_internal.h +2 -0
@@ -28,4 +28,6 @@ void update_cache_mode_entry(unsigned entry, enum page_cache_mode cache);extern unsigned long tlb_single_page_flush_ceiling; +u64 set_phys_mem_limit (u64 size); +#endif /* __X86_MM_INTERNAL_H */
arch/x86/mm/physaddr.c +8 -0
@@ -21,6 +21,9 @@ unsigned long __phys_addr(unsigned long x) x = y + phys_base;VIRTUAL_BUG_ON (y >=KERNEL_IMAGE_SIZE ); + }else if (cpu_feature_enabled (X86_FEATURE_ASI ) && x >ASI_LOCAL_MAP ) { + x -=ASI_LOCAL_MAP ; +VIRTUAL_BUG_ON (!phys_addr_valid (x)); }else { x = y + (__START_KERNEL_map -PAGE_OFFSET );
@@ -28,6 +31,7 @@ unsigned long __phys_addr(unsigned long x)VIRTUAL_BUG_ON ((x > y) || !phys_addr_valid (x)); } +VIRTUAL_BUG_ON (!pfn_valid (x >>PAGE_SHIFT ));return x; }EXPORT_SYMBOL (__phys_addr);
@@ -54,6 +58,10 @@ bool __virt_addr_valid(unsigned long x)if (y >=KERNEL_IMAGE_SIZE )return false; + }else if (cpu_feature_enabled (X86_FEATURE_ASI ) && x >ASI_LOCAL_MAP ) { + x -=ASI_LOCAL_MAP ; +if (!phys_addr_valid (x)) +return false; }else { x = y + (__START_KERNEL_map -PAGE_OFFSET );
include/linux/page-flags.h +3 -0
@@ -143,6 +143,7 @@ enum pageflags {#endif #ifdef CONFIG_ADDRESS_SPACE_ISOLATION PG_global_nonsensitive, + PG_local_nonsensitive, #endif __NR_PAGEFLAGS,
@@ -547,8 +548,10 @@ PAGEFLAG(Idle, idle, PF_ANY)#ifdef CONFIG_ADDRESS_SPACE_ISOLATION __PAGEFLAG (GlobalNonSensitive, global_nonsensitive,PF_ANY ); +__PAGEFLAG (LocalNonSensitive, local_nonsensitive,PF_ANY );#else __PAGEFLAG_FALSE (GlobalNonSensitive, global_nonsensitive); +__PAGEFLAG_FALSE (LocalNonSensitive, local_nonsensitive);#endif #ifdef CONFIG_KASAN_HW_TAGS
include/trace/events/mmflags.h +2 -1
@@ -129,7 +129,8 @@ IF_HAVE_PG_IDLE(PG_young, "young" ) \ IF_HAVE_PG_IDLE(PG_idle, "idle" ) \ IF_HAVE_PG_ARCH_2(PG_arch_2, "arch_2" ) \ IF_HAVE_PG_SKIP_KASAN_POISON(PG_skip_kasan_poison, "skip_kasan_poison") \ -IF_HAVE_ASI(PG_global_nonsensitive, "global_nonsensitive") +IF_HAVE_ASI(PG_global_nonsensitive, "global_nonsensitive") \ +IF_HAVE_ASI(PG_local_nonsensitive, "local_nonsensitive")#define show_page_flags (flags) \ (flags) ? __print_flags(flags, "|", \
security/Kconfig +1 -0
@@ -70,6 +70,7 @@ config ADDRESS_SPACE_ISOLATION default n depends on X86_64 && !UML && SLAB && !NEED_PER_CPU_KM depends on !PARAVIRT + depends on !MEMORY_HOTPLUG help This feature provides the ability to run some kernel code with a reduced kernel address space. This can be used to
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 18/47 mm: asi: Support for pre-ASI-init local non-sensitive allocations
In reply to Junaid ShahidLocal non-sensitive allocations can be made before an actual ASI instance is initialized. To support this, a process-wide pseudo-PGD is created, which contains mappings for all locally non-sensitive allocations. Memory can be mapped into this pseudo-PGD by using ASI_LOCAL_NONSENSITIVE when calling asi_map(). The mappings will be copied to an actual ASI PGD when an ASI instance is initialized in that process, by copying all the PGD entries in the local non-sensitive range from the pseudo-PGD to the ASI PGD. In addition, the page fault handler will copy any new PGD entries that get added after the initialization of the ASI instance.
--- arch/x86/include/asm/asi.h | 6 +++- arch/x86/mm/asi.c | 74 +++++++++++++++++++++++++++++++++++++- arch/x86/mm/fault.c | 7 ++++ include/asm-generic/asi.h | 12 ++++++- kernel/fork.c | 8 +++-- 5 files changed, 102 insertions(+), 5 deletions(-)
arch/x86/include/asm/asi.h +5 -1
@@ -16,6 +16,7 @@#define ASI_MAX_NUM (1 << ASI_MAX_NUM_ORDER)#define ASI_GLOBAL_NONSENSITIVE (&init_mm.asi[0]) +#define ASI_LOCAL_NONSENSITIVE (¤t->mm->asi[0])struct asi_state {struct asi *curr_asi;
@@ -45,7 +46,8 @@ DECLARE_PER_CPU_ALIGNED(struct asi_state, asi_cpu_state);extern pgd_t asi_global_nonsensitive_pgd[]; -void asi_init_mm_state (struct mm_struct *mm); +int asi_init_mm_state (struct mm_struct *mm); +void asi_free_mm_state (struct mm_struct *mm);int asi_register_class (const char *name,uint flags,const struct asi_hooks *ops);
@@ -61,6 +63,8 @@ int asi_map_gfp(struct asi *asi, void *addr, size_t len, gfp_t gfp_flags);int asi_map (struct asi *asi,void *addr,size_t len);void asi_unmap (struct asi *asi,void *addr,size_t len,bool flush_tlb);void asi_flush_tlb_range (struct asi *asi,void *addr,size_t len); +void asi_sync_mapping (struct asi *asi,void *addr,size_t len); +void asi_do_lazy_map (struct asi *asi,size_t addr);static inline void asi_init_thread_state (struct thread_struct *thread) {
arch/x86/mm/asi.c +73 -1
101 lines not on this page · raw message
arch/x86/mm/fault.c +7 -0
@@ -1498,6 +1498,12 @@ DEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault) {unsigned long address =read_cr2 ();irqentry_state_t state; +/* +* There is a very small chance that an NMI could cause an asi_exit() +* before this asi_get_current(), but that is ok, we will just do +* the fixup on the next page fault. +*/ +struct asi *asi =asi_get_current ();prefetchw (¤t->mm->mmap_lock);
@@ -1539,6 +1545,7 @@ DEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault)instrumentation_begin ();handle_page_fault (regs, error_code, address); +asi_do_lazy_map (asi, address);instrumentation_end ();irqentry_exit (regs, state);
include/asm-generic/asi.h +11 -1
@@ -13,6 +13,7 @@#define ASI_MAX_NUM 0#define ASI_GLOBAL_NONSENSITIVE NULL +#define ASI_LOCAL_NONSENSITIVE NULL#define VMALLOC_GLOBAL_NONSENSITIVE_START VMALLOC_START#define VMALLOC_GLOBAL_NONSENSITIVE_END VMALLOC_END
@@ -31,7 +32,9 @@ int asi_register_class(const char *name, uint flags,static inline void asi_unregister_class (int asi_index) { } -static inline void asi_init_mm_state (struct mm_struct *mm) { } +static inline int asi_init_mm_state (struct mm_struct *mm) {return 0 ; } + +static inline void asi_free_mm_state (struct mm_struct *mm) { }static inline int asi_init (struct mm_struct *mm,int asi_index,struct asi **out_asi)
@@ -67,9 +70,16 @@ static inline int asi_map(struct asi *asi, void *addr, size_t len)return 0 ; } +static inline +void asi_sync_mapping (struct asi *asi,void *addr,size_t len) { } +static inline void asi_unmap (struct asi *asi,void *addr,size_t len,bool flush_tlb) { } + +static inline +void asi_do_lazy_map (struct asi *asi,size_t addr) { } +static inline void asi_flush_tlb_range (struct asi *asi,void *addr,size_t len) { }
kernel/fork.c +6 -2
@@ -699,6 +699,7 @@ void __mmdrop(struct mm_struct *mm)mm_free_pgd (mm);destroy_context (mm);mmu_notifier_subscriptions_destroy (mm); +asi_free_mm_state (mm);check_mm (mm);put_user_ns (mm->user_ns);free_mm (mm);
@@ -1072,17 +1073,20 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, mm->def_flags =0 ; } -asi_init_mm_state (mm); -if (mm_alloc_pgd (mm)) goto fail_nopgd;if (init_new_context (p, mm)) goto fail_nocontext; +if (asi_init_mm_state (mm)) + goto fail_noasi; + mm->user_ns =get_user_ns (user_ns); +return mm; +fail_noasi: fail_nocontext:mm_free_pgd (mm); fail_nopgd:
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 19/47 mm: asi: Support for locally nonsensitive page allocations
In reply to Junaid ShahidA new GFP flag, __GFP_LOCAL_NONSENSITIVE, is added to allocate pages that are considered non-sensitive within the context of the current process, but sensitive in the context of other processes.
For these allocations, page->asi_mm is set to the current mm during allocation. It must be set to the same value when the page is freed. Though it can potentially be overwritten and used for some other purpose in the meantime, as long as it is restored before freeing.
--- include/linux/gfp.h | 5 +++- include/linux/mm_types.h | 17 ++++++++++-- include/trace/events/mmflags.h | 1 + mm/page_alloc.c | 47 ++++++++++++++++++++++++++++------ tools/perf/builtin-kmem.c | 1 + 5 files changed, 60 insertions(+), 11 deletions(-)
include/linux/gfp.h +4 -1
@@ -62,8 +62,10 @@ struct vm_area_struct;#endif #ifdef CONFIG_ADDRESS_SPACE_ISOLATION #define ___GFP_GLOBAL_NONSENSITIVE 0x4000000u +#define ___GFP_LOCAL_NONSENSITIVE 0x8000000u#else #define ___GFP_GLOBAL_NONSENSITIVE 0 +#define ___GFP_LOCAL_NONSENSITIVE 0#endif /* If the above are modified, __GFP_BITS_SHIFT may need updating */
@@ -255,9 +257,10 @@ struct vm_area_struct;/* Allocate non-sensitive memory */ #define __GFP_GLOBAL_NONSENSITIVE ((__force gfp_t)___GFP_GLOBAL_NONSENSITIVE) +#define __GFP_LOCAL_NONSENSITIVE ((__force gfp_t)___GFP_LOCAL_NONSENSITIVE)/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT 27 +#define __GFP_BITS_SHIFT 28#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/**
include/linux/mm_types.h +15 -2
@@ -193,8 +193,21 @@ struct page {struct rcu_head rcu_head;#ifdef CONFIG_ADDRESS_SPACE_ISOLATION -/* Links the pages_to_free_async list */ -struct llist_node async_free_node; +struct { +/* Links the pages_to_free_async list */ +struct llist_node async_free_node; + +unsigned long _asi_pad_1; +unsigned long _asi_pad_2; + +/* +* Upon allocation of a locally non-sensitive page, set +* to the allocating mm. Must be set to the same mm when +* the page is freed. May potentially be overwritten in +* the meantime, as long as it is restored before free. +*/ +struct mm_struct *asi_mm; + };#endif };
include/trace/events/mmflags.h +1 -0
@@ -51,6 +51,7 @@ {(unsigned long)__GFP_KSWAPD_RECLAIM, "__GFP_KSWAPD_RECLAIM"},\ {(unsigned long)__GFP_ZEROTAGS, "__GFP_ZEROTAGS"}, \ {(unsigned long)__GFP_SKIP_KASAN_POISON,"__GFP_SKIP_KASAN_POISON"},\ + {(unsigned long)__GFP_LOCAL_NONSENSITIVE, "__GFP_LOCAL_NONSENSITIVE"},\ {(unsigned long)__GFP_GLOBAL_NONSENSITIVE, "__GFP_GLOBAL_NONSENSITIVE"}\ #define show_gfp_flags(flags) \
mm/page_alloc.c +39 -8
84 lines not on this page · raw message
tools/perf/builtin-kmem.c +1 -0
@@ -661,6 +661,7 @@ static const struct { { "__GFP_DIRECT_RECLAIM", "DR" }, { "__GFP_KSWAPD_RECLAIM", "KR" }, { "__GFP_GLOBAL_NONSENSITIVE", "GNS" }, + { "__GFP_LOCAL_NONSENSITIVE", "LNS" }, };static size_t max_gfp_len;
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 20/47 mm: asi: Support for locally non-sensitive vmalloc allocations
In reply to Junaid ShahidA new flag, VM_LOCAL_NONSENSITIVE is added to designate locally non-sensitive vmalloc/vmap areas. When using the __vmalloc / __vmalloc_node APIs, if the corresponding GFP flag is specified, the VM flag is automatically added. When using the __vmalloc_node_range API, either flag can be specified independently. The VM flag will only map the vmalloc area as non-sensitive, while the GFP flag will only map the underlying direct map area as non-sensitive.
When using the __vmalloc_node_range API, instead of VMALLOC_START/END, VMALLOC_LOCAL_NONSENSITIVE_START/END should be used. This is the range that will have different ASI page tables for each process, thereby providing the local mapping.
A command line parameter vmalloc_local_nonsensitive_percent is added to specify the approximate division between the per-process and global vmalloc ranges. Note that regular/sensitive vmalloc/vmap allocations are not restricted by this division and can go anywhere in the entire vmalloc range. The division only applies to non-sensitive allocations.
Since no attempt is made to balance regular/sensitive allocations across the division, it is possible that one of these ranges gets filled up by regular allocations, leaving no room for the non-sensitive allocations for which that range was designated. But since the vmalloc range is fairly large, so hopefully that will not be a problem in practice. If that assumption turns out to be incorrect, we could implement a more sophisticated scheme.
--- arch/x86/include/asm/asi.h | 2 + arch/x86/include/asm/page_64.h | 2 + arch/x86/include/asm/pgtable_64_types.h | 7 ++- arch/x86/mm/asi.c | 57 ++++++++++++++++++ include/asm-generic/asi.h | 5 ++ include/linux/vmalloc.h | 6 ++ mm/vmalloc.c | 78 ++++++++++++++++++++----- 7 files changed, 142 insertions(+), 15 deletions(-)
arch/x86/include/asm/asi.h +2 -0
@@ -46,6 +46,8 @@ DECLARE_PER_CPU_ALIGNED(struct asi_state, asi_cpu_state);extern pgd_t asi_global_nonsensitive_pgd[]; +void asi_vmalloc_init (void ); +int asi_init_mm_state (struct mm_struct *mm);void asi_free_mm_state (struct mm_struct *mm);
arch/x86/include/asm/page_64.h +2 -0
@@ -18,6 +18,8 @@ extern unsigned long vmemmap_base;#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +extern unsigned long vmalloc_global_nonsensitive_start; +extern unsigned long vmalloc_local_nonsensitive_end;extern unsigned long asi_local_map_base;DECLARE_STATIC_KEY_FALSE (asi_local_map_initialized);
arch/x86/include/asm/pgtable_64_types.h +6 -1
@@ -142,8 +142,13 @@ extern unsigned int ptrs_per_p4d;#define VMALLOC_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1)#ifdef CONFIG_ADDRESS_SPACE_ISOLATION -#define VMALLOC_GLOBAL_NONSENSITIVE_START VMALLOC_START + +#define VMALLOC_LOCAL_NONSENSITIVE_START VMALLOC_START +#define VMALLOC_LOCAL_NONSENSITIVE_END vmalloc_local_nonsensitive_end + +#define VMALLOC_GLOBAL_NONSENSITIVE_START vmalloc_global_nonsensitive_start#define VMALLOC_GLOBAL_NONSENSITIVE_END VMALLOC_END +#endif #define MODULES_VADDR (__START_KERNEL_map + KERNEL_IMAGE_SIZE)
arch/x86/mm/asi.c +57 -0
84 lines not on this page · raw message
include/asm-generic/asi.h +5 -0
@@ -18,6 +18,9 @@#define VMALLOC_GLOBAL_NONSENSITIVE_START VMALLOC_START#define VMALLOC_GLOBAL_NONSENSITIVE_END VMALLOC_END +#define VMALLOC_LOCAL_NONSENSITIVE_START VMALLOC_START +#define VMALLOC_LOCAL_NONSENSITIVE_END VMALLOC_END +#ifndef _ASSEMBLY_struct asi_hooks {};
@@ -36,6 +39,8 @@ static inline int asi_init_mm_state(struct mm_struct *mm) { return 0; }static inline void asi_free_mm_state (struct mm_struct *mm) { } +static inline void asi_vmalloc_init (void ) { } +static inline int asi_init (struct mm_struct *mm,int asi_index,struct asi **out_asi) {
include/linux/vmalloc.h +6 -0
@@ -41,8 +41,10 @@ struct notifier_block; /* in notifier.h */#ifdef CONFIG_ADDRESS_SPACE_ISOLATION #define VM_GLOBAL_NONSENSITIVE 0x00000800/* Similar to __GFP_GLOBAL_NONSENSITIVE */ +#define VM_LOCAL_NONSENSITIVE 0x00001000/* Similar to __GFP_LOCAL_NONSENSITIVE */ #else #define VM_GLOBAL_NONSENSITIVE 0 +#define VM_LOCAL_NONSENSITIVE 0#endif /* bits [20..32] reserved for arch specific ioremap internals */
@@ -67,6 +69,10 @@ struct vm_struct {unsigned int nr_pages;phys_addr_t phys_addr;const void *caller; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +/* Valid if flags contain VM_*_NONSENSITIVE */ +struct asi *asi; +#endif };struct vmap_area {
mm/vmalloc.c +64 -14
127 lines not on this page · raw message
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 21/47 mm: asi: Add support for locally non-sensitive VM_USERMAP pages
In reply to Junaid ShahidVM_USERMAP pages can be mapped into userspace, which would overwrite the asi_mm field, so we restore that field when freeing these pages.
--- mm/vmalloc.c | 8 ++++++++ 1 file changed, 8 insertions(+)
mm/vmalloc.c +8 -0
@@ -2424,6 +2424,14 @@ static void asi_unmap_vm_area(struct vm_struct *area)*/ if (area->flags & (VM_GLOBAL_NONSENSITIVE |VM_LOCAL_NONSENSITIVE ))asi_unmap (area->asi, area->addr,get_vm_area_size (area), true); + +if (area->flags &VM_USERMAP ) { +uint i; + +for (i =0 ; i < area->nr_pages; i++) +if (PageLocalNonSensitive (area->pages[i])) + area->pages[i]->asi_mm = area->asi->mm; + } }#else
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 22/47 mm: asi: Added refcounting when initilizing an asi
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
Some KVM tests initilize multiple VMs in a single process. For these cases, we want to suppurt multiple callse to asi_init() before a single asi_destroy is called. We want the initilization to happen exactly once. IF asi_destroy() is called, release the resources only if the counter reached zero. In our current implementation, asi's are tied to a specific mm. This may change in a future implementation. In which case, the mutex for the refcounting will need to move to struct asi.
--- arch/x86/include/asm/asi.h | 1 + arch/x86/mm/asi.c | 52 +++++++++++++++++++++++++++++++++----- include/linux/mm_types.h | 2 ++ kernel/fork.c | 3 +++ 4 files changed, 51 insertions(+), 7 deletions(-)
arch/x86/include/asm/asi.h +1 -0
@@ -40,6 +40,7 @@ struct asi {pgd_t *pgd;struct asi_class *class;struct mm_struct *mm; +int64_t asi_ref_count; }; DECLARE_PER_CPU_ALIGNED(struct asi_state, asi_cpu_state);
arch/x86/mm/asi.c +45 -7
87 lines not on this page · raw message
include/linux/mm_types.h +2 -0
@@ -16,6 +16,7 @@#include <linux/page-flags-layout.h> #include <linux/workqueue.h> #include <linux/seqlock.h> +#include <linux/mutex.h> #include <asm/mmu.h> #include <asm/asi.h>
@@ -628,6 +629,7 @@ struct mm_struct {* these resources for every mm in the system, we expect that * only VM mm's will have this flag set. */ bool asi_enabled; +struct mutex asi_init_lock;#endif struct user_namespace *user_ns;
kernel/fork.c +3 -0
@@ -1084,6 +1084,9 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, mm->user_ns =get_user_ns (user_ns); +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +mutex_init (&mm->asi_init_lock); +#endif return mm; fail_noasi:
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 23/47 mm: asi: Add support for mapping all userspace memory into ASI
In reply to Junaid ShahidThis adds a new ASI class flag, ASI_MAP_ALL_USERSPACE, which if set, would automatically map all userspace addresses into that ASI address space. This is achieved by lazily cloning the userspace PGD entries during page faults encountered while in that restricted address space.
When the userspace PGD entry is cleared (e.g. in munmap()), we go through all restricted address spaces with the ASI_MAP_ALL_USERSPACE flag and clear the corresponding entry in those address spaces as well.
--- arch/x86/include/asm/asi.h | 2 + arch/x86/mm/asi.c | 81 ++++++++++++++++++++++++++++++++++++++ include/asm-generic/asi.h | 7 ++++ mm/memory.c | 2 + 4 files changed, 92 insertions(+)
arch/x86/include/asm/asi.h +2 -0
@@ -68,6 +68,8 @@ void asi_unmap(struct asi *asi, void *addr, size_t len, bool flush_tlb);void asi_flush_tlb_range (struct asi *asi,void *addr,size_t len);void asi_sync_mapping (struct asi *asi,void *addr,size_t len);void asi_do_lazy_map (struct asi *asi,size_t addr); +void asi_clear_user_pgd (struct mm_struct *mm,size_t addr); +void asi_clear_user_p4d (struct mm_struct *mm,size_t addr);static inline void asi_init_thread_state (struct thread_struct *thread) {
arch/x86/mm/asi.c +81 -0
96 lines not on this page · raw message
include/asm-generic/asi.h +7 -0
@@ -6,6 +6,7 @@/* ASI class flags */ #define ASI_MAP_STANDARD_NONSENSITIVE 1 +#define ASI_MAP_ALL_USERSPACE 2#ifndef CONFIG_ADDRESS_SPACE_ISOLATION
@@ -85,6 +86,12 @@ void asi_unmap(struct asi *asi, void *addr, size_t len, bool flush_tlb) { }static inline void asi_do_lazy_map (struct asi *asi,size_t addr) { } +static inline +void asi_clear_user_pgd (struct mm_struct *mm,size_t addr) { } + +static inline +void asi_clear_user_p4d (struct mm_struct *mm,size_t addr) { } +static inline void asi_flush_tlb_range (struct asi *asi,void *addr,size_t len) { }
mm/memory.c +2 -0
@@ -296,6 +296,7 @@ static inline void free_pud_range(struct mmu_gather *tlb, p4d_t *p4d, pud =pud_offset (p4d, start);p4d_clear (p4d); +asi_clear_user_p4d (tlb->mm, start);pud_free_tlb (tlb, pud, start);mm_dec_nr_puds (tlb->mm); }
@@ -330,6 +331,7 @@ static inline void free_p4d_range(struct mmu_gather *tlb, pgd_t *pgd, p4d =p4d_offset (pgd, start);pgd_clear (pgd); +asi_clear_user_pgd (tlb->mm, start);p4d_free_tlb (tlb, p4d, start); }
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 24/47 mm: asi: Support for local non-sensitive slab caches
In reply to Junaid ShahidA new flag SLAB_LOCAL_NONSENSITIVE is added to designate that a slab cache can be used for local non-sensitive allocations. For such caches, a per-process child cache will be created when a process tries to make an allocation from that cache for the first time, similar to the per-memcg child caches that used to exist before the object based memcg charging mechanism. (A lot of the infrastructure for handling these child caches is derived from the original per-memcg cache code).
If a cache only has SLAB_LOCAL_NONSENSITIVE, then all allocations from that cache will automatically be considered locally non-sensitive. But if a cache has both SLAB_LOCAL_NONSENSITIVE and SLAB_GLOBAL_NONSENSITIVE, then each allocation must specify one of __GFP_LOCAL_NONSENSITIVE or __GFP_GLOBAL_NONSENSITIVE.
Note that the first locally non-sensitive allocation that a process makes from a given slab cache must occur from a sleepable context. If that is not the case, then a new kmem_cache_precreate_local* API must be called from a sleepable context before the first allocation.
--- arch/x86/mm/asi.c | 5 + include/linux/mm_types.h | 4 + include/linux/sched/mm.h | 12 ++ include/linux/slab.h | 38 +++- include/linux/slab_def.h | 4 + kernel/fork.c | 3 +- mm/slab.c | 41 ++++- mm/slab.h | 151 +++++++++++++++- mm/slab_common.c | 363 ++++++++++++++++++++++++++++++++++++++- 9 files changed, 602 insertions(+), 19 deletions(-)
arch/x86/mm/asi.c +5 -0
@@ -4,6 +4,7 @@#include <linux/memblock.h> #include <linux/memcontrol.h> #include <linux/moduleparam.h> +#include <linux/slab.h> #include <asm/asi.h> #include <asm/pgalloc.h>
@@ -455,6 +456,8 @@ int asi_init_mm_state(struct mm_struct *mm)memset (mm->asi,0 ,sizeof (mm->asi)); mm->asi_enabled = false; +RCU_INIT_POINTER (mm->local_slab_caches,NULL ); + mm->local_slab_caches_array_size =0 ;/* * TODO: In addition to a cgroup flag, we may also want a per-process
@@ -482,6 +485,8 @@ void asi_free_mm_state(struct mm_struct *mm)if (!boot_cpu_has (X86_FEATURE_ASI ) || !mm->asi_enabled)return ; +free_local_slab_caches (mm); +asi_free_pgd_range (&mm->asi[0 ],pgd_index (ASI_LOCAL_MAP ),pgd_index (ASI_LOCAL_MAP +PFN_PHYS (max_possible_pfn)) +1 );
include/linux/mm_types.h +4 -0
@@ -517,6 +517,10 @@ struct mm_struct {struct asi asi[ASI_MAX_NUM ]; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +struct kmem_cache *__rcu *local_slab_caches; +uint local_slab_caches_array_size; +#endif /** * @mm_users: The number of users including userspace. *
include/linux/sched/mm.h +12 -0
@@ -37,9 +37,21 @@ static inline void mmgrab(struct mm_struct *mm) }extern void __mmdrop (struct mm_struct *mm); +extern void mmdrop_async (struct mm_struct *mm);static inline void mmdrop (struct mm_struct *mm) { +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +/* +* We really only need to do this if we are in an atomic context. +* Unfortunately, there doesn't seem to be a reliable way to detect +* atomic context across all kernel configs. So we just always do async. +*/ +if (rcu_access_pointer (mm->local_slab_caches)) { +mmdrop_async (mm); +return ; + } +#endif /* * The implicit full barrier implied by atomic_dec_and_test() is * required by the membarrier system call before returning to
include/linux/slab.h +37 -1
@@ -93,6 +93,8 @@/* Avoid kmemleak tracing */ #define SLAB_NOLEAKTRACE ((slab_flags_t __force)0x00800000U) +/* 0x01000000U is used below for SLAB_LOCAL_NONSENSITIVE */ +/* Fault injection mark */ #ifdef CONFIG_FAILSLAB # define SLAB_FAILSLAB ((slab_flags_t __force)0x02000000U)
@@ -121,8 +123,10 @@#define SLAB_DEACTIVATED ((slab_flags_t __force)0x10000000U)#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +#define SLAB_LOCAL_NONSENSITIVE ((slab_flags_t __force)0x01000000U)#define SLAB_GLOBAL_NONSENSITIVE ((slab_flags_t __force)0x20000000U)#else +#define SLAB_LOCAL_NONSENSITIVE 0#define SLAB_GLOBAL_NONSENSITIVE 0#endif
@@ -377,7 +381,8 @@ static __always_inline struct kmem_cache *get_kmalloc_cache(gfp_t flags, {#ifdef CONFIG_ADDRESS_SPACE_ISOLATION -if (static_asi_enabled () && (flags & __GFP_GLOBAL_NONSENSITIVE)) +if (static_asi_enabled () && + (flags & (__GFP_GLOBAL_NONSENSITIVE | __GFP_LOCAL_NONSENSITIVE)))return nonsensitive_kmalloc_caches[kmalloc_type (flags)][index];#endif return kmalloc_caches[kmalloc_type (flags)][index];
@@ -800,4 +805,35 @@ int slab_dead_cpu(unsigned int cpu);#define slab_dead_cpu NULL#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION + +struct kmem_cache *get_local_kmem_cache (struct kmem_cache *s, +struct mm_struct *mm,gfp_t flags); +void free_local_slab_caches (struct mm_struct *mm); +int kmem_cache_precreate_local (struct kmem_cache *s); +int kmem_cache_precreate_local_kmalloc (size_t size,gfp_t flags); + +#else + +static inline +struct kmem_cache *get_local_kmem_cache (struct kmem_cache *s, +struct mm_struct *mm,gfp_t flags) +{ +return NULL ; +} + +static inline void free_local_slab_caches (struct mm_struct *mm) { } + +static inline int kmem_cache_precreate_local (struct kmem_cache *s) +{ +return 0 ; +} + +static inline int kmem_cache_precreate_local_kmalloc (size_t size,gfp_t flags) +{ +return 0 ; +} + +#endif +#endif /* _LINUX_SLAB_H */
include/linux/slab_def.h +4 -0
@@ -81,6 +81,10 @@ struct kmem_cache {unsigned int *random_seq;#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +struct kmem_local_cache_info local_cache_info; +#endif +unsigned int useroffset;/* Usercopy region offset */ unsigned int usersize;/* Usercopy region size */
kernel/fork.c +2 -1
@@ -714,13 +714,14 @@ static void mmdrop_async_fn(struct work_struct *work)__mmdrop (mm); } -static void mmdrop_async (struct mm_struct *mm) +void mmdrop_async (struct mm_struct *mm) {if (unlikely (atomic_dec_and_test (&mm->mm_count))) {INIT_WORK (&mm->async_put_work, mmdrop_async_fn);schedule_work (&mm->async_put_work); } } +EXPORT_SYMBOL (mmdrop_async);static inline void free_signal_struct (struct signal_struct *sig) {
mm/slab.c +36 -5
73 lines not on this page · raw message
mm/slab.h +148 -3
190 lines not on this page · raw message
mm/slab_common.c +354 -9
474 lines not on this page · raw message
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 25/47 mm: asi: Avoid warning from NMI userspace accesses in ASI context
In reply to Junaid Shahidnmi_uaccess_okay() emits a warning if current CR3 != mm->pgd. Limit the warning to only when ASI is not active.
--- arch/x86/mm/tlb.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/tlb.c +2 -1
@@ -1292,7 +1292,8 @@ bool nmi_uaccess_okay(void)if (loaded_mm != current_mm)return false; -VM_WARN_ON_ONCE (current_mm->pgd !=__va (read_cr3_pa ())); +VM_WARN_ON_ONCE (current_mm->pgd !=__va (read_cr3_pa ()) && + !is_asi_active ());return true; }
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 26/47 mm: asi: Use separate PCIDs for restricted address spaces
In reply to Junaid ShahidEach restricted address space is assigned a separate PCID. Since currently only one ASI instance per-class exists for a given process, the PCID is just derived from the class index.
This commit only sets the appropriate PCID when switching CR3, but does not set the NOFLUSH bit. That will be done by later patches.
--- arch/x86/include/asm/asi.h | 3 ++- arch/x86/include/asm/tlbflush.h | 3 +++ arch/x86/mm/asi.c | 6 +++-- arch/x86/mm/tlb.c | 45 ++++++++++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 7 deletions(-)
arch/x86/include/asm/asi.h +2 -1
@@ -40,7 +40,8 @@ struct asi {pgd_t *pgd;struct asi_class *class;struct mm_struct *mm; -int64_t asi_ref_count; +u16 pcid_index; +int64_t asi_ref_count; }; DECLARE_PER_CPU_ALIGNED(struct asi_state, asi_cpu_state);
arch/x86/include/asm/tlbflush.h +3 -0
@@ -260,6 +260,9 @@ static inline void arch_tlbbatch_add_mm(struct arch_tlbflush_unmap_batch *batch,extern void arch_tlbbatch_flush (struct arch_tlbflush_unmap_batch *batch);unsigned long build_cr3 (pgd_t *pgd,u16 asid); +unsigned long build_cr3_pcid (pgd_t *pgd,u16 pcid,bool noflush); + +u16 asi_pcid (struct asi *asi,u16 asid);#endif /* !MODULE */
arch/x86/mm/asi.c +4 -2
@@ -335,6 +335,7 @@ int asi_init(struct mm_struct *mm, int asi_index, struct asi **out_asi) asi->class = &asi_class[asi_index]; asi->mm = mm; + asi->pcid_index = asi_index;if (asi->class->flags &ASI_MAP_STANDARD_NONSENSITIVE ) {uint i;
@@ -386,6 +387,7 @@ EXPORT_SYMBOL_GPL(asi_destroy);void __asi_enter (void ) {u64 asi_cr3; +u16 pcid;struct asi *target =this_cpu_read (asi_cpu_state.target_asi);VM_BUG_ON (preemptible ());
@@ -399,8 +401,8 @@ void __asi_enter(void)this_cpu_write (asi_cpu_state.curr_asi, target); - asi_cr3 =build_cr3 (target->pgd, -this_cpu_read (cpu_tlbstate.loaded_mm_asid)); + pcid =asi_pcid (target,this_cpu_read (cpu_tlbstate.loaded_mm_asid)); + asi_cr3 =build_cr3_pcid (target->pgd, pcid, false);write_cr3 (asi_cr3);if (target->class->ops.post_asi_enter)
arch/x86/mm/tlb.c +41 -4
67 lines not on this page · raw message
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 27/47 mm: asi: Avoid TLB flushes during ASI CR3 switches when possible
In reply to Junaid ShahidThe TLB flush functions are modified to flush the ASI PCIDs in addition to the unrestricted kernel PCID and the KPTI PCID. Some tracking is also added to figure out when the TLB state for ASI PCIDs is out-of-date (e.g. due to lack of INVPCID support), and ASI Enter/Exit use this information to skip a TLB flush during the CR3 switch when the TLB is already up-to-date.
--- arch/x86/include/asm/asi.h | 11 ++- arch/x86/include/asm/tlbflush.h | 47 ++++++++++ arch/x86/mm/asi.c | 38 +++++++- arch/x86/mm/tlb.c | 152 ++++++++++++++++++++++++++++++-- 4 files changed, 234 insertions(+), 14 deletions(-)
arch/x86/include/asm/asi.h +10 -1
@@ -126,11 +126,18 @@ static inline void asi_intr_exit(void)if (static_cpu_has (X86_FEATURE_ASI )) {barrier (); -if (--current->thread.intr_nest_depth ==0 ) +if (--current->thread.intr_nest_depth ==0 ) { +barrier ();__asi_enter (); + } } } +static inline int asi_intr_nest_depth (void ) +{ +return current->thread.intr_nest_depth; +} +#define INIT_MM_ASI (init_mm) \ .asi = { \ [0] = { \
@@ -150,6 +157,8 @@ static inline void asi_intr_enter(void) { }static inline void asi_intr_exit (void ) { } +static inline int asi_intr_nest_depth (void ) {return 0 ; } +static inline void asi_init_thread_state (struct thread_struct *thread) { }static inline pgd_t *asi_pgd (struct asi *asi) {return NULL ; }
arch/x86/include/asm/tlbflush.h +47 -0
76 lines not on this page · raw message
arch/x86/mm/asi.c +34 -4
@@ -388,6 +388,7 @@ void __asi_enter(void) {u64 asi_cr3;u16 pcid; +bool need_flush = false;struct asi *target =this_cpu_read (asi_cpu_state.target_asi);VM_BUG_ON (preemptible ());
@@ -401,8 +402,18 @@ void __asi_enter(void)this_cpu_write (asi_cpu_state.curr_asi, target); +if (static_cpu_has (X86_FEATURE_PCID )) + need_flush =asi_get_and_clear_tlb_flush_pending (target); + +/* +* It is possible that we may get a TLB flush IPI after +* already reading need_flush, in which case we won't do the +* flush below. However, in that case the interrupt epilog +* will also call __asi_enter(), which will do the flush. +*/ + pcid =asi_pcid (target,this_cpu_read (cpu_tlbstate.loaded_mm_asid)); - asi_cr3 =build_cr3_pcid (target->pgd, pcid, false); + asi_cr3 =build_cr3_pcid (target->pgd, pcid, !need_flush);write_cr3 (asi_cr3);if (target->class->ops.post_asi_enter)
@@ -437,12 +448,31 @@ void asi_exit(void) asi =this_cpu_read (asi_cpu_state.curr_asi);if (asi) { +bool need_flush = false; +if (asi->class->ops.pre_asi_exit) asi->class->ops.pre_asi_exit (); - unrestricted_cr3 = -build_cr3 (this_cpu_read (cpu_tlbstate.loaded_mm)->pgd, -this_cpu_read (cpu_tlbstate.loaded_mm_asid)); +if (static_cpu_has (X86_FEATURE_PCID ) && + !static_cpu_has (X86_FEATURE_INVPCID_SINGLE )) { + need_flush =this_cpu_read ( + cpu_tlbstate.kern_pcid_needs_flush); +this_cpu_write (cpu_tlbstate.kern_pcid_needs_flush, + false); + } + +/* +* It is possible that we may get a TLB flush IPI after +* already reading need_flush. However, in that case the IPI +* will not set flush_pending for the unrestricted address +* space, as that is done by flush_tlb_one_user() only if +* asi_intr_nest_depth() is 0. +*/ + + unrestricted_cr3 =build_cr3_pcid ( +this_cpu_read (cpu_tlbstate.loaded_mm)->pgd, +kern_pcid (this_cpu_read (cpu_tlbstate.loaded_mm_asid)), + !need_flush);write_cr3 (unrestricted_cr3);this_cpu_write (asi_cpu_state.curr_asi,NULL );
arch/x86/mm/tlb.c +143 -9
225 lines not on this page · raw message
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 28/47 mm: asi: Avoid TLB flush IPIs to CPUs not in ASI context
In reply to Junaid ShahidMost CPUs will not be running in a restricted ASI address space at any given time. So when we need to do an ASI TLB flush, we can skip those CPUs and let them do a flush at the time of the next ASI Enter.
Furthermore, for flushes related to local non-sensitive memory, we can restrict the CPU set even further to those CPUs that have that specific mm_struct loaded.
--- arch/x86/include/asm/asi.h | 9 +- arch/x86/include/asm/tlbflush.h | 47 +++---- arch/x86/mm/asi.c | 73 +++++++++-- arch/x86/mm/tlb.c | 209 ++++++++++++++++++++++++++++++-- 4 files changed, 282 insertions(+), 56 deletions(-)
arch/x86/include/asm/asi.h +8 -1
@@ -41,6 +41,8 @@ struct asi {struct asi_class *class;struct mm_struct *mm;u16 pcid_index; +atomic64_t *tlb_gen; +atomic64_t __tlb_gen;int64_t asi_ref_count; };
@@ -138,11 +140,16 @@ static inline int asi_intr_nest_depth(void)return current->thread.intr_nest_depth; } +void asi_get_latest_tlb_gens (struct asi *asi,u64 *latest_local_tlb_gen, +u64 *latest_global_tlb_gen); +#define INIT_MM_ASI (init_mm) \ .asi = { \ [0] = { \ .pgd = asi_global_nonsensitive_pgd, \ - .mm = &init_mm \ + .mm = &init_mm, \ + .__tlb_gen = ATOMIC64_INIT(1), \ + .tlb_gen = &init_mm.asi[0].__tlb_gen \ } \ },
arch/x86/include/asm/tlbflush.h +16 -31
65 lines not on this page · raw message
arch/x86/mm/asi.c +62 -11
120 lines not on this page · raw message
arch/x86/mm/tlb.c +196 -13
270 lines not on this page · raw message
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 29/47 mm: asi: Reduce TLB flushes when freeing pages asynchronously
In reply to Junaid ShahidWhen we are freeing pages asynchronously (because the original free was issued with IRQs disabled), issue only one TLB flush per execution of the async work function. If there is only one page to free, we do a targeted flush for that page only. Otherwise, we just do a full flush.
--- arch/x86/include/asm/tlbflush.h | 8 +++++ arch/x86/mm/tlb.c | 52 ++++++++++++++++++++------------- include/linux/mm_types.h | 30 +++++++++++++------ mm/page_alloc.c | 40 ++++++++++++++++++++----- 4 files changed, 93 insertions(+), 37 deletions(-)
arch/x86/include/asm/tlbflush.h +8 -0
@@ -296,6 +296,14 @@ unsigned long build_cr3_pcid(pgd_t *pgd, u16 pcid, bool noflush);u16 kern_pcid (u16 asid);u16 asi_pcid (struct asi *asi,u16 asid); +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION + +void __asi_prepare_tlb_flush (struct asi *asi,u64 *new_tlb_gen); +void __asi_flush_tlb_range (u64 mm_context_id,u16 pcid_index,u64 new_tlb_gen, +size_t start,size_t end,const cpumask_t *cpu_mask); + +#endif /* CONFIG_ADDRESS_SPACE_ISOLATION */ +#endif /* !MODULE */ #endif /* _ASM_X86_TLBFLUSH_H */
arch/x86/mm/tlb.c +31 -21
74 lines not on this page · raw message
include/linux/mm_types.h +21 -9
@@ -193,21 +193,33 @@ struct page {/** @rcu_head: You can use this to free a page by RCU. */ struct rcu_head rcu_head; -#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +#if defined(CONFIG_ADDRESS_SPACE_ISOLATION ) && !defined(BUILD_VDSO32 )struct {/* Links the pages_to_free_async list */ struct llist_node async_free_node;unsigned long _asi_pad_1; -unsigned long _asi_pad_2; +u64 asi_tlb_gen; -/* -* Upon allocation of a locally non-sensitive page, set -* to the allocating mm. Must be set to the same mm when -* the page is freed. May potentially be overwritten in -* the meantime, as long as it is restored before free. -*/ -struct mm_struct *asi_mm; +union { +/* +* Upon allocation of a locally non-sensitive +* page, set to the allocating mm. Must be set +* to the same mm when the page is freed. May +* potentially be overwritten in the meantime, +* as long as it is restored before free. +*/ +struct mm_struct *asi_mm; + +/* +* Set to the above mm's context ID if the page +* is being freed asynchronously. Can't directly +* use the mm_struct, unless we take additional +* steps to avoid it from being freed while the +* async work is pending. +*/ +u64 asi_mm_ctx_id; + }; };#endif };
mm/page_alloc.c +33 -7
@@ -5182,20 +5182,41 @@ static void async_free_work_fn(struct work_struct *work) {struct page *page, *tmp;struct llist_node *pages_to_free; -void *va; -size_t len; +size_t addr;uint order; pages_to_free =llist_del_all (this_cpu_ptr (&pages_to_free_async)); -/* A later patch will do a more optimized TLB flush. */ +if (!pages_to_free) +return ; + +/* If we only have one page to free, then do a targeted TLB flush. */ +if (!llist_next (pages_to_free)) { + page = llist_entry(pages_to_free, struct page, async_free_node); + addr = (size_t )page_to_virt (page); + order = page->private; + +__asi_flush_tlb_range (page->asi_mm_ctx_id,0 , page->asi_tlb_gen, + addr, addr +PAGE_SIZE * (1 << order), + cpu_online_mask); +/* Need to clear, since it shares space with page->mapping. */ + page->asi_tlb_gen =0 ; + +__free_the_page (page, order); +return ; + } + +/* +* Otherwise, do a full flush. We could potentially try to optimize it +* via taking a union of what needs to be flushed, but it may not be +* worth the additional complexity. +*/ +asi_flush_tlb_range (ASI_GLOBAL_NONSENSITIVE ,0 ,TLB_FLUSH_ALL );llist_for_each_entry_safe (page, tmp, pages_to_free, async_free_node) { - va =page_to_virt (page); order = page->private; - len =PAGE_SIZE * (1 << order); - -asi_flush_tlb_range (ASI_GLOBAL_NONSENSITIVE , va, len); +/* Need to clear, since it shares space with page->mapping. */ + page->asi_tlb_gen =0 ;__free_the_page (page, order); } }
@@ -5291,6 +5312,11 @@ static bool asi_unmap_freed_pages(struct page *page, unsigned int order)if (!async_flush_needed)return true; + page->asi_mm_ctx_id =PageGlobalNonSensitive (page) + ?U64_MAX : asi->mm->context.ctx_id; + +__asi_prepare_tlb_flush (asi, &page->asi_tlb_gen); + page->private = order;llist_add (&page->async_free_node,this_cpu_ptr (&pages_to_free_async));
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 30/47 mm: asi: Add API for mapping userspace address ranges
In reply to Junaid Shahidasi_map_user()/asi_unmap_user() can be used to map userspace address ranges for ASI classes that do not specify ASI_MAP_ALL_USERSPACE. In addition, another structure, asi_pgtbl_pool, allows for pre-allocating a set of pages to avoid having to allocate memory for page tables within asi_map_user(), which makes it easier to use that function while holding locks.
--- arch/x86/include/asm/asi.h | 19 +++ arch/x86/mm/asi.c | 252 ++++++++++++++++++++++++++++++++++--- include/asm-generic/asi.h | 21 ++++ include/linux/mm_types.h | 2 +- 4 files changed, 275 insertions(+), 19 deletions(-)
arch/x86/include/asm/asi.h +19 -0
@@ -44,6 +44,12 @@ struct asi {atomic64_t *tlb_gen;atomic64_t __tlb_gen;int64_t asi_ref_count; +rwlock_t user_map_lock; +}; + +struct asi_pgtbl_pool { +struct page *pgtbl_list; +uint count; }; DECLARE_PER_CPU_ALIGNED(struct asi_state, asi_cpu_state);
@@ -74,6 +80,19 @@ void asi_do_lazy_map(struct asi *asi, size_t addr);void asi_clear_user_pgd (struct mm_struct *mm,size_t addr);void asi_clear_user_p4d (struct mm_struct *mm,size_t addr); +int asi_map_user (struct asi *asi,void *addr,size_t len, +struct asi_pgtbl_pool *pool, +size_t allowed_start,size_t allowed_end); +void asi_unmap_user (struct asi *asi,void *va,size_t len); +int asi_fill_pgtbl_pool (struct asi_pgtbl_pool *pool,uint count,gfp_t flags); +void asi_clear_pgtbl_pool (struct asi_pgtbl_pool *pool); + +static inline void asi_init_pgtbl_pool (struct asi_pgtbl_pool *pool) +{ + pool->pgtbl_list =NULL ; + pool->count =0 ; +} +static inline void asi_init_thread_state (struct thread_struct *thread) { thread->intr_nest_depth =0 ;
arch/x86/mm/asi.c +234 -18
316 lines not on this page · raw message
include/asm-generic/asi.h +21 -0
@@ -26,6 +26,7 @@struct asi_hooks {};struct asi {}; +struct asi_pgtbl_pool {}; static inline int asi_register_class(const char *name, uint flags,
@@ -92,6 +93,26 @@ void asi_clear_user_pgd(struct mm_struct *mm, size_t addr) { }static inline void asi_clear_user_p4d (struct mm_struct *mm,size_t addr) { } +static inline +int asi_map_user (struct asi *asi,void *addr,size_t len, +struct asi_pgtbl_pool *pool, +size_t allowed_start,size_t allowed_end) +{ +return 0 ; +} + +static inline void asi_unmap_user (struct asi *asi,void *va,size_t len) { } + +static inline +int asi_fill_pgtbl_pool (struct asi_pgtbl_pool *pool,uint count,gfp_t flags) +{ +return 0 ; +} + +static inline void asi_clear_pgtbl_pool (struct asi_pgtbl_pool *pool) { } + +static inline void asi_init_pgtbl_pool (struct asi_pgtbl_pool *pool) { } +static inline void asi_flush_tlb_range (struct asi *asi,void *addr,size_t len) { }
include/linux/mm_types.h +1 -1
@@ -198,7 +198,7 @@ struct page {/* Links the pages_to_free_async list */ struct llist_node async_free_node; -unsigned long _asi_pad_1; +struct page *asi_pgtbl_pool_next;u64 asi_tlb_gen;union {
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 31/47 mm: asi: Support for non-sensitive SLUB caches
In reply to Junaid ShahidThis adds support for allocating global and local non-sensitive objects using the SLUB allocator. Similar to SLAB, per-process child caches are created for locally non-sensitive allocations. This mechanism is based on a modified form of the earlier implementation of per-memcg caches.
--- include/linux/slub_def.h | 6 ++ mm/slab.h | 5 ++ mm/slab_common.c | 33 +++++++-- mm/slub.c | 140 ++++++++++++++++++++++++++++++++++++++- security/Kconfig | 3 +- 5 files changed, 179 insertions(+), 8 deletions(-)
include/linux/slub_def.h +6 -0
@@ -137,6 +137,12 @@ struct kmem_cache {struct kasan_cache kasan_info;#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +struct kmem_local_cache_info local_cache_info; +/* For propagation, maximum size of a stored attr */ +unsigned int max_attr_size; +#endif +unsigned int useroffset;/* Usercopy region offset */ unsigned int usersize;/* Usercopy region size */
mm/slab.h +5 -0
@@ -216,6 +216,7 @@ int __kmem_cache_shutdown(struct kmem_cache *);void __kmem_cache_release (struct kmem_cache *);int __kmem_cache_shrink (struct kmem_cache *);void slab_kmem_cache_release (struct kmem_cache *); +void kmem_cache_shrink_all (struct kmem_cache *s);struct seq_file ;struct file ;
@@ -344,6 +345,7 @@ void restore_page_nonsensitive_metadata(struct page *page, }void set_nonsensitive_cache_params (struct kmem_cache *s); +void init_local_cache_info (struct kmem_cache *s,struct kmem_cache *root);#else /* CONFIG_ADDRESS_SPACE_ISOLATION */
@@ -380,6 +382,9 @@ static inline void restore_page_nonsensitive_metadata(struct page *page,static inline void set_nonsensitive_cache_params (struct kmem_cache *s) { } +static inline +void init_local_cache_info (struct kmem_cache *s,struct kmem_cache *root) { } +#endif /* CONFIG_ADDRESS_SPACE_ISOLATION */ #ifdef CONFIG_MEMCG_KMEM
mm/slab_common.c +29 -4
@@ -142,7 +142,7 @@ int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t nr,LIST_HEAD (slab_root_caches); -static void init_local_cache_info (struct kmem_cache *s,struct kmem_cache *root) +void init_local_cache_info (struct kmem_cache *s,struct kmem_cache *root) {if (root) { s->local_cache_info.root_cache = root;
@@ -194,9 +194,6 @@ void set_nonsensitive_cache_params(struct kmem_cache *s)#else -static inline -void init_local_cache_info (struct kmem_cache *s,struct kmem_cache *root) { } -static inline void cleanup_local_cache_info (struct kmem_cache *s) { }#endif /* CONFIG_ADDRESS_SPACE_ISOLATION */
@@ -644,6 +641,34 @@ int kmem_cache_shrink(struct kmem_cache *cachep) }EXPORT_SYMBOL (kmem_cache_shrink); +/** +* kmem_cache_shrink_all - shrink a cache and all child caches for root cache +* @s: The cache pointer +*/ +void kmem_cache_shrink_all (struct kmem_cache *s) +{ +struct kmem_cache *c; + +if (!static_asi_enabled () || !is_root_cache (s)) { +kmem_cache_shrink (s); +return ; + } + +kasan_cache_shrink (s); +__kmem_cache_shrink (s); + +/* +* We have to take the slab_mutex to protect from the child cache list +* modification. +*/ +mutex_lock (&slab_mutex); +for_each_child_cache (c, s) { +kasan_cache_shrink (c); +__kmem_cache_shrink (c); + } +mutex_unlock (&slab_mutex); +} +bool slab_is_available (void ) {return slab_state >=UP ;
mm/slub.c +137 -3
212 lines not on this page · raw message
security/Kconfig +2 -1
@@ -68,7 +68,8 @@ config PAGE_TABLE_ISOLATION config ADDRESS_SPACE_ISOLATION bool "Allow code to run with a reduced kernel address space" default n - depends on X86_64 && !UML && SLAB && !NEED_PER_CPU_KM + depends on X86_64 && !UML && !NEED_PER_CPU_KM + depends on SLAB || SLUB depends on !PARAVIRT depends on !MEMORY_HOTPLUG help
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 32/47 x86: asi: Allocate FPU state separately when ASI is enabled.
In reply to Junaid ShahidWe are going to be mapping the task_struct in the restricted ASI address space. However, the task_struct also contains the FPU register state embedded inside it, which can contain sensitive information. So when ASI is enabled, always allocate the FPU state from a separate slab cache to keep it out of task_struct.
--- arch/x86/include/asm/fpu/api.h | 1 + arch/x86/kernel/fpu/core.c | 45 ++++++++++++++++++++++++++++++++-- arch/x86/kernel/fpu/init.c | 7 ++++-- arch/x86/kernel/fpu/internal.h | 1 + arch/x86/kernel/fpu/xstate.c | 21 +++++++++++++--- arch/x86/kernel/process.c | 7 +++++- 6 files changed, 74 insertions(+), 8 deletions(-)
arch/x86/include/asm/fpu/api.h +1 -0
@@ -112,6 +112,7 @@ extern void fpu__init_cpu(void);extern void fpu__init_system (struct cpuinfo_x86 *c);extern void fpu__init_check_bugs (void );extern void fpu__resume_cpu (void ); +extern void fpstate_cache_init (void );#ifdef CONFIG_MATH_EMULATION extern void fpstate_init_soft (struct swregs_state *soft);
arch/x86/kernel/fpu/core.c +43 -2
75 lines not on this page · raw message
arch/x86/kernel/fpu/init.c +5 -2
@@ -161,9 +161,11 @@ static void __init fpu__init_task_struct_size(void)/* * Add back the dynamically-calculated register state -* size. +* size, except when ASI is enabled, since in that case +* the FPU state is always allocated dynamically. */ - task_size += fpu_kernel_cfg.default_size; +if (!cpu_feature_enabled (X86_FEATURE_ASI )) + task_size += fpu_kernel_cfg.default_size;/* * We dynamically size 'struct fpu', so we require that
@@ -223,6 +225,7 @@ static void __init fpu__init_init_fpstate(void)*/ void __init fpu__init_system (struct cpuinfo_x86 *c) { + current->thread.fpu.fpstate = ¤t->thread.fpu.__fpstate;fpstate_reset (¤t->thread.fpu);fpu__init_system_early_generic (c);
arch/x86/kernel/fpu/internal.h +1 -0
@@ -3,6 +3,7 @@#define __X86_KERNEL_FPU_INTERNAL_Hextern struct fpstate init_fpstate; +extern struct kmem_cache *fpstate_cachep;/* CPU feature check wrappers */ static __always_inline __pure bool use_xsave(void)
arch/x86/kernel/fpu/xstate.c +18 -3
@@ -13,6 +13,7 @@#include <linux/seq_file.h> #include <linux/proc_fs.h> #include <linux/vmalloc.h> +#include <linux/slab.h> #include <asm/fpu/api.h> #include <asm/fpu/regset.h>
@@ -1495,8 +1496,15 @@ arch_initcall(xfd_update_static_branch)void fpstate_free (struct fpu *fpu) { -if (fpu->fpstate && fpu->fpstate != &fpu->__fpstate) -vfree (fpu->fpstate); +WARN_ON_ONCE (cpu_feature_enabled (X86_FEATURE_ASI ) && + fpu->fpstate == &fpu->__fpstate); + +if (fpu->fpstate && fpu->fpstate != &fpu->__fpstate) { +if (fpu->fpstate->is_valloc) +vfree (fpu->fpstate); +else +kmem_cache_free (fpstate_cachep, fpu->fpstate); + } }/**
@@ -1574,7 +1582,14 @@ static int fpstate_realloc(u64 xfeatures, unsigned int ksize,fpregs_unlock (); -vfree (curfps); +WARN_ON_ONCE (cpu_feature_enabled (X86_FEATURE_ASI ) && !curfps); +if (curfps) { +if (curfps->is_valloc) +vfree (curfps); +else +kmem_cache_free (fpstate_cachep, curfps); + } +return 0 ; }
arch/x86/kernel/process.c +6 -1
@@ -80,6 +80,11 @@ EXPORT_PER_CPU_SYMBOL(cpu_tss_rw);DEFINE_PER_CPU (bool, __tss_limit_invalid);EXPORT_PER_CPU_SYMBOL_GPL (__tss_limit_invalid); +void __init arch_task_cache_init (void ) +{ +fpstate_cache_init (); +} +/* * this gets called so that we can store lazy state into memory and copy the * current task into the new thread.
@@ -101,7 +106,7 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)#ifdef CONFIG_X86_64 void arch_release_task_struct (struct task_struct *tsk) { -if (fpu_state_size_dynamic ()) +if (fpu_state_size_dynamic () ||cpu_feature_enabled (X86_FEATURE_ASI ))fpstate_free (&tsk->thread.fpu); }#endif
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 33/47 kvm: asi: Map guest memory into restricted ASI address space
In reply to Junaid ShahidA module parameter treat_all_userspace_as_nonsensitive is added, which if set, maps the entire userspace of the process running the VM into the ASI restricted address space.
If the flag is not set (the default), then just the userspace memory mapped into the VM's address space is mapped into the ASI restricted address space.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 6 ++++ arch/x86/kvm/mmu/mmu.c | 54 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/paging_tmpl.h | 14 +++++++++ arch/x86/kvm/x86.c | 19 +++++++++++- include/linux/kvm_host.h | 3 ++ virt/kvm/kvm_main.c | 7 +++++ 7 files changed, 104 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -681,6 +681,8 @@ struct kvm_vcpu_arch {struct kvm_mmu_memory_cache mmu_gfn_array_cache;struct kvm_mmu_memory_cache mmu_page_header_cache; +struct asi_pgtbl_pool asi_pgtbl_pool; +/* * QEMU userspace and the guest each have their own FPU state. * In vcpu_run, we switch between the user and guest FPU contexts.
arch/x86/kvm/mmu.h +6 -0
@@ -49,6 +49,12 @@#define KVM_MMU_CR0_ROLE_BITS (X86_CR0_PG | X86_CR0_WP) +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +extern bool treat_all_userspace_as_nonsensitive; +#else +#define treat_all_userspace_as_nonsensitive true +#endif + static __always_inline u64 rsvd_bits(int s, int e) {BUILD_BUG_ON (__builtin_constant_p (e) &&__builtin_constant_p (s) && e < s);
arch/x86/kvm/mmu/mmu.c +54 -0
114 lines not on this page · raw message
arch/x86/kvm/mmu/paging_tmpl.h +14 -0
@@ -584,6 +584,9 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,if (is_error_pfn (pfn))return false; +if (!treat_all_userspace_as_nonsensitive) +asi_map_gfn_range (vcpu, slot, gfn,1 ); +mmu_set_spte (vcpu, slot, spte, pte_access, gfn, pfn,NULL );kvm_release_pfn_clean (pfn);return true;
@@ -836,6 +839,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultint r;unsigned long mmu_seq;bool is_self_change_mapping; +bool try_asi_map;pgprintk ("%s: addr %lx err %x\n" , __func__, fault->addr, fault->error_code);WARN_ON_ONCE (fault->is_tdp);
@@ -890,6 +894,12 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultif (handle_abnormal_pfn (vcpu, fault, walker.pte_access, &r))return r; + try_asi_map = !treat_all_userspace_as_nonsensitive && + !is_noslot_pfn (fault->pfn); + +if (try_asi_map) +vcpu_fill_asi_pgtbl_pool (vcpu); +/* * Do not change pte_access if the pfn is a mmio page, otherwise * we will cache the incorrect access into mmio spte.
@@ -919,6 +929,10 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault r =make_mmu_pages_available (vcpu);if (r) goto out_unlock; + +if (try_asi_map) +asi_map_gfn_range (vcpu, fault->slot, walker.gfn,1 ); + r =FNAME (fetch)(vcpu, fault, &walker);kvm_mmu_audit (vcpu,AUDIT_POST_PAGE_FAULT );
arch/x86/kvm/x86.c +18 -1
@@ -8722,7 +8722,10 @@ int kvm_arch_init(void *opaque) goto out_free_percpu;if (ops->runtime_ops->flush_sensitive_cpu_state) { - r =asi_register_class ("KVM" ,ASI_MAP_STANDARD_NONSENSITIVE , + r =asi_register_class ("KVM" , +ASI_MAP_STANDARD_NONSENSITIVE | + (treat_all_userspace_as_nonsensitive ? +ASI_MAP_ALL_USERSPACE :0 ), &kvm_asi_hooks);if (r <0 ) goto out_mmu_exit;
@@ -9675,6 +9678,17 @@ void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, apic_address =gfn_to_hva (kvm,APIC_DEFAULT_PHYS_BASE >>PAGE_SHIFT );if (start <= apic_address && apic_address < end)kvm_make_all_cpus_request (kvm,KVM_REQ_APIC_PAGE_RELOAD ); + +if (!treat_all_userspace_as_nonsensitive) +asi_unmap_user (kvm->asi, (void *)start, end - start); +} + +void kvm_arch_mmu_notifier_invalidate_range_start (struct kvm *kvm, +unsigned long start, +unsigned long end) +{ +if (!treat_all_userspace_as_nonsensitive) +asi_unmap_user (kvm->asi, (void *)start, end - start); }void kvm_vcpu_reload_apic_access_page (struct kvm_vcpu *vcpu)
@@ -11874,6 +11888,9 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,void kvm_arch_flush_shadow_all (struct kvm *kvm) { +if (!treat_all_userspace_as_nonsensitive) +asi_unmap_user (kvm->asi,0 ,TASK_SIZE_MAX ); +kvm_mmu_zap_all (kvm); }
include/linux/kvm_host.h +3 -0
@@ -1819,6 +1819,9 @@ static inline long kvm_arch_vcpu_async_ioctl(struct file *filp,void kvm_arch_mmu_notifier_invalidate_range (struct kvm *kvm,unsigned long start,unsigned long end); +void kvm_arch_mmu_notifier_invalidate_range_start (struct kvm *kvm, +unsigned long start, +unsigned long end);#ifdef CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE int kvm_arch_vcpu_run_pid_change (struct kvm_vcpu *vcpu);
virt/kvm/kvm_main.c +7 -0
@@ -162,6 +162,12 @@ __weak void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, { } +__weak void kvm_arch_mmu_notifier_invalidate_range_start (struct kvm *kvm, +unsigned long start, +unsigned long end) +{ +} +bool kvm_is_zone_device_pfn (kvm_pfn_t pfn) {/*
@@ -685,6 +691,7 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,spin_unlock (&kvm->mn_invalidate_lock);__kvm_handle_hva_range (kvm, &hva_range); +kvm_arch_mmu_notifier_invalidate_range_start (kvm, range->start, range->end);return 0 ; }
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 34/47 kvm: asi: Unmap guest memory from ASI address space when using nested virt
In reply to Junaid ShahidL1 guest memory as a whole cannot be considered non-sensitive when an L2 is running. Even if L1 is using its own mitigations, L2 VM Exits could, in theory, bring into the cache some sensitive L1 memory without L1 getting a chance to flush it.
For simplicity, we just unmap the entire L1 memory from the ASI restricted address space when nested virtualization is turned on. Though this is overridden if the treat_all_userspace_as_nonsensitive flag is enabled.
In the future, we could potentially map some portions of L1 memory which are known to contain non-sensitive memory, which would reduce ASI overhead during nested virtualization.
Note that unmapping the guest memory still leaves a slight hole because L2 could also potentially access copies of L1 VCPU registers stored in L0 kernel structures. In the future, this could be mitigated by having a separate ASI address space for each VCPU and treating the associated structures as locally non-sensitive only within that VCPU's ASI address space.
--- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/mmu/mmu.c | 10 ++++++++++ arch/x86/kvm/vmx/nested.c | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+)
arch/x86/include/asm/kvm_host.h +6 -0
@@ -1200,6 +1200,12 @@ struct kvm_arch {*/ struct list_head tdp_mmu_pages; +/* +* Number of VCPUs that have enabled nested virtualization. +* Currently only maintained when ASI is enabled. +*/ +int nested_virt_enabled_count; +/* * Protects accesses to the following fields when the MMU lock * is held in read mode:
arch/x86/kvm/mmu/mmu.c +10 -0
@@ -94,6 +94,7 @@ module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644);#ifdef CONFIG_ADDRESS_SPACE_ISOLATION bool __ro_after_init treat_all_userspace_as_nonsensitive;module_param (treat_all_userspace_as_nonsensitive, bool,0444 ); +EXPORT_SYMBOL_GPL (treat_all_userspace_as_nonsensitive);#endif /*
@@ -2769,6 +2770,15 @@ static void asi_map_gfn_range(struct kvm_vcpu *vcpu,int err;size_t hva =__gfn_to_hva_memslot (slot, gfn); +/* +* For now, we just don't map any guest memory when using nested +* virtualization. In the future, we could potentially map some +* portions of guest memory which are known to contain only memory +* which would be considered non-sensitive. +*/ +if (vcpu->kvm->arch.nested_virt_enabled_count) +return ; + err =asi_map_user (vcpu->kvm->asi, (void *)hva,PAGE_SIZE * npages, &vcpu->arch.asi_pgtbl_pool, slot->userspace_addr, slot->userspace_addr + slot->npages *PAGE_SIZE );
arch/x86/kvm/vmx/nested.c +22 -0
@@ -318,6 +318,14 @@ static void free_nested(struct kvm_vcpu *vcpu)nested_release_evmcs (vcpu);free_loaded_vmcs (&vmx->nested.vmcs02); + +if (cpu_feature_enabled (X86_FEATURE_ASI ) && + !treat_all_userspace_as_nonsensitive) { +write_lock (&vcpu->kvm->mmu_lock); +WARN_ON (vcpu->kvm->arch.nested_virt_enabled_count <=0 ); + vcpu->kvm->arch.nested_virt_enabled_count--; +write_unlock (&vcpu->kvm->mmu_lock); + } }/*
@@ -4876,6 +4884,20 @@ static int enter_vmx_operation(struct kvm_vcpu *vcpu)pt_update_intercept_for_msr (vcpu); } +if (cpu_feature_enabled (X86_FEATURE_ASI ) && + !treat_all_userspace_as_nonsensitive) { +/* +* We do the increment under the MMU lock in order to prevent +* it from happening concurrently with asi_map_gfn_range(). +*/ +write_lock (&vcpu->kvm->mmu_lock); +WARN_ON (vcpu->kvm->arch.nested_virt_enabled_count <0 ); + vcpu->kvm->arch.nested_virt_enabled_count++; +write_unlock (&vcpu->kvm->mmu_lock); + +asi_unmap_user (vcpu->kvm->asi,0 ,TASK_SIZE_MAX ); + } +return 0 ; out_shadow_vmcs:
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 35/47 mm: asi: asi_exit() on PF, skip handling if address is accessible
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
On a page-fault - do asi_exit(). Then check if now after the exit the address is accessible. We do this by refactoring spurious_kernel_fault() into two parts:
1. Verify that the error code value is something that could arise from a lazy TLB update.
2. Walk the page table and verify permissions, which is now called is_address_accessible_now(). We also define PTE_PRESENT() and PMD_PRESENT() which are suitable for checking userspace pages. For the sake of spurious faualts, pte_present() and pmd_present() are only good for kernelspace pages. This is because these macros might return true even if the present bit is 0 (only relevant for userspace).
--- arch/x86/mm/fault.c | 60 ++++++++++++++++++++++++++++++++++------ include/linux/mm_types.h | 3 ++ 2 files changed, 55 insertions(+), 8 deletions(-)
arch/x86/mm/fault.c +52 -8
@@ -982,6 +982,8 @@ static int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte)return 1 ; } +static int is_address_accessible_now (unsigned long error_code,unsigned long address, +pgd_t *pgd);/* * Handle a spurious fault caused by a stale TLB entry. *
@@ -1003,15 +1005,13 @@ static int spurious_kernel_fault_check(unsigned long error_code, pte_t *pte)* See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3 * (Optional Invalidation). */ +/* A spurious fault is also possible when Address Space Isolation (ASI) is in +* use. Specifically, code running withing an ASI domain touched memory outside +* the domain. This access causes a page-fault --> asi_exit() */ static noinline intspurious_kernel_fault (unsigned long error_code,unsigned long address) {pgd_t *pgd; -p4d_t *p4d; -pud_t *pud; -pmd_t *pmd; -pte_t *pte; -int ret;/* * Only writes to RO or instruction fetches from NX may cause
@@ -1027,6 +1027,37 @@ spurious_kernel_fault(unsigned long error_code, unsigned long address)return 0 ; pgd = init_mm.pgd +pgd_index (address); +return is_address_accessible_now (error_code, address, pgd); +} +NOKPROBE_SYMBOL (spurious_kernel_fault); + + +/* Check if an address (kernel or userspace) would cause a page fault if +* accessed now. +* +* For kernel addresses, pte_present and pmd_present are sufficioent. For +* userspace, we must use PTE_PRESENT and PMD_PRESENT, which will only check the +* present bits. +* The existing pmd_present() in arch/x86/include/asm/pgtable.h is misleading. +* The PMD page might be in the middle of split_huge_page with present bit +* clear, but pmd_present will still return true. We are inteerested in knowing +* if the page is accessible to hardware - that is - the present bit is 1. */ +#define PMD_PRESENT (pmd) (pmd_flags(pmd) & _PAGE_PRESENT) + +/* pte_present will return true is _PAGE_PROTNONE is 1. We care if the hardware +* can actually access the page right now. */ +#define PTE_PRESENT (pte) (pte_flags(pte) & _PAGE_PRESENT) + +static noinline int +is_address_accessible_now (unsigned long error_code,unsigned long address, +pgd_t *pgd) +{ +p4d_t *p4d; +pud_t *pud; +pmd_t *pmd; +pte_t *pte; +int ret; +if (!pgd_present (*pgd))return 0 ;
@@ -1045,14 +1076,14 @@ spurious_kernel_fault(unsigned long error_code, unsigned long address)return spurious_kernel_fault_check (error_code, (pte_t *) pud); pmd =pmd_offset (pud, address); -if (!pmd_present (*pmd)) +if (!PMD_PRESENT (*pmd))return 0 ;if (pmd_large (*pmd))return spurious_kernel_fault_check (error_code, (pte_t *) pmd); pte =pte_offset_kernel (pmd, address); -if (!pte_present (*pte)) +if (!PTE_PRESENT (*pte))return 0 ; ret =spurious_kernel_fault_check (error_code, pte);
@@ -1068,7 +1099,6 @@ spurious_kernel_fault(unsigned long error_code, unsigned long address)return ret; } -NOKPROBE_SYMBOL (spurious_kernel_fault);int show_unhandled_signals =1 ;
@@ -1504,6 +1534,20 @@ DEFINE_IDTENTRY_RAW_ERRORCODE(exc_page_fault)* the fixup on the next page fault. */ struct asi *asi =asi_get_current (); +if (asi) +asi_exit (); + +/* handle_page_fault() might call BUG() if we run it for a kernel +* address. This might be the case if we got here due to an ASI fault. +* We avoid this case by checking whether the address is now, after a +* potential asi_exit(), accessible by hardware. If it is - there's +* nothing to do. +*/ +if (current &&mm_asi_enabled (current->mm)) { +pgd_t *pgd = (pgd_t *)__va (read_cr3_pa ()) +pgd_index (address); +if (is_address_accessible_now (error_code, address, pgd)) +return ; + }prefetchw (¤t->mm->mmap_lock);
include/linux/mm_types.h +3 -0
@@ -707,6 +707,9 @@ extern struct mm_struct init_mm;#ifdef CONFIG_ADDRESS_SPACE_ISOLATION static inline bool mm_asi_enabled (struct mm_struct *mm) { +if (!mm) +return false; +return mm->asi_enabled; }#else
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 36/47 mm: asi: Adding support for dynamic percpu ASI allocations
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
Adding infrastructure to support pcpu_alloc with gfp flag of __GFP_GLOBAL_NONSENSITIVE. We use a similar mechanism as the earlier infrastructure for memcg percpu allocations and add pcpu type PCPU_CHUNK_ASI_NONSENSITIVE. pcpu_chunk_list(PCPU_CHUNK_ASI_NONSENSITIVE) will return a list of ASI nonsensitive percpu chunks, allowing most of the code to be unchanged.
--- mm/percpu-internal.h | 23 ++++++- mm/percpu-km.c | 5 +- mm/percpu-vm.c | 6 +- mm/percpu.c | 139 ++++++++++++++++++++++++++++++++++--------- 4 files changed, 141 insertions(+), 32 deletions(-)
mm/percpu-internal.h +22 -1
@@ -5,6 +5,15 @@#include <linux/types.h> #include <linux/percpu.h> +enum pcpu_chunk_type { +PCPU_CHUNK_ROOT , +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +PCPU_CHUNK_ASI_NONSENSITIVE , +#endif +PCPU_NR_CHUNK_TYPES , +PCPU_FAIL_ALLOC =PCPU_NR_CHUNK_TYPES +}; +/* * pcpu_block_md is the metadata block struct. * Each chunk's bitmap is split into a number of full blocks.
@@ -59,6 +68,9 @@ struct pcpu_chunk {#ifdef CONFIG_MEMCG_KMEM struct obj_cgroup **obj_cgroups;/* vector of object cgroups */ #endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +bool is_asi_nonsensitive;/* ASI nonsensitive chunk */ +#endif int nr_pages;/* # of pages served by this chunk */ int nr_populated;/* # of populated pages */
@@ -68,7 +80,7 @@ struct pcpu_chunk {extern spinlock_t pcpu_lock; -extern struct list_head *pcpu_chunk_lists; +extern struct list_head *pcpu_chunk_lists[PCPU_NR_CHUNK_TYPES ];extern int pcpu_nr_slots;extern int pcpu_sidelined_slot;extern int pcpu_to_depopulate_slot;
@@ -113,6 +125,15 @@ static inline int pcpu_chunk_map_bits(struct pcpu_chunk *chunk)return pcpu_nr_pages_to_map_bits (chunk->nr_pages); } +static inline enum pcpu_chunk_type pcpu_chunk_type (struct pcpu_chunk *chunk) +{ +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +if (chunk->is_asi_nonsensitive) +return PCPU_CHUNK_ASI_NONSENSITIVE ; +#endif +return PCPU_CHUNK_ROOT ; +} +#ifdef CONFIG_PERCPU_STATS #include <linux/spinlock.h>
mm/percpu-km.c +3 -2
@@ -50,7 +50,8 @@ static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk,/* nada */ } -static struct pcpu_chunk *pcpu_create_chunk (gfp_t gfp) +static struct pcpu_chunk *pcpu_create_chunk (enum pcpu_chunk_type type, +gfp_t gfp) {const int nr_pages = pcpu_group_sizes[0 ] >>PAGE_SHIFT ;struct pcpu_chunk *chunk;
@@ -58,7 +59,7 @@ static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp)unsigned long flags;int i; - chunk =pcpu_alloc_chunk (gfp); + chunk =pcpu_alloc_chunk (type, gfp);if (!chunk)return NULL ;
mm/percpu-vm.c +4 -2
@@ -357,7 +357,8 @@ static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk,pcpu_free_pages (chunk, pages, page_start, page_end); } -static struct pcpu_chunk *pcpu_create_chunk (gfp_t gfp) +static struct pcpu_chunk *pcpu_create_chunk (enum pcpu_chunk_type type, +gfp_t gfp) {struct pcpu_chunk *chunk;struct vm_struct **vms;
@@ -368,7 +369,8 @@ static struct pcpu_chunk *pcpu_create_chunk(gfp_t gfp) gfp &= ~__GFP_GLOBAL_NONSENSITIVE; - chunk =pcpu_alloc_chunk (gfp); + chunk =pcpu_alloc_chunk (type, gfp); +if (!chunk)return NULL ;
mm/percpu.c +112 -27
310 lines not on this page · raw message
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 37/47 mm: asi: ASI annotation support for static variables.
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
Added the following annotations:
__asi_not_sensitive: for static variables which are considered not sensitive.
__asi_not_sensitive_readmostly: similar to __read_mostly, for non-sensitive static variables.
--- arch/x86/include/asm/asi.h | 12 ++++++++++++ include/asm-generic/asi.h | 6 ++++++ include/asm-generic/vmlinux.lds.h | 18 +++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-)
arch/x86/include/asm/asi.h +12 -0
@@ -177,6 +177,18 @@ static inline pgd_t *asi_pgd(struct asi *asi) return asi->pgd; } +/* IMPORTANT: Any modification to the name here should also be applied to +* include/asm-generic/vmlinux.lds.h */ +#define ASI_NON_SENSITIVE_SECTION_NAME ".data..asi_non_sensitive" +#define ASI_NON_SENSITIVE_READ_MOSTLY_SECTION_NAME \ + ".data..asi_non_sensitive_readmostly" + +#define __asi_not_sensitive \ + __section(ASI_NON_SENSITIVE_SECTION_NAME) + +#define __asi_not_sensitive_readmostly \ + __section(ASI_NON_SENSITIVE_READ_MOSTLY_SECTION_NAME) +#else /* CONFIG_ADDRESS_SPACE_ISOLATION */ static inline void asi_intr_enter (void ) { }
include/asm-generic/asi.h +6 -0
@@ -121,6 +121,12 @@ void asi_flush_tlb_range(struct asi *asi, void *addr, size_t len) { }#define static_asi_enabled () false +/* IMPORTANT: Any modification to the name here should also be applied to +* include/asm-generic/vmlinux.lds.h */ + +#define __asi_not_sensitive +#define __asi_not_sensitive_readmostly +#endif /* !_ASSEMBLY_ */ #endif /* !CONFIG_ADDRESS_SPACE_ISOLATION */
include/asm-generic/vmlinux.lds.h +17 -1
@@ -374,10 +374,26 @@ . = ALIGN(PAGE_SIZE); \ __nosave_end = .; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +#define ASI_NOT_SENSITIVE_DATA (page_align) \ + . = ALIGN(page_align); \ + __start_asi_nonsensitive = .; \ + *(.data..asi_non_sensitive) \ + . = ALIGN(page_align); \ + __end_asi_nonsensitive = .; \ + __start_asi_nonsensitive_readmostly = .; \ + *(.data..asi_non_sensitive_readmostly) \ + . = ALIGN(page_align); \ + __end_asi_nonsensitive_readmostly = .; +#else +#define ASI_NOT_SENSITIVE_DATA +#endif +#define PAGE_ALIGNED_DATA (page_align) \ . = ALIGN(page_align); \ *(.data..page_aligned) \ - . = ALIGN(page_align); + . = ALIGN(page_align); \ + ASI_NOT_SENSITIVE_DATA(page_align)#define READ_MOSTLY_DATA (align) \ . = ALIGN(align); \
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 38/47 mm: asi: ASI annotation support for dynamic modules.
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
Adding support for use of ASI static variable annotations in dynamic modules:
- __asi_not_sensitive and
- __asi_not_sensitive_readmostly
Per module, we now have the following offsets:
1. asi_section_offset/size - which should be mapped into asi global pool
2. asi_readmostly_section/size - same as above, for read mostly data;
3. once_section_offset/size - is considered asi non-sensitive
--- arch/x86/include/asm/asi.h | 3 ++ arch/x86/mm/asi.c | 66 ++++++++++++++++++++++++++++++++++++++ include/asm-generic/asi.h | 3 ++ include/linux/module.h | 9 ++++++ kernel/module.c | 58 +++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+)
arch/x86/include/asm/asi.h +3 -0
@@ -98,6 +98,9 @@ static inline void asi_init_thread_state(struct thread_struct *thread) thread->intr_nest_depth =0 ; } +int asi_load_module (struct module * module); +void asi_unload_module (struct module * module); +static inline void asi_set_target_unrestricted (void ) {if (static_cpu_has (X86_FEATURE_ASI )) {
arch/x86/mm/asi.c +66 -0
78 lines not on this page · raw message
include/asm-generic/asi.h +3 -0
@@ -120,6 +120,7 @@ void asi_flush_tlb_range(struct asi *asi, void *addr, size_t len) { }#define static_asi_enabled () false +static inline int asi_load_module (struct module * module) {return 0 ;}/* IMPORTANT: Any modification to the name here should also be applied to * include/asm-generic/vmlinux.lds.h */
@@ -127,6 +128,8 @@ void asi_flush_tlb_range(struct asi *asi, void *addr, size_t len) { }#define __asi_not_sensitive#define __asi_not_sensitive_readmostly +static inline void asi_unload_module (struct module * module) { } +#endif /* !_ASSEMBLY_ */ #endif /* !CONFIG_ADDRESS_SPACE_ISOLATION */
include/linux/module.h +9 -0
@@ -336,6 +336,15 @@ struct module_layout {#ifdef CONFIG_MODULES_TREE_LOOKUP struct mod_tree_node mtn;#endif + +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +unsigned int asi_section_offset; +unsigned int asi_section_size; +unsigned int asi_readmostly_section_offset; +unsigned int asi_readmostly_section_size; +unsigned int once_section_offset; +unsigned int once_section_size; +#endif };#ifdef CONFIG_MODULES_TREE_LOOKUP
kernel/module.c +58 -0
94 lines not on this page · raw message
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 39/47 mm: asi: Skip conventional L1TF/MDS mitigations
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
If ASI is enabled for an mm, then the L1D flushes and MDS mitigations will be taken care of ASI. We check if asi is enabled by checking current->mm->asi_enabled. To use ASI, a cgroup flag must be set before the VM process is forked - causing a flag mm->asi_enabled to be set.
--- arch/x86/kvm/vmx/vmx.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +5 -1
@@ -6609,7 +6609,11 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,kvm_guest_enter_irqoff (); -vmx_flush_sensitive_cpu_state (vcpu); +/* If Address Space Isolation is enabled, it will take care of L1D +* flushes, and will also mitigate MDS. In other words, if no ASI - +* flush sensitive cpu state. */ +if (!static_asi_enabled () || !mm_asi_enabled (current->mm)) +vmx_flush_sensitive_cpu_state (vcpu);asi_enter (vcpu->kvm->asi);
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 40/47 mm: asi: support for static percpu DEFINE_PER_CPU*_ASI
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
Implemented the following PERCPU static declarations:
- DECLARE/DEFINE_PER_CPU_ASI_NOT_SENSITIVE
- DECLARE/DEFINE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE
- DECLARE/DEFINE_PER_CPU_ALIGNED_ASI_NOT_SENSITIVE
- DECLARE/DEFINE_PER_CPU_PAGE_ALIGNED_ASI_NOT_SENSITIVE
These definitions are also supported in dynamic modules. To support percpu variables in dynamic modules, we're creating an ASI pcpu reserved chunk. The reserved size PERCPU_MODULE_RESERVE is now split between the normal reserved chunk and the ASI one.
--- arch/x86/mm/asi.c | 39 +++++++- include/asm-generic/percpu.h | 6 ++ include/asm-generic/vmlinux.lds.h | 5 + include/linux/module.h | 6 ++ include/linux/percpu-defs.h | 39 ++++++++ include/linux/percpu.h | 8 +- kernel/module-internal.h | 1 + kernel/module.c | 154 ++++++++++++++++++++++++++---- mm/percpu.c | 134 ++++++++++++++++++++++---- 9 files changed, 356 insertions(+), 36 deletions(-)
arch/x86/mm/asi.c +38 -1
@@ -309,6 +309,32 @@ static int __init set_asi_param(char *str) }early_param ("asi" , set_asi_param); +static int asi_map_percpu (struct asi *asi,void *percpu_addr,size_t len) +{ +int cpu, err; +void *ptr; + +for_each_possible_cpu (cpu) { + ptr =per_cpu_ptr (percpu_addr, cpu); + err =asi_map (asi, ptr, len); +if (err) +return err; + } + +return 0 ; +} + +static void asi_unmap_percpu (struct asi *asi,void *percpu_addr,size_t len) +{ +int cpu; +void *ptr; + +for_each_possible_cpu (cpu) { + ptr =per_cpu_ptr (percpu_addr, cpu); +asi_unmap (asi, ptr, len, true); + } +} +/* asi_load_module() is called from layout_and_allocate() in kernel/module.c * We map the module and its data in init_mm.asi_pgd[0]. */
@@ -347,7 +373,13 @@ int asi_load_module(struct module* module)if (err)return err; -return 0 ; + err =asi_map_percpu (ASI_GLOBAL_NONSENSITIVE , + module->percpu_asi, + module->percpu_asi_size ); +if (err) +return err; + +return 0 ; }EXPORT_SYMBOL_GPL (asi_load_module);
@@ -372,6 +404,9 @@ void asi_unload_module(struct module* module) module->core_layout.once_section_offset, module->core_layout.once_section_size, true); +asi_unmap_percpu (ASI_GLOBAL_NONSENSITIVE , module->percpu_asi, + module->percpu_asi_size); + }static int __init asi_global_init (void )
@@ -399,6 +434,8 @@ static int __init asi_global_init(void)static_branch_enable (&asi_local_map_initialized); +pcpu_map_asi_reserved_chunk (); +return 0 ; }subsys_initcall (asi_global_init)
include/asm-generic/percpu.h +6 -0
@@ -50,6 +50,12 @@ extern void setup_per_cpu_areas(void);#endif /* SMP */ +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +void __init pcpu_map_asi_reserved_chunk (void ); +#else +static inline void pcpu_map_asi_reserved_chunk (void ) {} +#endif +#ifndef PER_CPU_BASE_SECTION #ifdef CONFIG_SMP #define PER_CPU_BASE_SECTION ".data..percpu"
include/asm-generic/vmlinux.lds.h +5 -0
@@ -1080,6 +1080,11 @@ . = ALIGN(cacheline); \ *(.data..percpu) \ *(.data..percpu..shared_aligned) \ + . = ALIGN(PAGE_SIZE); \ + __per_cpu_asi_start = .; \ + *(.data..percpu..asi_non_sensitive) \ + . = ALIGN(PAGE_SIZE); \ + __per_cpu_asi_end = .; \ PERCPU_DECRYPTED_SECTION \ __per_cpu_end = .;
include/linux/module.h +6 -0
@@ -463,6 +463,12 @@ struct module {/* Per-cpu data. */ void __percpu *percpu;unsigned int percpu_size; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +/* Per-cpu data for ASI */ +void __percpu *percpu_asi; +unsigned int percpu_asi_size; +#endif /* CONFIG_ADDRESS_SPACE_ISOLATION */ +#endif void *noinstr_text_start;unsigned int noinstr_text_size;
include/linux/percpu-defs.h +39 -0
@@ -170,6 +170,45 @@#define DEFINE_PER_CPU_READ_MOSTLY (type, name) \ DEFINE_PER_CPU_SECTION(type, name, "..read_mostly") +/* +* Declaration/definition used for per-CPU variables which for the sake for +* address space isolation (ASI) are deemed not sensitive +*/ +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +#define ASI_PERCPU_SECTION "..asi_non_sensitive" +#else +#define ASI_PERCPU_SECTION "" +#endif + +#define DECLARE_PER_CPU_ASI_NOT_SENSITIVE (type, name) \ + DECLARE_PER_CPU_SECTION(type, name, ASI_PERCPU_SECTION) + +#define DECLARE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE (type, name) \ + DECLARE_PER_CPU_SECTION(type, name, ASI_PERCPU_SECTION) \ + ____cacheline_aligned_in_smp + +#define DECLARE_PER_CPU_ALIGNED_ASI_NOT_SENSITIVE (type, name) \ + DECLARE_PER_CPU_SECTION(type, name, ASI_PERCPU_SECTION) \ + ____cacheline_aligned + +#define DECLARE_PER_CPU_PAGE_ALIGNED_ASI_NOT_SENSITIVE (type, name) \ + DECLARE_PER_CPU_SECTION(type, name, ASI_PERCPU_SECTION) \ + __aligned(PAGE_SIZE) + +#define DEFINE_PER_CPU_ASI_NOT_SENSITIVE (type, name) \ + DEFINE_PER_CPU_SECTION(type, name, ASI_PERCPU_SECTION) + +#define DEFINE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE (type, name) \ + DEFINE_PER_CPU_SECTION(type, name, ASI_PERCPU_SECTION) \ + ____cacheline_aligned_in_smp + +#define DEFINE_PER_CPU_ALIGNED_ASI_NOT_SENSITIVE (type, name) \ + DEFINE_PER_CPU_SECTION(type, name, ASI_PERCPU_SECTION) \ + ____cacheline_aligned + +#define DEFINE_PER_CPU_PAGE_ALIGNED_ASI_NOT_SENSITIVE (type, name) \ + DEFINE_PER_CPU_SECTION(type, name, ASI_PERCPU_SECTION) \ + __aligned(PAGE_SIZE)/* * Declaration/definition used for per-CPU variables that should be accessed
include/linux/percpu.h +7 -1
@@ -13,7 +13,8 @@/* enough to cover all DEFINE_PER_CPUs in modules */ #ifdef CONFIG_MODULES -#define PERCPU_MODULE_RESERVE (8 << 10) +/* #define PERCPU_MODULE_RESERVE (8 << 10) */ +#define PERCPU_MODULE_RESERVE (16 << 10)#else #define PERCPU_MODULE_RESERVE 0#endif
@@ -123,6 +124,11 @@ extern int __init pcpu_page_first_chunk(size_t reserved_size,#endif extern void __percpu *__alloc_reserved_percpu (size_t size,size_t align)__alloc_size(1) ; + +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +extern void __percpu *__alloc_reserved_percpu_asi (size_t size,size_t align); +#endif +extern bool __is_kernel_percpu_address (unsigned long addr,unsigned long *can_addr);extern bool is_kernel_percpu_address (unsigned long addr);
kernel/module-internal.h +1 -0
@@ -25,6 +25,7 @@ struct load_info {#endif struct {unsigned int sym, str, mod, vers, info, pcpu; +unsigned int pcpu_asi; } index; };
kernel/module.c +138 -16
258 lines not on this page · raw message
mm/percpu.c +116 -18
182 lines not on this page · raw message
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 41/47 mm: asi: Annotation of static variables to be nonsensitive
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
The heart of ASI is to diffrentiate between sensitive and non-sensitive data access. This commit marks certain static variables as not sensitive.
Some static variables are accessed frequently and therefore would cause many ASI exits. The frequency of these accesses is monitored by tracing asi_exits and analyzing the accessed addresses. Many of these variables don't contain sensitive information and can therefore be mapped into the global ASI region. This commit applies the __asi_not_sensitive* attributes to these frequenmtly-accessed yet not sensitive variables. The end result is a very significant reduction in ASI exits on real benchmarks.
--- arch/x86/events/core.c | 4 ++-- arch/x86/events/intel/core.c | 2 +- arch/x86/events/msr.c | 2 +- arch/x86/events/perf_event.h | 2 +- arch/x86/include/asm/kvm_host.h | 4 ++-- arch/x86/kernel/alternative.c | 2 +- arch/x86/kernel/cpu/bugs.c | 2 +- arch/x86/kernel/setup.c | 4 ++-- arch/x86/kernel/smp.c | 2 +- arch/x86/kernel/tsc.c | 8 +++---- arch/x86/kvm/lapic.c | 2 +- arch/x86/kvm/mmu/spte.c | 2 +- arch/x86/kvm/mmu/spte.h | 2 +- arch/x86/kvm/mtrr.c | 2 +- arch/x86/kvm/vmx/capabilities.h | 14 ++++++------ arch/x86/kvm/vmx/vmx.c | 37 ++++++++++++++++--------------- arch/x86/kvm/x86.c | 35 +++++++++++++++-------------- arch/x86/mm/asi.c | 4 ++-- include/linux/debug_locks.h | 4 ++-- include/linux/jiffies.h | 4 ++-- include/linux/notifier.h | 2 +- include/linux/profile.h | 2 +- include/linux/rcupdate.h | 4 +++- include/linux/rcutree.h | 2 +- include/linux/sched/sysctl.h | 1 + init/main.c | 2 +- kernel/cgroup/cgroup.c | 5 +++-- kernel/cpu.c | 14 ++++++------ kernel/events/core.c | 4 ++-- kernel/freezer.c | 2 +- kernel/locking/lockdep.c | 14 ++++++------ kernel/panic.c | 2 +- kernel/printk/printk.c | 4 ++-- kernel/profile.c | 4 ++-- kernel/rcu/tree.c | 10 ++++----- kernel/rcu/update.c | 4 ++-- kernel/sched/clock.c | 2 +- kernel/sched/core.c | 6 ++--- kernel/sched/cpuacct.c | 2 +- kernel/sched/cputime.c | 2 +- kernel/sched/fair.c | 4 ++-- kernel/sched/loadavg.c | 2 +- kernel/sched/rt.c | 2 +- kernel/sched/sched.h | 4 ++-- kernel/smp.c | 2 +- kernel/softirq.c | 3 ++- kernel/time/hrtimer.c | 2 +- kernel/time/jiffies.c | 8 ++++++- kernel/time/ntp.c | 30 ++++++++++++------------- kernel/time/tick-common.c | 4 ++-- kernel/time/tick-internal.h | 2 +- kernel/time/tick-sched.c | 2 +- kernel/time/timekeeping.c | 10 ++++----- kernel/time/timekeeping.h | 2 +- kernel/time/timer.c | 2 +- kernel/trace/trace.c | 2 +- kernel/trace/trace_sched_switch.c | 4 ++-- lib/debug_locks.c | 5 +++-- mm/memory.c | 2 +- mm/page_alloc.c | 2 +- mm/sparse.c | 4 ++-- virt/kvm/kvm_main.c | 2 +- 62 files changed, 170 insertions(+), 156 deletions(-)
arch/x86/events/core.c +2 -2
@@ -44,7 +44,7 @@#include "perf_event.h" -struct x86_pmu x86_pmu__read_mostly ; +struct x86_pmu x86_pmu __asi_not_sensitive_readmostly;static struct pmu pmu; DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
@@ -2685,7 +2685,7 @@ static int x86_pmu_filter_match(struct perf_event *event)return 1 ; } -static struct pmu pmu = { +static struct pmu pmu __asi_not_sensitive = { .pmu_enable = x86_pmu_enable, .pmu_disable = x86_pmu_disable,
arch/x86/events/intel/core.c +1 -1
@@ -189,7 +189,7 @@ static struct event_constraint intel_slm_event_constraints[] __read_mostly =EVENT_CONSTRAINT_END }; -static struct event_constraint intel_skl_event_constraints[] = { +static struct event_constraint intel_skl_event_constraints[] __asi_not_sensitive = {FIXED_EVENT_CONSTRAINT (0x00c0 ,0 ),/* INST_RETIRED.ANY */ FIXED_EVENT_CONSTRAINT (0x003c ,1 ),/* CPU_CLK_UNHALTED.CORE */ FIXED_EVENT_CONSTRAINT (0x0300 ,2 ),/* CPU_CLK_UNHALTED.REF */
arch/x86/events/msr.c +1 -1
@@ -280,7 +280,7 @@ static int msr_event_add(struct perf_event *event, int flags)return 0 ; } -static struct pmu pmu_msr = { +static struct pmu pmu_msr __asi_not_sensitive = { .task_ctx_nr = perf_sw_context, .attr_groups = attr_groups, .event_init = msr_event_init,
arch/x86/events/perf_event.h +1 -1
@@ -1020,7 +1020,7 @@ static struct perf_pmu_format_hybrid_attr format_attr_hybrid_##_name = {\ }struct pmu *x86_get_pmu (unsigned int cpu); -extern struct x86_pmu x86_pmu__read_mostly ; +extern struct x86_pmu x86_pmu __asi_not_sensitive_readmostly; static __always_inline struct x86_perf_task_context_opt *task_context_opt(void *ctx) {
arch/x86/include/asm/kvm_host.h +2 -2
@@ -1542,8 +1542,8 @@ struct kvm_arch_async_pf {extern u32 __read_mostly kvm_nr_uret_msrs;extern u64 __read_mostly host_efer; -extern bool __read_mostly allow_smaller_maxphyaddr; -extern bool __read_mostly enable_apicv; +extern bool __asi_not_sensitive_readmostly allow_smaller_maxphyaddr; +extern bool __asi_not_sensitive_readmostly enable_apicv;extern struct kvm_x86_ops kvm_x86_ops;#define KVM_X86_OP (func) \
arch/x86/kernel/alternative.c +1 -1
@@ -31,7 +31,7 @@#include <asm/paravirt.h> #include <asm/asm-prototypes.h> -int __read_mostly alternatives_patched; +int __asi_not_sensitive alternatives_patched;EXPORT_SYMBOL_GPL (alternatives_patched);
arch/x86/kernel/cpu/bugs.c +1 -1
@@ -46,7 +46,7 @@ static void __init srbds_select_mitigation(void);static void __init l1d_flush_select_mitigation (void );/* The base value of the SPEC_CTRL MSR that always has to be preserved. */ -u64 x86_spec_ctrl_base; +u64 x86_spec_ctrl_base __asi_not_sensitive;EXPORT_SYMBOL_GPL (x86_spec_ctrl_base);static DEFINE_MUTEX (spec_ctrl_mutex);
arch/x86/kernel/setup.c +2 -2
@@ -116,7 +116,7 @@ static struct resource bss_resource = {struct cpuinfo_x86 new_cpu_data;/* Common CPU data for all CPUs */ -struct cpuinfo_x86 boot_cpu_data__read_mostly ; +struct cpuinfo_x86 boot_cpu_data __asi_not_sensitive_readmostly;EXPORT_SYMBOL (boot_cpu_data);unsigned int def_to_bigsmp;
@@ -133,7 +133,7 @@ struct ist_info ist_info;#endif #else -struct cpuinfo_x86 boot_cpu_data__read_mostly ; +struct cpuinfo_x86 boot_cpu_data __asi_not_sensitive_readmostly;EXPORT_SYMBOL (boot_cpu_data);#endif
arch/x86/kernel/smp.c +1 -1
@@ -257,7 +257,7 @@ static int __init nonmi_ipi_setup(char *str)__setup ("nonmi_ipi" , nonmi_ipi_setup); -struct smp_ops smp_ops = { +struct smp_ops smp_ops __asi_not_sensitive = { .smp_prepare_boot_cpu = native_smp_prepare_boot_cpu, .smp_prepare_cpus = native_smp_prepare_cpus, .smp_cpus_done = native_smp_cpus_done,
arch/x86/kernel/tsc.c +4 -4
@@ -30,10 +30,10 @@#include <asm/i8259.h> #include <asm/uv/uv.h> -unsigned int __read_mostly cpu_khz;/* TSC clocks / usec, not used here */ +unsigned int __asi_not_sensitive_readmostly cpu_khz; /* TSC clocks / usec, not used here */EXPORT_SYMBOL (cpu_khz); -unsigned int __read_mostly tsc_khz; +unsigned int __asi_not_sensitive_readmostly tsc_khz;EXPORT_SYMBOL (tsc_khz);#define KHZ 1000
@@ -41,7 +41,7 @@ EXPORT_SYMBOL(tsc_khz);/* * TSC can be unstable due to cpufreq or due to unsynced TSCs */ -static int __read_mostly tsc_unstable; +static int __asi_not_sensitive_readmostly tsc_unstable;static unsigned int __initdata tsc_early_khz;static DEFINE_STATIC_KEY_FALSE (__use_tsc);
@@ -1146,7 +1146,7 @@ static struct clocksource clocksource_tsc_early = {* this one will immediately take over. We will only register if TSC has * been found good. */ -static struct clocksource clocksource_tsc = { +static struct clocksource clocksource_tsc __asi_not_sensitive = { .name ="tsc" , .rating =300 , .read = read_tsc,
arch/x86/kvm/lapic.c +1 -1
@@ -60,7 +60,7 @@#define MAX_APIC_VECTOR 256#define APIC_VECTORS_PER_REG 32 -static bool lapic_timer_advance_dynamic__read_mostly ; +static bool lapic_timer_advance_dynamic __asi_not_sensitive_readmostly;#define LAPIC_TIMER_ADVANCE_ADJUST_MIN 100/* clock cycles */ #define LAPIC_TIMER_ADVANCE_ADJUST_MAX 10000/* clock cycles */ #define LAPIC_TIMER_ADVANCE_NS_INIT 1000
arch/x86/kvm/mmu/spte.c +1 -1
@@ -33,7 +33,7 @@ u64 __read_mostly shadow_mmio_mask;u64 __read_mostly shadow_mmio_access_mask;u64 __read_mostly shadow_present_mask;u64 __read_mostly shadow_me_mask; -u64 __read_mostly shadow_acc_track_mask; +u64 __asi_not_sensitive_readmostly shadow_acc_track_mask;u64 __read_mostly shadow_nonpresent_or_rsvd_mask;u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_mask;
arch/x86/kvm/mmu/spte.h +1 -1
@@ -151,7 +151,7 @@ extern u64 __read_mostly shadow_me_mask;* shadow_acc_track_mask is the set of bits to be cleared in non-accessed * pages. */ -extern u64 __read_mostly shadow_acc_track_mask; +extern u64 __asi_not_sensitive_readmostly shadow_acc_track_mask;/* * This mask must be set on all non-zero Non-Present or Reserved SPTEs in order
arch/x86/kvm/mtrr.c +1 -1
@@ -138,7 +138,7 @@ struct fixed_mtrr_segment {int range_start; }; -static struct fixed_mtrr_segment fixed_seg_table[] = { +static struct fixed_mtrr_segment fixed_seg_table[] __asi_not_sensitive = {/* MSR_MTRRfix64K_00000, 1 unit. 64K fixed mtrr. */ { .start =0x0 ,
arch/x86/kvm/vmx/capabilities.h +7 -7
@@ -6,13 +6,13 @@#include "lapic.h" -extern bool __read_mostly enable_vpid; -extern bool __read_mostly flexpriority_enabled; -extern bool __read_mostly enable_ept; -extern bool __read_mostly enable_unrestricted_guest; -extern bool __read_mostly enable_ept_ad_bits; -extern bool __read_mostly enable_pml; -extern int __read_mostly pt_mode; +extern bool __asi_not_sensitive_readmostly enable_vpid; +extern bool __asi_not_sensitive_readmostly flexpriority_enabled; +extern bool __asi_not_sensitive_readmostly enable_ept; +extern bool __asi_not_sensitive_readmostly enable_unrestricted_guest; +extern bool __asi_not_sensitive_readmostly enable_ept_ad_bits; +extern bool __asi_not_sensitive_readmostly enable_pml; +extern int __asi_not_sensitive_readmostly pt_mode;#define PT_MODE_SYSTEM 0#define PT_MODE_HOST_GUEST 1
arch/x86/kvm/vmx/vmx.c +19 -18
@@ -78,29 +78,29 @@ static const struct x86_cpu_id vmx_cpu_id[] = {MODULE_DEVICE_TABLE (x86cpu, vmx_cpu_id);#endif -bool __read_mostly enable_vpid =1 ; +bool __asi_not_sensitive_readmostly enable_vpid = 1;module_param_named (vpid, enable_vpid, bool,0444 ); -static bool __read_mostly enable_vnmi =1 ; +static bool __asi_not_sensitive_readmostly enable_vnmi = 1;module_param_named (vnmi, enable_vnmi, bool,S_IRUGO ); -bool __read_mostly flexpriority_enabled =1 ; +bool __asi_not_sensitive_readmostly flexpriority_enabled = 1;module_param_named (flexpriority, flexpriority_enabled, bool,S_IRUGO ); -bool __read_mostly enable_ept =1 ; +bool __asi_not_sensitive_readmostly enable_ept = 1;module_param_named (ept, enable_ept, bool,S_IRUGO ); -bool __read_mostly enable_unrestricted_guest =1 ; +bool __asi_not_sensitive_readmostly enable_unrestricted_guest = 1;module_param_named (unrestricted_guest, enable_unrestricted_guest, bool,S_IRUGO ); -bool __read_mostly enable_ept_ad_bits =1 ; +bool __asi_not_sensitive_readmostly enable_ept_ad_bits = 1;module_param_named (eptad, enable_ept_ad_bits, bool,S_IRUGO ); -static bool __read_mostly emulate_invalid_guest_state = true; +static bool __asi_not_sensitive_readmostly emulate_invalid_guest_state = true;module_param (emulate_invalid_guest_state, bool,S_IRUGO ); -static bool __read_mostly fasteoi =1 ; +static bool __asi_not_sensitive_readmostly fasteoi = 1;module_param (fasteoi, bool,S_IRUGO );module_param (enable_apicv, bool,S_IRUGO );
@@ -110,13 +110,13 @@ module_param(enable_apicv, bool, S_IRUGO);* VMX and be a hypervisor for its own guests. If nested=0, guests may not * use VMX instructions. */ -static bool __read_mostly nested =1 ; +static bool __asi_not_sensitive_readmostly nested = 1;module_param (nested, bool,S_IRUGO ); -bool __read_mostly enable_pml =1 ; +bool __asi_not_sensitive_readmostly enable_pml = 1;module_param_named (pml, enable_pml, bool,S_IRUGO ); -static bool __read_mostly dump_invalid_vmcs =0 ; +static bool __asi_not_sensitive_readmostly dump_invalid_vmcs = 0;module_param (dump_invalid_vmcs, bool,0644 );#define MSR_BITMAP_MODE_X2APIC 1
@@ -125,13 +125,13 @@ module_param(dump_invalid_vmcs, bool, 0644);#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL/* Guest_tsc -> host_tsc conversion requires 64-bit division. */ -static int __read_mostly cpu_preemption_timer_multi; -static bool __read_mostly enable_preemption_timer =1 ; +static int __asi_not_sensitive_readmostly cpu_preemption_timer_multi; +static bool __asi_not_sensitive_readmostly enable_preemption_timer = 1;#ifdef CONFIG_X86_64 module_param_named (preemption_timer, enable_preemption_timer, bool,S_IRUGO );#endif -extern bool __read_mostly allow_smaller_maxphyaddr; +extern bool __asi_not_sensitive_readmostly allow_smaller_maxphyaddr;module_param (allow_smaller_maxphyaddr, bool,S_IRUGO );#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
@@ -202,7 +202,7 @@ static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;module_param (ple_window_max, uint,0444 );/* Default is SYSTEM mode, 1 for host-guest mode */ -int __read_mostly pt_mode =PT_MODE_SYSTEM ; +int __asi_not_sensitive_readmostly pt_mode = PT_MODE_SYSTEM;module_param (pt_mode, int,S_IRUGO );static DEFINE_STATIC_KEY_FALSE (vmx_l1d_should_flush);
@@ -421,7 +421,7 @@ static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);static DEFINE_SPINLOCK (vmx_vpid_lock); -struct vmcs_config vmcs_config; +struct vmcs_config vmcs_config __asi_not_sensitive;struct vmx_capability vmx_capability;#define VMX_SEGMENT_FIELD (seg) \
@@ -453,7 +453,7 @@ static inline void vmx_segment_cache_clear(struct vcpu_vmx *vmx) vmx->segment_cache.bitmask =0 ; } -static unsigned long host_idt_base; +static unsigned long host_idt_base __asi_not_sensitive;#if IS_ENABLED (CONFIG_HYPERV )static bool __read_mostly enlightened_vmcs = true;
@@ -5549,7 +5549,8 @@ static int handle_bus_lock_vmexit(struct kvm_vcpu *vcpu)* may resume. Otherwise they set the kvm_run parameter to indicate what needs * to be done to userspace and return 0. */ -static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { +static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) __asi_not_sensitive += { [EXIT_REASON_EXCEPTION_NMI ] = handle_exception_nmi, [EXIT_REASON_EXTERNAL_INTERRUPT ] = handle_external_interrupt, [EXIT_REASON_TRIPLE_FAULT ] = handle_triple_fault,
arch/x86/kvm/x86.c +18 -17
@@ -123,7 +123,7 @@ static int sync_regs(struct kvm_vcpu *vcpu);static int __set_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2);static void __get_sregs2 (struct kvm_vcpu *vcpu,struct kvm_sregs2 *sregs2); -struct kvm_x86_ops kvm_x86_ops__read_mostly ; +struct kvm_x86_ops kvm_x86_ops __asi_not_sensitive_readmostly;EXPORT_SYMBOL_GPL (kvm_x86_ops);#define KVM_X86_OP (func) \
@@ -148,17 +148,17 @@ module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);static bool __read_mostly kvmclock_periodic_sync = true;module_param (kvmclock_periodic_sync, bool,S_IRUGO ); -bool __read_mostly kvm_has_tsc_control; +bool __asi_not_sensitive_readmostly kvm_has_tsc_control;EXPORT_SYMBOL_GPL (kvm_has_tsc_control); -u32 __read_mostly kvm_max_guest_tsc_khz; +u32 __asi_not_sensitive_readmostly kvm_max_guest_tsc_khz;EXPORT_SYMBOL_GPL (kvm_max_guest_tsc_khz); -u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits; +u8 __asi_not_sensitive_readmostly kvm_tsc_scaling_ratio_frac_bits;EXPORT_SYMBOL_GPL (kvm_tsc_scaling_ratio_frac_bits); -u64 __read_mostly kvm_max_tsc_scaling_ratio; +u64 __asi_not_sensitive_readmostly kvm_max_tsc_scaling_ratio;EXPORT_SYMBOL_GPL (kvm_max_tsc_scaling_ratio); -u64 __read_mostly kvm_default_tsc_scaling_ratio; +u64 __asi_not_sensitive_readmostly kvm_default_tsc_scaling_ratio;EXPORT_SYMBOL_GPL (kvm_default_tsc_scaling_ratio); -bool __read_mostly kvm_has_bus_lock_exit; +bool __asi_not_sensitive_readmostly kvm_has_bus_lock_exit;EXPORT_SYMBOL_GPL (kvm_has_bus_lock_exit);/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
@@ -171,20 +171,20 @@ module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);* advancement entirely. Any other value is used as-is and disables adaptive * tuning, i.e. allows privileged userspace to set an exact advancement time. */ -static int __read_mostly lapic_timer_advance_ns =-1 ; +static int __asi_not_sensitive_readmostly lapic_timer_advance_ns = -1;module_param (lapic_timer_advance_ns, int,S_IRUGO |S_IWUSR ); -static bool __read_mostly vector_hashing = true; +static bool __asi_not_sensitive_readmostly vector_hashing = true;module_param (vector_hashing, bool,S_IRUGO ); -bool __read_mostly enable_vmware_backdoor = false; +bool __asi_not_sensitive_readmostly enable_vmware_backdoor = false;module_param (enable_vmware_backdoor, bool,S_IRUGO );EXPORT_SYMBOL_GPL (enable_vmware_backdoor); -static bool __read_mostly force_emulation_prefix = false; +static bool __asi_not_sensitive_readmostly force_emulation_prefix = false;module_param (force_emulation_prefix, bool,S_IRUGO ); -int __read_mostly pi_inject_timer =-1 ; +int __asi_not_sensitive_readmostly pi_inject_timer = -1;module_param (pi_inject_timer, bint,S_IRUGO |S_IWUSR );/*
@@ -216,13 +216,14 @@ static struct kvm_user_return_msrs __percpu *user_return_msrs;u64 __read_mostly host_efer;EXPORT_SYMBOL_GPL (host_efer); -bool __read_mostly allow_smaller_maxphyaddr =0 ; +bool __asi_not_sensitive_readmostly allow_smaller_maxphyaddr = 0;EXPORT_SYMBOL_GPL (allow_smaller_maxphyaddr); -bool __read_mostly enable_apicv = true; +bool __asi_not_sensitive_readmostly enable_apicv = true;EXPORT_SYMBOL_GPL (enable_apicv); -u64 __read_mostly host_xss; +/* TODO(oweisse): how dangerous is this variable, from a security standpoint? */ +u64 __asi_not_sensitive_readmostly host_xss;EXPORT_SYMBOL_GPL (host_xss);u64 __read_mostly supported_xss;EXPORT_SYMBOL_GPL (supported_xss);
@@ -292,7 +293,7 @@ const struct kvm_stats_header kvm_vcpu_stats_header = { sizeof(kvm_vcpu_stats_desc), }; -u64 __read_mostly host_xcr0; +u64 __asi_not_sensitive_readmostly host_xcr0;u64 __read_mostly supported_xcr0;EXPORT_SYMBOL_GPL (supported_xcr0);
@@ -2077,7 +2078,7 @@ struct pvclock_gtod_data {u64 wall_time_sec; }; -static struct pvclock_gtod_data pvclock_gtod_data; +static struct pvclock_gtod_data pvclock_gtod_data __asi_not_sensitive;static void update_pvclock_gtod (struct timekeeper *tk) {
arch/x86/mm/asi.c +2 -2
@@ -17,8 +17,8 @@#undef pr_fmt#define pr_fmt (fmt) "ASI: " fmt -static struct asi_class asi_class[ASI_MAX_NUM ]; -static DEFINE_SPINLOCK (asi_class_lock); +static struct asi_class asi_class[ASI_MAX_NUM] __asi_not_sensitive; +static DEFINE_SPINLOCK(asi_class_lock __asi_not_sensitive);DEFINE_PER_CPU_ALIGNED (struct asi_state , asi_cpu_state);EXPORT_PER_CPU_SYMBOL_GPL (asi_cpu_state);
include/linux/debug_locks.h +2 -2
@@ -7,8 +7,8 @@struct task_struct ; -extern int debug_locks__read_mostly ; -extern int debug_locks_silent__read_mostly ; +extern int debug_locks; +extern int debug_locks_silent; static __always_inline int __debug_locks_off(void)
include/linux/jiffies.h +2 -2
@@ -76,8 +76,8 @@ extern int register_refined_jiffies(long clock_tick_rate);* without sampling the sequence number in jiffies_lock. * get_jiffies_64() will do this for you as appropriate. */ -extern u64 __cacheline_aligned_in_smp jiffies_64; -extern unsigned long volatile __cacheline_aligned_in_smp __jiffy_arch_data jiffies; +extern u64 jiffies_64; +extern unsigned long volatile __jiffy_arch_data jiffies;#if (BITS_PER_LONG <64 )u64 get_jiffies_64 (void );
include/linux/notifier.h +1 -1
@@ -117,7 +117,7 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);struct blocking_notifier_head name = \ BLOCKING_NOTIFIER_INIT(name)#define RAW_NOTIFIER_HEAD (name) \ - struct raw_notifier_head name = \ + struct raw_notifier_head name __asi_not_sensitive = \ RAW_NOTIFIER_INIT(name)#ifdef CONFIG_TREE_SRCU
include/linux/profile.h +1 -1
@@ -38,7 +38,7 @@ enum profile_type {#ifdef CONFIG_PROFILING -extern int prof_on__read_mostly ; +extern int prof_on;/* init basic kernel profiler */ int profile_init (void );
include/linux/rcupdate.h +3 -1
@@ -84,7 +84,7 @@ static inline int rcu_preempt_depth(void)/* Internal to kernel */ void rcu_init (void ); -extern int rcu_scheduler_active__read_mostly ; +extern int rcu_scheduler_active;void rcu_sched_clock_irq (int user);void rcu_report_dead (unsigned int cpu);void rcutree_migrate_callbacks (int cpu);
@@ -308,6 +308,8 @@ static inline int rcu_read_lock_any_held(void)#ifdef CONFIG_PROVE_RCU +/* TODO: ASI - (oweisse) we might want to switch ".data.unlikely" to some other +* section that will be mapped to ASI. */ /** * RCU_LOCKDEP_WARN - emit lockdep splat if specified condition is met * @c: condition to check
include/linux/rcutree.h +1 -1
@@ -62,7 +62,7 @@ static inline void rcu_irq_exit_check_preempt(void) { }void exit_rcu (void );void rcu_scheduler_starting (void ); -extern int rcu_scheduler_active__read_mostly ; +extern int rcu_scheduler_active;void rcu_end_inkernel_boot (void );bool rcu_inkernel_boot_has_ended (void );bool rcu_is_watching (void );
include/linux/sched/sysctl.h +1 -0
@@ -3,6 +3,7 @@#define _LINUX_SCHED_SYSCTL_H#include <linux/types.h> +#include <asm/asi.h> struct ctl_table ;
init/main.c +1 -1
@@ -123,7 +123,7 @@ extern void radix_tree_init(void);* operations which are not allowed with IRQ disabled are allowed while the * flag is set. */ -bool early_boot_irqs_disabled__read_mostly ; +bool early_boot_irqs_disabled __asi_not_sensitive;enum system_states system_state__read_mostly ;EXPORT_SYMBOL (system_state);
kernel/cgroup/cgroup.c +3 -2
@@ -162,7 +162,8 @@ static struct static_key_true *cgroup_subsys_on_dfl_key[] = { static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu);/* the default hierarchy */ -struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu }; +struct cgroup_root cgrp_dfl_root __asi_not_sensitive = + { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu };EXPORT_SYMBOL_GPL (cgrp_dfl_root);/*
@@ -755,7 +756,7 @@ EXPORT_SYMBOL_GPL(of_css);* reference-counted, to improve performance when child cgroups * haven't been created. */ -struct css_set init_css_set = { +struct css_set init_css_set __asi_not_sensitive = { .refcount =REFCOUNT_INIT (1 ), .dom_cset = &init_css_set, .tasks =LIST_HEAD_INIT (init_css_set.tasks),
kernel/cpu.c +7 -7
@@ -2581,26 +2581,26 @@ const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;EXPORT_SYMBOL (cpu_all_bits);#ifdef CONFIG_INIT_ALL_POSSIBLE -struct cpumask __cpu_possible_mask__read_mostly +struct cpumask __cpu_possible_mask __asi_not_sensitive_readmostly = {CPU_BITS_ALL };#else -struct cpumask __cpu_possible_mask__read_mostly ; +struct cpumask __cpu_possible_mask __asi_not_sensitive_readmostly;#endif EXPORT_SYMBOL (__cpu_possible_mask); -struct cpumask __cpu_online_mask__read_mostly ; +struct cpumask __cpu_online_mask __asi_not_sensitive_readmostly;EXPORT_SYMBOL (__cpu_online_mask); -struct cpumask __cpu_present_mask__read_mostly ; +struct cpumask __cpu_present_mask __asi_not_sensitive_readmostly;EXPORT_SYMBOL (__cpu_present_mask); -struct cpumask __cpu_active_mask__read_mostly ; +struct cpumask __cpu_active_mask __asi_not_sensitive_readmostly;EXPORT_SYMBOL (__cpu_active_mask); -struct cpumask __cpu_dying_mask__read_mostly ; +struct cpumask __cpu_dying_mask __asi_not_sensitive_readmostly;EXPORT_SYMBOL (__cpu_dying_mask); -atomic_t __num_online_cpus__read_mostly ; +atomic_t __num_online_cpus __asi_not_sensitive_readmostly;EXPORT_SYMBOL (__num_online_cpus);void init_cpu_present (const struct cpumask *src)
kernel/events/core.c +2 -2
@@ -9651,7 +9651,7 @@ static int perf_swevent_init(struct perf_event *event)return 0 ; } -static struct pmu perf_swevent = { +static struct pmu perf_swevent __asi_not_sensitive = { .task_ctx_nr = perf_sw_context, .capabilities =PERF_PMU_CAP_NO_NMI ,
@@ -9800,7 +9800,7 @@ static int perf_tp_event_init(struct perf_event *event)return 0 ; } -static struct pmu perf_tracepoint = { +static struct pmu perf_tracepoint __asi_not_sensitive = { .task_ctx_nr = perf_sw_context, .event_init = perf_tp_event_init,
kernel/freezer.c +1 -1
@@ -13,7 +13,7 @@#include <linux/kthread.h> /* total number of freezing conditions in effect */ -atomic_t system_freezing_cnt =ATOMIC_INIT (0 ); +atomic_t __asi_not_sensitive system_freezing_cnt = ATOMIC_INIT(0);EXPORT_SYMBOL (system_freezing_cnt);/* indicate whether PM freezing is in effect, protected by
kernel/locking/lockdep.c +7 -7
@@ -64,7 +64,7 @@#include <trace/events/lock.h> #ifdef CONFIG_PROVE_LOCKING -int prove_locking =1 ; +int prove_locking __asi_not_sensitive = 1;module_param (prove_locking, int,0644 );#else #define prove_locking 0
@@ -186,8 +186,8 @@ unsigned long nr_zapped_classes;#ifndef CONFIG_DEBUG_LOCKDEP static#endif -struct lock_class lock_classes[MAX_LOCKDEP_KEYS ]; -static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS); +struct lock_class lock_classes[MAX_LOCKDEP_KEYS] __asi_not_sensitive; +static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS) __asi_not_sensitive;static inline struct lock_class *hlock_class (struct held_lock *hlock) {
@@ -389,7 +389,7 @@ static struct hlist_head classhash_table[CLASSHASH_SIZE];#define __chainhashfn (chain) hash_long(chain, CHAINHASH_BITS)#define chainhashentry (chain) (chainhash_table + __chainhashfn((chain))) -static struct hlist_head chainhash_table[CHAINHASH_SIZE ]; +static struct hlist_head chainhash_table[CHAINHASH_SIZE] __asi_not_sensitive;/* * the id of held_lock
@@ -599,7 +599,7 @@ u64 lockdep_stack_hash_count(void)unsigned int nr_hardirq_chains;unsigned int nr_softirq_chains;unsigned int nr_process_chains; -unsigned int max_lockdep_depth; +unsigned int max_lockdep_depth __asi_not_sensitive;#ifdef CONFIG_DEBUG_LOCKDEP /*
@@ -3225,8 +3225,8 @@ check_prevs_add(struct task_struct *curr, struct held_lock *next)return 0 ; } -struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS ]; -static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS); +struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS] __asi_not_sensitive; +static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS) __asi_not_sensitive;static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS ];unsigned long nr_zapped_lock_chains;unsigned int nr_free_chain_hlocks;/* Free chain_hlocks in buckets */
kernel/panic.c +1 -1
@@ -56,7 +56,7 @@ int panic_on_warn __read_mostly;unsigned long panic_on_taint;bool panic_on_taint_nousertaint = false; -int panic_timeout =CONFIG_PANIC_TIMEOUT ; +int panic_timeout __asi_not_sensitive = CONFIG_PANIC_TIMEOUT;EXPORT_SYMBOL_GPL (panic_timeout);#define PANIC_PRINT_TASK_INFO 0x00000001
kernel/printk/printk.c +2 -2
@@ -75,7 +75,7 @@ EXPORT_SYMBOL(ignore_console_lock_warning);* Low level drivers may need that to know if they can schedule in * their unblank() callback or not. So let's export it. */ -int oops_in_progress; +int oops_in_progress __asi_not_sensitive;EXPORT_SYMBOL (oops_in_progress);/*
@@ -2001,7 +2001,7 @@ static u8 *__printk_recursion_counter(void)local_irq_restore (flags); \ }while (0 ) -int printk_delay_msec __read_mostly; +int printk_delay_msec __asi_not_sensitive_readmostly;static inline void printk_delay (void ) {
kernel/profile.c +2 -2
@@ -44,10 +44,10 @@ static atomic_t *prof_buffer;static unsigned long prof_len;static unsigned short int prof_shift; -int prof_on__read_mostly ; +int prof_on __asi_not_sensitive_readmostly;EXPORT_SYMBOL_GPL (prof_on); -static cpumask_var_t prof_cpu_mask; +static cpumask_var_t prof_cpu_mask __asi_not_sensitive;#if defined(CONFIG_SMP ) && defined(CONFIG_PROC_FS ) static DEFINE_PER_CPU(struct profile_hit *[2], cpu_profile_hits); static DEFINE_PER_CPU(int, cpu_profile_flip);
kernel/rcu/tree.c +5 -5
@@ -82,7 +82,7 @@ static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = { .cblist.flags = SEGCBLIST_SOFTIRQ_ONLY,#endif }; -static struct rcu_state rcu_state = { +static struct rcu_state rcu_state __asi_not_sensitive = { .level = { &rcu_state.node[0 ] }, .gp_state =RCU_GP_IDLE , .gp_seq = (0UL -300UL ) <<RCU_SEQ_CTR_SHIFT ,
@@ -98,7 +98,7 @@ static struct rcu_state rcu_state = {static bool dump_tree;module_param (dump_tree, bool,0444 );/* By default, use RCU_SOFTIRQ instead of rcuc kthreads. */ -static bool use_softirq = !IS_ENABLED (CONFIG_PREEMPT_RT ); +static __asi_not_sensitive bool use_softirq = !IS_ENABLED(CONFIG_PREEMPT_RT);#ifndef CONFIG_PREEMPT_RT module_param (use_softirq, bool,0444 );#endif
@@ -125,7 +125,7 @@ int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */* transitions from RCU_SCHEDULER_INIT to RCU_SCHEDULER_RUNNING after RCU * is fully initialized, including all of its kthreads having been spawned. */ -int rcu_scheduler_active__read_mostly ; +int rcu_scheduler_active __asi_not_sensitive;EXPORT_SYMBOL_GPL (rcu_scheduler_active);/*
@@ -140,7 +140,7 @@ EXPORT_SYMBOL_GPL(rcu_scheduler_active);* early boot to take responsibility for these callbacks, but one step at * a time. */ -static int rcu_scheduler_fully_active__read_mostly ; +static int rcu_scheduler_fully_active __asi_not_sensitive;static void rcu_report_qs_rnp (unsigned long mask,struct rcu_node *rnp,unsigned long gps,unsigned long flags);
@@ -470,7 +470,7 @@ module_param(qovld, long, 0444);static ulong jiffies_till_first_fqs =IS_ENABLED (CONFIG_RCU_STRICT_GRACE_PERIOD ) ?0 :ULONG_MAX ;static ulong jiffies_till_next_fqs =ULONG_MAX ; -static bool rcu_kick_kthreads; +static bool rcu_kick_kthreads __asi_not_sensitive;static int rcu_divisor =7 ;module_param (rcu_divisor, int,0644 );
kernel/rcu/update.c +2 -2
@@ -243,7 +243,7 @@ core_initcall(rcu_set_runtime_mode);#ifdef CONFIG_DEBUG_LOCK_ALLOC static struct lock_class_key rcu_lock_key; -struct lockdep_map rcu_lock_map = { +struct lockdep_map rcu_lock_map __asi_not_sensitive = { .name ="rcu_read_lock" , .key = &rcu_lock_key, .wait_type_outer = LD_WAIT_FREE,
@@ -494,7 +494,7 @@ EXPORT_SYMBOL_GPL(rcutorture_sched_setaffinity);#ifdef CONFIG_RCU_STALL_COMMON int rcu_cpu_stall_ftrace_dump__read_mostly ;module_param (rcu_cpu_stall_ftrace_dump, int,0644 ); -int rcu_cpu_stall_suppress__read_mostly ;// !0 = suppress stall warnings. +int rcu_cpu_stall_suppress __asi_not_sensitive_readmostly; // !0 = suppress stall warnings.EXPORT_SYMBOL_GPL (rcu_cpu_stall_suppress);module_param (rcu_cpu_stall_suppress, int,0644 );int rcu_cpu_stall_timeout__read_mostly =CONFIG_RCU_CPU_STALL_TIMEOUT ;
kernel/sched/clock.c +1 -1
@@ -84,7 +84,7 @@ static int __sched_clock_stable_early = 1;/* * We want: ktime_get_ns() + __gtod_offset == sched_clock() + __sched_clock_offset */ -__read_mostly u64 __sched_clock_offset; +__asi_not_sensitive u64 __sched_clock_offset;static __read_mostly u64 __gtod_offset;struct sched_clock_data {
kernel/sched/core.c +3 -3
@@ -76,9 +76,9 @@ __read_mostly int sysctl_resched_latency_warn_once = 1;* Limited because this is done with IRQs disabled. */ #ifdef CONFIG_PREEMPT_RT -const_debug unsigned int sysctl_sched_nr_migrate = 8; +unsigned int sysctl_sched_nr_migrate __asi_not_sensitive_readmostly = 8;#else -const_debug unsigned int sysctl_sched_nr_migrate = 32; +unsigned int sysctl_sched_nr_migrate __asi_not_sensitive_readmostly = 32;#endif /*
@@ -9254,7 +9254,7 @@ int in_sched_functions(unsigned long addr)* Default task group. * Every task in system belongs to this group at bootup. */ -struct task_group root_task_group; +struct task_group root_task_group __asi_not_sensitive;LIST_HEAD (task_groups);/* Cacheline aligned slab cache for task_group */
kernel/sched/cpuacct.c +1 -1
@@ -50,7 +50,7 @@ static inline struct cpuacct *parent_ca(struct cpuacct *ca) } static DEFINE_PER_CPU(struct cpuacct_usage, root_cpuacct_cpuusage); -static struct cpuacct root_cpuacct = { +static struct cpuacct root_cpuacct __asi_not_sensitive = { .cpustat = &kernel_cpustat, .cpuusage = &root_cpuacct_cpuusage, };
kernel/sched/cputime.c +1 -1
@@ -19,7 +19,7 @@*/ DEFINE_PER_CPU(struct irqtime, cpu_irqtime); -static int sched_clock_irqtime; +static int __asi_not_sensitive sched_clock_irqtime;void enable_sched_clock_irqtime (void ) {
kernel/sched/fair.c +2 -2
@@ -35,7 +35,7 @@* * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds) */ -unsigned int sysctl_sched_latency =6000000ULL ; +__asi_not_sensitive unsigned int sysctl_sched_latency = 6000000ULL;static unsigned int normalized_sysctl_sched_latency =6000000ULL ;/*
@@ -90,7 +90,7 @@ unsigned int sysctl_sched_child_runs_first __read_mostly;unsigned int sysctl_sched_wakeup_granularity =1000000UL ;static unsigned int normalized_sysctl_sched_wakeup_granularity =1000000UL ; -const_debug unsigned int sysctl_sched_migration_cost = 500000UL; +unsigned int sysctl_sched_migration_cost __asi_not_sensitive_readmostly = 500000UL;int sched_thermal_decay_shift;static int __init setup_sched_thermal_decay_shift (char *str)
kernel/sched/loadavg.c +1 -1
@@ -57,7 +57,7 @@/* Variables and functions for calc_load */ atomic_long_t calc_load_tasks; -unsigned long calc_load_update; +unsigned long calc_load_update __asi_not_sensitive;unsigned long avenrun[3 ];EXPORT_SYMBOL (avenrun);/* should be removed */
kernel/sched/rt.c +1 -1
@@ -14,7 +14,7 @@ static const u64 max_rt_runtime = MAX_BW;static int do_sched_rt_period_timer (struct rt_bandwidth *rt_b,int overrun); -struct rt_bandwidth def_rt_bandwidth; +struct rt_bandwidth def_rt_bandwidth __asi_not_sensitive;static enum hrtimer_restart sched_rt_period_timer (struct hrtimer *timer) {
kernel/sched/sched.h +2 -2
@@ -2379,8 +2379,8 @@ extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);extern void check_preempt_curr (struct rq *rq,struct task_struct *p,int flags); -extern const_debug unsigned int sysctl_sched_nr_migrate; -extern const_debug unsigned int sysctl_sched_migration_cost; +extern unsigned int sysctl_sched_nr_migrate; +extern unsigned int sysctl_sched_migration_cost;#ifdef CONFIG_SCHED_DEBUG extern unsigned int sysctl_sched_latency;
kernel/smp.c +1 -1
@@ -1070,7 +1070,7 @@ static int __init maxcpus(char *str)early_param ("maxcpus" , maxcpus);/* Setup number of possible processor ids */ -unsigned int nr_cpu_ids__read_mostly =NR_CPUS ; +unsigned int nr_cpu_ids __asi_not_sensitive = NR_CPUS;EXPORT_SYMBOL (nr_cpu_ids);/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
kernel/softirq.c +2 -1
@@ -56,7 +56,8 @@ DEFINE_PER_CPU_ALIGNED(irq_cpustat_t, irq_stat);EXPORT_PER_CPU_SYMBOL (irq_stat);#endif -static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp; +static struct softirq_action softirq_vec[NR_SOFTIRQS] +__asi_not_sensitive____cacheline_aligned ; DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
kernel/time/hrtimer.c +1 -1
@@ -706,7 +706,7 @@ hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal)* High resolution timer enabled ? */ static bool hrtimer_hres_enabled__read_mostly = true; -unsigned int hrtimer_resolution__read_mostly =LOW_RES_NSEC ; +unsigned int hrtimer_resolution __asi_not_sensitive = LOW_RES_NSEC;EXPORT_SYMBOL_GPL (hrtimer_resolution);/*
kernel/time/jiffies.c +7 -1
@@ -40,7 +40,13 @@ static struct clocksource clocksource_jiffies = { .max_cycles = 10, }; -__cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK (jiffies_lock ); +/* TODO(oweisse): __cacheline_aligned_in_smp is expanded to +__section__(".data..cacheline_aligned"))) which is at odds with +__asi_not_sensitive. We should consider instead using +__attribute__ ((__aligned__(XXX))) where XXX is a def for cacheline or +something*/ +/* __cacheline_aligned_in_smp */ +__asi_not_sensitive DEFINE_RAW_SPINLOCK (jiffies_lock ); __cacheline_aligned_in_smp seqcount_raw_spinlock_t jiffies_seq =SEQCNT_RAW_SPINLOCK_ZERO (jiffies_seq, &jiffies_lock);
kernel/time/ntp.c +15 -15
@@ -31,13 +31,13 @@/* USER_HZ period (usecs): */ -unsigned long tick_usec =USER_TICK_USEC ; +unsigned long tick_usec __asi_not_sensitive =USER_TICK_USEC ;/* SHIFTED_HZ period (nsecs): */ -unsigned long tick_nsec; +unsigned long tick_nsec __asi_not_sensitive; -static u64 tick_length; -static u64 tick_length_base; +static u64 tick_length __asi_not_sensitive; +static u64 tick_length_base __asi_not_sensitive;#define SECS_PER_DAY 86400#define MAX_TICKADJ 500LL/* usecs */
@@ -54,36 +54,36 @@ static u64 tick_length_base;* * (TIME_ERROR prevents overwriting the CMOS clock) */ -static int time_state =TIME_OK ; +static int time_state __asi_not_sensitive = TIME_OK;/* clock status bits: */ -static int time_status =STA_UNSYNC ; +static int time_status __asi_not_sensitive = STA_UNSYNC;/* time adjustment (nsecs): */ -static s64 time_offset; +static s64 time_offset __asi_not_sensitive;/* pll time constant: */ -static long time_constant =2 ; +static long time_constant __asi_not_sensitive =2 ;/* maximum error (usecs): */ -static long time_maxerror =NTP_PHASE_LIMIT ; +static long time_maxerror __asi_not_sensitive =NTP_PHASE_LIMIT ;/* estimated error (usecs): */ -static long time_esterror =NTP_PHASE_LIMIT ; +static long time_esterror __asi_not_sensitive =NTP_PHASE_LIMIT ;/* frequency offset (scaled nsecs/secs): */ -static s64 time_freq; +static s64 time_freq __asi_not_sensitive;/* time at last adjustment (secs): */ -static time64_t time_reftime; +static time64_t time_reftime __asi_not_sensitive; -static long time_adjust; +static long time_adjust __asi_not_sensitive;/* constant (boot-param configurable) NTP tick adjustment (upscaled) */ -static s64 ntp_tick_adj; +static s64 ntp_tick_adj __asi_not_sensitive;/* second value of the next pending leapsecond, or TIME64_MAX if no leap */ -static time64_t ntp_next_leap_sec =TIME64_MAX ; +static time64_t ntp_next_leap_sec __asi_not_sensitive = TIME64_MAX;#ifdef CONFIG_NTP_PPS
kernel/time/tick-common.c +2 -2
@@ -31,7 +31,7 @@ DEFINE_PER_CPU(struct tick_device, tick_cpu_device);* CPU which handles the tick and protected by jiffies_lock. There is * no requirement to write hold the jiffies seqcount for it. */ -ktime_t tick_next_period; +ktime_t tick_next_period __asi_not_sensitive;/* * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
@@ -47,7 +47,7 @@ ktime_t tick_next_period;* at it will take over and keep the time keeping alive. The handover * procedure also covers cpu hotplug. */ -int tick_do_timer_cpu__read_mostly =TICK_DO_TIMER_BOOT ; +int tick_do_timer_cpu __asi_not_sensitive_readmostly = TICK_DO_TIMER_BOOT;#ifdef CONFIG_NO_HZ_FULL /* * tick_do_timer_boot_cpu indicates the boot CPU temporarily owns
kernel/time/tick-internal.h +1 -1
@@ -15,7 +15,7 @@ DECLARE_PER_CPU(struct tick_device, tick_cpu_device);extern ktime_t tick_next_period; -extern int tick_do_timer_cpu__read_mostly ; +extern int tick_do_timer_cpu;extern void tick_setup_periodic (struct clock_event_device *dev,int broadcast);extern void tick_handle_periodic (struct clock_event_device *dev);
kernel/time/tick-sched.c +1 -1
@@ -49,7 +49,7 @@ struct tick_sched *tick_get_tick_sched(int cpu)* jiffies_lock and jiffies_seq. tick_nohz_next_event() needs to get a * consistent view of jiffies and last_jiffies_update. */ -static ktime_t last_jiffies_update; +static ktime_t last_jiffies_update __asi_not_sensitive;/* * Must be called with interrupts disabled !
kernel/time/timekeeping.c +5 -5
@@ -39,7 +39,7 @@ enum timekeeping_adv_mode { TK_ADV_FREQ }; -DEFINE_RAW_SPINLOCK (timekeeper_lock); +__asi_not_sensitive DEFINE_RAW_SPINLOCK (timekeeper_lock );/* * The most important data for readout fits into a single 64 byte
@@ -48,14 +48,14 @@ DEFINE_RAW_SPINLOCK(timekeeper_lock);static struct {seqcount_raw_spinlock_t seq;struct timekeeper timekeeper; -} tk_core____cacheline_aligned = { +} tk_core ____cacheline_aligned __asi_not_sensitive = { .seq =SEQCNT_RAW_SPINLOCK_ZERO (tk_core.seq, &timekeeper_lock), }; -static struct timekeeper shadow_timekeeper; +static struct timekeeper shadow_timekeeper __asi_not_sensitive;/* flag for if timekeeping is suspended */ -int __read_mostly timekeeping_suspended; +int __asi_not_sensitive_readmostly timekeeping_suspended;/** * struct tk_fast - NMI safe timekeeper
@@ -72,7 +72,7 @@ struct tk_fast { };/* Suspend-time cycles value for halted fast timekeeper. */ -static u64 cycles_at_suspend; +static u64 cycles_at_suspend __asi_not_sensitive;static u64 dummy_clock_read (struct clocksource *cs) {
kernel/time/timekeeping.h +1 -1
@@ -26,7 +26,7 @@ extern void update_process_times(int user);extern void do_timer (unsigned long ticks);extern void update_wall_time (void ); -extern raw_spinlock_t jiffies_lock; +extern __asi_not_sensitive raw_spinlock_t jiffies_lock;extern seqcount_raw_spinlock_t jiffies_seq;#define CS_NAME_LEN 32
kernel/time/timer.c +1 -1
@@ -56,7 +56,7 @@#define CREATE_TRACE_POINTS #include <trace/events/timer.h> -__visible u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES; +u64 jiffies_64 __asi_not_sensitive ____cacheline_aligned = INITIAL_JIFFIES;EXPORT_SYMBOL (jiffies_64);
kernel/trace/trace.c +1 -1
@@ -432,7 +432,7 @@ EXPORT_SYMBOL_GPL(unregister_ftrace_export);* The global_trace is the descriptor that holds the top-level tracing * buffers for the live tracing. */ -static struct trace_array global_trace = { +static struct trace_array global_trace __asi_not_sensitive = { .trace_flags =TRACE_DEFAULT_FLAGS , };
kernel/trace/trace_sched_switch.c +2 -2
@@ -16,8 +16,8 @@#define RECORD_CMDLINE 1#define RECORD_TGID 2 -static int sched_cmdline_ref; -static int sched_tgid_ref; +static int sched_cmdline_ref __asi_not_sensitive; +static int sched_tgid_ref __asi_not_sensitive;static DEFINE_MUTEX (sched_register_mutex); static void
lib/debug_locks.c +3 -2
@@ -14,6 +14,7 @@#include <linux/export.h> #include <linux/spinlock.h> #include <linux/debug_locks.h> +#include <asm/asi.h> /* * We want to turn all lock-debugging facilities on/off at once,
@@ -22,7 +23,7 @@* that would just muddy the log. So we report the first one and * shut up after that. */ -int debug_locks__read_mostly =1 ; +int debug_locks __asi_not_sensitive_readmostly = 1;EXPORT_SYMBOL_GPL (debug_locks);/*
@@ -30,7 +31,7 @@ EXPORT_SYMBOL_GPL(debug_locks);* 'silent failure': nothing is printed to the console when * a locking bug is detected. */ -int debug_locks_silent__read_mostly ; +int debug_locks_silent __asi_not_sensitive_readmostly;EXPORT_SYMBOL_GPL (debug_locks_silent);/*
mm/memory.c +1 -1
@@ -152,7 +152,7 @@ static int __init disable_randmaps(char *s) }__setup ("norandmaps" , disable_randmaps); -unsigned long zero_pfn__read_mostly ; +unsigned long zero_pfn __asi_not_sensitive;EXPORT_SYMBOL (zero_pfn);unsigned long highest_memmap_pfn__read_mostly ;
mm/page_alloc.c +1 -1
@@ -183,7 +183,7 @@ unsigned long totalreserve_pages __read_mostly;unsigned long totalcma_pages__read_mostly ;int percpu_pagelist_high_fraction; -gfp_t gfp_allowed_mask__read_mostly =GFP_BOOT_MASK ; +gfp_t gfp_allowed_mask __asi_not_sensitive_readmostly = GFP_BOOT_MASK;DEFINE_STATIC_KEY_MAYBE (CONFIG_INIT_ON_ALLOC_DEFAULT_ON , init_on_alloc);EXPORT_SYMBOL (init_on_alloc);
mm/sparse.c +2 -2
@@ -24,10 +24,10 @@* 1) mem_section - memory sections, mem_map's for valid memory */ #ifdef CONFIG_SPARSEMEM_EXTREME -struct mem_section **mem_section; +struct mem_section **mem_section __asi_not_sensitive;#else struct mem_section mem_section[NR_SECTION_ROOTS][SECTIONS_PER_ROOT] -____cacheline_internodealigned_in_smp ; +____cacheline_internodealigned_in_smp __asi_not_sensitive;#endif EXPORT_SYMBOL (mem_section);
virt/kvm/kvm_main.c +1 -1
@@ -3497,7 +3497,7 @@ static int kvm_vcpu_release(struct inode *inode, struct file *filp)return 0 ; } -static struct file_operations kvm_vcpu_fops = { +static struct file_operations kvm_vcpu_fops __asi_not_sensitive = { .release = kvm_vcpu_release, .unlocked_ioctl = kvm_vcpu_ioctl, .mmap = kvm_vcpu_mmap,
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 42/47 mm: asi: Annotation of PERCPU variables to be nonsensitive
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
The heart of ASI is to diffrentiate between sensitive and non-sensitive data access. This commit marks certain static PERCPU variables as not sensitive.
Some static variables are accessed frequently and therefore would cause many ASI exits. The frequency of these accesses is monitored by tracing asi_exits and analyzing the accessed addresses. Many of these variables don't contain sensitive information and can therefore be mapped into the global ASI region. This commit modifiedDEFINE_PER_CPU --> DEFINE_PER_CPU_ASI_NOT_SENSITIVE to variables which are frequenmtly-accessed yet not sensitive variables.
The end result is a very significant reduction in ASI exits on real benchmarks.
--- arch/x86/events/core.c | 2 +- arch/x86/events/intel/bts.c | 2 +- arch/x86/events/perf_event.h | 2 +- arch/x86/include/asm/asi.h | 2 +- arch/x86/include/asm/current.h | 2 +- arch/x86/include/asm/debugreg.h | 2 +- arch/x86/include/asm/desc.h | 2 +- arch/x86/include/asm/fpu/api.h | 2 +- arch/x86/include/asm/hardirq.h | 2 +- arch/x86/include/asm/hw_irq.h | 2 +- arch/x86/include/asm/percpu.h | 2 +- arch/x86/include/asm/preempt.h | 2 +- arch/x86/include/asm/processor.h | 12 ++++++------ arch/x86/include/asm/smp.h | 2 +- arch/x86/include/asm/tlbflush.h | 4 ++-- arch/x86/include/asm/topology.h | 2 +- arch/x86/kernel/apic/apic.c | 2 +- arch/x86/kernel/apic/x2apic_cluster.c | 6 +++--- arch/x86/kernel/cpu/common.c | 12 ++++++------ arch/x86/kernel/fpu/core.c | 2 +- arch/x86/kernel/hw_breakpoint.c | 2 +- arch/x86/kernel/irq.c | 2 +- arch/x86/kernel/irqinit.c | 2 +- arch/x86/kernel/nmi.c | 6 +++--- arch/x86/kernel/process.c | 4 ++-- arch/x86/kernel/setup_percpu.c | 4 ++-- arch/x86/kernel/smpboot.c | 3 ++- arch/x86/kernel/tsc.c | 2 +- arch/x86/kvm/x86.c | 2 +- arch/x86/kvm/x86.h | 2 +- arch/x86/mm/asi.c | 2 +- arch/x86/mm/init.c | 2 +- arch/x86/mm/tlb.c | 2 +- include/asm-generic/irq_regs.h | 2 +- include/linux/arch_topology.h | 2 +- include/linux/hrtimer.h | 2 +- include/linux/interrupt.h | 2 +- include/linux/kernel_stat.h | 4 ++-- include/linux/prandom.h | 2 +- kernel/events/core.c | 6 +++--- kernel/irq_work.c | 6 +++--- kernel/rcu/tree.c | 2 +- kernel/sched/core.c | 6 +++--- kernel/sched/cpufreq.c | 3 ++- kernel/sched/cputime.c | 2 +- kernel/sched/sched.h | 21 +++++++++++---------- kernel/sched/topology.c | 14 +++++++------- kernel/smp.c | 7 ++++--- kernel/softirq.c | 2 +- kernel/time/hrtimer.c | 2 +- kernel/time/tick-common.c | 2 +- kernel/time/tick-internal.h | 4 ++-- kernel/time/tick-sched.c | 2 +- kernel/time/timer.c | 2 +- kernel/trace/trace.c | 2 +- kernel/trace/trace_preemptirq.c | 2 +- kernel/watchdog.c | 12 ++++++------ lib/irq_regs.c | 2 +- lib/random32.c | 3 ++- virt/kvm/kvm_main.c | 2 +- 60 files changed, 112 insertions(+), 107 deletions(-)
arch/x86/events/core.c +1 -1
@@ -47,7 +47,7 @@ struct x86_pmu x86_pmu __asi_not_sensitive_readmostly;static struct pmu pmu; -DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct cpu_hw_events, cpu_hw_events) = { .enabled =1 , .pmu = &pmu, };
arch/x86/events/intel/bts.c +1 -1
@@ -36,7 +36,7 @@ enum { BTS_STATE_ACTIVE, }; -static DEFINE_PER_CPU(struct bts_ctx, bts_ctx); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct bts_ctx, bts_ctx);#define BTS_RECORD_SIZE 24#define BTS_SAFETY_MARGIN 4080
arch/x86/events/perf_event.h +1 -1
@@ -1036,7 +1036,7 @@ static inline bool x86_pmu_has_lbr_callstack(void) x86_pmu.lbr_sel_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT ] >0 ; } -DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct cpu_hw_events, cpu_hw_events);int x86_perf_event_set_period (struct perf_event *event);
arch/x86/include/asm/asi.h +1 -1
@@ -52,7 +52,7 @@ struct asi_pgtbl_pool {uint count; }; -DECLARE_PER_CPU_ALIGNED(struct asi_state, asi_cpu_state); +DECLARE_PER_CPU_ALIGNED_ASI_NOT_SENSITIVE(struct asi_state, asi_cpu_state);extern pgd_t asi_global_nonsensitive_pgd[];
arch/x86/include/asm/current.h +1 -1
@@ -8,7 +8,7 @@#ifndef __ASSEMBLY__struct task_struct ; -DECLARE_PER_CPU(struct task_struct *, current_task); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct task_struct *, current_task); static __always_inline struct task_struct *get_current(void) {
arch/x86/include/asm/debugreg.h +1 -1
@@ -6,7 +6,7 @@#include <linux/bug.h> #include <uapi/asm/debugreg.h> -DECLARE_PER_CPU(unsigned long, cpu_dr7); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, cpu_dr7);#ifndef CONFIG_PARAVIRT_XXL /*
arch/x86/include/asm/desc.h +1 -1
@@ -298,7 +298,7 @@ static inline void native_load_tls(struct thread_struct *t, unsigned int cpu) gdt[GDT_ENTRY_TLS_MIN + i] = t->tls_array[i]; } -DECLARE_PER_CPU (bool, __tss_limit_invalid); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE (bool, __tss_limit_invalid);static inline void force_reload_TR (void ) {
arch/x86/include/asm/fpu/api.h +1 -1
@@ -121,7 +121,7 @@ static inline void fpstate_init_soft(struct swregs_state *soft) {}#endif /* State tracking */ -DECLARE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct fpu *, fpu_fpregs_owner_ctx);/* Process cleanup */ #ifdef CONFIG_X86_64
arch/x86/include/asm/hardirq.h +1 -1
@@ -46,7 +46,7 @@ typedef struct {#endif }____cacheline_aligned irq_cpustat_t; -DECLARE_PER_CPU_SHARED_ALIGNED (irq_cpustat_t, irq_stat); +DECLARE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE (irq_cpustat_t, irq_stat);#define __ARCH_IRQ_STAT
arch/x86/include/asm/hw_irq.h +1 -1
@@ -128,7 +128,7 @@ extern char spurious_entries_start[];#define VECTOR_RETRIGGERED ((void *)-2L)typedef struct irq_desc *vector_irq_t [NR_VECTORS ]; -DECLARE_PER_CPU (vector_irq_t, vector_irq); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE (vector_irq_t, vector_irq);#endif /* !ASSEMBLY_ */
arch/x86/include/asm/percpu.h +1 -1
@@ -390,7 +390,7 @@ static inline bool x86_this_cpu_variable_test_bit(int nr,#include <asm-generic/percpu.h> /* We can use this directly for local CPU (faster). */ -DECLARE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, this_cpu_off);#endif /* !__ASSEMBLY__ */
arch/x86/include/asm/preempt.h +1 -1
@@ -7,7 +7,7 @@#include <linux/thread_info.h> #include <linux/static_call_types.h> -DECLARE_PER_CPU (int, __preempt_count); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE (int, __preempt_count);/* We use the MSB mostly because its available */ #define PREEMPT_NEED_RESCHED 0x80000000
arch/x86/include/asm/processor.h +6 -6
@@ -417,14 +417,14 @@ struct tss_struct {struct x86_io_bitmap io_bitmap; }__aligned(PAGE_SIZE) ; -DECLARE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw); +DECLARE_PER_CPU_PAGE_ALIGNED_ASI_NOT_SENSITIVE(struct tss_struct, cpu_tss_rw);/* Per CPU interrupt stacks */ struct irq_stack {char stack[IRQ_STACK_SIZE ]; }__aligned(IRQ_STACK_SIZE) ; -DECLARE_PER_CPU(unsigned long, cpu_current_top_of_stack); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, cpu_current_top_of_stack);#ifdef CONFIG_X86_64 struct fixed_percpu_data {
@@ -448,8 +448,8 @@ static inline unsigned long cpu_kernelmode_gs_base(int cpu)return (unsigned long )per_cpu (fixed_percpu_data.gs_base, cpu); } -DECLARE_PER_CPU(void *, hardirq_stack_ptr); -DECLARE_PER_CPU (bool, hardirq_stack_inuse); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(void *, hardirq_stack_ptr); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE (bool, hardirq_stack_inuse); extern asmlinkage void ignore_sysret(void);/* Save actual FS/GS selectors and bases to current->thread */
@@ -458,8 +458,8 @@ void current_save_fsgs(void);#ifdef CONFIG_STACKPROTECTOR DECLARE_PER_CPU(unsigned long, __stack_chk_guard);#endif -DECLARE_PER_CPU(struct irq_stack *, hardirq_stack_ptr); -DECLARE_PER_CPU(struct irq_stack *, softirq_stack_ptr); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct irq_stack *, hardirq_stack_ptr); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct irq_stack *, softirq_stack_ptr);#endif /* !X86_64 */ struct perf_event ;
arch/x86/include/asm/smp.h +1 -1
@@ -19,7 +19,7 @@ DECLARE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);DECLARE_PER_CPU_READ_MOSTLY (cpumask_var_t, cpu_l2c_shared_map);DECLARE_PER_CPU_READ_MOSTLY (u16, cpu_llc_id);DECLARE_PER_CPU_READ_MOSTLY (u16, cpu_l2c_id); -DECLARE_PER_CPU_READ_MOSTLY (int, cpu_number); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE (int, cpu_number);static inline struct cpumask *cpu_llc_shared_mask (int cpu) {
arch/x86/include/asm/tlbflush.h +2 -2
@@ -151,7 +151,7 @@ struct tlb_state {*/ struct tlb_context ctxs[TLB_NR_DYN_ASIDS ]; }; -DECLARE_PER_CPU_ALIGNED(struct tlb_state, cpu_tlbstate); +DECLARE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE(struct tlb_state, cpu_tlbstate);struct tlb_state_shared {/*
@@ -171,7 +171,7 @@ struct tlb_state_shared {*/ bool is_lazy; }; -DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state_shared, cpu_tlbstate_shared); +DECLARE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE(struct tlb_state_shared, cpu_tlbstate_shared);bool nmi_uaccess_okay (void );#define nmi_uaccess_okay nmi_uaccess_okay
arch/x86/include/asm/topology.h +1 -1
@@ -203,7 +203,7 @@ DECLARE_STATIC_KEY_FALSE(arch_scale_freq_key);#define arch_scale_freq_invariant () static_branch_likely(&arch_scale_freq_key) -DECLARE_PER_CPU(unsigned long, arch_freq_scale); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, arch_freq_scale);static inline long arch_scale_freq_capacity (int cpu) {
arch/x86/kernel/apic/apic.c +1 -1
@@ -548,7 +548,7 @@ static struct clock_event_device lapic_clockevent = { .rating = 100, .irq = -1, }; -static DEFINE_PER_CPU(struct clock_event_device, lapic_events); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct clock_event_device, lapic_events);static const struct x86_cpu_id deadline_match[]__initconst = {X86_MATCH_INTEL_FAM6_MODEL_STEPPINGS (HASWELL_X ,X86_STEPPINGS (0x2 ,0x2 ),0x3a ),/* EP */
arch/x86/kernel/apic/x2apic_cluster.c +3 -3
@@ -20,10 +20,10 @@ struct cluster_mask {* x86_cpu_to_logical_apicid for all online cpus in a sequential way. * Using per cpu variable would cost one cache line per cpu. */ -static u32 *x86_cpu_to_logical_apicid__read_mostly ; +static u32 *x86_cpu_to_logical_apicid __asi_not_sensitive_readmostly; -static DEFINE_PER_CPU(cpumask_var_t, ipi_mask); -static DEFINE_PER_CPU_READ_MOSTLY(struct cluster_mask *, cluster_masks); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(cpumask_var_t, ipi_mask); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct cluster_mask *, cluster_masks);static struct cluster_mask *cluster_hotplug_mask;static int x2apic_acpi_madt_oem_check (char *oem_id,char *oem_table_id)
arch/x86/kernel/cpu/common.c +6 -6
@@ -1775,17 +1775,17 @@ EXPORT_PER_CPU_SYMBOL_GPL(fixed_percpu_data);* The following percpu variables are hot. Align current_task to * cacheline size such that they fall in the same cacheline. */ -DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned = +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct task_struct *, current_task) ____cacheline_aligned = &init_task;EXPORT_PER_CPU_SYMBOL (current_task); -DEFINE_PER_CPU(void *, hardirq_stack_ptr); -DEFINE_PER_CPU (bool, hardirq_stack_inuse); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(void *, hardirq_stack_ptr); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE (bool, hardirq_stack_inuse); -DEFINE_PER_CPU (int, __preempt_count) =INIT_PREEMPT_COUNT ; +DEFINE_PER_CPU_ASI_NOT_SENSITIVE (int, __preempt_count) =INIT_PREEMPT_COUNT ;EXPORT_PER_CPU_SYMBOL (__preempt_count); -DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) = TOP_OF_INIT_STACK; +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, cpu_current_top_of_stack) = TOP_OF_INIT_STACK;/* May not be marked __init: used by software suspend */ void syscall_init (void )
@@ -1826,7 +1826,7 @@ void syscall_init(void)#else /* CONFIG_X86_64 */ -DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task; +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct task_struct *, current_task) = &init_task;EXPORT_PER_CPU_SYMBOL (current_task);DEFINE_PER_CPU (int, __preempt_count) =INIT_PREEMPT_COUNT ;EXPORT_PER_CPU_SYMBOL (__preempt_count);
arch/x86/kernel/fpu/core.c +1 -1
@@ -57,7 +57,7 @@ static DEFINE_PER_CPU(bool, in_kernel_fpu);/* * Track which context is using the FPU on the CPU: */ -DEFINE_PER_CPU(struct fpu *, fpu_fpregs_owner_ctx); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct fpu *, fpu_fpregs_owner_ctx);struct kmem_cache *fpstate_cachep;
arch/x86/kernel/hw_breakpoint.c +1 -1
@@ -36,7 +36,7 @@#include <asm/tlbflush.h> /* Per cpu debug control register value */ -DEFINE_PER_CPU(unsigned long, cpu_dr7); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, cpu_dr7);EXPORT_PER_CPU_SYMBOL (cpu_dr7);/* Per cpu debug address registers values */
arch/x86/kernel/irq.c +1 -1
@@ -26,7 +26,7 @@#define CREATE_TRACE_POINTS #include <asm/trace/irq_vectors.h> -DEFINE_PER_CPU_SHARED_ALIGNED (irq_cpustat_t, irq_stat); +DEFINE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE (irq_cpustat_t, irq_stat);EXPORT_PER_CPU_SYMBOL (irq_stat);atomic_t irq_err_count;
arch/x86/kernel/irqinit.c +1 -1
@@ -46,7 +46,7 @@* (these are usually mapped into the 0x30-0xff vector range) */ -DEFINE_PER_CPU(vector_irq_t, vector_irq) = { +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(vector_irq_t, vector_irq) = { [0 ... NR_VECTORS - 1] = VECTOR_UNUSED, };
arch/x86/kernel/nmi.c +3 -3
@@ -469,9 +469,9 @@ enum nmi_states {NMI_EXECUTING , NMI_LATCHED, }; -static DEFINE_PER_CPU(enum nmi_states, nmi_state); -static DEFINE_PER_CPU(unsigned long, nmi_cr2); -static DEFINE_PER_CPU(unsigned long, nmi_dr7); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(enum nmi_states, nmi_state); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, nmi_cr2); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, nmi_dr7);DEFINE_IDTENTRY_RAW (exc_nmi ) {
arch/x86/kernel/process.c +2 -2
@@ -56,7 +56,7 @@* section. Since TSS's are completely CPU-local, we want them * on exact cacheline boundaries, to eliminate cacheline ping-pong. */ -__visible DEFINE_PER_CPU_PAGE_ALIGNED (struct tss_struct ,cpu_tss_rw ) = { +__visible DEFINE_PER_CPU_PAGE_ALIGNED_ASI_NOT_SENSITIVE (struct tss_struct ,cpu_tss_rw ) = { .x86_tss = {/* * .sp0 is only used when entering ring 0 from a lower
@@ -77,7 +77,7 @@ __visible DEFINE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw) = { };EXPORT_PER_CPU_SYMBOL (cpu_tss_rw); -DEFINE_PER_CPU (bool, __tss_limit_invalid); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE (bool, __tss_limit_invalid);EXPORT_PER_CPU_SYMBOL_GPL (__tss_limit_invalid);void __init arch_task_cache_init (void )
arch/x86/kernel/setup_percpu.c +2 -2
@@ -23,7 +23,7 @@#include <asm/cpu.h> #include <asm/stackprotector.h> -DEFINE_PER_CPU_READ_MOSTLY (int, cpu_number); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE (int, cpu_number);EXPORT_PER_CPU_SYMBOL (cpu_number);#ifdef CONFIG_X86_64
@@ -32,7 +32,7 @@ EXPORT_PER_CPU_SYMBOL(cpu_number);#define BOOT_PERCPU_OFFSET 0#endif -DEFINE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET; +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET;EXPORT_PER_CPU_SYMBOL (this_cpu_off);unsigned long __per_cpu_offset[NR_CPUS ]__ro_after_init = {
arch/x86/kernel/smpboot.c +2 -1
@@ -2224,7 +2224,8 @@ static void disable_freq_invariance_workfn(struct work_struct *work) static DECLARE_WORK(disable_freq_invariance_work, disable_freq_invariance_workfn); -DEFINE_PER_CPU (unsigned long ,arch_freq_scale ) =SCHED_CAPACITY_SCALE ; +DEFINE_PER_CPU_ASI_NOT_SENSITIVE (unsigned long ,arch_freq_scale ) = +SCHED_CAPACITY_SCALE ;void arch_scale_freq_tick (void ) {
arch/x86/kernel/tsc.c +1 -1
@@ -59,7 +59,7 @@ struct cyc2ns { };/* fits one cacheline */ -static DEFINE_PER_CPU_ALIGNED(struct cyc2ns, cyc2ns); +static DEFINE_PER_CPU_ALIGNED_ASI_NOT_SENSITIVE(struct cyc2ns, cyc2ns);static int __init tsc_early_khz_setup (char *buf) {
arch/x86/kvm/x86.c +1 -1
@@ -8523,7 +8523,7 @@ static void kvm_timer_init(void) kvmclock_cpu_online, kvmclock_cpu_down_prep); } -DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct kvm_vcpu *, current_vcpu);EXPORT_PER_CPU_SYMBOL_GPL (current_vcpu);int kvm_is_in_guest (void )
arch/x86/kvm/x86.h +1 -1
@@ -392,7 +392,7 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm)return kvm->arch.cstate_in_guest; } -DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct kvm_vcpu *, current_vcpu);static inline void kvm_before_interrupt (struct kvm_vcpu *vcpu) {
arch/x86/mm/asi.c +1 -1
@@ -20,7 +20,7 @@ static struct asi_class asi_class[ASI_MAX_NUM] __asi_not_sensitive; static DEFINE_SPINLOCK(asi_class_lock __asi_not_sensitive); -DEFINE_PER_CPU_ALIGNED (struct asi_state , asi_cpu_state); +DEFINE_PER_CPU_ALIGNED_ASI_NOT_SENSITIVE (struct asi_state , asi_cpu_state);EXPORT_PER_CPU_SYMBOL_GPL (asi_cpu_state);__aligned(PAGE_SIZE) pgd_t asi_global_nonsensitive_pgd[PTRS_PER_PGD ];
arch/x86/mm/init.c +1 -1
@@ -1025,7 +1025,7 @@ void __init zone_sizes_init(void)free_area_init (max_zone_pfns); } -__visible DEFINE_PER_CPU_ALIGNED (struct tlb_state ,cpu_tlbstate ) = { +__visible DEFINE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE (struct tlb_state ,cpu_tlbstate ) = { .loaded_mm = &init_mm, .next_asid =1 , .cr4 = ~0UL ,/* fail hard if we screw up cr4 shadow initialization */
arch/x86/mm/tlb.c +1 -1
@@ -972,7 +972,7 @@ static bool tlb_is_not_lazy(int cpu) static DEFINE_PER_CPU(cpumask_t, flush_tlb_mask); -DEFINE_PER_CPU_SHARED_ALIGNED (struct tlb_state_shared , cpu_tlbstate_shared); +DEFINE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE (struct tlb_state_shared , cpu_tlbstate_shared);EXPORT_PER_CPU_SYMBOL (cpu_tlbstate_shared); STATIC_NOPV void native_flush_tlb_multi(const struct cpumask *cpumask,
include/asm-generic/irq_regs.h +1 -1
@@ -14,7 +14,7 @@* Per-cpu current frame pointer - the location of the last exception frame on * the stack */ -DECLARE_PER_CPU(struct pt_regs *, __irq_regs); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct pt_regs *, __irq_regs);static inline struct pt_regs *get_irq_regs (void ) {
include/linux/arch_topology.h +1 -1
@@ -23,7 +23,7 @@ static inline unsigned long topology_get_cpu_scale(int cpu)void topology_set_cpu_scale (unsigned int cpu,unsigned long capacity); -DECLARE_PER_CPU(unsigned long, arch_freq_scale); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, arch_freq_scale);static inline unsigned long topology_get_freq_scale (int cpu) {
include/linux/hrtimer.h +1 -1
@@ -355,7 +355,7 @@ static inline void timerfd_clock_was_set(void) { }static inline void timerfd_resume (void ) { }#endif -DECLARE_PER_CPU(struct tick_device, tick_cpu_device); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct tick_device, tick_cpu_device);#ifdef CONFIG_PREEMPT_RT void hrtimer_cancel_wait_running (const struct hrtimer *timer);
include/linux/interrupt.h +1 -1
@@ -554,7 +554,7 @@ extern void __raise_softirq_irqoff(unsigned int nr);extern void raise_softirq_irqoff (unsigned int nr);extern void raise_softirq (unsigned int nr); -DECLARE_PER_CPU(struct task_struct *, ksoftirqd); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct task_struct *, ksoftirqd);static inline struct task_struct *this_cpu_ksoftirqd (void ) {
include/linux/kernel_stat.h +2 -2
@@ -40,8 +40,8 @@ struct kernel_stat {unsigned int softirqs[NR_SOFTIRQS ]; }; -DECLARE_PER_CPU(struct kernel_stat, kstat); -DECLARE_PER_CPU(struct kernel_cpustat, kernel_cpustat); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct kernel_stat, kstat); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct kernel_cpustat, kernel_cpustat);/* Must have preemption disabled for this to be meaningful. */ #define kstat_this_cpu this_cpu_ptr(&kstat)
include/linux/prandom.h +1 -1
@@ -16,7 +16,7 @@ void prandom_bytes(void *buf, size_t nbytes);void prandom_seed (u32 seed);void prandom_reseed_late (void ); -DECLARE_PER_CPU(unsigned long, net_rand_noise); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, net_rand_noise);#define PRANDOM_ADD_NOISE (a, b, c, d) \ prandom_u32_add_noise((unsigned long)(a), (unsigned long)(b), \
kernel/events/core.c +3 -3
@@ -1207,7 +1207,7 @@ void perf_pmu_enable(struct pmu *pmu) pmu->pmu_enable (pmu); } -static DEFINE_PER_CPU(struct list_head, active_ctx_list); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct list_head, active_ctx_list);/* * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
@@ -4007,8 +4007,8 @@ do { \return div64_u64 (dividend, divisor); } -static DEFINE_PER_CPU(int, perf_throttled_count); -static DEFINE_PER_CPU(u64, perf_throttled_seq); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(int, perf_throttled_count); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(u64, perf_throttled_seq);static void perf_adjust_period (struct perf_event *event,u64 nsec,u64 count,bool disable) {
kernel/irq_work.c +3 -3
@@ -22,9 +22,9 @@#include <asm/processor.h> #include <linux/kasan.h> -static DEFINE_PER_CPU(struct llist_head, raised_list); -static DEFINE_PER_CPU(struct llist_head, lazy_list); -static DEFINE_PER_CPU(struct task_struct *, irq_workd); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct llist_head, raised_list); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct llist_head, lazy_list); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct task_struct *, irq_workd);static void wake_irq_workd (void ) {
kernel/rcu/tree.c +1 -1
@@ -74,7 +74,7 @@/* Data structures. */ -static DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_data, rcu_data) = { +static DEFINE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE(struct rcu_data, rcu_data) = { .dynticks_nesting =1 , .dynticks_nmi_nesting =DYNTICK_IRQ_NONIDLE , .dynticks =ATOMIC_INIT (1 ),
kernel/sched/core.c +3 -3
@@ -43,7 +43,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(sched_util_est_cfs_tp);EXPORT_TRACEPOINT_SYMBOL_GPL (sched_util_est_se_tp);EXPORT_TRACEPOINT_SYMBOL_GPL (sched_update_nr_running_tp); -DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); +DEFINE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE(struct rq, runqueues);#ifdef CONFIG_SCHED_DEBUG /*
@@ -5104,8 +5104,8 @@ void sched_exec(void)#endif -DEFINE_PER_CPU(struct kernel_stat, kstat); -DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct kernel_stat, kstat); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct kernel_cpustat, kernel_cpustat);EXPORT_PER_CPU_SYMBOL (kstat);EXPORT_PER_CPU_SYMBOL (kernel_cpustat);
kernel/sched/cpufreq.c +2 -1
@@ -9,7 +9,8 @@#include "sched.h" -DEFINE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct update_util_data __rcu *, + cpufreq_update_util_data);/** * cpufreq_add_update_util_hook - Populate the CPU's update_util_data pointer.
kernel/sched/cputime.c +1 -1
@@ -17,7 +17,7 @@* task when irq is in progress while we read rq->clock. That is a worthy * compromise in place of having locks on each irq in account_system_time. */ -DEFINE_PER_CPU(struct irqtime, cpu_irqtime); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct irqtime, cpu_irqtime); static int __asi_not_sensitive sched_clock_irqtime;
kernel/sched/sched.h +11 -10
@@ -1360,7 +1360,7 @@ static inline void update_idle_core(struct rq *rq)static inline void update_idle_core (struct rq *rq) { }#endif -DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); +DECLARE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE(struct rq, runqueues);#define cpu_rq (cpu) (&per_cpu(runqueues, (cpu)))#define this_rq () this_cpu_ptr(&runqueues)
@@ -1760,13 +1760,13 @@ static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)return sd; } -DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc); -DECLARE_PER_CPU (int, sd_llc_size); -DECLARE_PER_CPU (int, sd_llc_id); -DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared); -DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa); -DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing); -DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct sched_domain __rcu *, sd_llc); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE (int, sd_llc_size); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE (int, sd_llc_id); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct sched_domain_shared __rcu *, sd_llc_shared); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct sched_domain __rcu *, sd_numa); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct sched_domain __rcu *, sd_asym_packing); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct sched_domain __rcu *, sd_asym_cpucapacity);extern struct static_key_false sched_asym_cpucapacity;struct sched_group_capacity {
@@ -2753,7 +2753,7 @@ struct irqtime {struct u64_stats_sync sync; }; -DECLARE_PER_CPU(struct irqtime, cpu_irqtime); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct irqtime, cpu_irqtime);/* * Returns the irqtime minus the softirq time computed by ksoftirqd.
@@ -2776,7 +2776,8 @@ static inline u64 irq_time_read(int cpu)#endif /* CONFIG_IRQ_TIME_ACCOUNTING */ #ifdef CONFIG_CPU_FREQ -DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct update_util_data __rcu *, + cpufreq_update_util_data);/** * cpufreq_update_util - Take a note about CPU utilization changes.
kernel/sched/topology.c +7 -7
@@ -641,13 +641,13 @@ static void destroy_sched_domains(struct sched_domain *sd)* the cpumask of the domain), this allows us to quickly tell if * two CPUs are in the same cache domain, see cpus_share_cache(). */ -DEFINE_PER_CPU(struct sched_domain __rcu *, sd_llc); -DEFINE_PER_CPU (int, sd_llc_size); -DEFINE_PER_CPU (int, sd_llc_id); -DEFINE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared); -DEFINE_PER_CPU(struct sched_domain __rcu *, sd_numa); -DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing); -DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct sched_domain __rcu *, sd_llc); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE (int, sd_llc_size); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE (int, sd_llc_id); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct sched_domain_shared __rcu *, sd_llc_shared); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct sched_domain __rcu *, sd_numa); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct sched_domain __rcu *, sd_asym_packing); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct sched_domain __rcu *, sd_asym_cpucapacity);DEFINE_STATIC_KEY_FALSE (sched_asym_cpucapacity);static void update_top_cache_domain (int cpu)
kernel/smp.c +4 -3
@@ -92,9 +92,10 @@ struct call_function_data {cpumask_var_t cpumask_ipi; }; -static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data); +static DEFINE_PER_CPU_ALIGNED_ASI_NOT_SENSITIVE(struct call_function_data, cfd_data); -static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue); +static DEFINE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE(struct llist_head, + call_single_queue);static void flush_smp_call_function_queue (bool warn_cpu_offline);
@@ -464,7 +465,7 @@ static __always_inline void csd_unlock(struct __call_single_data *csd)smp_store_release (&csd->node.u_flags,0 ); } -static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data); +static DEFINE_PER_CPU_SHARED_ALIGNED_ASI_NOT_SENSITIVE(call_single_data_t, csd_data);void __smp_call_single_queue (int cpu,struct llist_node *node) {
kernel/softirq.c +1 -1
@@ -59,7 +59,7 @@ EXPORT_PER_CPU_SYMBOL(irq_stat); static struct softirq_action softirq_vec[NR_SOFTIRQS] __asi_not_sensitive____cacheline_aligned ; -DEFINE_PER_CPU(struct task_struct *, ksoftirqd); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct task_struct *, ksoftirqd);const char *const softirq_to_name[NR_SOFTIRQS ] = {"HI" ,"TIMER" ,"NET_TX" ,"NET_RX" ,"BLOCK" ,"IRQ_POLL" ,
kernel/time/hrtimer.c +1 -1
@@ -65,7 +65,7 @@* to reach a base using a clockid, hrtimer_clockid_to_base() * is used to convert from clockid to the proper hrtimer_base_type. */ -DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct hrtimer_cpu_base, hrtimer_bases) = { .lock =__RAW_SPIN_LOCK_UNLOCKED (hrtimer_bases.lock), .clock_base =
kernel/time/tick-common.c +1 -1
@@ -25,7 +25,7 @@/* * Tick devices */ -DEFINE_PER_CPU(struct tick_device, tick_cpu_device); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct tick_device, tick_cpu_device);/* * Tick next event: keeps track of the tick time. It's updated by the * CPU which handles the tick and protected by jiffies_lock. There is
kernel/time/tick-internal.h +2 -2
@@ -13,7 +13,7 @@# define TICK_DO_TIMER_NONE -1# define TICK_DO_TIMER_BOOT -2 -DECLARE_PER_CPU(struct tick_device, tick_cpu_device); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct tick_device, tick_cpu_device);extern ktime_t tick_next_period;extern int tick_do_timer_cpu;
@@ -161,7 +161,7 @@ static inline void timers_update_nohz(void) { }#define tick_nohz_active (0)#endif -DECLARE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases); +DECLARE_PER_CPU_ASI_NOT_SENSITIVE(struct hrtimer_cpu_base, hrtimer_bases);extern u64 get_next_timer_interrupt (unsigned long basej,u64 basem);void timer_clear_idle (void );
kernel/time/tick-sched.c +1 -1
@@ -36,7 +36,7 @@/* * Per-CPU nohz control structure */ -static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct tick_sched, tick_cpu_sched);struct tick_sched *tick_get_tick_sched (int cpu) {
kernel/time/timer.c +1 -1
@@ -212,7 +212,7 @@ struct timer_base {struct hlist_head vectors[WHEEL_SIZE ]; }____cacheline_aligned ; -static DEFINE_PER_CPU(struct timer_base, timer_bases[NR_BASES]); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct timer_base, timer_bases[NR_BASES]);#ifdef CONFIG_NO_HZ_COMMON
kernel/trace/trace.c +1 -1
@@ -106,7 +106,7 @@ dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)* tracing is active, only save the comm when a trace event * occurred. */ -static DEFINE_PER_CPU(bool, trace_taskinfo_save); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(bool, trace_taskinfo_save);/* * Kill all tracing for good (never come back).
kernel/trace/trace_preemptirq.c +1 -1
@@ -17,7 +17,7 @@#ifdef CONFIG_TRACE_IRQFLAGS /* Per-cpu variable to prevent redundant calls when IRQs already off */ -static DEFINE_PER_CPU(int, tracing_irq_cpu); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(int, tracing_irq_cpu);/* * Like trace_hardirqs_on() but without the lockdep invocation. This is
kernel/watchdog.c +6 -6
@@ -174,13 +174,13 @@ static bool softlockup_initialized __read_mostly;static u64 __read_mostly sample_period;/* Timestamp taken after the last successful reschedule. */ -static DEFINE_PER_CPU(unsigned long, watchdog_touch_ts); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, watchdog_touch_ts);/* Timestamp of the last softlockup report. */ -static DEFINE_PER_CPU(unsigned long, watchdog_report_ts); -static DEFINE_PER_CPU(struct hrtimer, watchdog_hrtimer); -static DEFINE_PER_CPU(bool, softlockup_touch_sync); -static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts); -static DEFINE_PER_CPU(unsigned long, hrtimer_interrupts_saved); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, watchdog_report_ts); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct hrtimer, watchdog_hrtimer); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(bool, softlockup_touch_sync); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, hrtimer_interrupts); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, hrtimer_interrupts_saved);static unsigned long soft_lockup_nmi_warn;static int __init nowatchdog_setup (char *str)
lib/irq_regs.c +1 -1
@@ -9,6 +9,6 @@#include <asm/irq_regs.h> #ifndef ARCH_HAS_OWN_IRQ_REGS -DEFINE_PER_CPU(struct pt_regs *, __irq_regs); +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct pt_regs *, __irq_regs);EXPORT_PER_CPU_SYMBOL (__irq_regs);#endif
lib/random32.c +2 -1
@@ -339,7 +339,8 @@ struct siprand_state { }; static DEFINE_PER_CPU(struct siprand_state, net_rand_state) __latent_entropy; -DEFINE_PER_CPU(unsigned long, net_rand_noise); +/* TODO(oweisse): Is this entropy sensitive?? */ +DEFINE_PER_CPU_ASI_NOT_SENSITIVE(unsigned long, net_rand_noise);EXPORT_PER_CPU_SYMBOL (net_rand_noise);/*
virt/kvm/kvm_main.c +1 -1
@@ -110,7 +110,7 @@ static atomic_t hardware_enable_failed;static struct kmem_cache *kvm_vcpu_cache;static __read_mostly struct preempt_ops kvm_preempt_ops; -static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_running_vcpu); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct kvm_vcpu *, kvm_running_vcpu);struct dentry *kvm_debugfs_dir;EXPORT_SYMBOL_GPL (kvm_debugfs_dir);
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 43/47 mm: asi: Annotation of dynamic variables to be nonsensitive
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
The heart of ASI is to diffrentiate between sensitive and non-sensitive data access. This commit marks certain dynamic allocations as not sensitive.
Some dynamic variables are accessed frequently and therefore would cause many ASI exits. The frequency of these accesses is monitored by tracing asi_exits and analyzing the accessed addresses. Many of these variables don't contain sensitive information and can therefore be mapped into the global ASI region. This commit adds GFP_LOCAL/GLOBAL_NONSENSITIVE attributes to these frequenmtly-accessed yet not sensitive variables. The end result is a very significant reduction in ASI exits on real benchmarks.
--- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kernel/apic/x2apic_cluster.c | 2 +- arch/x86/kvm/cpuid.c | 4 ++- arch/x86/kvm/lapic.c | 9 ++++--- arch/x86/kvm/mmu/mmu.c | 7 ++++++ arch/x86/kvm/vmx/vmx.c | 6 +++-- arch/x86/kvm/x86.c | 8 +++--- fs/binfmt_elf.c | 2 +- fs/eventfd.c | 2 +- fs/eventpoll.c | 10 +++++--- fs/exec.c | 2 ++ fs/file.c | 3 ++- fs/timerfd.c | 2 +- include/linux/kvm_host.h | 2 +- include/linux/kvm_types.h | 3 +++ kernel/cgroup/cgroup.c | 4 +-- kernel/events/core.c | 15 +++++++---- kernel/exit.c | 2 ++ kernel/fork.c | 36 +++++++++++++++++++++------ kernel/rcu/srcutree.c | 3 ++- kernel/sched/core.c | 6 +++-- kernel/sched/cpuacct.c | 8 +++--- kernel/sched/fair.c | 3 ++- kernel/sched/topology.c | 14 +++++++---- kernel/smp.c | 17 +++++++------ kernel/trace/ring_buffer.c | 5 ++-- kernel/tracepoint.c | 2 +- lib/radix-tree.c | 6 ++--- mm/memcontrol.c | 7 +++--- mm/util.c | 3 ++- mm/vmalloc.c | 3 ++- net/core/skbuff.c | 2 +- net/core/sock.c | 2 +- virt/kvm/coalesced_mmio.c | 2 +- virt/kvm/eventfd.c | 5 ++-- virt/kvm/kvm_main.c | 12 ++++++--- 36 files changed, 148 insertions(+), 74 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -1
@@ -1562,7 +1562,8 @@ static inline void kvm_ops_static_call_update(void)#define __KVM_HAVE_ARCH_VM_ALLOCstatic inline struct kvm *kvm_arch_alloc_vm (void ) { -return __vmalloc (kvm_x86_ops.vm_size,GFP_KERNEL_ACCOUNT | __GFP_ZERO); +return __vmalloc (kvm_x86_ops.vm_size,GFP_KERNEL_ACCOUNT | __GFP_ZERO | + __GFP_GLOBAL_NONSENSITIVE); }#define __KVM_HAVE_ARCH_VM_FREE
arch/x86/kernel/apic/x2apic_cluster.c +1 -1
@@ -144,7 +144,7 @@ static int alloc_clustermask(unsigned int cpu, int node) } cluster_hotplug_mask =kzalloc_node (sizeof (*cluster_hotplug_mask), -GFP_KERNEL , node); +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE, node);if (!cluster_hotplug_mask)return -ENOMEM ; cluster_hotplug_mask->node = node;
arch/x86/kvm/cpuid.c +3 -1
@@ -310,7 +310,9 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,if (IS_ERR (e))return PTR_ERR (e); - e2 =kvmalloc_array (cpuid->nent,sizeof (*e2),GFP_KERNEL_ACCOUNT ); + e2 =kvmalloc_array (cpuid->nent,sizeof (*e2), +GFP_KERNEL_ACCOUNT | + __GFP_LOCAL_NONSENSITIVE);if (!e2) { r = -ENOMEM ; goto out_free_cpuid;
arch/x86/kvm/lapic.c +5 -4
@@ -213,7 +213,7 @@ void kvm_recalculate_apic_map(struct kvm *kvm) new =kvzalloc (sizeof (struct kvm_apic_map ) +sizeof (struct kvm_lapic *) * ((u64 )max_id +1 ), -GFP_KERNEL_ACCOUNT ); +GFP_KERNEL_ACCOUNT | __GFP_LOCAL_NONSENSITIVE);if (!new) goto out;
@@ -993,7 +993,7 @@ bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src, *r =-1 ;if (irq->shorthand ==APIC_DEST_SELF ) { - *r =kvm_apic_set_irq (src->vcpu, irq, dest_map); + *r =kvm_apic_set_irq (src->vcpu, irq, dest_map);return true; }
@@ -2455,13 +2455,14 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns)ASSERT (vcpu !=NULL ); - apic =kzalloc (sizeof (*apic),GFP_KERNEL_ACCOUNT ); + apic =kzalloc (sizeof (*apic),GFP_KERNEL_ACCOUNT | __GFP_LOCAL_NONSENSITIVE);if (!apic) goto nomem; vcpu->arch.apic = apic; - apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT ); + apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT + | __GFP_LOCAL_NONSENSITIVE);if (!apic->regs) {printk (KERN_ERR "malloc apic regs error for vcpu %x\n" , vcpu->vcpu_id);
arch/x86/kvm/mmu/mmu.c +7 -0
@@ -5630,6 +5630,13 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO; vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +if (static_cpu_has (X86_FEATURE_ASI ) &&mm_asi_enabled (current->mm)) + vcpu->arch.mmu_shadow_page_cache.gfp_asi = + __GFP_LOCAL_NONSENSITIVE; +else + vcpu->arch.mmu_shadow_page_cache.gfp_asi =0 ; +#endif vcpu->arch.mmu = &vcpu->arch.root_mmu; vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
arch/x86/kvm/vmx/vmx.c +4 -2
@@ -2629,7 +2629,7 @@ void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)free_vmcs (loaded_vmcs->vmcs); loaded_vmcs->vmcs =NULL ;if (loaded_vmcs->msr_bitmap) -free_page ((unsigned long )loaded_vmcs->msr_bitmap); +kfree (loaded_vmcs->msr_bitmap);WARN_ON (loaded_vmcs->shadow_vmcs !=NULL ); }
@@ -2648,7 +2648,9 @@ int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)if (cpu_has_vmx_msr_bitmap ()) { loaded_vmcs->msr_bitmap = (unsigned long *) -__get_free_page (GFP_KERNEL_ACCOUNT ); +kzalloc (PAGE_SIZE , +GFP_KERNEL_ACCOUNT | + __GFP_LOCAL_NONSENSITIVE );if (!loaded_vmcs->msr_bitmap) goto out_vmcs;memset (loaded_vmcs->msr_bitmap,0xff ,PAGE_SIZE );
arch/x86/kvm/x86.c +5 -3
@@ -329,7 +329,8 @@ static struct kmem_cache *kvm_alloc_emulator_cache(void)return kmem_cache_create_usercopy ("x86_emulator" , size, __alignof__(struct x86_emulate_ctxt ), -SLAB_ACCOUNT , useroffset, +SLAB_ACCOUNT |SLAB_LOCAL_NONSENSITIVE , + useroffset, size - useroffset,NULL ); }
@@ -10969,7 +10970,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) r = -ENOMEM ; - page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_LOCAL_NONSENSITIVE);if (!page) goto fail_free_lapic; vcpu->arch.pio_data =page_address (page);
@@ -11718,7 +11719,8 @@ static int kvm_alloc_memslot_metadata(struct kvm *kvm, lpages =__kvm_mmu_slot_lpages (slot, npages, level); - linfo =kvcalloc (lpages,sizeof (*linfo),GFP_KERNEL_ACCOUNT ); + linfo =kvcalloc (lpages,sizeof (*linfo), +GFP_KERNEL_ACCOUNT | __GFP_LOCAL_NONSENSITIVE);if (!linfo) goto out_free;
fs/binfmt_elf.c +1 -1
@@ -477,7 +477,7 @@ static struct elf_phdr *load_elf_phdrs(const struct elfhdr *elf_ex,if (size ==0 || size >65536 || size >ELF_MIN_ALIGN ) goto out; - elf_phdata =kmalloc (size,GFP_KERNEL ); + elf_phdata =kmalloc (size,GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!elf_phdata) goto out;
fs/eventfd.c +1 -1
@@ -415,7 +415,7 @@ static int do_eventfd(unsigned int count, int flags)if (flags & ~EFD_FLAGS_SET )return -EINVAL ; - ctx =kmalloc (sizeof (*ctx),GFP_KERNEL ); + ctx =kmalloc (sizeof (*ctx),GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!ctx)return -ENOMEM ;
fs/eventpoll.c +6 -4
@@ -1239,7 +1239,7 @@ static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,if (unlikely (!epi))// an earlier allocation has failed return ; - pwq =kmem_cache_alloc (pwq_cache,GFP_KERNEL ); + pwq =kmem_cache_alloc (pwq_cache,GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (unlikely (!pwq)) { epq->epi =NULL ;return ;
@@ -1453,7 +1453,8 @@ static int ep_insert(struct eventpoll *ep, const struct epoll_event *event,return -ENOSPC ;percpu_counter_inc (&ep->user->epoll_watches); -if (!(epi =kmem_cache_zalloc (epi_cache,GFP_KERNEL ))) { +if (!(epi =kmem_cache_zalloc (epi_cache, +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE))) {percpu_counter_dec (&ep->user->epoll_watches);return -ENOMEM ; }
@@ -2373,11 +2374,12 @@ static int __init eventpoll_init(void)/* Allocates slab cache used to allocate "struct epitem" items */ epi_cache =kmem_cache_create ("eventpoll_epi" ,sizeof (struct epitem ), -0 ,SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_ACCOUNT ,NULL ); +0 ,SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_ACCOUNT |SLAB_GLOBAL_NONSENSITIVE ,NULL );/* Allocates slab cache used to allocate "struct eppoll_entry" */ pwq_cache =kmem_cache_create ("eventpoll_pwq" , -sizeof (struct eppoll_entry ),0 ,SLAB_PANIC |SLAB_ACCOUNT ,NULL ); +sizeof (struct eppoll_entry ),0 , +SLAB_PANIC |SLAB_ACCOUNT |SLAB_GLOBAL_NONSENSITIVE ,NULL ); ephead_cache =kmem_cache_create ("ep_head" ,sizeof (struct epitems_head ),0 ,SLAB_PANIC |SLAB_ACCOUNT ,NULL );
fs/exec.c +2 -0
@@ -1238,6 +1238,8 @@ int begin_new_exec(struct linux_binprm * bprm)struct task_struct *me = current;int retval; +/* TODO: (oweisse) unmap the stack from ASI */ +/* Once we are committed compute the creds */ retval =bprm_creds_from_file (bprm);if (retval)
fs/file.c +2 -1
@@ -117,7 +117,8 @@ static struct fdtable * alloc_fdtable(unsigned int nr)if (!fdt) goto out; fdt->max_fds = nr; - data =kvmalloc_array (nr,sizeof (struct file *),GFP_KERNEL_ACCOUNT ); + data =kvmalloc_array (nr,sizeof (struct file *), +GFP_KERNEL_ACCOUNT | __GFP_LOCAL_NONSENSITIVE);if (!data) goto out_fdt; fdt->fd = data;
fs/timerfd.c +1 -1
@@ -425,7 +425,7 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) !capable(CAP_WAKE_ALARM))return -EPERM ; - ctx =kzalloc (sizeof (*ctx),GFP_KERNEL ); + ctx =kzalloc (sizeof (*ctx),GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!ctx)return -ENOMEM ;
include/linux/kvm_host.h +1 -1
@@ -1085,7 +1085,7 @@ int kvm_arch_create_vm_debugfs(struct kvm *kvm);*/ static inline struct kvm *kvm_arch_alloc_vm (void ) { -return kzalloc (sizeof (struct kvm ),GFP_KERNEL ); +return kzalloc (sizeof (struct kvm ),GFP_KERNEL | __GFP_LOCAL_NONSENSITIVE); }#endif
include/linux/kvm_types.h +3 -0
@@ -64,6 +64,9 @@ struct gfn_to_hva_cache {struct kvm_mmu_memory_cache {int nobjs;gfp_t gfp_zero; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +gfp_t gfp_asi; +#endif struct kmem_cache *kmem_cache;void *objects[KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE ]; };
kernel/cgroup/cgroup.c +2 -2
@@ -1221,7 +1221,7 @@ static struct css_set *find_css_set(struct css_set *old_cset,if (cset)return cset; - cset =kzalloc (sizeof (*cset),GFP_KERNEL ); + cset =kzalloc (sizeof (*cset),GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!cset)return NULL ;
@@ -5348,7 +5348,7 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,/* allocate the cgroup and its ID, 0 is reserved for the root */ cgrp =kzalloc (struct_size (cgrp, ancestor_ids, (level +1 )), -GFP_KERNEL ); +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!cgrp)return ERR_PTR (-ENOMEM );
kernel/events/core.c +10 -5
@@ -4586,7 +4586,8 @@ alloc_perf_context(struct pmu *pmu, struct task_struct *task) {struct perf_event_context *ctx; - ctx =kzalloc (sizeof (struct perf_event_context ),GFP_KERNEL ); + ctx =kzalloc (sizeof (struct perf_event_context ), +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!ctx)return NULL ;
@@ -11062,7 +11063,8 @@ int perf_pmu_register(struct pmu *pmu, const char *name, int type)mutex_lock (&pmus_lock); ret = -ENOMEM ; - pmu->pmu_disable_count =alloc_percpu (int); + pmu->pmu_disable_count =alloc_percpu_gfp (int, +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!pmu->pmu_disable_count) goto unlock;
@@ -11112,7 +11114,8 @@ int perf_pmu_register(struct pmu *pmu, const char *name, int type) goto got_cpu_context; ret = -ENOMEM ; - pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); + pmu->pmu_cpu_context = alloc_percpu_gfp(struct perf_cpu_context, +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!pmu->pmu_cpu_context) goto free_dev;
@@ -11493,7 +11496,8 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, } node = (cpu >=0 ) ?cpu_to_node (cpu) :-1 ; - event =kmem_cache_alloc_node (perf_event_cache,GFP_KERNEL | __GFP_ZERO, + event =kmem_cache_alloc_node (perf_event_cache, +GFP_KERNEL | __GFP_ZERO | __GFP_GLOBAL_NONSENSITIVE, node);if (!event)return ERR_PTR (-ENOMEM );
@@ -13378,7 +13382,8 @@ void __init perf_event_init(void) ret =init_hw_breakpoint ();WARN (ret,"hw_breakpoint initialization failed with: %d" , ret); - perf_event_cache =KMEM_CACHE (perf_event,SLAB_PANIC ); + perf_event_cache =KMEM_CACHE (perf_event, +SLAB_PANIC |SLAB_GLOBAL_NONSENSITIVE );/* * Build time assertion that we keep the data_head at the intended
kernel/exit.c +2 -0
@@ -768,6 +768,8 @@ void __noreturn do_exit(long code)profile_task_exit (tsk);kcov_task_exit (tsk); +/* TODO: (oweisse) unmap the stack from ASI */ +coredump_task_exit (tsk);ptrace_event (PTRACE_EVENT_EXIT , code);
kernel/fork.c +29 -7
@@ -168,6 +168,8 @@ static struct kmem_cache *task_struct_cachep;static inline struct task_struct *alloc_task_struct_node (int node) { +/* TODO: Figure how to allocate this propperly to ASI process map. This +* should be mapped in a __GFP_LOCAL_NONSENSITIVE slab. */ return kmem_cache_alloc_node (task_struct_cachep,GFP_KERNEL , node); }
@@ -214,6 +216,7 @@ static int free_vm_stack_cache(unsigned int cpu)static unsigned long *alloc_thread_stack_node (struct task_struct *tsk,int node) { +/* TODO: (oweisse) Add annotation to map the stack into ASI */ #ifdef CONFIG_VMAP_STACK void *stack;int i;
@@ -242,9 +245,13 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)* so memcg accounting is performed manually on assigning/releasing * stacks to tasks. Drop __GFP_ACCOUNT. */ +/* ASI: We intentionally don't pass VM_LOCAL_NONSENSITIVE nor +* __GFP_LOCAL_NONSENSITIVE since we don't have an mm yet. Later on we'll +* map the stack into the mm asi map. That being said, we do care about +* the stack weing allocaed below VMALLOC_LOCAL_NONSENSITIVE_END */ stack =__vmalloc_node_range (THREAD_SIZE ,THREAD_ALIGN , -VMALLOC_START ,VMALLOC_END , -THREADINFO_GFP & ~__GFP_ACCOUNT, +VMALLOC_START ,VMALLOC_LOCAL_NONSENSITIVE_END , + (THREADINFO_GFP & (~__GFP_ACCOUNT)),PAGE_KERNEL ,0 , node,__builtin_return_address (0 ));
@@ -346,7 +353,8 @@ struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) {struct vm_area_struct *vma; - vma =kmem_cache_alloc (vm_area_cachep,GFP_KERNEL ); + vma =kmem_cache_alloc (vm_area_cachep, +GFP_KERNEL );if (vma)vma_init (vma, mm);return vma;
@@ -683,6 +691,8 @@ static void check_mm(struct mm_struct *mm)#endif } +/* TODO: (oweisse) ASI: we need to allocate mm such that it will only be visible +* within itself. */ #define allocate_mm () (kmem_cache_alloc(mm_cachep, GFP_KERNEL))#define free_mm (mm) (kmem_cache_free(mm_cachep, (mm)))
@@ -823,9 +833,12 @@ void __init fork_init(void)/* create a slab on which task_structs can be allocated */ task_struct_whitelist (&useroffset, &usersize); +/* TODO: (oweisse) for the time being this cache is shared among all tasks. We +* mark it SLAB_NONSENSITIVE so task_struct can be accessed withing ASI. +* A final secure solution should have this memory LOCAL, not GLOBAL.*/ task_struct_cachep =kmem_cache_create_usercopy ("task_struct" , arch_task_struct_size, align, -SLAB_PANIC |SLAB_ACCOUNT , +SLAB_PANIC |SLAB_ACCOUNT |SLAB_GLOBAL_NONSENSITIVE , useroffset, usersize,NULL );#endif
@@ -1601,6 +1614,7 @@ static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)refcount_inc (¤t->sighand->count);return 0 ; } +/* TODO: (oweisse) ASI replace with proper ASI allcation. */ sig =kmem_cache_alloc (sighand_cachep,GFP_KERNEL );RCU_INIT_POINTER (tsk->sighand, sig);if (!sig)
@@ -1649,6 +1663,8 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)if (clone_flags &CLONE_THREAD )return 0 ; +/* TODO: (oweisse) figure out how to properly allocate this in ASI for local +* process */ sig =kmem_cache_zalloc (signal_cachep,GFP_KERNEL ); tsk->signal = sig;if (!sig)
@@ -2923,7 +2939,8 @@ void __init proc_caches_init(void) SLAB_ACCOUNT, sighand_ctor); signal_cachep =kmem_cache_create ("signal_cache" ,sizeof (struct signal_struct ),0 , -SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_ACCOUNT , +SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_ACCOUNT | +SLAB_GLOBAL_NONSENSITIVE ,NULL ); files_cachep = kmem_cache_create("files_cache", sizeof(struct files_struct), 0,
@@ -2941,13 +2958,18 @@ void __init proc_caches_init(void)*/ mm_size =sizeof (struct mm_struct ) +cpumask_size (); +/* TODO: (oweisse) ASI replace with proper ASI allcation. */ mm_cachep =kmem_cache_create_usercopy ("mm_struct" , mm_size,ARCH_MIN_MMSTRUCT_ALIGN , -SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_ACCOUNT , +SLAB_HWCACHE_ALIGN |SLAB_PANIC |SLAB_ACCOUNT + |SLAB_GLOBAL_NONSENSITIVE , offsetof(struct mm_struct , saved_auxv), sizeof_field(struct mm_struct, saved_auxv),NULL ); - vm_area_cachep =KMEM_CACHE (vm_area_struct,SLAB_PANIC |SLAB_ACCOUNT ); + +/* TODO: (oweisse) ASI replace with proper ASI allcation. */ + vm_area_cachep =KMEM_CACHE (vm_area_struct, +SLAB_PANIC |SLAB_ACCOUNT |SLAB_LOCAL_NONSENSITIVE );mmap_init ();nsproxy_cache_init (); }
kernel/rcu/srcutree.c +2 -1
@@ -171,7 +171,8 @@ static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static)atomic_set (&ssp->srcu_barrier_cpu_cnt,0 );INIT_DELAYED_WORK (&ssp->work, process_srcu);if (!is_static) - ssp->sda = alloc_percpu(struct srcu_data); + ssp->sda = alloc_percpu_gfp(struct srcu_data, +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!ssp->sda)return -ENOMEM ;init_srcu_struct_nodes (ssp);
kernel/sched/core.c +4 -2
@@ -9329,7 +9329,8 @@ void __init sched_init(void)#endif /* CONFIG_RT_GROUP_SCHED */ #ifdef CONFIG_CGROUP_SCHED - task_group_cache =KMEM_CACHE (task_group,0 ); +/* TODO: (oweisse) add SLAB_NONSENSITIVE */ + task_group_cache =KMEM_CACHE (task_group,SLAB_GLOBAL_NONSENSITIVE );list_add (&root_task_group.list, &task_groups);INIT_LIST_HEAD (&root_task_group.children);
@@ -9741,7 +9742,8 @@ struct task_group *sched_create_group(struct task_group *parent) {struct task_group *tg; - tg =kmem_cache_alloc (task_group_cache,GFP_KERNEL | __GFP_ZERO); + tg =kmem_cache_alloc (task_group_cache, +GFP_KERNEL | __GFP_ZERO | __GFP_GLOBAL_NONSENSITIVE);if (!tg)return ERR_PTR (-ENOMEM );
kernel/sched/cpuacct.c +5 -3
@@ -64,15 +64,17 @@ cpuacct_css_alloc(struct cgroup_subsys_state *parent_css)if (!parent_css)return &root_cpuacct.css; - ca =kzalloc (sizeof (*ca),GFP_KERNEL ); + ca =kzalloc (sizeof (*ca),GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!ca) goto out; - ca->cpuusage = alloc_percpu(struct cpuacct_usage); + ca->cpuusage = alloc_percpu_gfp(struct cpuacct_usage, +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!ca->cpuusage) goto out_free_ca; - ca->cpustat = alloc_percpu(struct kernel_cpustat); + ca->cpustat = alloc_percpu_gfp(struct kernel_cpustat, +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!ca->cpustat) goto out_free_cpuusage;
kernel/sched/fair.c +2 -1
@@ -11486,7 +11486,8 @@ int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)for_each_possible_cpu (i) { cfs_rq =kzalloc_node (sizeof (struct cfs_rq ), -GFP_KERNEL ,cpu_to_node (i)); +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE, +cpu_to_node (i));if (!cfs_rq) goto err;
kernel/sched/topology.c +9 -5
@@ -569,7 +569,7 @@ static struct root_domain *alloc_rootdomain(void) {struct root_domain *rd; - rd =kzalloc (sizeof (*rd),GFP_KERNEL ); + rd =kzalloc (sizeof (*rd),GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!rd)return NULL ;
@@ -2044,21 +2044,24 @@ static int __sdt_alloc(const struct cpumask *cpu_map)struct sched_group_capacity *sgc; sd =kzalloc_node (sizeof (struct sched_domain ) +cpumask_size (), -GFP_KERNEL ,cpu_to_node (j)); +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE, +cpu_to_node (j));if (!sd)return -ENOMEM ; *per_cpu_ptr (sdd->sd, j) = sd; sds =kzalloc_node (sizeof (struct sched_domain_shared ), -GFP_KERNEL ,cpu_to_node (j)); +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE, +cpu_to_node (j));if (!sds)return -ENOMEM ; *per_cpu_ptr (sdd->sds, j) = sds; sg =kzalloc_node (sizeof (struct sched_group ) +cpumask_size (), -GFP_KERNEL ,cpu_to_node (j)); +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE, +cpu_to_node (j));if (!sg)return -ENOMEM ;
@@ -2067,7 +2070,8 @@ static int __sdt_alloc(const struct cpumask *cpu_map) *per_cpu_ptr (sdd->sg, j) = sg; sgc =kzalloc_node (sizeof (struct sched_group_capacity ) +cpumask_size (), -GFP_KERNEL ,cpu_to_node (j)); +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE, +cpu_to_node (j));if (!sgc)return -ENOMEM ;
kernel/smp.c +10 -7
@@ -103,15 +103,18 @@ int smpcfd_prepare_cpu(unsigned int cpu) {struct call_function_data *cfd = &per_cpu (cfd_data, cpu); -if (!zalloc_cpumask_var_node (&cfd->cpumask,GFP_KERNEL , +if (!zalloc_cpumask_var_node (&cfd->cpumask, +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE,cpu_to_node (cpu)))return -ENOMEM ; -if (!zalloc_cpumask_var_node (&cfd->cpumask_ipi,GFP_KERNEL , +if (!zalloc_cpumask_var_node (&cfd->cpumask_ipi, +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE,cpu_to_node (cpu))) {free_cpumask_var (cfd->cpumask);return -ENOMEM ; } - cfd->pcpu = alloc_percpu(struct cfd_percpu); + cfd->pcpu = alloc_percpu_gfp(struct cfd_percpu, +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!cfd->pcpu) {free_cpumask_var (cfd->cpumask);free_cpumask_var (cfd->cpumask_ipi);
@@ -179,10 +182,10 @@ static int __init csdlock_debug(char *str) }early_param ("csdlock_debug" , csdlock_debug); -static DEFINE_PER_CPU(call_single_data_t *, cur_csd); -static DEFINE_PER_CPU(smp_call_func_t, cur_csd_func); -static DEFINE_PER_CPU(void *, cur_csd_info); -static DEFINE_PER_CPU(struct cfd_seq_local, cfd_seq_local); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(call_single_data_t *, cur_csd); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(smp_call_func_t, cur_csd_func); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(void *, cur_csd_info); +static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct cfd_seq_local, cfd_seq_local);#define CSD_LOCK_TIMEOUT (5ULL * NSEC_PER_SEC)static atomic_t csd_bug_count =ATOMIC_INIT (0 );
kernel/trace/ring_buffer.c +3 -2
@@ -1539,7 +1539,8 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* gracefully without invoking oom-killer and the system is not * destabilized. */ - mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL; +/* TODO(oweisse): this is a hack to enable ASI tracing. */ + mflags =GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_GLOBAL_NONSENSITIVE;/* * If a user thread allocates too much, and si_mem_available()
@@ -1718,7 +1719,7 @@ struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags,/* keep it in its own cache line */ buffer =kzalloc (ALIGN (sizeof (*buffer),cache_line_size ()), -GFP_KERNEL ); +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!buffer)return NULL ;
kernel/tracepoint.c +1 -1
@@ -107,7 +107,7 @@ static void tp_stub_func(void)static inline void *allocate_probes (int count) {struct tp_probes *p =kmalloc (struct_size (p, probes, count), -GFP_KERNEL ); +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);return p ==NULL ?NULL : p->probes; }
lib/radix-tree.c +3 -3
@@ -248,8 +248,7 @@ radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent,* cache first for the new node to get accounted to the memory * cgroup. */ - ret =kmem_cache_alloc (radix_tree_node_cachep, - gfp_mask | __GFP_NOWARN); + ret =kmem_cache_alloc (radix_tree_node_cachep, gfp_mask | __GFP_NOWARN);if (ret) goto out;
@@ -1597,9 +1596,10 @@ void __init radix_tree_init(void)BUILD_BUG_ON (RADIX_TREE_MAX_TAGS + __GFP_BITS_SHIFT >32 );BUILD_BUG_ON (ROOT_IS_IDR & ~GFP_ZONEMASK );BUILD_BUG_ON (XA_CHUNK_SIZE >255 ); +/*TODO: (oweisse) ASI add SLAB_NONSENSITIVE */ radix_tree_node_cachep =kmem_cache_create ("radix_tree_node" ,sizeof (struct radix_tree_node ),0 , -SLAB_PANIC |SLAB_RECLAIM_ACCOUNT , +SLAB_PANIC |SLAB_RECLAIM_ACCOUNT |SLAB_GLOBAL_NONSENSITIVE , radix_tree_node_ctor); ret =cpuhp_setup_state_nocalls (CPUHP_RADIX_DEAD ,"lib/radix:dead" ,NULL , radix_tree_cpu_dead);
mm/memcontrol.c +4 -3
@@ -5143,20 +5143,21 @@ static struct mem_cgroup *mem_cgroup_alloc(void) size =sizeof (struct mem_cgroup ); size += nr_node_ids *sizeof (struct mem_cgroup_per_node *); - memcg =kzalloc (size,GFP_KERNEL ); + memcg =kzalloc (size,GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (!memcg)return ERR_PTR (error); memcg->id.id =idr_alloc (&mem_cgroup_idr,NULL ,1 ,MEM_CGROUP_ID_MAX , -GFP_KERNEL ); +GFP_KERNEL | __GFP_GLOBAL_NONSENSITIVE);if (memcg->id.id <0 ) { error = memcg->id.id; goto fail; } memcg->vmstats_percpu = alloc_percpu_gfp(struct memcg_vmstats_percpu, -GFP_KERNEL_ACCOUNT ); +GFP_KERNEL_ACCOUNT | + __GFP_GLOBAL_NONSENSITIVE);if (!memcg->vmstats_percpu) goto fail;
mm/util.c +2 -1
@@ -196,7 +196,8 @@ void *vmemdup_user(const void __user *src, size_t len) {void *p; - p =kvmalloc (len,GFP_USER ); +/* TODO(oweisse): is this secure? */ + p =kvmalloc (len,GFP_USER | __GFP_LOCAL_NONSENSITIVE);if (!p)return ERR_PTR (-ENOMEM );
mm/vmalloc.c +2 -1
@@ -3309,7 +3309,8 @@ EXPORT_SYMBOL(vzalloc);void *vmalloc_user (unsigned long size) {return __vmalloc_node_range (size,SHMLBA ,VMALLOC_START ,VMALLOC_END , -GFP_KERNEL | __GFP_ZERO,PAGE_KERNEL , +GFP_KERNEL | __GFP_ZERO + | __GFP_LOCAL_NONSENSITIVE,PAGE_KERNEL ,VM_USERMAP ,NUMA_NO_NODE ,__builtin_return_address (0 )); }
net/core/skbuff.c +1 -1
@@ -404,7 +404,7 @@ struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, ? skbuff_fclone_cache : skbuff_head_cache;if (sk_memalloc_socks () && (flags &SKB_ALLOC_RX )) - gfp_mask |= __GFP_MEMALLOC; + gfp_mask |= __GFP_MEMALLOC | __GFP_GLOBAL_NONSENSITIVE;/* Get the HEAD */ if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI &&
net/core/sock.c +1 -1
@@ -2704,7 +2704,7 @@ bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t gfp)/* Avoid direct reclaim but allow kswapd to wake */ pfrag->page =alloc_pages ((gfp & ~__GFP_DIRECT_RECLAIM) | __GFP_COMP | __GFP_NOWARN | - __GFP_NORETRY, + __GFP_NORETRY | __GFP_GLOBAL_NONSENSITIVE,SKB_FRAG_PAGE_ORDER );if (likely (pfrag->page)) { pfrag->size =PAGE_SIZE <<SKB_FRAG_PAGE_ORDER ;
virt/kvm/coalesced_mmio.c +1 -1
@@ -111,7 +111,7 @@ int kvm_coalesced_mmio_init(struct kvm *kvm) {struct page *page; - page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_LOCAL_NONSENSITIVE);if (!page)return -ENOMEM ;
virt/kvm/eventfd.c +3 -2
@@ -306,7 +306,8 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)if (!kvm_arch_irqfd_allowed (kvm, args))return -EINVAL ; - irqfd =kzalloc (sizeof (*irqfd),GFP_KERNEL_ACCOUNT ); + irqfd =kzalloc (sizeof (*irqfd), +GFP_KERNEL_ACCOUNT | __GFP_GLOBAL_NONSENSITIVE);if (!irqfd)return -ENOMEM ;
@@ -813,7 +814,7 @@ static int kvm_assign_ioeventfd_idx(struct kvm *kvm,if (IS_ERR (eventfd))return PTR_ERR (eventfd); - p =kzalloc (sizeof (*p),GFP_KERNEL_ACCOUNT ); + p =kzalloc (sizeof (*p),GFP_KERNEL_ACCOUNT | __GFP_GLOBAL_NONSENSITIVE);if (!p) { ret = -ENOMEM ; goto fail;
virt/kvm/kvm_main.c +8 -4
@@ -370,6 +370,9 @@ static inline void *mmu_memory_cache_alloc_obj(struct kvm_mmu_memory_cache *mc, gfp_t gfp_flags) { gfp_flags |= mc->gfp_zero; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION + gfp_flags |= mc->gfp_asi; +#endif if (mc->kmem_cache)return kmem_cache_alloc (mc->kmem_cache, gfp_flags);
@@ -863,7 +866,8 @@ static struct kvm_memslots *kvm_alloc_memslots(void)int i;struct kvm_memslots *slots; - slots =kvzalloc (sizeof (struct kvm_memslots ),GFP_KERNEL_ACCOUNT ); + slots =kvzalloc (sizeof (struct kvm_memslots ), +GFP_KERNEL_ACCOUNT | __GFP_LOCAL_NONSENSITIVE);if (!slots)return NULL ;
@@ -1529,7 +1533,7 @@ static struct kvm_memslots *kvm_dup_memslots(struct kvm_memslots *old,else new_size =kvm_memslots_size (old->used_slots); - slots =kvzalloc (new_size,GFP_KERNEL_ACCOUNT ); + slots =kvzalloc (new_size,GFP_KERNEL_ACCOUNT | __GFP_LOCAL_NONSENSITIVE);if (likely (slots))kvm_copy_memslots (slots, old);
@@ -3565,7 +3569,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) }BUILD_BUG_ON (sizeof (struct kvm_run ) >PAGE_SIZE ); - page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_LOCAL_NONSENSITIVE);if (!page) { r = -ENOMEM ; goto vcpu_free;
@@ -4959,7 +4963,7 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,return -ENOSPC ; new_bus =kmalloc (struct_size (bus, range, bus->dev_count +1 ), -GFP_KERNEL_ACCOUNT ); +GFP_KERNEL_ACCOUNT | __GFP_LOCAL_NONSENSITIVE);if (!new_bus)return -ENOMEM ;
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 44/47 kvm: asi: Splitting kvm_vcpu_arch into non/sensitive parts
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
The part that was allocated via ASI LOCAL SENSITIVE is in struct kvm_vcpu_arch_private. The rest is in struct kvm_vcpu_arch. The latter contains a pointer private which is allocated to be ASI non-sensitive from a cache.
--- arch/x86/include/asm/kvm_host.h | 109 ++++++++++++---------- arch/x86/kvm/cpuid.c | 14 +-- arch/x86/kvm/kvm_cache_regs.h | 22 ++--- arch/x86/kvm/mmu.h | 10 +- arch/x86/kvm/mmu/mmu.c | 138 +++++++++++++-------------- arch/x86/kvm/mmu/mmu_internal.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 26 +++--- arch/x86/kvm/mmu/spte.c | 4 +- arch/x86/kvm/mmu/tdp_mmu.c | 14 +-- arch/x86/kvm/svm/nested.c | 34 +++---- arch/x86/kvm/svm/sev.c | 70 +++++++------- arch/x86/kvm/svm/svm.c | 52 +++++------ arch/x86/kvm/trace.h | 10 +- arch/x86/kvm/vmx/nested.c | 68 +++++++------- arch/x86/kvm/vmx/vmx.c | 64 ++++++------- arch/x86/kvm/x86.c | 160 ++++++++++++++++---------------- arch/x86/kvm/x86.h | 2 +- virt/kvm/kvm_main.c | 38 ++++++-- 18 files changed, 436 insertions(+), 401 deletions(-)
arch/x86/include/asm/kvm_host.h +59 -50
@@ -606,14 +606,12 @@ struct kvm_vcpu_xen {u64 runstate_times[4 ]; }; -struct kvm_vcpu_arch { -/* +struct kvm_vcpu_arch_private { +/* * rip and regs accesses must go through * kvm_{register,rip}_{read,write} functions. */ unsigned long regs[NR_VCPU_REGS ]; -u32 regs_avail; -u32 regs_dirty;unsigned long cr0;unsigned long cr0_guest_owned_bits;
@@ -623,6 +621,63 @@ struct kvm_vcpu_arch {unsigned long cr4_guest_owned_bits;unsigned long cr4_guest_rsvd_bits;unsigned long cr8; + +/* +* QEMU userspace and the guest each have their own FPU state. +* In vcpu_run, we switch between the user and guest FPU contexts. +* While running a VCPU, the VCPU thread will have the guest FPU +* context. +* +* Note that while the PKRU state lives inside the fpu registers, +* it is switched out separately at VMENTER and VMEXIT time. The +* "guest_fpstate" state here contains the guest FPU context, with the +* host PRKU bits. +*/ +struct fpu_guest guest_fpu; + +u64 xcr0; +u64 guest_supported_xcr0; + +/* +* Paging state of the vcpu +* +* If the vcpu runs in guest mode with two level paging this still saves +* the paging mode of the l1 guest. This context is always used to +* handle faults. +*/ +struct kvm_mmu *mmu; + +/* Non-nested MMU for L1 */ +struct kvm_mmu root_mmu; + +/* L1 MMU when running nested */ +struct kvm_mmu guest_mmu; + +/* +* Pointer to the mmu context currently used for +* gva_to_gpa translations. +*/ +struct kvm_mmu *walk_mmu; + +/* +* Paging state of an L2 guest (used for nested npt) +* +* This context will save all necessary information to walk page tables +* of an L2 guest. This context is only initialized for page table +* walking and not for faulting since we never handle l2 page faults on +* the host. +*/ +struct kvm_mmu nested_mmu; + +struct x86_emulate_ctxt *emulate_ctxt; +}; + +struct kvm_vcpu_arch { +struct kvm_vcpu_arch_private *private; + +u32 regs_avail; +u32 regs_dirty; +u32 host_pkru;u32 pkru;u32 hflags;
@@ -645,36 +700,6 @@ struct kvm_vcpu_arch {u64 arch_capabilities;u64 perf_capabilities; -/* -* Paging state of the vcpu -* -* If the vcpu runs in guest mode with two level paging this still saves -* the paging mode of the l1 guest. This context is always used to -* handle faults. -*/ -struct kvm_mmu *mmu; - -/* Non-nested MMU for L1 */ -struct kvm_mmu root_mmu; - -/* L1 MMU when running nested */ -struct kvm_mmu guest_mmu; - -/* -* Paging state of an L2 guest (used for nested npt) -* -* This context will save all necessary information to walk page tables -* of an L2 guest. This context is only initialized for page table -* walking and not for faulting since we never handle l2 page faults on -* the host. -*/ -struct kvm_mmu nested_mmu; - -/* -* Pointer to the mmu context currently used for -* gva_to_gpa translations. -*/ -struct kvm_mmu *walk_mmu;struct kvm_mmu_memory_cache mmu_pte_list_desc_cache;struct kvm_mmu_memory_cache mmu_shadow_page_cache;
@@ -683,21 +708,6 @@ struct kvm_vcpu_arch {struct asi_pgtbl_pool asi_pgtbl_pool; -/* -* QEMU userspace and the guest each have their own FPU state. -* In vcpu_run, we switch between the user and guest FPU contexts. -* While running a VCPU, the VCPU thread will have the guest FPU -* context. -* -* Note that while the PKRU state lives inside the fpu registers, -* it is switched out separately at VMENTER and VMEXIT time. The -* "guest_fpstate" state here contains the guest FPU context, with the -* host PRKU bits. -*/ -struct fpu_guest guest_fpu; - -u64 xcr0; -u64 guest_supported_xcr0;struct kvm_pio_request pio;void *pio_data;
@@ -734,7 +744,6 @@ struct kvm_vcpu_arch {/* emulate context */ -struct x86_emulate_ctxt *emulate_ctxt;bool emulate_regs_need_sync_to_vcpu;bool emulate_regs_need_sync_from_vcpu;int (*complete_userspace_io)(struct kvm_vcpu *vcpu);
arch/x86/kvm/cpuid.c +7 -7
@@ -169,12 +169,12 @@ void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu) best =kvm_find_cpuid_entry (vcpu,0xD ,0 );if (best) - best->ebx =xstate_required_size (vcpu->arch.xcr0, false); + best->ebx =xstate_required_size (vcpu->arch.private->xcr0, false); best =kvm_find_cpuid_entry (vcpu,0xD ,1 );if (best && (cpuid_entry_has (best,X86_FEATURE_XSAVES ) ||cpuid_entry_has (best,X86_FEATURE_XSAVEC ))) - best->ebx =xstate_required_size (vcpu->arch.xcr0, true); + best->ebx =xstate_required_size (vcpu->arch.private->xcr0, true); best =kvm_find_kvm_cpuid_features (vcpu); if (kvm_hlt_in_guest(vcpu->kvm) && best &&
@@ -208,9 +208,9 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) best =kvm_find_cpuid_entry (vcpu,0xD ,0 );if (!best) - vcpu->arch.guest_supported_xcr0 =0 ; + vcpu->arch.private->guest_supported_xcr0 =0 ;else - vcpu->arch.guest_supported_xcr0 = + vcpu->arch.private->guest_supported_xcr0 = (best->eax | ((u64 )best->edx <<32 )) & supported_xcr0;/*
@@ -223,8 +223,8 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)*/ best =kvm_find_cpuid_entry (vcpu,0x12 ,0x1 );if (best) { - best->ecx &= vcpu->arch.guest_supported_xcr0 &0xffffffff ; - best->edx &= vcpu->arch.guest_supported_xcr0 >>32 ; + best->ecx &= vcpu->arch.private->guest_supported_xcr0 &0xffffffff ; + best->edx &= vcpu->arch.private->guest_supported_xcr0 >>32 ; best->ecx |=XFEATURE_MASK_FPSSE ; }
@@ -234,7 +234,7 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) vcpu->arch.reserved_gpa_bits =kvm_vcpu_reserved_gpa_bits_raw (vcpu);kvm_pmu_refresh (vcpu); - vcpu->arch.cr4_guest_rsvd_bits = + vcpu->arch.private->cr4_guest_rsvd_bits =__cr4_reserved_bits (guest_cpuid_has, vcpu);kvm_hv_set_cpuid (vcpu);
arch/x86/kvm/kvm_cache_regs.h +11 -11
@@ -12,12 +12,12 @@#define BUILD_KVM_GPR_ACCESSORS (lname, uname) \ static __always_inline unsigned long kvm_##lname##_read(struct kvm_vcpu *vcpu)\ { \ - return vcpu->arch.regs[VCPU_REGS_##uname]; \ + return vcpu->arch.private->regs[VCPU_REGS_##uname]; \ } \ static __always_inline void kvm_##lname##_write(struct kvm_vcpu *vcpu, \ unsigned long val) \ { \ - vcpu->arch.regs[VCPU_REGS_##uname] = val; \ + vcpu->arch.private->regs[VCPU_REGS_##uname] = val; \ } BUILD_KVM_GPR_ACCESSORS(rax, RAX)BUILD_KVM_GPR_ACCESSORS (rbx,RBX )
@@ -82,7 +82,7 @@ static inline unsigned long kvm_register_read_raw(struct kvm_vcpu *vcpu, int regif (!kvm_register_is_available (vcpu, reg))static_call (kvm_x86_cache_reg)(vcpu, reg); -return vcpu->arch.regs[reg]; +return vcpu->arch.private->regs[reg]; } static inline void kvm_register_write_raw(struct kvm_vcpu *vcpu, int reg,
@@ -91,7 +91,7 @@ static inline void kvm_register_write_raw(struct kvm_vcpu *vcpu, int reg,if (WARN_ON_ONCE ((unsigned int )reg >=NR_VCPU_REGS ))return ; - vcpu->arch.regs[reg] = val; + vcpu->arch.private->regs[reg] = val;kvm_register_mark_dirty (vcpu, reg); }
@@ -122,21 +122,21 @@ static inline u64 kvm_pdptr_read(struct kvm_vcpu *vcpu, int index)if (!kvm_register_is_available (vcpu,VCPU_EXREG_PDPTR ))static_call (kvm_x86_cache_reg)(vcpu,VCPU_EXREG_PDPTR ); -return vcpu->arch.walk_mmu->pdptrs[index]; +return vcpu->arch.private->walk_mmu->pdptrs[index]; }static inline void kvm_pdptr_write (struct kvm_vcpu *vcpu,int index,u64 value) { - vcpu->arch.walk_mmu->pdptrs[index] = value; + vcpu->arch.private->walk_mmu->pdptrs[index] = value; }static inline ulong kvm_read_cr0_bits (struct kvm_vcpu *vcpu,ulong mask) {ulong tmask = mask &KVM_POSSIBLE_CR0_GUEST_BITS ; -if ((tmask & vcpu->arch.cr0_guest_owned_bits) && +if ((tmask & vcpu->arch.private->cr0_guest_owned_bits) && !kvm_register_is_available (vcpu,VCPU_EXREG_CR0 ))static_call (kvm_x86_cache_reg)(vcpu,VCPU_EXREG_CR0 ); -return vcpu->arch.cr0 & mask; +return vcpu->arch.private->cr0 & mask; }static inline ulong kvm_read_cr0 (struct kvm_vcpu *vcpu)
@@ -147,17 +147,17 @@ static inline ulong kvm_read_cr0(struct kvm_vcpu *vcpu)static inline ulong kvm_read_cr4_bits (struct kvm_vcpu *vcpu,ulong mask) {ulong tmask = mask &KVM_POSSIBLE_CR4_GUEST_BITS ; -if ((tmask & vcpu->arch.cr4_guest_owned_bits) && +if ((tmask & vcpu->arch.private->cr4_guest_owned_bits) && !kvm_register_is_available (vcpu,VCPU_EXREG_CR4 ))static_call (kvm_x86_cache_reg)(vcpu,VCPU_EXREG_CR4 ); -return vcpu->arch.cr4 & mask; +return vcpu->arch.private->cr4 & mask; }static inline ulong kvm_read_cr3 (struct kvm_vcpu *vcpu) {if (!kvm_register_is_available (vcpu,VCPU_EXREG_CR3 ))static_call (kvm_x86_cache_reg)(vcpu,VCPU_EXREG_CR3 ); -return vcpu->arch.cr3; +return vcpu->arch.private->cr3; }static inline ulong kvm_read_cr4 (struct kvm_vcpu *vcpu)
arch/x86/kvm/mmu.h +5 -5
@@ -89,7 +89,7 @@ void kvm_mmu_sync_prev_roots(struct kvm_vcpu *vcpu);static inline int kvm_mmu_reload (struct kvm_vcpu *vcpu) { -if (likely (vcpu->arch.mmu->root_hpa !=INVALID_PAGE )) +if (likely (vcpu->arch.private->mmu->root_hpa !=INVALID_PAGE ))return 0 ;return kvm_mmu_load (vcpu);
@@ -111,13 +111,13 @@ static inline unsigned long kvm_get_active_pcid(struct kvm_vcpu *vcpu)static inline void kvm_mmu_load_pgd (struct kvm_vcpu *vcpu) { -u64 root_hpa = vcpu->arch.mmu->root_hpa; +u64 root_hpa = vcpu->arch.private->mmu->root_hpa;if (!VALID_PAGE (root_hpa))return ;static_call (kvm_x86_load_mmu_pgd)(vcpu, root_hpa, - vcpu->arch.mmu->shadow_root_level); + vcpu->arch.private->mmu->shadow_root_level); }struct kvm_page_fault {
@@ -193,7 +193,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .rsvd = err & PFERR_RSVD_MASK, .user = err & PFERR_USER_MASK, .prefetch = prefetch, - .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), + .is_tdp = likely(vcpu->arch.private->mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = is_nx_huge_page_enabled(), .max_level = KVM_MAX_HUGEPAGE_LEVEL,
@@ -204,7 +204,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,if (fault.is_tdp)return kvm_tdp_page_fault (vcpu, &fault);#endif -return vcpu->arch.mmu->page_fault (vcpu, &fault); +return vcpu->arch.private->mmu->page_fault (vcpu, &fault); }/*
arch/x86/kvm/mmu/mmu.c +69 -69
445 lines not on this page · raw message
arch/x86/kvm/mmu/mmu_internal.h +1 -1
@@ -114,7 +114,7 @@ static inline bool kvm_vcpu_ad_need_write_protect(struct kvm_vcpu *vcpu)* being enabled is mandatory as the bits used to denote WP-only SPTEs * are reserved for NPT w/ PAE (32-bit KVM). */ -return vcpu->arch.mmu == &vcpu->arch.guest_mmu && +return vcpu->arch.private->mmu == &vcpu->arch.private->guest_mmu && kvm_x86_ops.cpu_dirty_log_size; }
arch/x86/kvm/mmu/paging_tmpl.h +13 -13
@@ -194,11 +194,11 @@ static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu, goto no_present;/* if accessed bit is not supported prefetch non accessed gpte */ -if (PT_HAVE_ACCESSED_DIRTY (vcpu->arch.mmu) && +if (PT_HAVE_ACCESSED_DIRTY (vcpu->arch.private->mmu) && !(gpte &PT_GUEST_ACCESSED_MASK )) goto no_present; -if (FNAME (is_rsvd_bits_set)(vcpu->arch.mmu, gpte,PG_LEVEL_4K )) +if (FNAME (is_rsvd_bits_set)(vcpu->arch.private->mmu, gpte,PG_LEVEL_4K )) goto no_present;return false;
@@ -533,7 +533,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, }#endif walker->fault.address = addr; - walker->fault.nested_page_fault = mmu != vcpu->arch.walk_mmu; + walker->fault.nested_page_fault = mmu != vcpu->arch.private->walk_mmu; walker->fault.async_page_fault = false;trace_kvm_mmu_walker_error (walker->fault.error_code);
@@ -543,7 +543,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,static int FNAME (walk_addr )(struct guest_walker *walker,struct kvm_vcpu *vcpu,gpa_t addr,u32 access) { -return FNAME (walk_addr_generic)(walker, vcpu, vcpu->arch.mmu, addr, +return FNAME (walk_addr_generic)(walker, vcpu, vcpu->arch.private->mmu, addr, access); }
@@ -552,7 +552,7 @@ static int FNAME(walk_addr_nested)(struct guest_walker *walker, struct kvm_vcpu *vcpu, gva_t addr, u32 access) { - return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.nested_mmu, + return FNAME(walk_addr_generic)(walker, vcpu, &vcpu->arch.private->nested_mmu,addr ,access ); }#endif
@@ -573,7 +573,7 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, gfn =gpte_to_gfn (gpte); pte_access = sp->role.access &FNAME (gpte_access)(gpte); -FNAME (protect_clean_gpte)(vcpu->arch.mmu, &pte_access, gpte); +FNAME (protect_clean_gpte)(vcpu->arch.private->mmu, &pte_access, gpte); slot =gfn_to_memslot_dirty_bitmap (vcpu, gfn, no_dirty_log && (pte_access &ACC_WRITE_MASK ));
@@ -670,7 +670,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,WARN_ON_ONCE (gw->gfn != base_gfn); direct_access = gw->pte_access; - top_level = vcpu->arch.mmu->root_level; + top_level = vcpu->arch.private->mmu->root_level;if (top_level ==PT32E_ROOT_LEVEL ) top_level =PT32_ROOT_LEVEL ;/*
@@ -682,7 +682,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,if (FNAME (gpte_changed)(vcpu, gw, top_level)) goto out_gpte_changed; -if (WARN_ON (!VALID_PAGE (vcpu->arch.mmu->root_hpa))) +if (WARN_ON (!VALID_PAGE (vcpu->arch.private->mmu->root_hpa))) goto out_gpte_changed; for (shadow_walk_init(&it, vcpu, fault->addr);
@@ -806,7 +806,7 @@ FNAME(is_self_change_mapping)(struct kvm_vcpu *vcpu,bool self_changed = false;if (!(walker->pte_access &ACC_WRITE_MASK || - (!is_cr0_wp (vcpu->arch.mmu) && !user_fault))) + (!is_cr0_wp (vcpu->arch.private->mmu) && !user_fault)))return false;for (level = walker->level; level <= walker->max_level; level++) {
@@ -905,7 +905,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* we will cache the incorrect access into mmio spte. */ if (fault->write && !(walker.pte_access &ACC_WRITE_MASK ) && - !is_cr0_wp (vcpu->arch.mmu) && !fault->user && fault->slot) { + !is_cr0_wp (vcpu->arch.private->mmu) && !fault->user && fault->slot) { walker.pte_access |=ACC_WRITE_MASK ; walker.pte_access &= ~ACC_USER_MASK ;
@@ -915,7 +915,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* then we should prevent the kernel from executing it * if SMEP is enabled. */ -if (is_cr4_smep (vcpu->arch.mmu)) +if (is_cr4_smep (vcpu->arch.private->mmu)) walker.pte_access &= ~ACC_EXEC_MASK ; }
@@ -1071,7 +1071,7 @@ static gpa_t FNAME(gva_to_gpa_nested)(struct kvm_vcpu *vcpu, gpa_t vaddr,*/ static int FNAME (sync_page )(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp) { -union kvm_mmu_page_role mmu_role = vcpu->arch.mmu->mmu_role.base; +union kvm_mmu_page_role mmu_role = vcpu->arch.private->mmu->mmu_role.base;int i;bool host_writable;gpa_t first_pte_gpa;
@@ -1129,7 +1129,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) gfn =gpte_to_gfn (gpte); pte_access = sp->role.access; pte_access &=FNAME (gpte_access)(gpte); -FNAME (protect_clean_gpte)(vcpu->arch.mmu, &pte_access, gpte); +FNAME (protect_clean_gpte)(vcpu->arch.private->mmu, &pte_access, gpte);if (sync_mmio_spte (vcpu, &sp->spt[i], gfn, pte_access))continue ;
arch/x86/kvm/mmu/spte.c +2 -2
@@ -177,9 +177,9 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,if (prefetch) spte =mark_spte_for_access_track (spte); -WARN_ONCE (is_rsvd_spte (&vcpu->arch.mmu->shadow_zero_check, spte, level), +WARN_ONCE (is_rsvd_spte (&vcpu->arch.private->mmu->shadow_zero_check, spte, level),"spte = 0x%llx, level = %d, rsvd bits = 0x%llx" , spte, level, -get_rsvd_bits (&vcpu->arch.mmu->shadow_zero_check, spte, level)); +get_rsvd_bits (&vcpu->arch.private->mmu->shadow_zero_check, spte, level));if ((spte &PT_WRITABLE_MASK ) &&kvm_slot_dirty_track_enabled (slot)) {/* Enforced by kvm_mmu_hugepage_adjust. */
arch/x86/kvm/mmu/tdp_mmu.c +7 -7
@@ -162,7 +162,7 @@ static union kvm_mmu_page_role page_role_for_level(struct kvm_vcpu *vcpu, {union kvm_mmu_page_role role; - role = vcpu->arch.mmu->mmu_role.base; + role = vcpu->arch.private->mmu->mmu_role.base; role.level = level; role.direct = true; role.gpte_is_8_bytes = true;
@@ -198,7 +198,7 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)lockdep_assert_held_write (&kvm->mmu_lock); - role =page_role_for_level (vcpu, vcpu->arch.mmu->shadow_root_level); + role =page_role_for_level (vcpu, vcpu->arch.private->mmu->shadow_root_level);/* Check for an existing root before allocating a new one. */ for_each_tdp_mmu_root (kvm, root,kvm_mmu_role_as_id (role)) {
@@ -207,7 +207,7 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu) goto out; } - root =alloc_tdp_mmu_page (vcpu,0 , vcpu->arch.mmu->shadow_root_level); + root =alloc_tdp_mmu_page (vcpu,0 , vcpu->arch.private->mmu->shadow_root_level);refcount_set (&root->tdp_mmu_root_count,1 );spin_lock (&kvm->arch.tdp_mmu_pages_lock);
@@ -952,7 +952,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,*/ int kvm_tdp_mmu_map (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) { -struct kvm_mmu *mmu = vcpu->arch.mmu; +struct kvm_mmu *mmu = vcpu->arch.private->mmu;struct tdp_iter iter;struct kvm_mmu_page *sp;u64 *child_pt;
@@ -1486,11 +1486,11 @@ int kvm_tdp_mmu_get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes, int *root_level) {struct tdp_iter iter; -struct kvm_mmu *mmu = vcpu->arch.mmu; +struct kvm_mmu *mmu = vcpu->arch.private->mmu;gfn_t gfn = addr >>PAGE_SHIFT ;int leaf =-1 ; - *root_level = vcpu->arch.mmu->shadow_root_level; + *root_level = vcpu->arch.private->mmu->shadow_root_level;tdp_mmu_for_each_pte (iter, mmu, gfn, gfn +1 ) { leaf = iter.level;
@@ -1515,7 +1515,7 @@ u64 *kvm_tdp_mmu_fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, u64 addr, u64 *spte) {struct tdp_iter iter; -struct kvm_mmu *mmu = vcpu->arch.mmu; +struct kvm_mmu *mmu = vcpu->arch.private->mmu;gfn_t gfn = addr >>PAGE_SHIFT ;tdp_ptep_t sptep =NULL ;
arch/x86/kvm/svm/nested.c +17 -17
@@ -97,7 +97,7 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)WARN_ON (mmu_is_nested (vcpu)); - vcpu->arch.mmu = &vcpu->arch.guest_mmu; + vcpu->arch.private->mmu = &vcpu->arch.private->guest_mmu;/* * The NPT format depends on L1's CR4 and EFER, which is in vmcb01. Note,
@@ -107,16 +107,16 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)kvm_init_shadow_npt_mmu (vcpu,X86_CR0_PG , svm->vmcb01.ptr->save.cr4, svm->vmcb01.ptr->save.efer, svm->nested.ctl.nested_cr3); - vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3; - vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr; - vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit; - vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; + vcpu->arch.private->mmu->get_guest_pgd = nested_svm_get_tdp_cr3; + vcpu->arch.private->mmu->get_pdptr = nested_svm_get_tdp_pdptr; + vcpu->arch.private->mmu->inject_page_fault = nested_svm_inject_npf_exit; + vcpu->arch.private->walk_mmu = &vcpu->arch.private->nested_mmu; }static void nested_svm_uninit_mmu_context (struct kvm_vcpu *vcpu) { - vcpu->arch.mmu = &vcpu->arch.root_mmu; - vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; + vcpu->arch.private->mmu = &vcpu->arch.private->root_mmu; + vcpu->arch.private->walk_mmu = &vcpu->arch.private->root_mmu; }void recalc_intercepts (struct vcpu_svm *svm)
@@ -437,13 +437,13 @@ static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,return -EINVAL ;if (reload_pdptrs && !nested_npt &&is_pae_paging (vcpu) && -CC (!load_pdptrs (vcpu, vcpu->arch.walk_mmu, cr3))) +CC (!load_pdptrs (vcpu, vcpu->arch.private->walk_mmu, cr3)))return -EINVAL ;if (!nested_npt)kvm_mmu_new_pgd (vcpu, cr3); - vcpu->arch.cr3 = cr3; + vcpu->arch.private->cr3 = cr3;kvm_register_mark_available (vcpu,VCPU_EXREG_CR3 );/* Re-initialize the MMU, e.g. to pick up CR4 MMU role changes. */
@@ -500,7 +500,7 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12svm_set_cr0 (&svm->vcpu, vmcb12->save.cr0);svm_set_cr4 (&svm->vcpu, vmcb12->save.cr4); - svm->vcpu.arch.cr2 = vmcb12->save.cr2; + svm->vcpu.arch.private->cr2 = vmcb12->save.cr2;kvm_rax_write (&svm->vcpu, vmcb12->save.rax);kvm_rsp_write (&svm->vcpu, vmcb12->save.rsp);
@@ -634,7 +634,7 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa,return ret;if (!npt_enabled) - vcpu->arch.mmu->inject_page_fault = svm_inject_page_fault_nested; + vcpu->arch.private->mmu->inject_page_fault = svm_inject_page_fault_nested;if (!from_vmrun)kvm_make_request (KVM_REQ_GET_NESTED_STATE_PAGES , vcpu);
@@ -695,7 +695,7 @@ int nested_svm_vmrun(struct kvm_vcpu *vcpu)*/ svm->vmcb01.ptr->save.efer = vcpu->arch.efer; svm->vmcb01.ptr->save.cr0 =kvm_read_cr0 (vcpu); - svm->vmcb01.ptr->save.cr4 = vcpu->arch.cr4; + svm->vmcb01.ptr->save.cr4 = vcpu->arch.private->cr4; svm->vmcb01.ptr->save.rflags =kvm_get_rflags (vcpu); svm->vmcb01.ptr->save.rip =kvm_rip_read (vcpu);
@@ -805,7 +805,7 @@ int nested_svm_vmexit(struct vcpu_svm *svm) vmcb12->save.cr0 =kvm_read_cr0 (vcpu); vmcb12->save.cr3 =kvm_read_cr3 (vcpu); vmcb12->save.cr2 = vmcb->save.cr2; - vmcb12->save.cr4 = svm->vcpu.arch.cr4; + vmcb12->save.cr4 = svm->vcpu.arch.private->cr4; vmcb12->save.rflags =kvm_get_rflags (vcpu); vmcb12->save.rip =kvm_rip_read (vcpu); vmcb12->save.rsp =kvm_rsp_read (vcpu);
@@ -991,7 +991,7 @@ static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)if (!(vmcb_is_intercept (&svm->nested.ctl,INTERCEPT_MSR_PROT )))return NESTED_EXIT_HOST ; - msr = svm->vcpu.arch.regs[VCPU_REGS_RCX ]; + msr = svm->vcpu.arch.private->regs[VCPU_REGS_RCX ]; offset =svm_msrpm_offset (msr); write = svm->vmcb->control.exit_info_1 &1 ; mask =1 << ((2 * (msr &0xf )) + write);
@@ -1131,7 +1131,7 @@ static void nested_svm_inject_exception_vmexit(struct vcpu_svm *svm)else if (svm->vcpu.arch.exception.has_payload) svm->vmcb->control.exit_info_2 = svm->vcpu.arch.exception.payload;else - svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; + svm->vmcb->control.exit_info_2 = svm->vcpu.arch.private->cr2; }else if (nr ==DB_VECTOR ) {/* See inject_pending_event. */ kvm_deliver_exception_payload (&svm->vcpu);
@@ -1396,7 +1396,7 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,* Set it again to fix this. */ - ret =nested_svm_load_cr3 (&svm->vcpu, vcpu->arch.cr3, + ret =nested_svm_load_cr3 (&svm->vcpu, vcpu->arch.private->cr3,nested_npt_enabled (svm), false);if (WARN_ON_ONCE (ret)) goto out_free;
@@ -1449,7 +1449,7 @@ static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)* the guest CR3 might be restored prior to setting the nested * state which can lead to a load of wrong PDPTRs. */ -if (CC (!load_pdptrs (vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3))) +if (CC (!load_pdptrs (vcpu, vcpu->arch.private->walk_mmu, vcpu->arch.private->cr3)))return false;if (!nested_svm_vmrun_msrpm (svm)) {
arch/x86/kvm/svm/sev.c +35 -35
112 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +26 -26
149 lines not on this page · raw message
arch/x86/kvm/trace.h +5 -5
@@ -791,13 +791,13 @@ TRACE_EVENT(kvm_emulate_insn,TP_fast_assign ( __entry->csbase = static_call(kvm_x86_get_segment_base)(vcpu, VCPU_SREG_CS); - __entry->len = vcpu->arch.emulate_ctxt->fetch.ptr - - vcpu->arch.emulate_ctxt->fetch.data; - __entry->rip = vcpu->arch.emulate_ctxt->_eip - __entry->len; + __entry->len = vcpu->arch.private->emulate_ctxt->fetch.ptr + - vcpu->arch.private->emulate_ctxt->fetch.data; + __entry->rip = vcpu->arch.private->emulate_ctxt->_eip - __entry->len;memcpy (__entry->insn, - vcpu->arch.emulate_ctxt->fetch.data, + vcpu->arch.private->emulate_ctxt->fetch.data,15 ); - __entry->flags =kei_decode_mode (vcpu->arch.emulate_ctxt->mode); + __entry->flags =kei_decode_mode (vcpu->arch.private->emulate_ctxt->mode); __entry->failed = failed; ),
arch/x86/kvm/vmx/nested.c +34 -34
205 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +32 -32
177 lines not on this page · raw message
arch/x86/kvm/x86.c +80 -80
575 lines not on this page · raw message
arch/x86/kvm/x86.h +1 -1
@@ -183,7 +183,7 @@ static inline bool x86_exception_has_error_code(unsigned int vector)static inline bool mmu_is_nested (struct kvm_vcpu *vcpu) { -return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu; +return vcpu->arch.private->walk_mmu == &vcpu->arch.private->nested_mmu; }static inline int is_pae (struct kvm_vcpu *vcpu)
virt/kvm/kvm_main.c +32 -6
@@ -109,6 +109,8 @@ static atomic_t hardware_enable_failed;static struct kmem_cache *kvm_vcpu_cache; +static struct kmem_cache *kvm_vcpu_private_cache; +static __read_mostly struct preempt_ops kvm_preempt_ops; static DEFINE_PER_CPU_ASI_NOT_SENSITIVE(struct kvm_vcpu *, kvm_running_vcpu);
@@ -457,6 +459,7 @@ void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)put_pid (rcu_dereference_protected (vcpu->pid,1 ));free_page ((unsigned long )vcpu->run); +kmem_cache_free (kvm_vcpu_private_cache, vcpu->arch.private);kmem_cache_free (kvm_vcpu_cache, vcpu); }EXPORT_SYMBOL_GPL (kvm_vcpu_destroy);
@@ -2392,7 +2395,7 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,* tail pages of non-compound higher order allocations, which * would then underflow the refcount when the caller does the * required put_page. Don't allow those pages here. -*/ +*/ if (!kvm_try_get_pfn (pfn)) r = -EFAULT ;
@@ -3562,17 +3565,25 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)if (r) goto vcpu_decrement; - vcpu =kmem_cache_zalloc (kvm_vcpu_cache,GFP_KERNEL_ACCOUNT ); + vcpu =kmem_cache_zalloc (kvm_vcpu_cache, +GFP_KERNEL_ACCOUNT | __GFP_GLOBAL_NONSENSITIVE);if (!vcpu) { r = -ENOMEM ; goto vcpu_decrement; } + vcpu->arch.private =kmem_cache_zalloc (kvm_vcpu_private_cache, +GFP_KERNEL | __GFP_LOCAL_NONSENSITIVE); +if (!vcpu->arch.private) { + r = -ENOMEM ; + goto vcpu_free; + } +BUILD_BUG_ON (sizeof (struct kvm_run ) >PAGE_SIZE ); page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_LOCAL_NONSENSITIVE);if (!page) { r = -ENOMEM ; - goto vcpu_free; + goto vcpu_private_free; } vcpu->run =page_address (page);
@@ -3631,6 +3642,8 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)kvm_arch_vcpu_destroy (vcpu); vcpu_free_run_page:free_page ((unsigned long )vcpu->run); +vcpu_private_free: +kmem_cache_free (kvm_vcpu_private_cache, vcpu->arch.private); vcpu_free:kmem_cache_free (kvm_vcpu_cache, vcpu); vcpu_decrement:
@@ -5492,7 +5505,7 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, vcpu_align = __alignof__(struct kvm_vcpu ); kvm_vcpu_cache = kmem_cache_create_usercopy("kvm_vcpu", vcpu_size, vcpu_align, - SLAB_ACCOUNT, + SLAB_ACCOUNT|SLAB_GLOBAL_NONSENSITIVE, offsetof(struct kvm_vcpu, arch), offsetofend(struct kvm_vcpu, stats_id) - offsetof(struct kvm_vcpu, arch),
@@ -5501,12 +5514,22 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, r = -ENOMEM ; goto out_free_3; } - + kvm_vcpu_private_cache =kmem_cache_create_usercopy ("kvm_vcpu_private" , +sizeof (struct kvm_vcpu_arch_private ), + __alignof__(struct kvm_vcpu_arch_private ), +SLAB_ACCOUNT |SLAB_LOCAL_NONSENSITIVE , +0 , +sizeof (struct kvm_vcpu_arch_private ), +NULL ); +if (!kvm_vcpu_private_cache) { + r = -ENOMEM ; + goto out_free_4; + }for_each_possible_cpu (cpu) {if (!alloc_cpumask_var_node (&per_cpu (cpu_kick_mask, cpu),GFP_KERNEL ,cpu_to_node (cpu))) { r = -ENOMEM ; - goto out_free_4; + goto out_free_vcpu_private_cache; } }
@@ -5541,6 +5564,8 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, out_free_5:for_each_possible_cpu (cpu)free_cpumask_var (per_cpu (cpu_kick_mask, cpu)); +out_free_vcpu_private_cache: +kmem_cache_destroy (kvm_vcpu_private_cache); out_free_4:kmem_cache_destroy (kvm_vcpu_cache); out_free_3:
@@ -5567,6 +5592,7 @@ void kvm_exit(void)misc_deregister (&kvm_dev);for_each_possible_cpu (cpu)free_cpumask_var (per_cpu (cpu_kick_mask, cpu)); +kmem_cache_destroy (kvm_vcpu_private_cache);kmem_cache_destroy (kvm_vcpu_cache);kvm_async_pf_deinit ();unregister_syscore_ops (&kvm_syscore_ops);
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 45/47 mm: asi: Mapping global nonsensitive areas in asi_global_init
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
There are several areas in memory which we consider non sensitive. These areas should be mapped in every ASI domain. We map there areas in asi_global_init(). We modified some of the linking scripts to ensure these areas are starting and ending on page boundaries.
The areas:
- _stext --> _etext
- __init_begin --> __init_end
- __start_rodata --> __end_rodata
- __start_once --> __end_once
- __start___ex_table --> __stop___ex_table
- __start_asi_nonsensitive --> __end_asi_nonsensitive
- __start_asi_nonsensitive_readmostly -->
__end_asi_nonsensitive_readmostly
- __vvar_page --> + PAGE_SIZE
- APIC_BASE --> + PAGE_SIZE
- phys_base --> + PAGE_SIZE
- __start___tracepoints_ptrs --> __stop___tracepoints_ptrs
- __start___tracepoint_str --> __stop___tracepoint_str
- __per_cpu_asi_start --> __per_cpu_asi_end (percpu)
- irq_stack_backing_store --> + sizeof(irq_stack_backing_store)(percpu)
The pgd's of the following addresses are cloned, modeled after KPTI:
- CPU_ENTRY_AREA_BASE
- ESPFIX_BASE_ADDR
--- arch/x86/kernel/head_64.S | 12 +++++ arch/x86/kernel/vmlinux.lds.S | 2 +- arch/x86/mm/asi.c | 82 +++++++++++++++++++++++++++++++ include/asm-generic/vmlinux.lds.h | 13 +++-- 4 files changed, 105 insertions(+), 4 deletions(-)
arch/x86/kernel/head_64.S +12 -0
@@ -574,9 +574,21 @@ SYM_DATA_LOCAL(early_gdt_descr_base, .quad INIT_PER_CPU_VAR(gdt_page)) .align 16 /* This must match the first entry in level2_kernel_pgt */ + +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +/* TODO: Find a way to mark .section for phys_base */ +/* Ideally, we want to map phys_base in .data..asi_non_sensitive. That doesn't + * seem to work properly. For now, we just make sure phys_base is in it's own + * page. */ + .align PAGE_SIZE +#endif SYM_DATA(phys_base, .quad 0x0) EXPORT_SYMBOL(phys_base) +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION + .align PAGE_SIZE +#endif + #include "../../x86/xen/xen-head.S" __PAGE_ALIGNED_BSS
arch/x86/kernel/vmlinux.lds.S +1 -1
@@ -148,8 +148,8 @@ SECTIONS } :text =0xcccc /* End of text section, which should occupy whole number of pages */ - _etext = .; . = ALIGN(PAGE_SIZE); + _etext = .; X86_ALIGN_RODATA_BEGIN RO_DATA(PAGE_SIZE)
arch/x86/mm/asi.c +82 -0
106 lines not on this page · raw message
include/asm-generic/vmlinux.lds.h +10 -3
@@ -235,8 +235,10 @@ #define TRACE_PRINTKS() __start___trace_bprintk_fmt = .; \ KEEP(*(__trace_printk_fmt)) /* Trace_printk fmt' pointer */ \ __stop___trace_bprintk_fmt = .; -#define TRACEPOINT_STR() __start___tracepoint_str = .; \ +#define TRACEPOINT_STR() . = ALIGN(PAGE_SIZE); \ + __start___tracepoint_str = .; \ KEEP(*(__tracepoint_str)) /* Trace_printk fmt' pointer */ \ + . = ALIGN(PAGE_SIZE); \ __stop___tracepoint_str = .;#else #define TRACE_PRINTKS ()
@@ -348,8 +350,10 @@ MEM_KEEP(init.data*) \ MEM_KEEP(exit.data*) \ *(.data.unlikely) \ + . = ALIGN(PAGE_SIZE); \ __start_once = .; \ *(.data.once) \ + . = ALIGN(PAGE_SIZE); \ __end_once = .; \ STRUCT_ALIGN(); \ *(__tracepoints) \
@@ -453,9 +457,10 @@ *(.rodata) *(.rodata.*) \ SCHED_DATA \ RO_AFTER_INIT_DATA /* Read only after init */ \ - . = ALIGN(8); \ + . = ALIGN(PAGE_SIZE); \ __start___tracepoints_ptrs = .; \ KEEP(*(__tracepoints_ptrs)) /* Tracepoints: pointer array */ \ + . = ALIGN(PAGE_SIZE); \ __stop___tracepoints_ptrs = .; \ *(__tracepoints_strings)/* Tracepoints: strings */ \ } \
@@ -671,11 +676,13 @@*/ #define EXCEPTION_TABLE (align) \ . = ALIGN(align); \ + . = ALIGN(PAGE_SIZE); \ __ex_table : AT(ADDR(__ex_table) - LOAD_OFFSET) { \ __start___ex_table = .; \ KEEP(*(__ex_table)) \ + . = ALIGN(PAGE_SIZE); \ __stop___ex_table = .; \ - } + } \/* * .BTF
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 46/47 kvm: asi: Do asi_exit() in vcpu_run loop before returning to userspace
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
For the time being, we switch to the full kernel address space before returning back to userspace. Once KPTI is also implemented using ASI, we could potentially also switch to the KPTI address space directly.
--- arch/x86/kvm/x86.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +5 -1
@@ -10148,13 +10148,17 @@ static int vcpu_run(struct kvm_vcpu *vcpu)srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); r =xfer_to_guest_mode_handle_work (vcpu);if (r) -return r; + goto exit; vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); } }srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); +exit: +/* TODO(oweisse): trace this exit if we're still within an ASI. */ +asi_exit (); +return r; }
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
RFC PATCH 47/47 mm: asi: Properly un/mapping task stack from ASI + tlb flush
In reply to Junaid ShahidFrom: Ofir Weisse <[email protected]>
There are several locations where this is important. Especially since a task_struct might be reused, potentially with a different mm.
1. Map in vcpu_run() @ arch/x86/kvm/x86.c
1. Unmap in release_task_stack() @ kernel/fork.c
2. Unmap in do_exit() @ kernel/exit.c
3. Unmap in begin_new_exec() @ fs/exec.c
--- arch/x86/include/asm/asi.h | 6 ++++ arch/x86/kvm/x86.c | 6 ++++ arch/x86/mm/asi.c | 59 ++++++++++++++++++++++++++++++++++++++ fs/exec.c | 7 ++++- include/asm-generic/asi.h | 16 +++++++++-- include/linux/sched.h | 5 ++++ kernel/exit.c | 2 +- kernel/fork.c | 22 +++++++++++++- 8 files changed, 118 insertions(+), 5 deletions(-)
arch/x86/include/asm/asi.h +6 -0
@@ -87,6 +87,12 @@ void asi_unmap_user(struct asi *asi, void *va, size_t len);int asi_fill_pgtbl_pool (struct asi_pgtbl_pool *pool,uint count,gfp_t flags);void asi_clear_pgtbl_pool (struct asi_pgtbl_pool *pool); +int asi_map_task_stack (struct task_struct *tsk,struct asi *asi); +void asi_unmap_task_stack (struct task_struct *tsk); +void asi_mark_pages_local_nonsensitive (struct page *pages,uint order, +struct mm_struct *mm); +void asi_clear_pages_local_nonsensitive (struct page *pages,uint order); +static inline void asi_init_pgtbl_pool (struct asi_pgtbl_pool *pool) { pool->pgtbl_list =NULL ;
arch/x86/kvm/x86.c +6 -0
@@ -10122,6 +10122,12 @@ static int vcpu_run(struct kvm_vcpu *vcpu) vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); vcpu->arch.l1tf_flush_l1d = true; +/* We must have current->stack mapped into asi. This function can be +* safely called many times, as it will only do the actual mapping once. */ + r =asi_map_task_stack (current, vcpu->kvm->asi); +if (r !=0 ) +return r; +for (;;) {if (kvm_vcpu_running (vcpu)) { r =vcpu_enter_guest (vcpu);
arch/x86/mm/asi.c +59 -0
68 lines not on this page · raw message
fs/exec.c +6 -1
@@ -69,6 +69,7 @@#include <linux/uaccess.h> #include <asm/mmu_context.h> #include <asm/tlb.h> +#include <asm/asi.h> #include <trace/events/task.h> #include "internal.h"
@@ -1238,7 +1239,11 @@ int begin_new_exec(struct linux_binprm * bprm)struct task_struct *me = current;int retval; -/* TODO: (oweisse) unmap the stack from ASI */ +/* The old mm is about to be released later on in exec_mmap. We are +* reusing the task, including its stack which was mapped to +* mm->asi_pgd[0]. We need to asi_unmap the stack, so the destructor of +* the mm won't complain on "lingering" asi mappings. */ +asi_unmap_task_stack (current);/* Once we are committed compute the creds */ retval =bprm_creds_from_file (bprm);
include/asm-generic/asi.h +14 -2
@@ -66,8 +66,13 @@ static inline struct asi *asi_get_target(void) { return NULL; }static inline struct asi *asi_get_current (void ) {return NULL ; } -static inline -int asi_map_gfp (struct asi *asi,void *addr,size_t len,gfp_t gfp_flags) +static inline int asi_map_task_stack (struct task_struct *tsk,struct asi *asi) +{return 0 ; } + +static inline void asi_unmap_task_stack (struct task_struct *tsk) { } + +static inline int asi_map_gfp (struct asi *asi,void *addr,size_t len, +gfp_t gfp_flags) {return 0 ; }
@@ -130,6 +135,13 @@ static inline int asi_load_module(struct module* module) {return 0;}static inline void asi_unload_module (struct module * module) { } +static inline +void asi_mark_pages_local_nonsensitive (struct page *pages,uint order, +struct mm_struct *mm) { } + +static inline +void asi_clear_pages_local_nonsensitive (struct page *pages,uint order) { } +#endif /* !_ASSEMBLY_ */ #endif /* !CONFIG_ADDRESS_SPACE_ISOLATION */
include/linux/sched.h +5 -0
@@ -67,6 +67,7 @@ struct sighand_struct;struct signal_struct ;struct task_delay_info ;struct task_group ; +struct asi ;/* * Task state bitmask. NOTE! These bits are also
@@ -1470,6 +1471,10 @@ struct task_struct {int mce_count;#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +struct asi *asi_stack_mapped; +#endif +#ifdef CONFIG_KRETPROBES struct llist_head kretprobe_instances;#endif
kernel/exit.c +1 -1
@@ -768,7 +768,7 @@ void __noreturn do_exit(long code)profile_task_exit (tsk);kcov_task_exit (tsk); -/* TODO: (oweisse) unmap the stack from ASI */ +asi_unmap_task_stack (tsk);coredump_task_exit (tsk);ptrace_event (PTRACE_EVENT_EXIT , code);
kernel/fork.c +21 -1
@@ -216,7 +216,6 @@ static int free_vm_stack_cache(unsigned int cpu)static unsigned long *alloc_thread_stack_node (struct task_struct *tsk,int node) { -/* TODO: (oweisse) Add annotation to map the stack into ASI */ #ifdef CONFIG_VMAP_STACK void *stack;int i;
@@ -269,7 +268,16 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)struct page *page =alloc_pages_node (node,THREADINFO_GFP ,THREAD_SIZE_ORDER ); +/* When marking pages as PageLocalNonSesitive we set the page->mm to be +* NULL. We must make sure the flag is cleared from the stack pages +* before free_pages is called. Otherwise, page->mm will be accessed +* which will reuslt in NULL reference. page_address() below will yield +* an aliased address after ASI_LOCAL_MAP, thanks to +* PageLocalNonSesitive flag. */ if (likely (page)) { +asi_mark_pages_local_nonsensitive (page, +THREAD_SIZE_ORDER , +NULL ); tsk->stack =kasan_reset_tag (page_address (page));return tsk->stack; }
@@ -301,6 +309,14 @@ static inline void free_thread_stack(struct task_struct *tsk) }#endif +/* We must clear the PageNonSensitive flag before calling free_pages(). +* Otherwise page->mm (which is NULL) will be accessed, in order to +* unmap the pages from ASI. Specifically for the stack, we assume the +* pages were already unmapped from ASI before we got here, via +* asi_unmap_task_stack(). */ +asi_clear_pages_local_nonsensitive (virt_to_page (tsk->stack), +THREAD_SIZE_ORDER ); +__free_pages (virt_to_page (tsk->stack),THREAD_SIZE_ORDER ); }# else
@@ -436,6 +452,7 @@ static void release_task_stack(struct task_struct *tsk)if (WARN_ON (READ_ONCE (tsk->__state) !=TASK_DEAD ))return ;/* Better to leak the stack than to free prematurely */ +asi_unmap_task_stack (tsk);account_kernel_stack (tsk,-1 );free_thread_stack (tsk); tsk->stack =NULL ;
@@ -916,6 +933,9 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)* functions again. */ tsk->stack = stack; +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION + tsk->asi_stack_mapped =NULL ; +#endif #ifdef CONFIG_VMAP_STACK tsk->stack_vm_area = stack_vm_area;#endif
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
Quoted 17 lines · Junaid Shahid
This patch series is a proof-of-concept RFC for an end-to-end implementation of Address Space Isolation for KVM. It has similar goals and a somewhat similar high-level design as the original ASI patches from Alexandre Chartre ([1],[2],[3],[4]), but with a different underlying implementation. This also includes several memory management changes to help with differentiating between sensitive and non-sensitive memory and mapping of non-sensitive memory into the ASI restricted address spaces.
This RFC is intended as a demonstration of what a full ASI implementation for KVM could look like, not necessarily as a direct proposal for what might eventually be merged. In particular, these patches do not yet implement KPTI on top of ASI, although the framework is generic enough to be able to support it. Similarly, these patches do not include non-sensitive annotations for data structures that did not get frequently accessed during execution of our test workloads, but the framework is designed such that new non-sensitive memory annotations can be added trivially.
Quoted 2 lines
The patches apply on top of Linux v5.16. These patches are also available via gerrit at https://linux-review.googlesource.com/q/topic:asi-rfc.
[+Cc slab maintainers/reviewers]
Please Cc relevant people.
patch 14, 24, 31 need to be reviewed by slab people :)
Quoted 446 lines
Background
==========
Address Space Isolation is a comprehensive security mitigation for several types of speculative execution attacks. Even though the kernel already has several speculative execution vulnerability mitigations, some of them can be quite expensive if enabled fully e.g. to fully mitigate L1TF using the existing mechanisms requires doing an L1 cache flush on every single VM entry as well as disabling hyperthreading altogether. (Although core scheduling can provide some protection when hyperthreading is enabled, it is not sufficient by itself to protect against all leaks unless sibling hyperthread stunning is also performed on every VM exit.) ASI provides a much less expensive mitigation for such vulnerabilities while still providing an almost similar level of protection.There are a couple of basic insights/assumptions behind ASI:
1. Most execution paths in the kernel (especially during virtual machine execution) access only memory that is not particularly sensitive even if it were to get leaked to the executing process/VM (setting aside for a moment what exactly should be considered sensitive or non-sensitive).
2. Even when executing speculatively, the CPU can generally only bring memory that is mapped in the current page tables into its various caches and internal buffers.Given these, the idea of using ASI to thwart speculative attacks is that we can execute the kernel using a restricted set of page tables most of the time and switch to the full unrestricted kernel address space only when the kernel needs to access something that is not mapped in the restricted address space. And we keep track of when a switch to the full kernel address space is done, so that before returning back to the process/VM, we can switch back to the restricted address space. In the paths where the kernel is able to execute entirely while remaining in the restricted address space, we can skip other mitigations for speculative execution attacks (such as L1 cache / micro-arch buffer flushes, sibling hyperthread stunning etc.). Only in the cases where we do end up switching the page tables, we perform these more expensive mitigations. Assuming that happens relatively infrequently, the performance can be significantly better compared to performing these mitigations all the time.
Please note that although we do have a sibling hyperthread stunning implementation internally, which is fully integrated with KVM-ASI, it is not included in this RFC for the time being. The earlier upstream proposal for sibling stunning [6] could potentially be integrated into an upstream ASI implementation.
Basic concepts
==============
Different types of restricted address spaces are represented by different ASI classes. For instance, KVM-ASI is an ASI class used during VM execution. KPTI would be another ASI class. An ASI instance (struct asi) represents a single restricted address space. There is a separate ASI instance for each untrusted context (e.g. a userspace process, a VM, or even a single VCPU etc.) Note that there can be multiple untrusted security contexts (and thus multiple restricted address spaces) within a single process e.g. in the case of VMs, the userspace process is a different security context than the guest VM, and in principle, even each VCPU could be considered a separate security context (That would be primarily useful for securing nested virtualization).In this RFC, a process can have at most one ASI instance of each class, though this is not an inherent limitation and multiple instances of the same class should eventually be supported. (A process can still have ASI instances of different classes e.g. KVM-ASI and KPTI.) In fact, in principle, it is not even entirely necessary to tie an ASI instance to a process. That is just a simplification for the initial implementation.
An
asi_enteroperation switches into the restricted address space represented by the given ASI instance. Anasi_exitoperation switches to the full unrestricted kernel address space. Each ASI class can provide hooks to be executed during these operations, which can be used to perform speculative attack mitigations relevant to that class. For instance, the KVM-ASI hooks would perform a sibling-hyperthread-stun operation in theasi_exithook, and L1-flush/MDS-clear and sibling-hyperthread-unstun operations in theasi_enterhook. On the other hand, the hooks for the KPTI class would be NO-OP, since the switching of the page tables is enough mitigation in that case.If the kernel attempts to access memory that is not mapped in the currently active ASI instance, the page fault handler automatically performs an
asi_exitoperation. This means that except for a few critical pieces of memory, leaving something out of an unrestricted address space will result in only a performance hit, rather than a catastrophic failure. The kernel can also perform explicitasi_exitoperations in some paths as needed.Apart from the page fault handler, other exceptions and interrupts (even NMIs) do not automatically cause an
asi_exitand could potentially be executed completely within a restricted address space if they don't end up accessing any sensitive piece of memory.The mappings within a restricted address space are always a subset of the full kernel address space and each mapping is always the same as the corresponding mapping in the full kernel address space. This is necessary because we could potentially end up performing an
asi_exitat any point.Although this RFC only includes an implementation of the KVM-ASI class, a KPTI class could also be implemented on top of the same infrastructure. Furthermore, in the future we could also implement a KPTI-Next class that actually uses the ASI model for userspace processes i.e. mapping non-sensitive kernel memory in the restricted address space and trying to execute most syscalls/interrupts without switching to the full kernel address space, as opposed to the current KPTI which requires an address space switch on every kernel/user mode transition.
Memory classification
=====================
We divide memory into three categories.1. Sensitive memory
This is memory that should never get leaked to any process or VM. Sensitive memory is only mapped in the unrestricted kernel page tables. By default, all memory is considered sensitive unless specifically categorized otherwise.2. Globally non-sensitive memory
This is memory that does not present a substantial security threat even if it were to get leaked to any process or VM in the system. Globally non-sensitive memory is mapped in the restricted address spaces for all processes.3. Locally non-sensitive memory
This is memory that does not present a substantial security threat if it were to get leaked to the currently running process or VM, but would present a security issue if it were to get leaked to any other process or VM in the system. Examples include userspace memory (or guest memory in the case of VMs) or kernel structures containing userspace/guest register context etc. Locally non-sensitive memory is mapped only in the restricted address space of a single process.Various mechanisms are provided to annotate different types of memory (static, buddy allocator, slab, vmalloc etc.) as globally or locally non-sensitive. In addition, the ASI infrastructure takes care to ensure that different classes of memory do not share the same physical page. This includes separation of sensitive, globally non-sensitive and locally non-sensitive memory into different pages and also separation of locally non-sensitive memory for different processes into different pages as well.
What exactly should be considered non-sensitive (either globally or locally) is somewhat open-ended. Some things are clearly sensitive or non-sensitive, but many things also fall into a gray area, depending on how paranoid one wants to be. For this proof of concept, we have generally treated such things as non-sensitive, though that may not necessarily be the ideal classification in each case. Similarly, there is also a gray area between globally and locally non-sensitive classifications in some cases, and in those cases this RFC has mostly erred on the side of marking them as locally non-sensitive, even though many of those cases could likely be safely classified as globally non-sensitive.
Although this implementation includes fairly extensive support for marking most types of dynamically allocated memory as locally non-sensitive, it is possibly feasible, at least for KVM-ASI, to get away with a simpler implementation (such as [5]), if we are very selective about what memory we treat as locally non-sensitive (as opposed to globally non-sensitive). Nevertheless, the more general mechanism is included in this proof of concept as an illustration for what could be done if we really needed to treat any arbitrary kernel memory as locally non-sensitive.
It is also possible to have ASI classes that do not utilize the above described infrastructure and instead manage all the memory mappings inside the restricted address space on their own.
References
==========
[1] https://lore.kernel.org/lkml/[email protected]
[2] https://lore.kernel.org/lkml/[email protected]
[3] https://lore.kernel.org/lkml/[email protected]
[4] https://lore.kernel.org/lkml/[email protected]
[5] https://lore.kernel.org/lkml/[email protected]
[6] https://lore.kernel.org/lkml/[email protected]Cc Paul Turner <[email protected]>Cc Jim Mattson <[email protected]>Cc Alexandre Chartre <[email protected]>Cc Mike Rapoport <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Dave Hansen <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Andy Lutomirski <[email protected]>Junaid Shahid (32):
mm: asi: Introduce ASI core API
mm: asi: Add command-line parameter to enable/disable ASI
mm: asi: Switch to unrestricted address space when entering scheduler
mm: asi: ASI support in interrupts/exceptions
mm: asi: Make__get_current_cr3_fast()ASI-aware
mm: asi: ASI page table allocation and free functions
mm: asi: Functions to map/unmap a memory range into ASI page tables
mm: asi: Add basic infrastructure for global non-sensitive mappings
mm: Add__PAGEFLAG_FALSE
mm: asi: Support for global non-sensitive direct map allocations
mm: asi: Global non-sensitive vmalloc/vmap support
mm: asi: Support for global non-sensitive slab caches
mm: asi: Disable ASI API when ASI is not enabled for a process
kvm: asi: Restricted address space for VM execution
mm: asi: Support for mapping non-sensitive pcpu chunks
mm: asi: Aliased direct map for local non-sensitive allocations
mm: asi: Support for pre-ASI-init local non-sensitive allocations
mm: asi: Support for locally nonsensitive page allocations
mm: asi: Support for locally non-sensitive vmalloc allocations
mm: asi: Add support for locally non-sensitiveVM_USERMAPpages
mm: asi: Add support for mapping all userspace memory into ASI
mm: asi: Support for local non-sensitive slab caches
mm: asi: Avoid warning from NMI userspace accesses in ASI context
mm: asi: Use separate PCIDs for restricted address spaces
mm: asi: Avoid TLB flushes during ASI CR3 switches when possible
mm: asi: Avoid TLB flush IPIs to CPUs not in ASI context
mm: asi: Reduce TLB flushes when freeing pages asynchronously
mm: asi: Add API for mapping userspace address ranges
mm: asi: Support for non-sensitive SLUB caches
x86: asi: Allocate FPU state separately when ASI is enabled.
kvm: asi: Map guest memory into restricted ASI address space
kvm: asi: Unmap guest memory from ASI address space when using nested
virtOfir Weisse (15):
asi: Added ASI memory cgroup flag
mm: asi: Added refcounting when initilizing an asi
mm: asi:asi_exit()on PF, skip handling if address is accessible
mm: asi: Adding support for dynamic percpu ASI allocations
mm: asi: ASI annotation support for static variables.
mm: asi: ASI annotation support for dynamic modules.
mm: asi: Skip conventional L1TF/MDS mitigations
mm: asi: support for static percpu DEFINE_PER_CPU*_ASI
mm: asi: Annotation of static variables to be nonsensitive
mm: asi: Annotation of PERCPU variables to be nonsensitive
mm: asi: Annotation of dynamic variables to be nonsensitive
kvm: asi: Splittingkvm_vcpu_archinto non/sensitive parts
mm: asi: Mapping global nonsensitive areas inasi_global_init
kvm: asi: Doasi_exit()invcpu_runloop before returning to userspace
mm: asi: Properly un/mapping task stack from ASI + tlb flusharch/alpha/include/asm/Kbuild | 1 + arch/arc/include/asm/Kbuild | 1 + arch/arm/include/asm/Kbuild | 1 + arch/arm64/include/asm/Kbuild | 1 + arch/csky/include/asm/Kbuild | 1 + arch/h8300/include/asm/Kbuild | 1 + arch/hexagon/include/asm/Kbuild | 1 + arch/ia64/include/asm/Kbuild | 1 + arch/m68k/include/asm/Kbuild | 1 + arch/microblaze/include/asm/Kbuild | 1 + arch/mips/include/asm/Kbuild | 1 + arch/nds32/include/asm/Kbuild | 1 + arch/nios2/include/asm/Kbuild | 1 + arch/openrisc/include/asm/Kbuild | 1 + arch/parisc/include/asm/Kbuild | 1 + arch/powerpc/include/asm/Kbuild | 1 + arch/riscv/include/asm/Kbuild | 1 + arch/s390/include/asm/Kbuild | 1 + arch/sh/include/asm/Kbuild | 1 + arch/sparc/include/asm/Kbuild | 1 + arch/um/include/asm/Kbuild | 1 + arch/x86/events/core.c | 6 +- arch/x86/events/intel/bts.c | 2 +- arch/x86/events/intel/core.c | 2 +- arch/x86/events/msr.c | 2 +- arch/x86/events/perf_event.h | 4 +- arch/x86/include/asm/asi.h | 215 ++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/current.h | 2 +- arch/x86/include/asm/debugreg.h | 2 +- arch/x86/include/asm/desc.h | 2 +- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/fpu/api.h | 3 +- arch/x86/include/asm/hardirq.h | 2 +- arch/x86/include/asm/hw_irq.h | 2 +- arch/x86/include/asm/idtentry.h | 25 +- arch/x86/include/asm/kvm_host.h | 124 +- arch/x86/include/asm/page.h | 19 +- arch/x86/include/asm/page_64.h | 27 +- arch/x86/include/asm/page_64_types.h | 20 + arch/x86/include/asm/percpu.h | 2 +- arch/x86/include/asm/pgtable_64_types.h | 10 + arch/x86/include/asm/preempt.h | 2 +- arch/x86/include/asm/processor.h | 17 +- arch/x86/include/asm/smp.h | 2 +- arch/x86/include/asm/tlbflush.h | 49 +- arch/x86/include/asm/topology.h | 2 +- arch/x86/kernel/alternative.c | 2 +- arch/x86/kernel/apic/apic.c | 2 +- arch/x86/kernel/apic/x2apic_cluster.c | 8 +- arch/x86/kernel/cpu/bugs.c | 2 +- arch/x86/kernel/cpu/common.c | 12 +- arch/x86/kernel/e820.c | 7 +- arch/x86/kernel/fpu/core.c | 47 +- arch/x86/kernel/fpu/init.c | 7 +- arch/x86/kernel/fpu/internal.h | 1 + arch/x86/kernel/fpu/xstate.c | 21 +- arch/x86/kernel/head_64.S | 12 + arch/x86/kernel/hw_breakpoint.c | 2 +- arch/x86/kernel/irq.c | 2 +- arch/x86/kernel/irqinit.c | 2 +- arch/x86/kernel/nmi.c | 6 +- arch/x86/kernel/process.c | 13 +- arch/x86/kernel/setup.c | 4 +- arch/x86/kernel/setup_percpu.c | 4 +- arch/x86/kernel/smp.c | 2 +- arch/x86/kernel/smpboot.c | 3 +- arch/x86/kernel/traps.c | 2 + arch/x86/kernel/tsc.c | 10 +- arch/x86/kernel/vmlinux.lds.S | 2 +- arch/x86/kvm/cpuid.c | 18 +- arch/x86/kvm/kvm_cache_regs.h | 22 +- arch/x86/kvm/lapic.c | 11 +- arch/x86/kvm/mmu.h | 16 +- arch/x86/kvm/mmu/mmu.c | 209 ++-- arch/x86/kvm/mmu/mmu_internal.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 40 +- arch/x86/kvm/mmu/spte.c | 6 +- arch/x86/kvm/mmu/spte.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 14 +- arch/x86/kvm/mtrr.c | 2 +- arch/x86/kvm/svm/nested.c | 34 +- arch/x86/kvm/svm/sev.c | 70 +- arch/x86/kvm/svm/svm.c | 52 +- arch/x86/kvm/trace.h | 10 +- arch/x86/kvm/vmx/capabilities.h | 14 +- arch/x86/kvm/vmx/nested.c | 90 +- arch/x86/kvm/vmx/vmx.c | 152 ++- arch/x86/kvm/x86.c | 315 +++-- arch/x86/kvm/x86.h | 4 +- arch/x86/mm/Makefile | 1 + arch/x86/mm/asi.c | 1397 ++++++++++++++++++++++ arch/x86/mm/fault.c | 67 +- arch/x86/mm/init.c | 7 +- arch/x86/mm/init_64.c | 26 +- arch/x86/mm/kaslr.c | 34 +- arch/x86/mm/mm_internal.h | 5 + arch/x86/mm/physaddr.c | 8 + arch/x86/mm/tlb.c | 419 ++++++- arch/xtensa/include/asm/Kbuild | 1 + fs/binfmt_elf.c | 2 +- fs/eventfd.c | 2 +- fs/eventpoll.c | 10 +- fs/exec.c | 7 + fs/file.c | 3 +- fs/timerfd.c | 2 +- include/asm-generic/asi.h | 149 +++ include/asm-generic/irq_regs.h | 2 +- include/asm-generic/percpu.h | 6 + include/asm-generic/vmlinux.lds.h | 36 +- include/linux/arch_topology.h | 2 +- include/linux/debug_locks.h | 4 +- include/linux/gfp.h | 13 +- include/linux/hrtimer.h | 2 +- include/linux/interrupt.h | 2 +- include/linux/jiffies.h | 4 +- include/linux/kernel_stat.h | 4 +- include/linux/kvm_host.h | 7 +- include/linux/kvm_types.h | 3 + include/linux/memcontrol.h | 3 + include/linux/mm_types.h | 59 + include/linux/module.h | 15 + include/linux/notifier.h | 2 +- include/linux/page-flags.h | 19 + include/linux/percpu-defs.h | 39 + include/linux/percpu.h | 8 +- include/linux/pgtable.h | 3 + include/linux/prandom.h | 2 +- include/linux/profile.h | 2 +- include/linux/rcupdate.h | 4 +- include/linux/rcutree.h | 2 +- include/linux/sched.h | 5 + include/linux/sched/mm.h | 12 + include/linux/sched/sysctl.h | 1 + include/linux/slab.h | 68 +- include/linux/slab_def.h | 4 + include/linux/slub_def.h | 6 + include/linux/vmalloc.h | 16 +- include/trace/events/mmflags.h | 14 +- init/main.c | 2 +- kernel/cgroup/cgroup.c | 9 +- kernel/cpu.c | 14 +- kernel/entry/common.c | 6 + kernel/events/core.c | 25 +- kernel/exit.c | 2 + kernel/fork.c | 69 +- kernel/freezer.c | 2 +- kernel/irq_work.c | 6 +- kernel/locking/lockdep.c | 14 +- kernel/module-internal.h | 1 + kernel/module.c | 210 +++- kernel/panic.c | 2 +- kernel/printk/printk.c | 4 +- kernel/profile.c | 4 +- kernel/rcu/srcutree.c | 3 +- kernel/rcu/tree.c | 12 +- kernel/rcu/update.c | 4 +- kernel/sched/clock.c | 2 +- kernel/sched/core.c | 23 +- kernel/sched/cpuacct.c | 10 +- kernel/sched/cpufreq.c | 3 +- kernel/sched/cputime.c | 4 +- kernel/sched/fair.c | 7 +- kernel/sched/loadavg.c | 2 +- kernel/sched/rt.c | 2 +- kernel/sched/sched.h | 25 +- kernel/sched/topology.c | 28 +- kernel/smp.c | 26 +- kernel/softirq.c | 5 +- kernel/time/hrtimer.c | 4 +- kernel/time/jiffies.c | 8 +- kernel/time/ntp.c | 30 +- kernel/time/tick-common.c | 6 +- kernel/time/tick-internal.h | 6 +- kernel/time/tick-sched.c | 4 +- kernel/time/timekeeping.c | 10 +- kernel/time/timekeeping.h | 2 +- kernel/time/timer.c | 4 +- kernel/trace/ring_buffer.c | 5 +- kernel/trace/trace.c | 4 +- kernel/trace/trace_preemptirq.c | 2 +- kernel/trace/trace_sched_switch.c | 4 +- kernel/tracepoint.c | 2 +- kernel/watchdog.c | 12 +- lib/debug_locks.c | 5 +- lib/irq_regs.c | 2 +- lib/radix-tree.c | 6 +- lib/random32.c | 3 +- mm/init-mm.c | 2 + mm/internal.h | 3 + mm/memcontrol.c | 37 +- mm/memory.c | 4 +- mm/page_alloc.c | 204 +++- mm/percpu-internal.h | 23 +- mm/percpu-km.c | 5 +- mm/percpu-vm.c | 57 +- mm/percpu.c | 273 ++++- mm/slab.c | 42 +- mm/slab.h | 166 ++- mm/slab_common.c | 461 ++++++- mm/slub.c | 140 ++- mm/sparse.c | 4 +- mm/util.c | 3 +- mm/vmalloc.c | 193 ++- net/core/skbuff.c | 2 +- net/core/sock.c | 2 +- security/Kconfig | 12 + tools/perf/builtin-kmem.c | 2 + virt/kvm/coalesced_mmio.c | 2 +- virt/kvm/eventfd.c | 5 +- virt/kvm/kvm_main.c | 61 +- 211 files changed, 5727 insertions(+), 959 deletions(-) create mode 100644 arch/x86/include/asm/asi.h create mode 100644 arch/x86/mm/asi.c create mode 100644 include/asm-generic/asi.hSignature · 1 line
2.35.1.473.g83b2b277ed-goog
Signature · 2 lines
Thank you, You are awesome! Hyeonggon :-)
Re: [RFC PATCH 04/47] mm: asi: ASI support in interrupts/exceptions
In reply to Junaid Shahidarch/x86/include/asm/idtentry.h static __always_inline void __##func(struct pt_regs *regs, \ · line 141
Quoted 7 lines · Junaid Shahid
#define
DEFINE_IDTENTRY_RAW(func)\arch/x86/include/asm/idtentry.h +5
-1-__visible noinstr void func(struct pt_regs *regs) +static __always_inline void __##func(struct pt_regs *regs); \ + \ +__visible noinstr void func(struct pt_regs *regs) \ +{ \ + asi_intr_enter(); \
This is wrong. You cannot invoke arbitrary code within a noinstr section.
Please enable CONFIG_VMLINUX_VALIDATION and watch the build result with and without your patches.
Thanks,
tglx
Re: [RFC PATCH 04/47] mm: asi: ASI support in interrupts/exceptions
Quoted 12 lines · Thomas Gleixner
arch/x86/include/asm/idtentry.h static __always_inline void __##func(struct pt_regs *regs, \ · line 141
Quoted 7 lines · Junaid Shahid
#define DEFINE_IDTENTRY_RAW(func) \arch/x86/include/asm/idtentry.h +5
-1-__visible noinstr void func(struct pt_regs *regs) +static __always_inline void __##func(struct pt_regs *regs); \ + \ +__visible noinstr void func(struct pt_regs *regs) \ +{ \ + asi_intr_enter(); \This is wrong. You cannot invoke arbitrary code within a noinstr section.
Quoted 6 lines
Please enable
CONFIG_VMLINUX_VALIDATIONand watch the build result with and without your patches.Thanks,
tglx
Thank you for the pointer. It seems that marking asi_intr_enter/exit and asi_enter/exit, and the few functions that they in turn call, as noinstr would fix this, correct? (Along with removing the VM_BUG_ONs from those functions and using notrace/nodebug variants of a couple of functions).
Thanks,
Junaid
Re: [RFC PATCH 04/47] mm: asi: ASI support in interrupts/exceptions
Junaid,
Quoted 13 lines · Junaid Shahid
Quoted 12 lines · Thomas Gleixner
arch/x86/include/asm/idtentry.h static __always_inline void __##func(struct pt_regs *regs, \ · line 141
Quoted 7 lines · Junaid Shahid
#define DEFINE_IDTENTRY_RAW(func) \arch/x86/include/asm/idtentry.h +5
-1-__visible noinstr void func(struct pt_regs *regs) +static __always_inline void __##func(struct pt_regs *regs); \ + \ +__visible noinstr void func(struct pt_regs *regs) \ +{ \ + asi_intr_enter(); \This is wrong. You cannot invoke arbitrary code within a noinstr section.
Quoted 8 lines
Quoted 3 lines
Please enable
CONFIG_VMLINUX_VALIDATIONand watch the build result with and without your patches.Thank you for the pointer. It seems that marking asi_intr_enter/exit and asi_enter/exit, and the few functions that they in turn call, as noinstr would fix this, correct? (Along with removing the
VM_BUG_ONsfrom those functions and using notrace/nodebug variants of a couple of functions).
you can keep the BUG_ON()s. If such a bug happens the noinstr correctness is the least of your worries, but it's important to get the information out, right?
Vs. adding noinstr. Yes, making the full callchain noinstr is going to cure it, but you really want to think hard whether these calls need to be in this section of the exception handlers.
These code sections have other constraints aside of being excluded from instrumentation, the main one being that you cannot use RCU there.
I'm not yet convinced that asi_intr_enter()/exit() need to be invoked in exactly the places you put it. The changelog does not give any clue about the why...
Thanks,
tglx
Re: [RFC PATCH 04/47] mm: asi: ASI support in interrupts/exceptions
Hi Thomas,
Quoted 13 lines · Thomas Gleixner
Quoted 4 lines
Quoted 4 lines
This is wrong. You cannot invoke arbitrary code within a noinstr section.
Quoted 8 lines
Quoted 3 lines
Please enable
CONFIG_VMLINUX_VALIDATIONand watch the build result with and without your patches.Thank you for the pointer. It seems that marking asi_intr_enter/exit and asi_enter/exit, and the few functions that they in turn call, as noinstr would fix this, correct? (Along with removing the
VM_BUG_ONsfrom those functions and using notrace/nodebug variants of a couple of functions).
Quoted 3 lines
you can keep the BUG_ON()s. If such a bug happens the noinstr correctness is the least of your worries, but it's important to get the information out, right?
Yes, that makes sense :)
Quoted 7 lines
Vs. adding noinstr. Yes, making the full callchain noinstr is going to cure it, but you really want to think hard whether these calls need to be in this section of the exception handlers.
These code sections have other constraints aside of being excluded from instrumentation, the main one being that you cannot use RCU there.
Neither of these functions need to use RCU, so that should be ok. Are there any other constraints that could matter here?
Quoted 4 lines
I'm not yet convinced that asi_intr_enter()/exit() need to be invoked in exactly the places you put it. The changelog does not give any clue about the why...
I had to place these calls early in the exception/interrupt handlers and specifically before the point where things like tracing and lockdep etc. can be used (and after the point where they can no longer be used, for the asi_intr_exit() calls). Otherwise, we would need to map all data structures touched by the tracing/lockdep infrastructure into the ASI restricted address spaces.
Basically, in general, if while running in a restricted address space, some kernel code touches some memory which is not mapped in the restricted address space, it will take an implicit ASI Exit via the page fault handler and continue running, so it would just be a small performance hit, but not a fatal issue. But there are 3 critical code regions where this implicit ASI Exit mechanism doesn't apply. The first is the region between an asi_enter() call and the asi_set_target_unrestricted() call. The second is the region between the start of an interrupt/exception handler and the asi_intr_enter() call, and the third is the region between the asi_intr_exit() call and the IRET. So any memory that is accessed by the code in these regions has to be mapped in the restricted address space, which is why I tried to place the asi_intr_enter/exit calls fairly early/late in the handlers. It is possible to move them further in, but if we accidentally miss annotating some data needed in that region, then it could potentially be fatal in some situations.
Thanks,
Junaid
Quoted 7 lines
Thanks,
tglx
Hi Junaid,
Quoted 17 lines · Junaid Shahid
This patch series is a proof-of-concept RFC for an end-to-end implementation of Address Space Isolation for KVM. It has similar goals and a somewhat similar high-level design as the original ASI patches from Alexandre Chartre ([1],[2],[3],[4]), but with a different underlying implementation. This also includes several memory management changes to help with differentiating between sensitive and non-sensitive memory and mapping of non-sensitive memory into the ASI restricted address spaces.
This RFC is intended as a demonstration of what a full ASI implementation for KVM could look like, not necessarily as a direct proposal for what might eventually be merged. In particular, these patches do not yet implement KPTI on top of ASI, although the framework is generic enough to be able to support it. Similarly, these patches do not include non-sensitive annotations for data structures that did not get frequently accessed during execution of our test workloads, but the framework is designed such that new non-sensitive memory annotations can be added trivially.
Quoted 3 lines
The patches apply on top of Linux v5.16. These patches are also available via gerrit at https://linux-review.googlesource.com/q/topic:asi-rfc.
Sorry for the late answer, and thanks for investigating possible ASI implementations. I have to admit I put ASI on the back-burner for a while because I am more and more wondering if the complexity of ASI is worth the benefit, especially given challenges to effectively exploit flaws that ASI is expected to mitigate, in particular when VMs are running on dedicated cpu cores, or when core scheduling is used. So I have been looking at a more simplistic approach (see below, A Possible Alternative to ASI).
But first, your implementation confirms that KVM-ASI can be broken up into different parts: pagetable management, ASI core and sibling cpus synchronization.
Pagetable Management
====================
For ASI, we need to build a pagetable with a subset of the kernel pagetable mappings. Your solution is interesting as it is provides a broad solution and also works well with dynamic allocations (while my approach to copy mappings had several limitations). The drawback is the extend of your changes which spread over all the mm code (while the simple solution to copy mappings can be done with a few self-contained independent functions).
ASI Core
========
KPTI
----
Implementing KPTI with ASI is possible but this is not straight forward. This requires some special handling in particular in the assembly kernel entry/exit code for syscall, interrupt and exception (see ASI RFC v4 [4] as an example) because we are also switching privilege level in addition of switching the pagetable. So this might be something to consider early in your implementation to ensure it is effectively compatible with KPTI.
Going beyond KPTI (with a KPTI-next) and trying to execute most syscalls/interrupts without switching to the full kernel address space is more challenging, because it would require much more kernel mapping in the user pagetable, and this would basically defeat the purpose of KPTI. You can refer to discussions about the RFC to defer CR3 switch to C code [7] which was an attempt to just reach the kernel entry C code with a KPTI pagetable.
Interrupts/Exceptions
---------------------
As long as interrupts/exceptions are not expected to be processed with ASI, it is probably better to explicitly exit ASI before processing an interrupt/exception, otherwise you will have an extra overhead on each interrupt/exception to take a page fault and then exit ASI.
This is particularily true if you have want to have KPTI use ASI, and in that case the ASI exit will need to be done early in the interrupt and exception assembly entry code.
ASI Hooks
---------
ASI hooks are certainly a good idea to perform specific actions on ASI enter or exit. However, I am not sure they are appropriate places for cpus stunning with KVM-ASI. That's because cpus stunning doesn't need to be done precisely when entering and exiting ASI, and it probably shouldn't be done there: it should be done right before VMEnter and right after VMExit (see below).
Sibling CPUs Synchronization
============================
KVM-ASI requires the synchronization of sibling CPUs from the same CPU core so that when a VM is running then sibling CPUs are running with the ASI associated with this VM (or an ASI compatible with the VM, depending on how ASI is defined). That way the VM can only spy on data from ASI and won't be able to access any sensitive data.
So, right before entering a VM, KVM should ensures that sibling CPUs are using ASI. If a sibling CPU is not using ASI then KVM can either wait for that sibling to run ASI, or force it to use ASI (or to become idle). This behavior should be enforced as long as any sibling is running the VM. When all siblings are not running the VM then other siblings can run any code (using ASI or not).
I would be interesting to see the code you use to achieve this, because I don't get how this is achieved from the description of your sibling hyperthread stun and unstun mechanism.
Note that this synchronization is critical for ASI to work, in particular when entering the VM, we need to be absolutely sure that sibling CPUs are effectively using ASI. The core scheduling sibling stunning code you referenced [6] uses a mechanism which is fine for userspace synchronization (the delivery of the IPI forces the sibling to immediately enter the kernel) but this won't work for ASI as the delivery of the IPI won't guarantee that the sibling as enter ASI yet. I did some experiments that show that data will leak if siblings are not perfectly synchronized.
A Possible Alternative to ASI?
=============================
ASI prevents access to sensitive data by unmapping them. On the other hand, the KVM code somewhat already identifies access to sensitive data as part of the L1TF/MDS mitigation, and when KVM is about to access sensitive data then it sets l1tf_flush_l1d to true (so that L1D gets flushed before VMEnter).
With KVM knowing when it accesses sensitive data, I think we can provide the same mitigation as ASI by simply allowing KVM code which doesn't access sensitive data to be run concurrently with a VM. This can be done by tagging the kernel thread when it enters KVM code which doesn't access sensitive data, and untagging the thread right before it accesses sensitive data. And when KVM is about to do a VMEnter then we synchronize siblings CPUs so that they run threads with the same tag. Sounds familar? Yes, because that's similar to core scheduling but inside the kernel (let's call it "kernel core scheduling").
I think the benefit of this approach would be that it should be much simpler to implement and less invasive than ASI, and it doesn't preclude to later do ASI: ASI can be done in addition and provide an extra level of mitigation in case some sensitive is still accessed by KVM. Also it would provide the critical sibling CPU synchronization mechanism that we also need with ASI.
I did some prototyping to implement this kernel core scheduling a while ago (and then get diverted on other stuff) but so far performances have been abyssal especially when doing a strict synchronization between sibling CPUs. I am planning go back and do more investigations when I have cycles but probably not that soon.
alex.
[4] https://lore.kernel.org/lkml/[email protected]
[6] https://lore.kernel.org/lkml/[email protected]
[7] https://lore.kernel.org/lkml/[email protected]
Quoted 163 lines
Background
==========
Address Space Isolation is a comprehensive security mitigation for several types of speculative execution attacks. Even though the kernel already has several speculative execution vulnerability mitigations, some of them can be quite expensive if enabled fully e.g. to fully mitigate L1TF using the existing mechanisms requires doing an L1 cache flush on every single VM entry as well as disabling hyperthreading altogether. (Although core scheduling can provide some protection when hyperthreading is enabled, it is not sufficient by itself to protect against all leaks unless sibling hyperthread stunning is also performed on every VM exit.) ASI provides a much less expensive mitigation for such vulnerabilities while still providing an almost similar level of protection.There are a couple of basic insights/assumptions behind ASI:
1. Most execution paths in the kernel (especially during virtual machine execution) access only memory that is not particularly sensitive even if it were to get leaked to the executing process/VM (setting aside for a moment what exactly should be considered sensitive or non-sensitive).
2. Even when executing speculatively, the CPU can generally only bring memory that is mapped in the current page tables into its various caches and internal buffers.Given these, the idea of using ASI to thwart speculative attacks is that we can execute the kernel using a restricted set of page tables most of the time and switch to the full unrestricted kernel address space only when the kernel needs to access something that is not mapped in the restricted address space. And we keep track of when a switch to the full kernel address space is done, so that before returning back to the process/VM, we can switch back to the restricted address space. In the paths where the kernel is able to execute entirely while remaining in the restricted address space, we can skip other mitigations for speculative execution attacks (such as L1 cache / micro-arch buffer flushes, sibling hyperthread stunning etc.). Only in the cases where we do end up switching the page tables, we perform these more expensive mitigations. Assuming that happens relatively infrequently, the performance can be significantly better compared to performing these mitigations all the time.
Please note that although we do have a sibling hyperthread stunning implementation internally, which is fully integrated with KVM-ASI, it is not included in this RFC for the time being. The earlier upstream proposal for sibling stunning [6] could potentially be integrated into an upstream ASI implementation.
Basic concepts
==============
Different types of restricted address spaces are represented by different ASI classes. For instance, KVM-ASI is an ASI class used during VM execution. KPTI would be another ASI class. An ASI instance (struct asi) represents a single restricted address space. There is a separate ASI instance for each untrusted context (e.g. a userspace process, a VM, or even a single VCPU etc.) Note that there can be multiple untrusted security contexts (and thus multiple restricted address spaces) within a single process e.g. in the case of VMs, the userspace process is a different security context than the guest VM, and in principle, even each VCPU could be considered a separate security context (That would be primarily useful for securing nested virtualization).In this RFC, a process can have at most one ASI instance of each class, though this is not an inherent limitation and multiple instances of the same class should eventually be supported. (A process can still have ASI instances of different classes e.g. KVM-ASI and KPTI.) In fact, in principle, it is not even entirely necessary to tie an ASI instance to a process. That is just a simplification for the initial implementation.
An
asi_enteroperation switches into the restricted address space represented by the given ASI instance. Anasi_exitoperation switches to the full unrestricted kernel address space. Each ASI class can provide hooks to be executed during these operations, which can be used to perform speculative attack mitigations relevant to that class. For instance, the KVM-ASI hooks would perform a sibling-hyperthread-stun operation in theasi_exithook, and L1-flush/MDS-clear and sibling-hyperthread-unstun operations in theasi_enterhook. On the other hand, the hooks for the KPTI class would be NO-OP, since the switching of the page tables is enough mitigation in that case.If the kernel attempts to access memory that is not mapped in the currently active ASI instance, the page fault handler automatically performs an
asi_exitoperation. This means that except for a few critical pieces of memory, leaving something out of an unrestricted address space will result in only a performance hit, rather than a catastrophic failure. The kernel can also perform explicitasi_exitoperations in some paths as needed.Apart from the page fault handler, other exceptions and interrupts (even NMIs) do not automatically cause an
asi_exitand could potentially be executed completely within a restricted address space if they don't end up accessing any sensitive piece of memory.The mappings within a restricted address space are always a subset of the full kernel address space and each mapping is always the same as the corresponding mapping in the full kernel address space. This is necessary because we could potentially end up performing an
asi_exitat any point.Although this RFC only includes an implementation of the KVM-ASI class, a KPTI class could also be implemented on top of the same infrastructure. Furthermore, in the future we could also implement a KPTI-Next class that actually uses the ASI model for userspace processes i.e. mapping non-sensitive kernel memory in the restricted address space and trying to execute most syscalls/interrupts without switching to the full kernel address space, as opposed to the current KPTI which requires an address space switch on every kernel/user mode transition.
Memory classification
=====================
We divide memory into three categories.1. Sensitive memory
This is memory that should never get leaked to any process or VM. Sensitive memory is only mapped in the unrestricted kernel page tables. By default, all memory is considered sensitive unless specifically categorized otherwise.2. Globally non-sensitive memory
This is memory that does not present a substantial security threat even if it were to get leaked to any process or VM in the system. Globally non-sensitive memory is mapped in the restricted address spaces for all processes.3. Locally non-sensitive memory
This is memory that does not present a substantial security threat if it were to get leaked to the currently running process or VM, but would present a security issue if it were to get leaked to any other process or VM in the system. Examples include userspace memory (or guest memory in the case of VMs) or kernel structures containing userspace/guest register context etc. Locally non-sensitive memory is mapped only in the restricted address space of a single process.Various mechanisms are provided to annotate different types of memory (static, buddy allocator, slab, vmalloc etc.) as globally or locally non-sensitive. In addition, the ASI infrastructure takes care to ensure that different classes of memory do not share the same physical page. This includes separation of sensitive, globally non-sensitive and locally non-sensitive memory into different pages and also separation of locally non-sensitive memory for different processes into different pages as well.
What exactly should be considered non-sensitive (either globally or locally) is somewhat open-ended. Some things are clearly sensitive or non-sensitive, but many things also fall into a gray area, depending on how paranoid one wants to be. For this proof of concept, we have generally treated such things as non-sensitive, though that may not necessarily be the ideal classification in each case. Similarly, there is also a gray area between globally and locally non-sensitive classifications in some cases, and in those cases this RFC has mostly erred on the side of marking them as locally non-sensitive, even though many of those cases could likely be safely classified as globally non-sensitive.
Although this implementation includes fairly extensive support for marking most types of dynamically allocated memory as locally non-sensitive, it is possibly feasible, at least for KVM-ASI, to get away with a simpler implementation (such as [5]), if we are very selective about what memory we treat as locally non-sensitive (as opposed to globally non-sensitive). Nevertheless, the more general mechanism is included in this proof of concept as an illustration for what could be done if we really needed to treat any arbitrary kernel memory as locally non-sensitive.
It is also possible to have ASI classes that do not utilize the above described infrastructure and instead manage all the memory mappings inside the restricted address space on their own.
References
==========
[1] https://lore.kernel.org/lkml/[email protected]
[2] https://lore.kernel.org/lkml/[email protected]
[3] https://lore.kernel.org/lkml/[email protected]
[4] https://lore.kernel.org/lkml/[email protected]
[5] https://lore.kernel.org/lkml/[email protected]
[6] https://lore.kernel.org/lkml/[email protected]
Junaid,
Quoted 2 lines · Junaid Shahid
The patches apply on top of Linux v5.16.
Why are you posting patches against some randomly chosen release?
Documentation/process/ is pretty clear about how this works. It's not optional.
Quoted 2 lines
These patches are also available via
gerrit at https://linux-review.googlesource.com/q/topic:asi-rfc.
This is useful because?
If you want to provide patches in a usable form then please expose them as git tree which can be pulled and not via the random tool of the day.
Thanks,
tglx
Quoted 10 lines · Thomas Gleixner
Junaid,
Quoted 2 lines · Junaid Shahid
The patches apply on top of Linux v5.16.
Why are you posting patches against some randomly chosen release?
Documentation/process/is pretty clear about how this works. It's not optional.
Sorry, I assumed that for an RFC, it may be acceptable to base on the last release version, but looks like I guessed wrong. I will base the next version of the RFC on the HEAD of the Linus tree.
Quoted 8 lines
Quoted 3 lines
These patches are also available via
gerrit at https://linux-review.googlesource.com/q/topic:asi-rfc.This is useful because?
If you want to provide patches in a usable form then please expose them as git tree which can be pulled and not via the random tool of the day.
The patches are now available as the branch "asi-rfc-v1" in the git repo https://github.com/googleprodkernel/linux-kvm.git
Thanks,
Junaid
Quoted 6 lines
Thanks,
tglx
Hi Alex,
Quoted 50 lines · Alexandre Chartre
Hi Junaid,
Quoted 17 lines · Junaid Shahid
This patch series is a proof-of-concept RFC for an end-to-end implementation of Address Space Isolation for KVM. It has similar goals and a somewhat similar high-level design as the original ASI patches from Alexandre Chartre ([1],[2],[3],[4]), but with a different underlying implementation. This also includes several memory management changes to help with differentiating between sensitive and non-sensitive memory and mapping of non-sensitive memory into the ASI restricted address spaces.
This RFC is intended as a demonstration of what a full ASI implementation for KVM could look like, not necessarily as a direct proposal for what might eventually be merged. In particular, these patches do not yet implement KPTI on top of ASI, although the framework is generic enough to be able to support it. Similarly, these patches do not include non-sensitive annotations for data structures that did not get frequently accessed during execution of our test workloads, but the framework is designed such that new non-sensitive memory annotations can be added trivially.
Quoted 3 lines
The patches apply on top of Linux v5.16. These patches are also available via gerrit at https://linux-review.googlesource.com/q/topic:asi-rfc.
Sorry for the late answer, and thanks for investigating possible ASI implementations. I have to admit I put ASI on the back-burner for a while because I am more and more wondering if the complexity of ASI is worth the benefit, especially given challenges to effectively exploit flaws that ASI is expected to mitigate, in particular when VMs are running on dedicated cpu cores, or when core scheduling is used. So I have been looking at a more simplistic approach (see below, A Possible Alternative to ASI).
But first, your implementation confirms that KVM-ASI can be broken up into different parts: pagetable management, ASI core and sibling cpus synchronization.
Pagetable Management
====================
For ASI, we need to build a pagetable with a subset of the kernel pagetable mappings. Your solution is interesting as it is provides a broad solution and also works well with dynamic allocations (while my approach to copy mappings had several limitations). The drawback is the extend of your changes which spread over all the mm code (while the simple solution to copy mappings can be done with a few self-contained independent functions).ASI Core
========
Quoted 8 lines
KPTI
----
Implementing KPTI with ASI is possible but this is not straight forward. This requires some special handling in particular in the assembly kernel entry/exit code for syscall, interrupt and exception (see ASI RFC v4 [4] as an example) because we are also switching privilege level in addition of switching the pagetable. So this might be something to consider early in your implementation to ensure it is effectively compatible with KPTI.
Yes, I will look in more detail into how to implement KPTI on top of this ASI implementation, but at least at a high level, it seems that it should work. Of course, the devil is always in the details :)
Quoted 8 lines
Going beyond KPTI (with a KPTI-next) and trying to execute most syscalls/interrupts without switching to the full kernel address space is more challenging, because it would require much more kernel mapping in the user pagetable, and this would basically defeat the purpose of KPTI. You can refer to discussions about the RFC to defer CR3 switch to C code [7] which was an attempt to just reach the kernel entry C code with a KPTI pagetable.
In principle, the ASI restricted address space would not contain any sensitive data, so having more mappings should be ok as long as they really are non-sensitive. Of course, it is possible that we may mistakenly think that some data is not sensitive and mark it as such, but in reality it really was sensitive in some way. In that sense, a strict KPTI is certainly a little more secure than the KPTI-Next that I mentioned, but KPTI-Next would also have lower performance overhead compared to the strict KPTI.
Quoted 7 lines
Interrupts/Exceptions
---------------------
As long as interrupts/exceptions are not expected to be processed with ASI, it is probably better to explicitly exit ASI before processing an interrupt/exception, otherwise you will have an extra overhead on each interrupt/exception to take a page fault and then exit ASI.
I agree that for those interrupts/exceptions that will need to access sensitive data, it is better to do an explicit ASI Exit at the start. But it is probably possible for many interrupts to be handled without needing to access sensitive data, in which case, it would be better to avoid the ASI Exit.
Quoted 5 lines
This is particularily true if you have want to have KPTI use ASI, and in that case the ASI exit will need to be done early in the interrupt and exception assembly entry code.
Quoted 9 lines
ASI Hooks
---------
ASI hooks are certainly a good idea to perform specific actions on ASI enter or exit. However, I am not sure they are appropriate places for cpus stunning with KVM-ASI. That's because cpus stunning doesn't need to be done precisely when entering and exiting ASI, and it probably shouldn't be done there: it should be done right before VMEnter and right after VMExit (see below).
I believe that performing sibling CPU stunning right after VM Exit will negate most of the performance advantage of ASI. I think that it is feasible to do the stunning on ASI Exit. Please see below for how we handle the problem that you have mentioned.
Quoted 19 lines
Sibling CPUs Synchronization
============================
KVM-ASI requires the synchronization of sibling CPUs from the same CPU core so that when a VM is running then sibling CPUs are running with the ASI associated with this VM (or an ASI compatible with the VM, depending on how ASI is defined). That way the VM can only spy on data from ASI and won't be able to access any sensitive data.So, right before entering a VM, KVM should ensures that sibling CPUs are using ASI. If a sibling CPU is not using ASI then KVM can either wait for that sibling to run ASI, or force it to use ASI (or to become idle). This behavior should be enforced as long as any sibling is running the VM. When all siblings are not running the VM then other siblings can run any code (using ASI or not).
I would be interesting to see the code you use to achieve this, because I don't get how this is achieved from the description of your sibling hyperthread stun and unstun mechanism.
Quoted 8 lines
Note that this synchronization is critical for ASI to work, in particular when entering the VM, we need to be absolutely sure that sibling CPUs are effectively using ASI. The core scheduling sibling stunning code you referenced [6] uses a mechanism which is fine for userspace synchronization (the delivery of the IPI forces the sibling to immediately enter the kernel) but this won't work for ASI as the delivery of the IPI won't guarantee that the sibling as enter ASI yet. I did some experiments that show that data will leak if siblings are not perfectly synchronized.
I agree that it is not secure to run one sibling in the unrestricted kernel address space while the other sibling is running in an ASI restricted address space, without doing a cache flush before re-entering the VM. However, I think that avoiding this situation does not require doing a sibling stun operation immediately after VM Exit. The way we avoid it is as follows.
First, we always use ASI in conjunction with core scheduling. This means that if HT0 is running a VCPU thread, then HT1 will be running either a VCPU thread of the same VM or the Idle thread. If it is running a VCPU thread, then if/when that thread takes a VM Exit, it will also be running in the same ASI restricted address space. For the idle thread, we have created another ASI Class, called Idle-ASI, which maps only globally non-sensitive kernel memory. The idle loop enters this ASI address space.
This means that when HT0 does a VM Exit, HT1 will either be running the guest code of a VCPU of the same VM, or it will be running kernel code in either a KVM-ASI or the Idle-ASI address space. (If HT1 is already running in the full kernel address space, that would imply that it had previously done an ASI Exit, which would have triggered a stun_sibling, which would have already caused HT0 to exit the VM and wait in the kernel).
If HT1 now does an ASI Exit, that will trigger the stun_sibling() operation in its pre_asi_exit() handler, which will set the state of the core/HT0 to Stunned (and possibly send an IPI too, though that will be ignored if HT0 was already in kernel mode). Now when HT0 tries to re-enter the VM, since its state is set to Stunned, it will just wait in a loop until HT1 does an unstun_sibling() operation, which it will do in its post_asi_enter handler the next time it does an ASI Enter (which would be either just before VM Enter if it was KVM-ASI, or in the next iteration of the idle loop if it was Idle-ASI). In either case, HT1's post_asi_enter() handler would also do a flush_sensitive_cpu_state operation before the unstun_sibling(), so when HT0 gets out of its wait-loop and does a VM Enter, there will not be any sensitive state left.
One thing that probably was not clear from the patch, is that the stun state check and wait-loop is still always executed before VM Enter, even if no ASI Exit happened in that execution.
Quoted 26 lines
A Possible Alternative to ASI?
=============================
ASI prevents access to sensitive data by unmapping them. On the other hand, the KVM code somewhat already identifies access to sensitive data as part of the L1TF/MDS mitigation, and when KVM is about to access sensitive data then it setsl1tf_flush_l1dto true (so that L1D gets flushed before VMEnter).With KVM knowing when it accesses sensitive data, I think we can provide the same mitigation as ASI by simply allowing KVM code which doesn't access sensitive data to be run concurrently with a VM. This can be done by tagging the kernel thread when it enters KVM code which doesn't access sensitive data, and untagging the thread right before it accesses sensitive data. And when KVM is about to do a VMEnter then we synchronize siblings CPUs so that they run threads with the same tag. Sounds familar? Yes, because that's similar to core scheduling but inside the kernel (let's call it "kernel core scheduling").
I think the benefit of this approach would be that it should be much simpler to implement and less invasive than ASI, and it doesn't preclude to later do ASI: ASI can be done in addition and provide an extra level of mitigation in case some sensitive is still accessed by KVM. Also it would provide the critical sibling CPU synchronization mechanism that we also need with ASI.
Quoted 6 lines
I did some prototyping to implement this kernel core scheduling a while ago (and then get diverted on other stuff) but so far performances have been abyssal especially when doing a strict synchronization between sibling CPUs. I am planning go back and do more investigations when I have cycles but probably not that soon.
This also seems like an interesting approach. It does have some different trade-offs compared to ASI. First, there is the trade-off between a blacklist vs. whitelist approach. Secondly, ASI has a more structured approach based on the sensitivity of the data itself, instead of having to audit every new code path to verify whether or not it can potentially access any sensitive data. On the other hand, as you point out, this approach is much simpler than ASI, which is certainly a plus.
Thanks,
Junaid
Quoted 25 lines · Junaid Shahid
Quoted 11 lines
ASI Core
========KPTI
----
Implementing KPTI with ASI is possible but this is not straight forward. This requires some special handling in particular in the assembly kernel entry/exit code for syscall, interrupt and exception (see ASI RFC v4 [4] as an example) because we are also switching privilege level in addition of switching the pagetable. So this might be something to consider early in your implementation to ensure it is effectively compatible with KPTI.Yes, I will look in more detail into how to implement KPTI on top of this ASI implementation, but at least at a high level, it seems that it should work. Of course, the devil is always in the details :)
Quoted 8 lines
Going beyond KPTI (with a KPTI-next) and trying to execute most syscalls/interrupts without switching to the full kernel address space is more challenging, because it would require much more kernel mapping in the user pagetable, and this would basically defeat the purpose of KPTI. You can refer to discussions about the RFC to defer CR3 switch to C code [7] which was an attempt to just reach the kernel entry C code with a KPTI pagetable.
Quoted 9 lines
In principle, the ASI restricted address space would not contain any sensitive data, so having more mappings should be ok as long as they really are non-sensitive. Of course, it is possible that we may mistakenly think that some data is not sensitive and mark it as such, but in reality it really was sensitive in some way. In that sense, a strict KPTI is certainly a little more secure than the KPTI-Next that I mentioned, but KPTI-Next would also have lower performance overhead compared to the strict KPTI.
Mappings are precisely the issue for KPTI-next. The RFC I submitted show that going beyond KPTI might require to map data which could be deemed sensitive. Also they are extra complications that make it difficult to reach C code with a KPTI page-table. This was discussed in v2 of the "Defer CR3 switch to C code" RFC:
https://lore.kernel.org/all/[email protected]/
Quoted 29 lines
Quoted 7 lines
Interrupts/Exceptions
---------------------
As long as interrupts/exceptions are not expected to be processed with ASI, it is probably better to explicitly exit ASI before processing an interrupt/exception, otherwise you will have an extra overhead on each interrupt/exception to take a page fault and then exit ASI.I agree that for those interrupts/exceptions that will need to access sensitive data, it is better to do an explicit ASI Exit at the start. But it is probably possible for many interrupts to be handled without needing to access sensitive data, in which case, it would be better to avoid the ASI Exit.
Quoted 5 lines
This is particularily true if you have want to have KPTI use ASI, and in that case the ASI exit will need to be done early in the interrupt and exception assembly entry code.
Quoted 9 lines
ASI Hooks
---------
ASI hooks are certainly a good idea to perform specific actions on ASI enter or exit. However, I am not sure they are appropriate places for cpus stunning with KVM-ASI. That's because cpus stunning doesn't need to be done precisely when entering and exiting ASI, and it probably shouldn't be done there: it should be done right before VMEnter and right after VMExit (see below).
Quoted 5 lines
I believe that performing sibling CPU stunning right after VM Exit will negate most of the performance advantage of ASI. I think that it is feasible to do the stunning on ASI Exit. Please see below for how we handle the problem that you have mentioned.
Right, I was confused by what you exactly meant by cpu stun/unstun but I think it's now clearer with your explanation below.
Quoted 45 lines
Quoted 20 lines
Sibling CPUs Synchronization
============================
KVM-ASI requires the synchronization of sibling CPUs from the same CPU core so that when a VM is running then sibling CPUs are running with the ASI associated with this VM (or an ASI compatible with the VM, depending on how ASI is defined). That way the VM can only spy on data from ASI and won't be able to access any sensitive data.So, right before entering a VM, KVM should ensures that sibling CPUs are using ASI. If a sibling CPU is not using ASI then KVM can either wait for that sibling to run ASI, or force it to use ASI (or to become idle). This behavior should be enforced as long as any sibling is running the VM. When all siblings are not running the VM then other siblings can run any code (using ASI or not).
I would be interesting to see the code you use to achieve this, because I don't get how this is achieved from the description of your sibling hyperthread stun and unstun mechanism.
Quoted 8 lines
Note that this synchronization is critical for ASI to work, in particular when entering the VM, we need to be absolutely sure that sibling CPUs are effectively using ASI. The core scheduling sibling stunning code you referenced [6] uses a mechanism which is fine for userspace synchronization (the delivery of the IPI forces the sibling to immediately enter the kernel) but this won't work for ASI as the delivery of the IPI won't guarantee that the sibling as enter ASI yet. I did some experiments that show that data will leak if siblings are not perfectly synchronized.
I agree that it is not secure to run one sibling in the unrestricted kernel address space while the other sibling is running in an ASI restricted address space, without doing a cache flush before re-entering the VM. However, I think that avoiding this situation does not require doing a sibling stun operation immediately after VM Exit. The way we avoid it is as follows.
First, we always use ASI in conjunction with core scheduling. This means that if HT0 is running a VCPU thread, then HT1 will be running either a VCPU thread of the same VM or the Idle thread. If it is running a VCPU thread, then if/when that thread takes a VM Exit, it will also be running in the same ASI restricted address space. For the idle thread, we have created another ASI Class, called Idle-ASI, which maps only globally non-sensitive kernel memory. The idle loop enters this ASI address space.
Quoted 7 lines
This means that when HT0 does a VM Exit, HT1 will either be running the guest code of a VCPU of the same VM, or it will be running kernel code in either a KVM-ASI or the Idle-ASI address space. (If HT1 is already running in the full kernel address space, that would imply that it had previously done an ASI Exit, which would have triggered a
stun_sibling, which would have already caused HT0 to exit the VM and wait in the kernel).
Note that using core scheduling (or not) is a detail, what is important is whether HT are running with ASI or not. Running core scheduling will just improve chances to have all siblings run ASI at the same time and so improve ASI performances.
Quoted 14 lines
If HT1 now does an ASI Exit, that will trigger the
stun_sibling()operation in itspre_asi_exit()handler, which will set the state of the core/HT0 to Stunned (and possibly send an IPI too, though that will be ignored if HT0 was already in kernel mode). Now when HT0 tries to re-enter the VM, since its state is set to Stunned, it will just wait in a loop until HT1 does anunstun_sibling()operation, which it will do in itspost_asi_enterhandler the next time it does an ASI Enter (which would be either just before VM Enter if it was KVM-ASI, or in the next iteration of the idle loop if it was Idle-ASI). In either case, HT1'spost_asi_enter()handler would also do aflush_sensitive_cpu_stateoperation before theunstun_sibling(), so when HT0 gets out of its wait-loop and does a VM Enter, there will not be any sensitive state left.
Quoted 4 lines
One thing that probably was not clear from the patch, is that the stun state check and wait-loop is still always executed before VM Enter, even if no ASI Exit happened in that execution.
So if I understand correctly, you have following sequence:
0 - Initially state is set to "stunned" for all cpus (i.e. a cpu should
wait before VMEnter)
1 - After ASI Enter: Set sibling state to "unstunned" (i.e. sibling can
do VMEnter)
2 - Before VMEnter : wait while my state is "stunned"
3 - Before ASI Exit : Set sibling state to "stunned" (i.e. sibling should
wait before VMEnter)
I have tried this kind of implementation, and the problem is with step 2 (wait while my state is "stunned"); how do you wait exactly? You can't just do an active wait otherwise you have all kind of problems (depending if you have interrupts enabled or not) especially as you don't know how long you have to wait for (this depends on what the other cpu is doing).
That's why I have been dissociating ASI and cpu stunning (and eventually move to only do kernel core scheduling). Basically I replaced step 2 by a call to the scheduler to select threads using ASI on all siblings (or run something else if there's higher priority threads to run) which means enabling kernel core scheduling at this point.
Quoted 33 lines
Quoted 26 lines
A Possible Alternative to ASI?
=============================
ASI prevents access to sensitive data by unmapping them. On the other hand, the KVM code somewhat already identifies access to sensitive data as part of the L1TF/MDS mitigation, and when KVM is about to access sensitive data then it setsl1tf_flush_l1dto true (so that L1D gets flushed before VMEnter).With KVM knowing when it accesses sensitive data, I think we can provide the same mitigation as ASI by simply allowing KVM code which doesn't access sensitive data to be run concurrently with a VM. This can be done by tagging the kernel thread when it enters KVM code which doesn't access sensitive data, and untagging the thread right before it accesses sensitive data. And when KVM is about to do a VMEnter then we synchronize siblings CPUs so that they run threads with the same tag. Sounds familar? Yes, because that's similar to core scheduling but inside the kernel (let's call it "kernel core scheduling").
I think the benefit of this approach would be that it should be much simpler to implement and less invasive than ASI, and it doesn't preclude to later do ASI: ASI can be done in addition and provide an extra level of mitigation in case some sensitive is still accessed by KVM. Also it would provide the critical sibling CPU synchronization mechanism that we also need with ASI.
Quoted 6 lines
I did some prototyping to implement this kernel core scheduling a while ago (and then get diverted on other stuff) but so far performances have been abyssal especially when doing a strict synchronization between sibling CPUs. I am planning go back and do more investigations when I have cycles but probably not that soon.
Quoted 8 lines
This also seems like an interesting approach. It does have some different trade-offs compared to ASI. First, there is the trade-off between a blacklist vs. whitelist approach. Secondly, ASI has a more structured approach based on the sensitivity of the data itself, instead of having to audit every new code path to verify whether or not it can potentially access any sensitive data. On the other hand, as you point out, this approach is much simpler than ASI, which is certainly a plus.
I think the main benefit is that it provides a mechanism for running specific kernel threads together on sibling cpus independently of ASI. So it will be easier to implement (you don't need ASI) and to test.
Then, once this mechanism has proven to work (and to be efficient), you can have KVM ASI use it.
alex.
Quoted 62 lines · Alexandre Chartre
Quoted 17 lines · Junaid Shahid
I agree that it is not secure to run one sibling in the unrestricted kernel address space while the other sibling is running in an ASI restricted address space, without doing a cache flush before re-entering the VM. However, I think that avoiding this situation does not require doing a sibling stun operation immediately after VM Exit. The way we avoid it is as follows.
First, we always use ASI in conjunction with core scheduling. This means that if HT0 is running a VCPU thread, then HT1 will be running either a VCPU thread of the same VM or the Idle thread. If it is running a VCPU thread, then if/when that thread takes a VM Exit, it will also be running in the same ASI restricted address space. For the idle thread, we have created another ASI Class, called Idle-ASI, which maps only globally non-sensitive kernel memory. The idle loop enters this ASI address space.
Quoted 7 lines
This means that when HT0 does a VM Exit, HT1 will either be running the guest code of a VCPU of the same VM, or it will be running kernel code in either a KVM-ASI or the Idle-ASI address space. (If HT1 is already running in the full kernel address space, that would imply that it had previously done an ASI Exit, which would have triggered a
stun_sibling, which would have already caused HT0 to exit the VM and wait in the kernel).Note that using core scheduling (or not) is a detail, what is important is whether HT are running with ASI or not. Running core scheduling will just improve chances to have all siblings run ASI at the same time and so improve ASI performances.
Quoted 14 lines
If HT1 now does an ASI Exit, that will trigger the
stun_sibling()operation in itspre_asi_exit()handler, which will set the state of the core/HT0 to Stunned (and possibly send an IPI too, though that will be ignored if HT0 was already in kernel mode). Now when HT0 tries to re-enter the VM, since its state is set to Stunned, it will just wait in a loop until HT1 does anunstun_sibling()operation, which it will do in itspost_asi_enterhandler the next time it does an ASI Enter (which would be either just before VM Enter if it was KVM-ASI, or in the next iteration of the idle loop if it was Idle-ASI). In either case, HT1'spost_asi_enter()handler would also do aflush_sensitive_cpu_stateoperation before theunstun_sibling(), so when HT0 gets out of its wait-loop and does a VM Enter, there will not be any sensitive state left.Quoted 4 lines
One thing that probably was not clear from the patch, is that the stun state check and wait-loop is still always executed before VM Enter, even if no ASI Exit happened in that execution.
So if I understand correctly, you have following sequence:
0 - Initially state is set to "stunned" for all cpus (i.e. a cpu should
wait before VMEnter)1 - After ASI Enter: Set sibling state to "unstunned" (i.e. sibling can
do VMEnter)2 - Before VMEnter : wait while my state is "stunned"
Quoted 8 lines
3 - Before ASI Exit : Set sibling state to "stunned" (i.e. sibling should
wait before VMEnter)I have tried this kind of implementation, and the problem is with step 2 (wait while my state is "stunned"); how do you wait exactly? You can't just do an active wait otherwise you have all kind of problems (depending if you have interrupts enabled or not) especially as you don't know how long you have to wait for (this depends on what the other cpu is doing).
In our stunning implementation, we do an active wait with interrupts enabled and with a need_resched() check to decide when to bail out to the scheduler (plus we also make sure that we re-enter ASI at the end of the wait in case some interrupt exited ASI). What kind of problems have you run into with an active wait, besides wasted CPU cycles?
In any case, the specific stunning mechanism is orthogonal to ASI. This implementation of ASI can be integrated with different stunning implementations. The "kernel core scheduling" that you proposed is also an alternative to stunning and could be similarly integrated with ASI.
Quoted 49 lines
That's why I have been dissociating ASI and cpu stunning (and eventually move to only do kernel core scheduling). Basically I replaced step 2 by a call to the scheduler to select threads using ASI on all siblings (or run something else if there's higher priority threads to run) which means enabling kernel core scheduling at this point.
Quoted 33 lines
Quoted 26 lines
A Possible Alternative to ASI?
=============================
ASI prevents access to sensitive data by unmapping them. On the other hand, the KVM code somewhat already identifies access to sensitive data as part of the L1TF/MDS mitigation, and when KVM is about to access sensitive data then it setsl1tf_flush_l1dto true (so that L1D gets flushed before VMEnter).With KVM knowing when it accesses sensitive data, I think we can provide the same mitigation as ASI by simply allowing KVM code which doesn't access sensitive data to be run concurrently with a VM. This can be done by tagging the kernel thread when it enters KVM code which doesn't access sensitive data, and untagging the thread right before it accesses sensitive data. And when KVM is about to do a VMEnter then we synchronize siblings CPUs so that they run threads with the same tag. Sounds familar? Yes, because that's similar to core scheduling but inside the kernel (let's call it "kernel core scheduling").
I think the benefit of this approach would be that it should be much simpler to implement and less invasive than ASI, and it doesn't preclude to later do ASI: ASI can be done in addition and provide an extra level of mitigation in case some sensitive is still accessed by KVM. Also it would provide the critical sibling CPU synchronization mechanism that we also need with ASI.
Quoted 6 lines
I did some prototyping to implement this kernel core scheduling a while ago (and then get diverted on other stuff) but so far performances have been abyssal especially when doing a strict synchronization between sibling CPUs. I am planning go back and do more investigations when I have cycles but probably not that soon.
Quoted 8 lines
This also seems like an interesting approach. It does have some different trade-offs compared to ASI. First, there is the trade-off between a blacklist vs. whitelist approach. Secondly, ASI has a more structured approach based on the sensitivity of the data itself, instead of having to audit every new code path to verify whether or not it can potentially access any sensitive data. On the other hand, as you point out, this approach is much simpler than ASI, which is certainly a plus.
Quoted 4 lines
I think the main benefit is that it provides a mechanism for running specific kernel threads together on sibling cpus independently of ASI. So it will be easier to implement (you don't need ASI) and to test.
It would be interesting to see the performance characteristics of this approach compared to stunning. I think it would really depend on how long do we typically end up staying in the full kernel address space when running VCPUs.
Note that stunning can also be implemented independently of ASI by integrating it with the same conditional L1TF mitigation mechanism (l1tf_flush_l1d) that currently exists in KVM. The way I see it, this kernel core scheduling is an alternative to stunning, regardless of whether we integrate it with ASI or with the existing conditional mitigation mechanism.
Quoted 3 lines
Then, once this mechanism has proven to work (and to be efficient), you can have KVM ASI use it.
Yes, if this mechanism seems to work better than stunning, then we could certainly integrate this with ASI. Though it is possible that we may end up needing ASI to get to the "efficient" part.
Thanks,
Junaid
Re: [RFC PATCH 10/47] mm: asi: Support for global non-sensitive direct map allocations
In reply to Junaid ShahidQuoted 3 lines · Junaid Shahid
standard ASI instances. A new page flag is also added so that when these pages are freed, they can also be unmapped from the ASI page tables.
It's cute how you just throw this in as an aside. Page flags are in high demand and just adding them is not to be done lightly. Is there any other way of accomplishing what you want?
include/linux/page-flags.h TESTCLEARFLAG(Young, young, PF_ANY) · line 549
Quoted 6 lines
include/linux/page-flags.h +2
-0@@ -542,6 +545,12 @@ TESTCLEARFLAG(Young, young, PF_ANY) PAGEFLAG(Idle, idle, PF_ANY)#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +__PAGEFLAG (GlobalNonSensitive, global_nonsensitive,PF_ANY );
Why is PF_ANY appropriate?
Re: [RFC PATCH 10/47] mm: asi: Support for global non-sensitive direct map allocations
Hi Matthew,
Quoted 9 lines · Matthew Wilcox
Quoted 3 lines · Junaid Shahid
standard ASI instances. A new page flag is also added so that when these pages are freed, they can also be unmapped from the ASI page tables.
It's cute how you just throw this in as an aside. Page flags are in high demand and just adding them is not to be done lightly. Is there any other way of accomplishing what you want?
I suppose we may be able to use page_ext instead. That certainly should be feasible for the PG_local_nonsensitive flag introduced in a later patch, although I am not completely sure about the PG_global_nonsensitive flag. That could get slightly tricky (though likely still possible to do) in case we need to allocate any non-sensitive memory before page_ext is initialized. One concern with using page_ext could be the extra memory usage on large machines.
BTW is page flag scarcity an issue on 64-bit systems as well, or only 32-bit systems? ASI is only supported on 64-bit systems (at least currently).
Quoted 9 lines
include/linux/page-flags.h TESTCLEARFLAG(Young, young, PF_ANY) · line 549
Quoted 6 lines
include/linux/page-flags.h +2
-0@@ -542,6 +545,12 @@ TESTCLEARFLAG(Young, young, PF_ANY) PAGEFLAG(Idle, idle, PF_ANY)#endif +#ifdef CONFIG_ADDRESS_SPACE_ISOLATION +__PAGEFLAG (GlobalNonSensitive, global_nonsensitive,PF_ANY );Why is
PF_ANYappropriate?
I think we actually can use PF_HEAD here. That would make the alloc_pages path faster too. I'll change to that. (Initially I had gone with PF_ANY because in theory, you could allocate a compound page and then break it later and free the individual pages, but I don't know if that actually happens apart from THP, and certainly doesn't look like the case for any of the stuff that we have marked as non-sensitive.)
Thanks,
Junaid
Re: [RFC PATCH 10/47] mm: asi: Support for global non-sensitive direct map allocations
Quoted 8 lines · Junaid Shahid
Hi Matthew,
Quoted 5 lines · Matthew Wilcox
Quoted 3 lines · Junaid Shahid
standard ASI instances. A new page flag is also added so that when these pages are freed, they can also be unmapped from the ASI page tables.
Quoted 9 lines
Quoted 4 lines
It's cute how you just throw this in as an aside. Page flags are in high demand and just adding them is not to be done lightly. Is there any other way of accomplishing what you want?
I suppose we may be able to use
page_extinstead. That certainly should be feasible for thePG_local_nonsensitiveflag introduced in a later patch, although I am not completely sure about thePG_global_nonsensitiveflag. That could get slightly tricky (though likely still possible to do) in case we need to allocate any non-sensitive memory beforepage_extis initialized. One concern with usingpage_extcould be the extra memory usage on large machines.BTW is page flag scarcity an issue on 64-bit systems as well, or only 32-bit systems? ASI is only supported on 64-bit systems (at least currently).
One other thing that we could do to remove the need for the PG_global_nonsensitive flag altogether (though not the PG_local_nonsensitive flag) would be to always try to unmap pages from the asi_global_nonsensitive_pgd in free_pages(). Basically, that would mean adding a page table walk to every free_pages() rather than just non-sensitive free_pages(). Do you think that may be a better trade-off in order to avoid the flag?
Thanks,
Junaid
Quoted 73 lines · Junaid Shahid
Quoted 65 lines · Alexandre Chartre
Quoted 17 lines · Junaid Shahid
I agree that it is not secure to run one sibling in the unrestricted kernel address space while the other sibling is running in an ASI restricted address space, without doing a cache flush before re-entering the VM. However, I think that avoiding this situation does not require doing a sibling stun operation immediately after VM Exit. The way we avoid it is as follows.
First, we always use ASI in conjunction with core scheduling. This means that if HT0 is running a VCPU thread, then HT1 will be running either a VCPU thread of the same VM or the Idle thread. If it is running a VCPU thread, then if/when that thread takes a VM Exit, it will also be running in the same ASI restricted address space. For the idle thread, we have created another ASI Class, called Idle-ASI, which maps only globally non-sensitive kernel memory. The idle loop enters this ASI address space.
Quoted 7 lines
This means that when HT0 does a VM Exit, HT1 will either be running the guest code of a VCPU of the same VM, or it will be running kernel code in either a KVM-ASI or the Idle-ASI address space. (If HT1 is already running in the full kernel address space, that would imply that it had previously done an ASI Exit, which would have triggered a
stun_sibling, which would have already caused HT0 to exit the VM and wait in the kernel).Note that using core scheduling (or not) is a detail, what is important is whether HT are running with ASI or not. Running core scheduling will just improve chances to have all siblings run ASI at the same time and so improve ASI performances.
Quoted 14 lines
If HT1 now does an ASI Exit, that will trigger the
stun_sibling()operation in itspre_asi_exit()handler, which
will set the state of the core/HT0 to Stunned (and possibly send
an IPI too, though that will be ignored if HT0 was already in
kernel mode). Now when HT0 tries to re-enter the VM, since its
state is set to Stunned, it will just wait in a loop until HT1
does anunstun_sibling()operation, which it will do in itspost_asi_enterhandler the next time it does an ASI Enter (which
would be either just before VM Enter if it was KVM-ASI, or in the
next iteration of the idle loop if it was Idle-ASI). In either
case, HT1'spost_asi_enter()handler would also do aflush_sensitive_cpu_stateoperation before theunstun_sibling(), so when HT0 gets out of its wait-loop and does a VM Enter, there will not be any sensitive state left.Quoted 4 lines
One thing that probably was not clear from the patch, is that the stun state check and wait-loop is still always executed before VM Enter, even if no ASI Exit happened in that execution.
So if I understand correctly, you have following sequence:
0 - Initially state is set to "stunned" for all cpus (i.e. a cpu should wait before VMEnter)
1 - After ASI Enter: Set sibling state to "unstunned" (i.e. sibling can do VMEnter)
2 - Before VMEnter : wait while my state is "stunned"
3 - Before ASI Exit : Set sibling state to "stunned" (i.e. sibling should wait before VMEnter)
Quoted 6 lines
I have tried this kind of implementation, and the problem is with step 2 (wait while my state is "stunned"); how do you wait exactly? You can't just do an active wait otherwise you have all kind of problems (depending if you have interrupts enabled or not) especially as you don't know how long you have to wait for (this depends on what the other cpu is doing).
Quoted 5 lines
In our stunning implementation, we do an active wait with interrupts enabled and with a
need_resched()check to decide when to bail out to the scheduler (plus we also make sure that we re-enter ASI at the
end of the wait in case some interrupt exited ASI). What kind of
problems have you run into with an active wait, besides wasted CPU
cycles?
If you wait with interrupts enabled then there is window after the wait and before interrupts get disabled where a cpu can get an interrupt, exit ASI while the sibling is entering the VM. Also after a CPU has passed the wait and have disable interrupts, it can't be notified if the sibling has exited ASI:
T+01 - cpu A and B enter ASI - interrupts are enabled
T+02 - cpu A and B pass the wait because both are using ASI - interrupts are enabled T+03 - cpu A gets an interrupt
T+04 - cpu B disables interrupts
T+05 - cpu A exit ASI and process interrupts
T+06 - cpu B enters VM => cpu B runs VM while cpu A is not using ASI
T+07 - cpu B exits VM
T+08 - cpu B exits ASI
T+09 - cpu A returns from interrupt
T+10 - cpu A disables interrupts and enter VM => cpu A runs VM while cpu A is not using ASI
Quoted 5 lines
In any case, the specific stunning mechanism is orthogonal to ASI. This implementation of ASI can be integrated with different stunning implementations. The "kernel core scheduling" that you proposed is also an alternative to stunning and could be similarly integrated with ASI.
Yes, but for ASI to be relevant with KVM to prevent data leak, you need a fully functional and reliable stunning mechanism, otherwise ASI is useless. That's why I think it is better to first focus on having an effective stunning mechanism and then implement ASI.
alex.
Hi Alex,
Quoted 30 lines · Junaid Shahid
Quoted 22 lines · Alexandre Chartre
Quoted 14 lines
So if I understand correctly, you have following sequence:
0 - Initially state is set to "stunned" for all cpus (i.e. a cpu should wait before VMEnter)
1 - After ASI Enter: Set sibling state to "unstunned" (i.e. sibling can do VMEnter)
2 - Before VMEnter : wait while my state is "stunned"
3 - Before ASI Exit : Set sibling state to "stunned" (i.e. sibling should wait before VMEnter)
Quoted 6 lines
I have tried this kind of implementation, and the problem is with step 2 (wait while my state is "stunned"); how do you wait exactly? You can't just do an active wait otherwise you have all kind of problems (depending if you have interrupts enabled or not) especially as you don't know how long you have to wait for (this depends on what the other cpu is doing).
Quoted 6 lines
In our stunning implementation, we do an active wait with interrupts enabled and with a
need_resched()check to decide when to bail out to the scheduler (plus we also make sure that we re-enter ASI at the end of the wait in case some interrupt exited ASI). What kind of problems have you run into with an active wait, besides wasted CPU cycles?
Quoted 3 lines
If you wait with interrupts enabled then there is window after the wait and before interrupts get disabled where a cpu can get an interrupt, exit ASI while the sibling is entering the VM.
We actually do another check after disabling interrupts and if it turns out that we need to wait again, we just go back to the wait loop after re-enabling interrupts. But, irrespective of that,
Quoted 3 lines
Also after a CPU has passed
the wait and have disable interrupts, it can't be notified if the sibling has exited ASI:
I don't think that this is actually the case. Yes, the IPI from the sibling will be blocked while the host kernel has disabled interrupts. However, when the host kernel executes a VMENTER, if there is a pending IPI, the VM will immediately exit back to the host even before executing any guest code. So AFAICT there is not going to be any data leak in the scenario that you mentioned. Basically, the "cpu B runs VM" in step T+06 won't actually happen.
Quoted 11 lines
T+01 - cpu A and B enter ASI - interrupts are enabled
T+02 - cpu A and B pass the wait because both are using ASI - interrupts are enabled T+03 - cpu A gets an interrupt
T+04 - cpu B disables interrupts
T+05 - cpu A exit ASI and process interrupts
T+06 - cpu B enters VM => cpu B runs VM while cpu A is not using ASI
T+07 - cpu B exits VM
T+08 - cpu B exits ASI
T+09 - cpu A returns from interrupt
T+10 - cpu A disables interrupts and enter VM => cpu A runs VM while cpu A is not using ASI
The "cpu A runs VM while cpu A is not using ASI" will also not happen, because cpu A will re-enter ASI after disabling interrupts and before entering the VM.
Quoted 7 lines
Quoted 6 lines
In any case, the specific stunning mechanism is orthogonal to ASI. This implementation of ASI can be integrated with different stunning implementations. The "kernel core scheduling" that you proposed is also an alternative to stunning and could be similarly integrated with ASI.
Quoted 5 lines
Yes, but for ASI to be relevant with KVM to prevent data leak, you need a fully functional and reliable stunning mechanism, otherwise ASI is useless. That's why I think it is better to first focus on having an effective stunning mechanism and then implement ASI.
Sure, that makes sense. The only caveat is that, at least in our testing, the overhead of stunning alone without ASI seemed too high. But I can try to see if we might be able to post our stunning implementation with the next version of the RFC.
Thanks,
Junaid
PS: I am away from the office for a few weeks, so email replies may be delayed until next month.