__  __    __   __  _____      _            _          _____ _          _ _ 
 |  \/  |   \ \ / / |  __ \    (_)          | |        / ____| |        | | |
 | \  / |_ __\ V /  | |__) | __ ___   ____ _| |_ ___  | (___ | |__   ___| | |
 | |\/| | '__|> <   |  ___/ '__| \ \ / / _` | __/ _ \  \___ \| '_ \ / _ \ | |
 | |  | | |_ / . \  | |   | |  | |\ V / (_| | ||  __/  ____) | | | |  __/ | |
 |_|  |_|_(_)_/ \_\ |_|   |_|  |_| \_/ \__,_|\__\___| |_____/|_| |_|\___V 2.1
 if you need WebShell for Seo everyday contact me on Telegram
 Telegram Address : @jackleet
        
        
For_More_Tools: Telegram: @jackleet | Bulk Smtp support mail sender | Business Mail Collector | Mail Bouncer All Mail | Bulk Office Mail Validator | Html Letter private



Upload:

Command:

www-data@216.73.216.139: ~ $
<!DOCTYPE html>

<html lang="en" data-content_root="../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>Locking lessons &#8212; The Linux Kernel  documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=fa44fd50" />
    <link rel="stylesheet" type="text/css" href="../_static/alabaster.css?v=3918102e" />
    <script src="../_static/documentation_options.js?v=5929fcd5"></script>
    <script src="../_static/doctools.js?v=9bcbadda"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Wound/Wait Deadlock-Proof Mutex Design" href="ww-mutex-design.html" />
    <link rel="prev" title="Sequence counters and sequential locks" href="seqlock.html" />
   
  <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  

  
  

  </head><body>
  <div class="document">
    
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/logo.svg" alt="Logo of The Linux Kernel"/>
            </a></p>
<h1 class="logo"><a href="../index.html">The Linux Kernel</a></h1>



<p class="blurb">6.18.50</p>







<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>


<p>
<h3 class="kernel-toc-contents">Contents</h3>
<input type="checkbox" class="kernel-toc-toggle" id = "kernel-toc-toggle" checked>
<label class="kernel-toc-title" for="kernel-toc-toggle"></label>

<div class="kerneltoc" id="kerneltoc">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../process/development-process.html">Development process</a></li>
<li class="toctree-l1"><a class="reference internal" href="../process/submitting-patches.html">Submitting patches</a></li>
<li class="toctree-l1"><a class="reference internal" href="../process/code-of-conduct.html">Code of conduct</a></li>
<li class="toctree-l1"><a class="reference internal" href="../maintainer/index.html">Maintainer handbook</a></li>
<li class="toctree-l1"><a class="reference internal" href="../process/index.html">All development-process docs</a></li>
</ul>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../core-api/index.html">Core API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../driver-api/index.html">Driver APIs</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../subsystem-apis.html">Subsystems</a><ul class="current">
<li class="toctree-l2 current"><a class="reference internal" href="../subsystem-apis.html#core-subsystems">Core subsystems</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="../core-api/index.html">Core API Documentation</a></li>
<li class="toctree-l3"><a class="reference internal" href="../driver-api/index.html">Driver implementer’s API guide</a></li>
<li class="toctree-l3"><a class="reference internal" href="../mm/index.html">Memory Management Documentation</a></li>
<li class="toctree-l3"><a class="reference internal" href="../power/index.html">Power Management</a></li>
<li class="toctree-l3"><a class="reference internal" href="../scheduler/index.html">Scheduler</a></li>
<li class="toctree-l3"><a class="reference internal" href="../timers/index.html">Timers</a></li>
<li class="toctree-l3 current"><a class="reference internal" href="index.html">Locking</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../subsystem-apis.html#human-interfaces">Human interfaces</a></li>
<li class="toctree-l2"><a class="reference internal" href="../subsystem-apis.html#networking-interfaces">Networking interfaces</a></li>
<li class="toctree-l2"><a class="reference internal" href="../subsystem-apis.html#storage-interfaces">Storage interfaces</a></li>
<li class="toctree-l2"><a class="reference internal" href="../subsystem-apis.html#other-subsystems">Other subsystems</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Locking</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="locktypes.html">Lock types and their rules</a></li>
<li class="toctree-l2"><a class="reference internal" href="lockdep-design.html">Runtime locking correctness validator</a></li>
<li class="toctree-l2"><a class="reference internal" href="lockstat.html">Lock Statistics</a></li>
<li class="toctree-l2"><a class="reference internal" href="locktorture.html">Kernel Lock Torture Test Operation</a></li>
<li class="toctree-l2"><a class="reference internal" href="mutex-design.html">Generic Mutex Subsystem</a></li>
<li class="toctree-l2"><a class="reference internal" href="rt-mutex-design.html">RT-mutex implementation design</a></li>
<li class="toctree-l2"><a class="reference internal" href="rt-mutex.html">RT-mutex subsystem with PI support</a></li>
<li class="toctree-l2"><a class="reference internal" href="seqlock.html">Sequence counters and sequential locks</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Locking lessons</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#lesson-1-spin-locks">Lesson 1: Spin locks</a></li>
<li class="toctree-l3"><a class="reference internal" href="#lesson-2-reader-writer-spinlocks">Lesson 2: reader-writer spinlocks.</a></li>
<li class="toctree-l3"><a class="reference internal" href="#lesson-3-spinlocks-revisited">Lesson 3: spinlocks revisited.</a></li>
<li class="toctree-l3"><a class="reference internal" href="#reference-information">Reference information:</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="ww-mutex-design.html">Wound/Wait Deadlock-Proof Mutex Design</a></li>
<li class="toctree-l2"><a class="reference internal" href="preempt-locking.html">Proper Locking Under a Preemptible Kernel: Keeping Kernel Code Preempt-Safe</a></li>
<li class="toctree-l2"><a class="reference internal" href="pi-futex.html">Lightweight PI-futexes</a></li>
<li class="toctree-l2"><a class="reference internal" href="futex-requeue-pi.html">Futex Requeue PI</a></li>
<li class="toctree-l2"><a class="reference internal" href="hwspinlock.html">Hardware Spinlock Framework</a></li>
<li class="toctree-l2"><a class="reference internal" href="percpu-rw-semaphore.html">Percpu rw semaphores</a></li>
<li class="toctree-l2"><a class="reference internal" href="robust-futexes.html">A description of what robust futexes are</a></li>
<li class="toctree-l2"><a class="reference internal" href="robust-futex-ABI.html">The robust futex ABI</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../process/license-rules.html">Licensing rules</a></li>
<li class="toctree-l1"><a class="reference internal" href="../doc-guide/index.html">Writing documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dev-tools/index.html">Development tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dev-tools/testing-overview.html">Testing guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../kernel-hacking/index.html">Hacking guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../trace/index.html">Tracing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../fault-injection/index.html">Fault injection</a></li>
<li class="toctree-l1"><a class="reference internal" href="../livepatch/index.html">Livepatching</a></li>
<li class="toctree-l1"><a class="reference internal" href="../rust/index.html">Rust</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../admin-guide/index.html">Administration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../kbuild/index.html">Build system</a></li>
<li class="toctree-l1"><a class="reference internal" href="../admin-guide/reporting-issues.html">Reporting issues</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html">Userspace tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../userspace-api/index.html">Userspace API</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../firmware-guide/index.html">Firmware</a></li>
<li class="toctree-l1"><a class="reference internal" href="../devicetree/index.html">Firmware and Devicetree</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../arch/index.html">CPU architectures</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../staging/index.html">Unsorted documentation</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../translations/index.html">Translations</a></li>
</ul>

</div>

<script type="text/javascript"> <!--
  var sbar = document.getElementsByClassName("sphinxsidebar")[0];
  let currents = document.getElementsByClassName("current")
  if (currents.length) {
    sbar.scrollTop = currents[currents.length - 1].offsetTop;
  }
  --> </script>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/locking/spinlocks.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
        </div>
      </div>
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  




<div class="language-selection">
English

<ul>

<li><a href="../translations/zh_CN/locking/spinlocks.html">Chinese (Simplified)</a></li>

</ul>
</div>
<section id="locking-lessons">
<h1>Locking lessons<a class="headerlink" href="#locking-lessons" title="Link to this heading">¶</a></h1>
<section id="lesson-1-spin-locks">
<h2>Lesson 1: Spin locks<a class="headerlink" href="#lesson-1-spin-locks" title="Link to this heading">¶</a></h2>
<p>The most basic primitive for locking is spinlock:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>static DEFINE_SPINLOCK(xxx_lock);

      unsigned long flags;

      spin_lock_irqsave(&amp;xxx_lock, flags);
      ... critical section here ..
      spin_unlock_irqrestore(&amp;xxx_lock, flags);
</pre></div>
</div>
<p>The above is always safe. It will disable interrupts _locally_, but the
spinlock itself will guarantee the global lock, so it will guarantee that
there is only one thread-of-control within the region(s) protected by that
lock. This works well even under UP also, so the code does _not_ need to
worry about UP vs SMP issues: the spinlocks work correctly under both.</p>
<blockquote>
<div><p>NOTE! Implications of spin_locks for memory are further described in:</p>
<blockquote>
<div><p>Documentation/memory-barriers.txt</p>
<blockquote>
<div><ol class="arabic simple" start="5">
<li><p>ACQUIRE operations.</p></li>
<li><p>RELEASE operations.</p></li>
</ol>
</div></blockquote>
</div></blockquote>
</div></blockquote>
<p>The above is usually pretty simple (you usually need and want only one
spinlock for most things - using more than one spinlock can make things a
lot more complex and even slower and is usually worth it only for
sequences that you <strong>know</strong> need to be split up: avoid it at all cost if you
aren’t sure).</p>
<p>This is really the only really hard part about spinlocks: once you start
using spinlocks they tend to expand to areas you might not have noticed
before, because you have to make sure the spinlocks correctly protect the
shared data structures <strong>everywhere</strong> they are used. The spinlocks are most
easily added to places that are completely independent of other code (for
example, internal driver data structures that nobody else ever touches).</p>
<blockquote>
<div><p>NOTE! The spin-lock is safe only when you <strong>also</strong> use the lock itself
to do locking across CPU’s, which implies that EVERYTHING that
touches a shared variable has to agree about the spinlock they want
to use.</p>
</div></blockquote>
</section>
<hr class="docutils" />
<section id="lesson-2-reader-writer-spinlocks">
<h2>Lesson 2: reader-writer spinlocks.<a class="headerlink" href="#lesson-2-reader-writer-spinlocks" title="Link to this heading">¶</a></h2>
<p>If your data accesses have a very natural pattern where you usually tend
to mostly read from the shared variables, the reader-writer locks
(rw_lock) versions of the spinlocks are sometimes useful. They allow multiple
readers to be in the same critical region at once, but if somebody wants
to change the variables it has to get an exclusive write lock.</p>
<blockquote>
<div><p>NOTE! reader-writer locks require more atomic memory operations than
simple spinlocks.  Unless the reader critical section is long, you
are better off just using spinlocks.</p>
</div></blockquote>
<p>The routines look the same as above:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>rwlock_t xxx_lock = __RW_LOCK_UNLOCKED(xxx_lock);

     unsigned long flags;

     read_lock_irqsave(&amp;xxx_lock, flags);
     .. critical section that only reads the info ...
     read_unlock_irqrestore(&amp;xxx_lock, flags);

     write_lock_irqsave(&amp;xxx_lock, flags);
     .. read and write exclusive access to the info ...
     write_unlock_irqrestore(&amp;xxx_lock, flags);
</pre></div>
</div>
<p>The above kind of lock may be useful for complex data structures like
linked lists, especially searching for entries without changing the list
itself.  The read lock allows many concurrent readers.  Anything that
<strong>changes</strong> the list will have to get the write lock.</p>
<blockquote>
<div><p>NOTE! RCU is better for list traversal, but requires careful
attention to design detail (see <a class="reference internal" href="../RCU/listRCU.html"><span class="doc">Using RCU to Protect Read-Mostly Linked Lists</span></a>).</p>
</div></blockquote>
<p>Also, you cannot “upgrade” a read-lock to a write-lock, so if you at _any_
time need to do any changes (even if you don’t do it every time), you have
to get the write-lock at the very beginning.</p>
<blockquote>
<div><p>NOTE! We are working hard to remove reader-writer spinlocks in most
cases, so please don’t add a new one without consensus.  (Instead, see
<a class="reference internal" href="../RCU/rcu.html"><span class="doc">RCU Concepts</span></a> for complete information.)</p>
</div></blockquote>
</section>
<hr class="docutils" />
<section id="lesson-3-spinlocks-revisited">
<h2>Lesson 3: spinlocks revisited.<a class="headerlink" href="#lesson-3-spinlocks-revisited" title="Link to this heading">¶</a></h2>
<p>The single spin-lock primitives above are by no means the only ones. They
are the most safe ones, and the ones that work under all circumstances,
but partly <strong>because</strong> they are safe they are also fairly slow. They are slower
than they’d need to be, because they do have to disable interrupts
(which is just a single instruction on a x86, but it’s an expensive one -
and on other architectures it can be worse).</p>
<p>If you have a case where you have to protect a data structure across
several CPU’s and you want to use spinlocks you can potentially use
cheaper versions of the spinlocks. IFF you know that the spinlocks are
never used in interrupt handlers, you can use the non-irq versions:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>spin_lock(&amp;lock);
...
spin_unlock(&amp;lock);
</pre></div>
</div>
<p>(and the equivalent read-write versions too, of course). The spinlock will
guarantee the same kind of exclusive access, and it will be much faster.
This is useful if you know that the data in question is only ever
manipulated from a “process context”, ie no interrupts involved.</p>
<p>The reasons you mustn’t use these versions if you have interrupts that
play with the spinlock is that you can get deadlocks:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>spin_lock(&amp;lock);
...
        &lt;- interrupt comes in:
                spin_lock(&amp;lock);
</pre></div>
</div>
<p>where an interrupt tries to lock an already locked variable. This is ok if
the other interrupt happens on another CPU, but it is _not_ ok if the
interrupt happens on the same CPU that already holds the lock, because the
lock will obviously never be released (because the interrupt is waiting
for the lock, and the lock-holder is interrupted by the interrupt and will
not continue until the interrupt has been processed).</p>
<p>(This is also the reason why the irq-versions of the spinlocks only need
to disable the _local_ interrupts - it’s ok to use spinlocks in interrupts
on other CPU’s, because an interrupt on another CPU doesn’t interrupt the
CPU that holds the lock, so the lock-holder can continue and eventually
releases the lock).</p>
<blockquote>
<div><p>Linus</p>
</div></blockquote>
</section>
<hr class="docutils" />
<section id="reference-information">
<h2>Reference information:<a class="headerlink" href="#reference-information" title="Link to this heading">¶</a></h2>
<p>For dynamic initialization, use <code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">spin_lock_init()</span></code> or <code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">rwlock_init()</span></code> as
appropriate:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>spinlock_t xxx_lock;
rwlock_t xxx_rw_lock;

static int __init xxx_init(void)
{
     spin_lock_init(&amp;xxx_lock);
     rwlock_init(&amp;xxx_rw_lock);
     ...
}

module_init(xxx_init);
</pre></div>
</div>
<p>For static initialization, use <code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">DEFINE_SPINLOCK()</span></code> / <code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">DEFINE_RWLOCK()</span></code> or
<code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">__SPIN_LOCK_UNLOCKED()</span></code> / <code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">__RW_LOCK_UNLOCKED()</span></code> as appropriate.</p>
</section>
</section>


          </div>
          
        </div>
      </div>
    <div class="clearer"></div>
  </div>
    <div class="footer">
      &#169;The kernel development community.
      
      |
      Powered by <a href="https://www.sphinx-doc.org/">Sphinx 8.1.3</a>
      &amp; <a href="https://alabaster.readthedocs.io">Alabaster 0.7.16</a>
      
      |
      <a href="../_sources/locking/spinlocks.rst.txt"
          rel="nofollow">Page source</a>
    </div>

    

    
  </body>
</html>

Filemanager

Name Type Size Permission Actions
futex-requeue-pi.html File 18.17 KB 0644
hwspinlock.html File 29.59 KB 0644
index.html File 12.21 KB 0644
lockdep-design.html File 47.67 KB 0644
lockstat.html File 24.48 KB 0644
locktorture.html File 20.03 KB 0644
locktypes.html File 38.53 KB 0644
mutex-design.html File 21.5 KB 0644
percpu-rw-semaphore.html File 11.76 KB 0644
pi-futex.html File 16.79 KB 0644
preempt-locking.html File 18.06 KB 0644
robust-futex-ABI.html File 20 KB 0644
robust-futexes.html File 23.14 KB 0644
rt-mutex-design.html File 38.87 KB 0644
rt-mutex.html File 14.82 KB 0644
seqlock.html File 98.28 KB 0644
spinlocks.html File 19.01 KB 0644
ww-mutex-design.html File 28.72 KB 0644
Filemanager