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

rcu

简介

RCU,全称 Read, Copy Update,即 随便读,写操作需要复制一份新数据出来进行修改, 然后再更新回去,最后在适当时候回收旧数据

RCU 涉及两个概念:

  • 指向要保护数据的指针(RCU protected pointer)

  • 通过指针访问的数据(RCU protected data)

详细写操作如下:

  1. 复制一份新数据出来进行修改,将 RCU protected pointer 指向新数据,一旦把 RCU protected pointer 指向新数据,在此之前的 reader 都是在旧数据进行访问,因为 reader 都是通过 pointer 访问数据

  2. 等待所有访问旧数据的 reader 离开临界区之后再回收旧数据,此等待时间被称为 grace period

spin lock/rw spin lock/RCU 对比:

  • spin lock 属于互斥锁,通过 next 和 owner 共享变量 保护临界区,任何时候只有一个 thread(reader or writer)进入临界区

  • rw spin lock 基于 memory 中的 共享变量 保护临界区,允许多个 reader 并发执行, 不过 reader 和 writer 不能并发执行,必须等于所有 reader 退出,writer 才能进入临界区

  • RCU 允许一个 updater(不能同时多个 updater 进入临界区,通过 spinlock 来保证) 和多个 reader 并发执行。当 RCU updater 进入临界区的时候,即便有 reader 存在也无所谓, 它可以长驱直入,不需要spin。同样的,即便有一个 updater 正在临界区工作,这并不能阻挡 RCU reader 的步伐。

RCU reader 不需要访问任何 共享变量,从而提升 reader 性能,而且 reader 和 updater 可以并发执行。

适用的场景

  1. RCU 只能保护动态分配的数据结构,并且必须是通过指针访问该数据结构

  2. 受 RCU 保护的临界区内不能 sleep(除了 SRCU)

  3. 对 writer 的性能没有特别要求,但是 reader 性能要求极高

  4. reader 对新旧数据不敏感

基本RCU操作

对于 reader,RCU 的操作包括:

  1. rcu_read_lock() 标识 RCU read side 临界区的开始

  2. rcu_dereference() 获得 RCU protected pointer

  3. rcu_read_unlock() 标识 reader 离开 RCU read side 临界区

对于 updater,RCU 的操作包括:

  1. rcu_assign_pointer() 在完成新数据更新后,调用此接口让 RCU protected pointer 指向 RCU protected data

  2. synchronize_rcu() 在完成新数据更新后,同步等待所有在旧数据上的 reader 离开临界区, 然后直接进行回收旧数据

  3. call_rcu() 在某些情况下(如:softirq context),updater 无法阻塞,调用此函数, 仅仅是注册 callback 函数就直接返回,然后在适当时机调用 callback 函数,完成回收旧数据操作

参考

Previouspage_tableNextkvm

Last updated 2 years ago

Was this helpful?

Linux内核同步机制之(七):RCU基础