![Eric Dumazet](/assets/img/avatar_default.png)
Marek reported a LOCKDEP issue occurring on 32bit host,
that we tracked down to the fact that usbnet could either
run from soft or hard irqs.
This patch adds u64_stats_update_begin_irqsave() and
u64_stats_update_end_irqrestore() helpers to solve this case.
[ 17.768040] ================================
[ 17.772239] WARNING: inconsistent lock state
[ 17.776511] 4.16.0-rc3-next-20180227-00007-g876c53a7493c #453 Not tainted
[ 17.783329] --------------------------------
[ 17.787580] inconsistent {IN-HARDIRQ-W} -> {HARDIRQ-ON-W} usage.
[ 17.793607] swapper/0/0 [HC0[0]:SC1[1]:HE1:SE0] takes:
[ 17.798751] (&syncp->seq#5){?.-.}, at: [<9b22e5f0>]
asix_rx_fixup_internal+0x188/0x288
[ 17.806790] {IN-HARDIRQ-W} state was registered at:
[ 17.811677] tx_complete+0x100/0x208
[ 17.815319] __usb_hcd_giveback_urb+0x60/0xf0
[ 17.819770] xhci_giveback_urb_in_irq+0xa8/0x240
[ 17.824469] xhci_td_cleanup+0xf4/0x16c
[ 17.828367] xhci_irq+0xe74/0x2240
[ 17.831827] usb_hcd_irq+0x24/0x38
[ 17.835343] __handle_irq_event_percpu+0x98/0x510
[ 17.840111] handle_irq_event_percpu+0x1c/0x58
[ 17.844623] handle_irq_event+0x38/0x5c
[ 17.848519] handle_fasteoi_irq+0xa4/0x138
[ 17.852681] generic_handle_irq+0x18/0x28
[ 17.856760] __handle_domain_irq+0x6c/0xe4
[ 17.860941] gic_handle_irq+0x54/0xa0
[ 17.864666] __irq_svc+0x70/0xb0
[ 17.867964] arch_cpu_idle+0x20/0x3c
[ 17.871578] arch_cpu_idle+0x20/0x3c
[ 17.875190] do_idle+0x144/0x218
[ 17.878468] cpu_startup_entry+0x18/0x1c
[ 17.882454] start_kernel+0x394/0x400
[ 17.886177] irq event stamp: 161912
[ 17.889616] hardirqs last enabled at (161912): [<7bedfacf>]
__netdev_alloc_skb+0xcc/0x140
[ 17.897893] hardirqs last disabled at (161911): [<d58261d0>]
__netdev_alloc_skb+0x94/0x140
[ 17.904903] exynos5-hsi2c 12ca0000.i2c: tx timeout
[ 17.906116] softirqs last enabled at (161904): [<387102ff>]
irq_enter+0x78/0x80
[ 17.906123] softirqs last disabled at (161905): [<cf4c628e>]
irq_exit+0x134/0x158
[ 17.925722].
[ 17.925722] other info that might help us debug this:
[ 17.933435] Possible unsafe locking scenario:
[ 17.933435].
[ 17.940331] CPU0
[ 17.942488] ----
[ 17.944894] lock(&syncp->seq#5);
[ 17.948274] <Interrupt>
[ 17.950847] lock(&syncp->seq#5);
[ 17.954386].
[ 17.954386] *** DEADLOCK ***
[ 17.954386].
[ 17.962422] no locks held by swapper/0/0.
Fixes: c8b5d129ee
("net: usbnet: support 64bit stats")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Greg Ungerer <gerg@linux-m68k.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
189 lines
5.4 KiB
C
189 lines
5.4 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef _LINUX_U64_STATS_SYNC_H
|
|
#define _LINUX_U64_STATS_SYNC_H
|
|
|
|
/*
|
|
* To properly implement 64bits network statistics on 32bit and 64bit hosts,
|
|
* we provide a synchronization point, that is a noop on 64bit or UP kernels.
|
|
*
|
|
* Key points :
|
|
* 1) Use a seqcount on SMP 32bits, with low overhead.
|
|
* 2) Whole thing is a noop on 64bit arches or UP kernels.
|
|
* 3) Write side must ensure mutual exclusion or one seqcount update could
|
|
* 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
|
|
* are consistent (remember point 1) : this is a noop on 64bit arches anyway)
|
|
*
|
|
* 5) readers are allowed to sleep or be preempted/interrupted : They perform
|
|
* pure reads. But if they have to fetch many values, it's better to not allow
|
|
* preemptions/interruptions to avoid many retries.
|
|
*
|
|
* 6) If counter might be written by an interrupt, readers should block interrupts.
|
|
* (On UP, there is no seqcount_t protection, a reader allowing interrupts could
|
|
* read partial values)
|
|
*
|
|
* 7) For irq and softirq uses, readers can use u64_stats_fetch_begin_irq() and
|
|
* u64_stats_fetch_retry_irq() helpers
|
|
*
|
|
* Usage :
|
|
*
|
|
* Stats producer (writer) should use following template granted it already got
|
|
* an exclusive access to counters (a lock is already taken, or per cpu
|
|
* data is used [in a non preemptable context])
|
|
*
|
|
* spin_lock_bh(...) or other synchronization to get exclusive access
|
|
* ...
|
|
* u64_stats_update_begin(&stats->syncp);
|
|
* stats->bytes64 += len; // non atomic operation
|
|
* stats->packets64++; // non atomic operation
|
|
* u64_stats_update_end(&stats->syncp);
|
|
*
|
|
* While a consumer (reader) should use following template to get consistent
|
|
* snapshot for each variable (but no guarantee on several ones)
|
|
*
|
|
* u64 tbytes, tpackets;
|
|
* unsigned int start;
|
|
*
|
|
* do {
|
|
* start = u64_stats_fetch_begin(&stats->syncp);
|
|
* tbytes = stats->bytes64; // non atomic operation
|
|
* tpackets = stats->packets64; // non atomic operation
|
|
* } while (u64_stats_fetch_retry(&stats->syncp, start));
|
|
*
|
|
*
|
|
* Example of use in drivers/net/loopback.c, using per_cpu containers,
|
|
* in BH disabled context.
|
|
*/
|
|
#include <linux/seqlock.h>
|
|
|
|
struct u64_stats_sync {
|
|
#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
|
|
seqcount_t seq;
|
|
#endif
|
|
};
|
|
|
|
|
|
static inline void u64_stats_init(struct u64_stats_sync *syncp)
|
|
{
|
|
#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
|
|
seqcount_init(&syncp->seq);
|
|
#endif
|
|
}
|
|
|
|
static inline void u64_stats_update_begin(struct u64_stats_sync *syncp)
|
|
{
|
|
#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
|
|
write_seqcount_begin(&syncp->seq);
|
|
#endif
|
|
}
|
|
|
|
static inline void u64_stats_update_end(struct u64_stats_sync *syncp)
|
|
{
|
|
#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
|
|
write_seqcount_end(&syncp->seq);
|
|
#endif
|
|
}
|
|
|
|
static inline unsigned long
|
|
u64_stats_update_begin_irqsave(struct u64_stats_sync *syncp)
|
|
{
|
|
unsigned long flags = 0;
|
|
|
|
#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
|
|
local_irq_save(flags);
|
|
write_seqcount_begin(&syncp->seq);
|
|
#endif
|
|
return flags;
|
|
}
|
|
|
|
static inline void
|
|
u64_stats_update_end_irqrestore(struct u64_stats_sync *syncp,
|
|
unsigned long flags)
|
|
{
|
|
#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
|
|
write_seqcount_end(&syncp->seq);
|
|
local_irq_restore(flags);
|
|
#endif
|
|
}
|
|
|
|
static inline void u64_stats_update_begin_raw(struct u64_stats_sync *syncp)
|
|
{
|
|
#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
|
|
raw_write_seqcount_begin(&syncp->seq);
|
|
#endif
|
|
}
|
|
|
|
static inline void u64_stats_update_end_raw(struct u64_stats_sync *syncp)
|
|
{
|
|
#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
|
|
raw_write_seqcount_end(&syncp->seq);
|
|
#endif
|
|
}
|
|
|
|
static inline unsigned int __u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
|
|
{
|
|
#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
|
|
return read_seqcount_begin(&syncp->seq);
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
static inline unsigned int u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
|
|
{
|
|
#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
|
|
preempt_disable();
|
|
#endif
|
|
return __u64_stats_fetch_begin(syncp);
|
|
}
|
|
|
|
static inline bool __u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
|
|
unsigned int start)
|
|
{
|
|
#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
|
|
return read_seqcount_retry(&syncp->seq, start);
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
static inline bool u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
|
|
unsigned int start)
|
|
{
|
|
#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
|
|
preempt_enable();
|
|
#endif
|
|
return __u64_stats_fetch_retry(syncp, start);
|
|
}
|
|
|
|
/*
|
|
* In case irq handlers can update u64 counters, readers can use following helpers
|
|
* - SMP 32bit arches use seqcount protection, irq safe.
|
|
* - UP 32bit must disable irqs.
|
|
* - 64bit have no problem atomically reading u64 values, irq safe.
|
|
*/
|
|
static inline unsigned int u64_stats_fetch_begin_irq(const struct u64_stats_sync *syncp)
|
|
{
|
|
#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
|
|
local_irq_disable();
|
|
#endif
|
|
return __u64_stats_fetch_begin(syncp);
|
|
}
|
|
|
|
static inline bool u64_stats_fetch_retry_irq(const struct u64_stats_sync *syncp,
|
|
unsigned int start)
|
|
{
|
|
#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
|
|
local_irq_enable();
|
|
#endif
|
|
return __u64_stats_fetch_retry(syncp, start);
|
|
}
|
|
|
|
#endif /* _LINUX_U64_STATS_SYNC_H */
|