Bringing restartable sequences out of the niche

#王者杯·14天创作挑战营·第5期#

The restartable sequences feature, which was added to the 4.18 kernel in 2018, exists to enable better performance in certain types of threaded applications. While there are users for restartable sequences, they tend to be relatively specialized code; this is not a tool that most application developers reach for. Over time, though, the use of restartable sequences has grown, and it looks to grow further as the feature is tied to new capabilities provided by the kernel. As restartable sequences become less of a niche feature, though, some problems have turned up; fixing one of them may involve an ABI change visible in user space.

可重启序列(restartable sequences)功能是在 2018 年的内核 4.18 版本中引入的,目的是在某些类型的多线程应用中提供更好的性能。虽然有一些用户在使用可重启序列,但它们往往出现在相对专业化的代码中;大多数应用开发者不会主动使用这一工具。不过,随着时间推移,可重启序列的使用范围逐渐扩大,并且由于它与内核新提供的能力绑定,其使用量还会继续增长。然而,随着这一功能不再是小众特性,一些问题也开始浮现;修复其中之一可能会涉及到用户空间可见的 ABI 变更。


A restartable sequences overview
As the number of CPUs in a system grows, so does the desire to write and run highly parallel programs. In user space, as well as in the kernel, concurrent code using locks eventually runs into scalability problems, leading to an interest in the use of lockless algorithms instead. The kernel has a distinct advantage when it comes to lockless concurrent access, though, in that code running in kernel space can prevent interruptions from interrupts, preemption, or migration to another CPU during critical sections; user space has no such guarantees. So any user-space lockless algorithm must work correctly in an environment where code execution can be interrupted at any time.

可重启序列概览
随着系统中 CPU 数量的增长,人们对编写和运行高并发程序的需求也越来越强。在用户空间和内核空间中,使用锁的并发代码最终都会遇到可扩展性问题,这促使人们转而关注无锁算法。然而,内核在无锁并发访问方面有明显优势,因为内核态运行的代码可以在关键区间内阻止中断、抢占,或者被迁移到另一个 CPU;而用户空间没有这样的保证。因此,任何用户空间的无锁算法都必须能够在随时可能被中断的环境下正确工作。


Restartable sequences are one solution to this problem. To use this feature, an application must designate a critical section that does some work, culminating in a single atomic instruction that commits whatever change is being made. One of the earliest uses of restartable sequences was a memory allocator that would, within its critical section, identify an object to allocate from a per-CPU list, then remove it from that list with an atomic compare-and-exchange operation.

可重启序列是解决这一问题的一种方法。为了使用该功能,应用必须指定一个关键区间来完成某些操作,并最终以一条原子指令提交所做的更改。可重启序列最早的应用场景之一是内存分配器:它在关键区间中从每个 CPU 的对象列表中找出一个可分配对象,然后通过一条原子比较交换操作将其从列表中移除。


It would obviously be problematic if this critical section were to be interrupted between the identification of the object to allocate and the final step committing the change. To work around this problem, the application informs the kernel of the critical section by filling a structure (struct rseq_cs) describing the address range of the instructions within that section, along with a special abort address. If the kernel interrupts the execution of the relevant thread, to preempt it, move it to another CPU, or to deliver a signal, it will, on return to user space check for the existence of this structure. If the cu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mounter625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值