Commit 6501bf87 authored by Ahmed S. Darwish's avatar Ahmed S. Darwish Committed by David S. Miller

u64_stats: Document writer non-preemptibility requirement

The u64_stats mechanism uses sequence counters to protect against 64-bit
values tearing on 32-bit architectures. Updating such statistics is a
sequence counter write side critical section.

Preemption must be disabled before entering this seqcount write critical
section.  Failing to do so, the seqcount read side can preempt the write
side section and spin for the entire scheduler tick.  If that reader
belongs to a real-time scheduling class, it can spin forever and the
kernel will livelock.

Document this statistics update side non-preemptibility requirement.

Reword the introductory paragraph to highlight u64_stats raison d'être:
64-bit values tearing protection on 32-bit architectures. Divide
documentation on a basis of internal design vs. usage constraints.

Reword the u64_stats header file top comment to always mention "Reader"
or "Writer" at the start of each bullet point, making it easier to
follow which side each point is actually for.

Clarify the statement "whole thing is a NOOP on 64bit arches or UP
kernels".  For 32-bit UP kernels, preemption is always disabled for the
statistics read side section.
Signed-off-by: default avatarAhmed S. Darwish <a.darwish@linutronix.de>
Reviewed-by: default avatarSebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 79cbb6bc
...@@ -3,33 +3,36 @@ ...@@ -3,33 +3,36 @@
#define _LINUX_U64_STATS_SYNC_H #define _LINUX_U64_STATS_SYNC_H
/* /*
* To properly implement 64bits network statistics on 32bit and 64bit hosts, * Protect against 64-bit values tearing on 32-bit architectures. This is
* we provide a synchronization point, that is a noop on 64bit or UP kernels. * typically used for statistics read/update in different subsystems.
* *
* Key points : * Key points :
* 1) Use a seqcount on SMP 32bits, with low overhead. *
* 2) Whole thing is a noop on 64bit arches or UP kernels. * - Use a seqcount on 32-bit SMP, only disable preemption for 32-bit UP.
* 3) Write side must ensure mutual exclusion or one seqcount update could * - The whole thing is a no-op on 64-bit architectures.
*
* Usage constraints:
*
* 1) Write side must ensure mutual exclusion, or one seqcount update could
* be lost, thus blocking readers forever. * be lost, thus blocking readers forever.
* If this synchronization point is not a mutex, but a spinlock or
* spinlock_bh() or disable_bh() :
* 3.1) Write side should not sleep.
* 3.2) Write side should not allow preemption.
* 3.3) If applicable, interrupts should be disabled.
* *
* 4) If reader fetches several counters, there is no guarantee the whole values * 2) Write side must disable preemption, or a seqcount reader can preempt the
* are consistent (remember point 1) : this is a noop on 64bit arches anyway) * writer and also spin forever.
*
* 3) Write side must use the _irqsave() variant if other writers, or a reader,
* can be invoked from an IRQ context.
* *
* 5) readers are allowed to sleep or be preempted/interrupted : They perform * 4) If reader fetches several counters, there is no guarantee the whole values
* pure reads. But if they have to fetch many values, it's better to not allow * are consistent w.r.t. each other (remember point #2: seqcounts are not
* preemptions/interruptions to avoid many retries. * used for 64bit architectures).
* *
* 6) If counter might be written by an interrupt, readers should block interrupts. * 5) Readers are allowed to sleep or be preempted/interrupted: they perform
* (On UP, there is no seqcount_t protection, a reader allowing interrupts could * pure reads.
* read partial values)
* *
* 7) For irq and softirq uses, readers can use u64_stats_fetch_begin_irq() and * 6) Readers must use both u64_stats_fetch_{begin,retry}_irq() if the stats
* u64_stats_fetch_retry_irq() helpers * might be updated from a hardirq or softirq context (remember point #1:
* seqcounts are not used for UP kernels). 32-bit UP stat readers could read
* corrupted 64-bit values otherwise.
* *
* Usage : * Usage :
* *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment