Notes
main
main
  • Introduction
  • linuxKernel
    • tips
    • make_help
    • old linux
      • compile_linux0.11
      • TestEnvironment
      • load_setup
      • get_hard_data
    • list
    • plist
    • fifo
    • idr
    • xarray
    • rbtree
    • maple_tree
    • syscall
    • bitmap
    • page
    • page_flags
    • page_size
    • page mapcount
    • page refcount
    • folio
    • slub
      • proc_slabinfo
      • slub_theory
      • kmalloc_kfree
      • kmem_cache
      • slab_alloc
      • slab_free
      • proc_meminfo_SReclaimable_SReclaimable
    • vmalloc
    • brk
    • mmap
    • mremap
    • mprotect
    • madvise
    • read
    • write
    • shmem
    • huge_page
    • page_fault
    • rmap
    • lru
    • multi-gen-LRU
    • page_reclaim
    • page_cache
    • page_table
    • rcu
    • kvm
    • aarch64_boot
    • tracing_system
    • cache_coherence_and_memory_consistency
    • cpu_speculates
    • mmap_lock
    • per-vma_lock
    • cgroup
    • symbol
    • campact
    • page_ext
    • mempool
    • kernelstack
    • filesystem
    • io_stack
    • workingset
    • ioremap
    • sched_period
  • linuxDebug
    • openocd_openjtag
    • i2c_tools
    • objdump
    • addr2line
    • gdb_useage
    • debug_linux_kernel_via_gdb
    • debug_linux_module_via_gdb
    • early_boot
    • sequentially_execute
    • dynamic_debug
    • research_linuxKernel_by_patch
    • tracefs
    • ebpf
    • bpftrace
    • perf
    • flame_graph
    • crash
    • ASAN_HWASAN_MTE_check_mem_bug
    • page_owner
    • vmtouch
    • fio
    • benchmark
  • linuxSystem
    • common
      • system_version
      • procfs
      • proc_sys_vm
      • cmd_ps
      • makefile
      • file_descriptor
      • psi
      • ulimit
      • top
      • delay_accounting
    • ubuntu
      • custom_kernel
      • get_cmd_src
      • record_ssh_info
      • log
      • run_custom_script
      • repo
      • cockpit
      • nfs
      • tftp
      • misc
    • fedora
      • system_upgrade
      • custom_kernel
      • lvextend
      • yt-dlp
      • jellyfin
  • linuxDriver
    • i2c_peripherals_driver
    • spi_peripherals_driver
    • gpio_subsystem
    • IRQ_driver
    • blockIO_unblockIO_async
    • linux_own_driver
    • misc_device
    • input_device
    • timer
    • atomic_spinlock_semaphore_mutex
    • lcd
    • touch_screen
    • debugfs
    • v4l2
    • mmap
  • hardware
    • paging_mmu_pt
    • iommu
  • process_thread_scheduler
    • scheduler01
    • scheduler02
    • scheduler03
    • scheduler04
    • scheduler05
    • scheduler06
  • memory_management
    • mm1
    • mm2
    • mm3
    • mm4
    • mm5
  • input_output_filesystem
    • io_fs_01
    • io_fs_02
    • io_fs_03
    • io_fs_04
  • lock_and_lockup_detector
    • general_lock
    • hung_task
    • softLockup_hardLockup
    • crash_experiment
  • MIT_6.S081
    • 6.S081_Operating_System_Engineering
    • Schedule.md
    • Class
      • Overview
      • Administrivia
    • Labs
      • Tools
      • Guidance
      • startup
      • syscall
      • page_table
      • Calling_Convention
      • traps
    • xv6
      • xv6
    • References.md
  • qemu
    • qemu_buildroot
    • qemu_busybox.md
    • Serial.md
    • demo_mini2440
      • 0_compilation_error_summary
      • 1_compilation_steps
      • 2_operation_mode
      • 3_transplant_tools_libraries
      • 4_tools_use
      • reference_website
  • tools
    • getKernelSourceCodeList
    • nat
    • shell
    • translating
    • YouCompleteMe
    • cscope
    • global
    • vscode
    • vim
    • binary
    • markdown
    • draw
    • git
    • tig
    • tmux
    • mail_client
    • download_patchset_from_LKML
    • minicom
    • clash
  • other
    • interview
    • interview_c_base
    • know_dontknow
    • Stop-Ask-Questions-The-Stupid-Ways
    • How-To-Ask-Questions-The-Smart-Way
    • docker
    • buildroot
    • rv32_to_rv64
Powered by GitBook
On this page

Was this helpful?

  1. linuxKernel

cpu_speculates

为了进一步提升指令执行效率,编译器与 CPU 都提供各自的优化策略。

编译器提供 likely() and unlikely() 函数给软件显式使用,用于告诉编译器哪一个分支执行概率大,从而帮忙编译器更好地优化代码执行,来提升指令执行效率。

CPU 提供了 speculative execution(推测执行),这是硬件内部优化技术,无需软件进行任何操作。主要动作是 CPU 尝试猜测下一步指令并预先执行,从而在需要时可以更快地提供结果,来提升指令执行效率。

这里有一个问题,CPU speculative execution 是来自于未经验证的数据,可能导致数组越界访问内存区域(如果这块内存区域刚好存储密码等重要信息),虽然该分支最终不会被实际执行,但是由于 L1/L2/L3 cache 的原因,这些预先执行指令得到的数据会存在 cache 中,然后就能够利用 cache 执行速度比普通内存快的特征来判断,得到这些缓存在 cache 中的数据。这也就是著名的 Meltdown/Spectre 安全漏洞。

下面是一个伪代码:

foo();

if (index < size)
    val = array[index];

当 CPU 执行 foo() 时,CPU speculative execution 可能预先执行 val = array[index] ,这样即使后面真正执行 if (index < size) 无法成立, 但是 array[index] 值已经缓存在 cache 中,然后就能够利用 cache 执行速度比普通内存快的特征来判断,得到这些缓存在 cache 中的数据。

于是我们需要告诉 CPU 硬件不要做分支预测或者说控制 index 不要越界,这就是 array_index_nospec() 的功能。

修改后的伪代码:

foo();

if (index < size) {
    index = array_index_nospec(index, size);
    val = array[index];
}

修改后,当 CPU speculative execution 时,array_index_nospec() 能够保证 index 是一个合法值,从 而 不会导致非法 array[index] 值被缓存在 cache 中。

还有其它一些防止 CPU speculative execution 带来的额外问题,可以参考 include/linux/nospec.h。

Previouscache_coherence_and_memory_consistencyNextmmap_lock

Last updated 1 year ago

Was this helpful?