Documentation/translations/zh_CN/core-api/this_cpu_ops.rst

Source file repositories/reference/linux-study-clean/Documentation/translations/zh_CN/core-api/this_cpu_ops.rst

File Facts

System
Linux kernel
Corpus path
Documentation/translations/zh_CN/core-api/this_cpu_ops.rst
Extension
.rst
Size
10010 bytes
Lines
286
Domain
Support Tooling And Documentation
Bucket
Documentation
Inferred role
Support Tooling And Documentation: documentation
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct s {
		int n,m;
	};

	DEFINE_PER_CPU(struct s, p);


这些字段的操作非常简单::

	this_cpu_inc(p.m)

	z = this_cpu_cmpxchg(p.m, 0, 1);


如果我们有一个相对于结构体s的偏移量::

	struct s __percpu *ps = &p;

	this_cpu_dec(ps->m);

	z = this_cpu_inc_return(ps->n);


如果我们后面不使用 ``this_cpu ops`` 来操作字段,则指针的计算可能需要使用
``this_cpu_ptr()``::

	struct s *pp;

	pp = this_cpu_ptr(&p);

	pp->m--;

	z = pp->n++;


this_cpu ops的变体
------------------

this_cpu的操作是中断安全的。一些架构不支持这些每CPU的本地操作。在这种情况下,该操
作必须被禁用中断的代码所取代,然后做那些保证是原子的操作,再重新启用中断。当然这
样做是很昂贵的。如果有其他原因导致调度器不能改变我们正在执行的处理器,那么就没有
理由禁用中断了。为此,我们提供了以下__this_cpu操作。

这些操作不能保证并发中断或抢占。如果在中断上下文中不使用每CPU变量并且调度程序无法
抢占,那么它们是安全的。如果在操作进行时仍有中断发生,并且中断也修改了变量,则无
法保证RMW操作是安全的::

	__this_cpu_read(pcp)
	__this_cpu_write(pcp, val)
	__this_cpu_add(pcp, val)
	__this_cpu_and(pcp, val)
	__this_cpu_or(pcp, val)
	__this_cpu_add_return(pcp, val)
	__this_cpu_xchg(pcp, nval)
	__this_cpu_cmpxchg(pcp, oval, nval)
	__this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
	__this_cpu_sub(pcp, val)
	__this_cpu_inc(pcp)
	__this_cpu_dec(pcp)
	__this_cpu_sub_return(pcp, val)
	__this_cpu_inc_return(pcp)
	__this_cpu_dec_return(pcp)


将增加x,并且不会回退到在无法通过地址重定位和同一指令中的读取-修改-写入操作实现原
子性的平台上禁用中断的代码。


&this_cpu_ptr(pp)->n 对比 this_cpu_ptr(&pp->n)
----------------------------------------------

Annotation

Implementation Notes