1.. SPDX-License-Identifier: GPL-2.0+ 2 3.. include:: ../disclaimer-zh_CN.rst 4 5:Original: Documentation/core-api/this_cpu_ops.rst 6 7:������: 8 9 ��������� Binbin Zhou <zhoubinbin@loongson.cn> 10 11:������: 12 13 ��������� Wu Xiangcheng <bobwxc@email.cn> 14 15============ 16this_cpu������ 17============ 18 19:������: Christoph Lameter, 2014���8���4��� 20:������: Pranith Kumar, 2014���8���2��� 21 22this_cpu���������������������������������������������������������������CPU������������������������������������������ 23������������������������������cpu������������������������������������������CPU��������������������������������� 24 25this_cpu������������CPU������������������������������������������������CPU��������������������������������������� 26���CPU��������������������������������� 27 28������������������������������������������������������������������������������������������������������������������������ 29������������������������������������������������������������������������������ 30 31������-������-��������������������������������������������������������������������������������������������������������� 32������������������������������������������������������������������������������������x86������������RMW������������ 33���������������������������������inc/dec/cmpxchg������������������������������������������������ 34 35������������������������������������������������������������������������������������������������������������������������ 36������������������CPU��������������������������������������������������������������������������������������������������� 37������������������ 38 39���������������������������������CPU������������������������������������������������������ ``this_cpu_*`` ������ 40���RMW��������������������������������������������������� 41 42this_cpu������������������������������������������������ 43 44������������������������������������������this_cpu()���������������������������������������������������������������:: 45 46 this_cpu_read(pcp) 47 this_cpu_write(pcp, val) 48 this_cpu_add(pcp, val) 49 this_cpu_and(pcp, val) 50 this_cpu_or(pcp, val) 51 this_cpu_add_return(pcp, val) 52 this_cpu_xchg(pcp, nval) 53 this_cpu_cmpxchg(pcp, oval, nval) 54 this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) 55 this_cpu_sub(pcp, val) 56 this_cpu_inc(pcp) 57 this_cpu_dec(pcp) 58 this_cpu_sub_return(pcp, val) 59 this_cpu_inc_return(pcp) 60 this_cpu_dec_return(pcp) 61 62 63this_cpu��������������������� 64---------------------- 65 66���x86������fs:���gs:���������������������CPU������������������������������������������������������������������CPU 67���������������������������������������������CPU���������������������CPU��������������������������������������������� 68������������������������������ 69 70������:: 71 72 DEFINE_PER_CPU(int, x); 73 int z; 74 75 z = this_cpu_read(x); 76 77���������������������:: 78 79 mov ax, gs:[x] 80 81���������������CPU������������������������������������������������������������������������������this_cpu_ops��������� 82���������������������������������/��������������������������������������������������������������������������������������� 83��������� 84 85���������������this_cpu������:: 86 87 this_cpu_inc(x) 88 89������������������������������������������������:: 90 91 inc gs:[x] 92 93������������������������������������������������������������������:: 94 95 int *y; 96 int cpu; 97 98 cpu = get_cpu(); 99 y = per_cpu_ptr(&x, cpu); 100 (*y)++; 101 put_cpu(); 102 103������������������������������������������������������������������CPU������������������������������������������������������ 104``this_cpu_inc()`` ���������������CPU������������������������������������������������������������������������ 105������this_cpu������������������������������������������������������������������������������������������������������ 106���������������������������������������CPU������������������������������������������������ 107 108���CPU������������������������������������������������������������������������������������������������������������������ 109������������������������������������������������CPU��������������������������������������������������������������������� 110������������������������������������������������������������CPU������������������ 111 112 113��������������� 114---------- 115 116:: 117 118 y = this_cpu_ptr(&x) 119 120���������CPU������������������(&x!)���������������������������������������������CPU������������������ 121``this_cpu_ptr`` ��������������� ``get_cpu``/``put_cpu`` ������������������������������������������ 122���������������������������������������CPU���������������������������������������������CPU��������������� 123 124������������������������������������������������������������������������������������������������������������������������ 125������������CPU��������������������������������������������������������������������������������������������������������� 126���������CPU��������� 127 128���CPU������������������ 129----------------- 130 131���CPU������������������CPU������������������������������������������������������������������������������������������ 132���������������������������������������������������������������CPU��������������������������������������������������������� 133 134���������������CPU������������������������������x���&x��������������������������������������������������������������� 135��������������������� 136 137:: 138 139 DEFINE_PER_CPU(int, x); 140 141������CPU���������������������������������������������x������������CPU������������������this_cpu������������������ 142���cpu��������� 143 144:: 145 146 int __percpu *p = &x; 147 148&x���p������CPU��������������������� ``this_cpu_ptr()`` ���������CPU��������������������������������������� 149��������������� 150 151 152���CPU������������������������ 153--------------------- 154 155������������������������CPU������:: 156 157 struct s { 158 int n,m; 159 }; 160 161 DEFINE_PER_CPU(struct s, p); 162 163 164���������������������������������:: 165 166 this_cpu_inc(p.m) 167 168 z = this_cpu_cmpxchg(p.m, 0, 1); 169 170 171���������������������������������������s������������:: 172 173 struct s __percpu *ps = &p; 174 175 this_cpu_dec(ps->m); 176 177 z = this_cpu_inc_return(ps->n); 178 179 180��������������������������� ``this_cpu ops`` ������������������������������������������������������ 181``this_cpu_ptr()``:: 182 183 struct s *pp; 184 185 pp = this_cpu_ptr(&p); 186 187 pp->m--; 188 189 z = pp->n++; 190 191 192this_cpu ops��������� 193------------------ 194 195this_cpu������������������������������������������������������������CPU��������������������������������������������� 196������������������������������������������������������������������������������������������������������������������������ 197������������������������������������������������������������������������������������������������������������������������ 198������������������������������������������������������__this_cpu��������� 199 200���������������������������������������������������������������������������������������CPU������������������������������ 201������������������������������������������������������������������������������������������������������������������������ 202���������RMW������������������:: 203 204 __this_cpu_read(pcp) 205 __this_cpu_write(pcp, val) 206 __this_cpu_add(pcp, val) 207 __this_cpu_and(pcp, val) 208 __this_cpu_or(pcp, val) 209 __this_cpu_add_return(pcp, val) 210 __this_cpu_xchg(pcp, nval) 211 __this_cpu_cmpxchg(pcp, oval, nval) 212 __this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) 213 __this_cpu_sub(pcp, val) 214 __this_cpu_inc(pcp) 215 __this_cpu_dec(pcp) 216 __this_cpu_sub_return(pcp, val) 217 __this_cpu_inc_return(pcp) 218 __this_cpu_dec_return(pcp) 219 220 221���������x���������������������������������������������������������������������������������-������-��������������������� 222������������������������������������������ 223 224 225&this_cpu_ptr(pp)->n ������ this_cpu_ptr(&pp->n) 226---------------------------------------------- 227 228���������������������������������������������������������������������n��������������������������������������������������� 229������������������������ 230 231������������������������������������������������������������������������������������������������������������������������ 232��������������� ``()`` ������������������������������ ``this_cpu_read()`` ��������������������������������� 233 234 235���������������CPU������ 236----------------- 237 238���CPU���������������������������������CPU��������������������������������������������������� ``this_cpu_ops()`` 239��������� ``���������`` ���������������������CPU��������������������������������� 240 241���������������������������������������������������������CPU������������������������������������������������������������ 242���������������������������������������������������������������������������������������������this_cpu��������������� 243���������������������������������this_cpu RMW��������� 244 245���������������������������������������������������CPU���������������������������������������������������IPI��������� 246������CPU���������������CPU��������������������� 247 248������������������CPU��������������������������� ``per_cpu_ptr()`` ������:: 249 250 251 DEFINE_PER_CPU(struct data, datap); 252 253 struct data *p = per_cpu_ptr(&datap, cpu); 254 255���������������������������������������������������CPU��������� 256 257������������������������������������datap������������������������:: 258 259 struct data *p = this_cpu_ptr(&datap); 260 261������������������this_cpu_ptr������������������������������cpu��������������������������������� 262 263������������������������������������������cpu������CPU���������������������this_cpu������������������������������ 264������������������������������������������ 265 266���������������������������������������������������������������������CPU������������������������������������������������ 267��������������������������������������������� 268 269������������������:: 270 271 272 struct test { 273 atomic_t a; 274 int b; 275 }; 276 277 DEFINE_PER_CPU(struct test, onecacheline); 278 279��������������������������������������� ``a`` ������������������������������this_cpu ops��������������� ``b`` ��� 280������������������������������������������������������������������������������������������������������������������������ 281������������������������������������������������������������������������IPI��������������������������������������������� 282���CPU��������� 283 284������������������������������������������������������������������������������������������������������������������������ 285������������������������������CPU������������������������������������������������������������������������ 286