€•$‡Œsphinx.addnodes”Œdocument”“”)”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)”}”(hhh]”(hŒ pending_xref”“”)”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…””}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ0/translations/zh_CN/kernel-hacking/false-sharing”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)”}”(hhh]”hŒChinese (Traditional)”…””}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ0/translations/zh_TW/kernel-hacking/false-sharing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒItalian”…””}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ0/translations/it_IT/kernel-hacking/false-sharing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒJapanese”…””}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ0/translations/ja_JP/kernel-hacking/false-sharing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒKorean”…””}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ0/translations/ko_KR/kernel-hacking/false-sharing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒPortuguese (Brazilian)”…””}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ0/translations/pt_BR/kernel-hacking/false-sharing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒSpanish”…””}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ0/translations/sp_SP/kernel-hacking/false-sharing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒcomment”“”)”}”(hŒ SPDX-License-Identifier: GPL-2.0”h]”hŒ SPDX-License-Identifier: GPL-2.0”…””}”hh·sbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1hµhhh²hh³ŒJ/var/lib/git/docbuild/linux/Documentation/kernel-hacking/false-sharing.rst”h´KubhŒsection”“”)”}”(hhh]”(hŒtitle”“”)”}”(hŒ False Sharing”h]”hŒ False Sharing”…””}”(hhÏh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhhÊh²hh³hÇh´KubhÉ)”}”(hhh]”(hÎ)”}”(hŒWhat is False Sharing”h]”hŒWhat is False Sharing”…””}”(hhàh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhhÝh²hh³hÇh´KubhŒ paragraph”“”)”}”(hŒßFalse sharing is related with cache mechanism of maintaining the data coherence of one cache line stored in multiple CPU's caches; then academic definition for it is in [1]_. Consider a struct with a refcount and a string::”h]”(hŒ«False sharing is related with cache mechanism of maintaining the data coherence of one cache line stored in multiple CPU’s caches; then academic definition for it is in ”…””}”(hhðh²hh³Nh´NubhŒfootnote_reference”“”)”}”(hŒ[1]_”h]”hŒ1”…””}”(hhúh²hh³Nh´Nubah}”(h]”Œid1”ah ]”h"]”h$]”h&]”Œrefid”Œid4”Œdocname”Œkernel-hacking/false-sharing”uh1høhhðŒresolved”KubhŒ1. Consider a struct with a refcount and a string:”…””}”(hhðh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K hhÝh²hubhŒ literal_block”“”)”}”(hŒustruct foo { refcount_t refcount; ... char name[16]; } ____cacheline_internodealigned_in_smp;”h]”hŒustruct foo { refcount_t refcount; ... char name[16]; } ____cacheline_internodealigned_in_smp;”…””}”hjsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jh³hÇh´KhhÝh²hubhï)”}”(hŒFMember 'refcount'(A) and 'name'(B) _share_ one cache line like below::”h]”hŒMMember ‘refcount’(A) and ‘name’(B) _share_ one cache line like below:”…””}”(hj(h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KhhÝh²hubj)”}”(hXß +-----------+ +-----------+ | CPU 0 | | CPU 1 | +-----------+ +-----------+ / | / | V V +----------------------+ +----------------------+ | A B | Cache 0 | A B | Cache 1 +----------------------+ +----------------------+ | | ---------------------------+------------------+----------------------------- | | +----------------------+ | | +----------------------+ Main Memory | A B | +----------------------+”h]”hXß +-----------+ +-----------+ | CPU 0 | | CPU 1 | +-----------+ +-----------+ / | / | V V +----------------------+ +----------------------+ | A B | Cache 0 | A B | Cache 1 +----------------------+ +----------------------+ | | ---------------------------+------------------+----------------------------- | | +----------------------+ | | +----------------------+ Main Memory | A B | +----------------------+”…””}”hj6sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jh³hÇh´KhhÝh²hubhï)”}”(hXx'refcount' is modified frequently, but 'name' is set once at object creation time and is never modified. When many CPUs access 'foo' at the same time, with 'refcount' being only bumped by one CPU frequently and 'name' being read by other CPUs, all those reading CPUs have to reload the whole cache line over and over due to the 'sharing', even though 'name' is never changed.”h]”hX”‘refcount’ is modified frequently, but ‘name’ is set once at object creation time and is never modified. When many CPUs access ‘foo’ at the same time, with ‘refcount’ being only bumped by one CPU frequently and ‘name’ being read by other CPUs, all those reading CPUs have to reload the whole cache line over and over due to the ‘sharing’, even though ‘name’ is never changed.”…””}”(hjDh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K(hhÝh²hubhï)”}”(hŒêThere are many real-world cases of performance regressions caused by false sharing. One of these is a rw_semaphore 'mmap_lock' inside mm_struct struct, whose cache line layout change triggered a regression and Linus analyzed in [2]_.”h]”(hŒéThere are many real-world cases of performance regressions caused by false sharing. One of these is a rw_semaphore ‘mmap_lock’ inside mm_struct struct, whose cache line layout change triggered a regression and Linus analyzed in ”…””}”(hjRh²hh³Nh´Nubhù)”}”(hŒ[2]_”h]”hŒ2”…””}”(hjZh²hh³Nh´Nubah}”(h]”Œid2”ah ]”h"]”h$]”h&]”j Œid5”j j uh1høhjRj KubhŒ.”…””}”(hjRh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K/hhÝh²hubhï)”}”(hŒ6There are two key factors for a harmful false sharing:”h]”hŒ6There are two key factors for a harmful false sharing:”…””}”(hjth²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K4hhÝh²hubhŒ bullet_list”“”)”}”(hhh]”(hŒ list_item”“”)”}”(hŒ-A global datum accessed (shared) by many CPUs”h]”hï)”}”(hj‹h]”hŒ-A global datum accessed (shared) by many CPUs”…””}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K6hj‰ubah}”(h]”h ]”h"]”h$]”h&]”uh1j‡hj„h²hh³hÇh´Nubjˆ)”}”(hŒpIn the concurrent accesses to the data, there is at least one write operation: write/write or write/read cases. ”h]”hï)”}”(hŒoIn the concurrent accesses to the data, there is at least one write operation: write/write or write/read cases.”h]”hŒoIn the concurrent accesses to the data, there is at least one write operation: write/write or write/read cases.”…””}”(hj¤h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K7hj ubah}”(h]”h ]”h"]”h$]”h&]”uh1j‡hj„h²hh³hÇh´Nubeh}”(h]”h ]”h"]”h$]”h&]”Œbullet”Œ*”uh1j‚h³hÇh´K6hhÝh²hubhï)”}”(hŒtThe sharing could be from totally unrelated kernel components, or different code paths of the same kernel component.”h]”hŒtThe sharing could be from totally unrelated kernel components, or different code paths of the same kernel component.”…””}”(hjÀh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K:hhÝh²hubeh}”(h]”Œwhat-is-false-sharing”ah ]”h"]”Œwhat is false sharing”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´KubhÉ)”}”(hhh]”(hÎ)”}”(hŒFalse Sharing Pitfalls”h]”hŒFalse Sharing Pitfalls”…””}”(hjÙh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjÖh²hh³hÇh´K?ubhï)”}”(hX˜Back in time when one platform had only one or a few CPUs, hot data members could be purposely put in the same cache line to make them cache hot and save cacheline/TLB, like a lock and the data protected by it. But for recent large system with hundreds of CPUs, this may not work when the lock is heavily contended, as the lock owner CPU could write to the data, while other CPUs are busy spinning the lock.”h]”hX˜Back in time when one platform had only one or a few CPUs, hot data members could be purposely put in the same cache line to make them cache hot and save cacheline/TLB, like a lock and the data protected by it. But for recent large system with hundreds of CPUs, this may not work when the lock is heavily contended, as the lock owner CPU could write to the data, while other CPUs are busy spinning the lock.”…””}”(hjçh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´K@hjÖh²hubhï)”}”(hŒYLooking at past cases, there are several frequently occurring patterns for false sharing:”h]”hŒYLooking at past cases, there are several frequently occurring patterns for false sharing:”…””}”(hjõh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KGhjÖh²hubjƒ)”}”(hhh]”(jˆ)”}”(hŒ]lock (spinlock/mutex/semaphore) and data protected by it are purposely put in one cache line.”h]”hï)”}”(hŒ]lock (spinlock/mutex/semaphore) and data protected by it are purposely put in one cache line.”h]”hŒ]lock (spinlock/mutex/semaphore) and data protected by it are purposely put in one cache line.”…””}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KJhjubah}”(h]”h ]”h"]”h$]”h&]”uh1j‡hjh²hh³hÇh´Nubjˆ)”}”(hŒ¿global data being put together in one cache line. Some kernel subsystems have many global parameters of small size (4 bytes), which can easily be grouped together and put into one cache line.”h]”hï)”}”(hŒ¿global data being put together in one cache line. Some kernel subsystems have many global parameters of small size (4 bytes), which can easily be grouped together and put into one cache line.”h]”hŒ¿global data being put together in one cache line. Some kernel subsystems have many global parameters of small size (4 bytes), which can easily be grouped together and put into one cache line.”…””}”(hj"h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KLhjubah}”(h]”h ]”h"]”h$]”h&]”uh1j‡hjh²hh³hÇh´Nubjˆ)”}”(hŒ™data members of a big data structure randomly sitting together without being noticed (cache line is usually 64 bytes or more), like 'mem_cgroup' struct. ”h]”hï)”}”(hŒ˜data members of a big data structure randomly sitting together without being noticed (cache line is usually 64 bytes or more), like 'mem_cgroup' struct.”h]”hŒœdata members of a big data structure randomly sitting together without being noticed (cache line is usually 64 bytes or more), like ‘mem_cgroup’ struct.”…””}”(hj:h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hîh³hÇh´KOhj6ubah}”(h]”h ]”h"]”h$]”h&]”uh1j‡hjh²hh³hÇh´Nubeh}”(h]”h ]”h"]”h$]”h&]”j¾j¿uh1j‚h³hÇh´KJhjÖh²hubhï)”}”(hŒ]”j×auŒ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”(j3jgj—eŒ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”jKs…”R”Œparse_messages”]”Œtransform_messages”]”Œ transformer”NŒ include_log”]”Œ decoration”Nh²hub.