Skip to content

Commit 316580b

Browse files
edumazetdavem330
authored andcommitted
u64_stats: provide u64_stats_t type
On 64bit arches, struct u64_stats_sync is empty and provides no help against load/store tearing. Using READ_ONCE()/WRITE_ONCE() would be needed. But the update side would be slightly more expensive. local64_t was defined so that we could use regular adds in a manner which is atomic wrt IRQs. However the u64_stats infra means we do not have to use local64_t on 32bit arches since the syncp provides the needed protection. Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 4a43b1f commit 316580b

1 file changed

Lines changed: 47 additions & 4 deletions

File tree

‎include/linux/u64_stats_sync.h‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
* spin_lock_bh(...) or other synchronization to get exclusive access
4141
* ...
4242
* u64_stats_update_begin(&stats->syncp);
43-
* stats->bytes64 += len; // non atomic operation
44-
* stats->packets64++; // non atomic operation
43+
* u64_stats_add(&stats->bytes64, len); // non atomic operation
44+
* u64_stats_inc(&stats->packets64); // non atomic operation
4545
* u64_stats_update_end(&stats->syncp);
4646
*
4747
* While a consumer (reader) should use following template to get consistent
@@ -52,8 +52,8 @@
5252
*
5353
* do {
5454
* start = u64_stats_fetch_begin(&stats->syncp);
55-
* tbytes = stats->bytes64; // non atomic operation
56-
* tpackets = stats->packets64; // non atomic operation
55+
* tbytes = u64_stats_read(&stats->bytes64); // non atomic operation
56+
* tpackets = u64_stats_read(&stats->packets64); // non atomic operation
5757
* } while (u64_stats_fetch_retry(&stats->syncp, start));
5858
*
5959
*
@@ -68,6 +68,49 @@ struct u64_stats_sync {
6868
#endif
6969
};
7070

71+
#if BITS_PER_LONG == 64
72+
#include <asm/local64.h>
73+
74+
typedef struct {
75+
local64_t v;
76+
} u64_stats_t ;
77+
78+
static inline u64 u64_stats_read(const u64_stats_t *p)
79+
{
80+
return local64_read(&p->v);
81+
}
82+
83+
static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
84+
{
85+
local64_add(val, &p->v);
86+
}
87+
88+
static inline void u64_stats_inc(u64_stats_t *p)
89+
{
90+
local64_inc(&p->v);
91+
}
92+
93+
#else
94+
95+
typedef struct {
96+
u64 v;
97+
} u64_stats_t;
98+
99+
static inline u64 u64_stats_read(const u64_stats_t *p)
100+
{
101+
return p->v;
102+
}
103+
104+
static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
105+
{
106+
p->v += val;
107+
}
108+
109+
static inline void u64_stats_inc(u64_stats_t *p)
110+
{
111+
p->v++;
112+
}
113+
#endif
71114

72115
static inline void u64_stats_init(struct u64_stats_sync *syncp)
73116
{

0 commit comments

Comments
 (0)