Skip to content

Commit 546be24

Browse files
herbertxDavid S. Miller
authored andcommitted
[IPSEC] xfrm: Undo afinfo lock proliferation
The number of locks used to manage afinfo structures can easily be reduced down to one each for policy and state respectively. This is based on the observation that the write locks are only held by module insertion/removal which are very rare events so there is no need to further differentiate between the insertion of modules like ipv6 versus esp6. The removal of the read locks in xfrm4_policy.c/xfrm6_policy.c might look suspicious at first. However, after you realise that nobody ever takes the corresponding write lock you'll feel better :) As far as I can gather it's an attempt to guard against the removal of the corresponding modules. Since neither module can be unloaded at all we can leave it to whoever fixes up IPv6 unloading :) Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 9cb3528 commit 546be24

7 files changed

Lines changed: 38 additions & 52 deletions

File tree

‎include/net/xfrm.h‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ struct xfrm_type;
204204
struct xfrm_dst;
205205
struct xfrm_policy_afinfo {
206206
unsigned short family;
207-
rwlock_t lock;
208-
struct xfrm_type_map *type_map;
207+
struct xfrm_type *type_map[256];
209208
struct dst_ops *dst_ops;
210209
void (*garbage_collect)(void);
211210
int (*dst_lookup)(struct xfrm_dst **dst, struct flowi *fl);
@@ -232,7 +231,6 @@ extern int __xfrm_state_delete(struct xfrm_state *x);
232231

233232
struct xfrm_state_afinfo {
234233
unsigned short family;
235-
rwlock_t lock;
236234
struct list_head *state_bydst;
237235
struct list_head *state_byspi;
238236
int (*init_flags)(struct xfrm_state *x);
@@ -264,11 +262,6 @@ struct xfrm_type
264262
u32 (*get_max_size)(struct xfrm_state *, int size);
265263
};
266264

267-
struct xfrm_type_map {
268-
rwlock_t lock;
269-
struct xfrm_type *map[256];
270-
};
271-
272265
extern int xfrm_register_type(struct xfrm_type *type, unsigned short family);
273266
extern int xfrm_unregister_type(struct xfrm_type *type, unsigned short family);
274267
extern struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family);

‎net/ipv4/xfrm4_policy.c‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
static struct dst_ops xfrm4_dst_ops;
1818
static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
1919

20-
static struct xfrm_type_map xfrm4_type_map = { .lock = RW_LOCK_UNLOCKED };
21-
2220
static int xfrm4_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
2321
{
2422
return __ip_route_output_key((struct rtable**)dst, fl);
@@ -237,9 +235,7 @@ _decode_session4(struct sk_buff *skb, struct flowi *fl)
237235

238236
static inline int xfrm4_garbage_collect(void)
239237
{
240-
read_lock(&xfrm4_policy_afinfo.lock);
241238
xfrm4_policy_afinfo.garbage_collect();
242-
read_unlock(&xfrm4_policy_afinfo.lock);
243239
return (atomic_read(&xfrm4_dst_ops.entries) > xfrm4_dst_ops.gc_thresh*2);
244240
}
245241

@@ -299,8 +295,6 @@ static struct dst_ops xfrm4_dst_ops = {
299295

300296
static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
301297
.family = AF_INET,
302-
.lock = RW_LOCK_UNLOCKED,
303-
.type_map = &xfrm4_type_map,
304298
.dst_ops = &xfrm4_dst_ops,
305299
.dst_lookup = xfrm4_dst_lookup,
306300
.find_bundle = __xfrm4_find_bundle,

‎net/ipv4/xfrm4_state.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ __xfrm4_find_acq(u8 mode, u32 reqid, u8 proto,
131131

132132
static struct xfrm_state_afinfo xfrm4_state_afinfo = {
133133
.family = AF_INET,
134-
.lock = RW_LOCK_UNLOCKED,
135134
.init_flags = xfrm4_init_flags,
136135
.init_tempsel = __xfrm4_init_tempsel,
137136
.state_lookup = __xfrm4_state_lookup,

‎net/ipv6/xfrm6_policy.c‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
static struct dst_ops xfrm6_dst_ops;
2424
static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
2525

26-
static struct xfrm_type_map xfrm6_type_map = { .lock = RW_LOCK_UNLOCKED };
27-
2826
static int xfrm6_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
2927
{
3028
int err = 0;
@@ -249,9 +247,7 @@ _decode_session6(struct sk_buff *skb, struct flowi *fl)
249247

250248
static inline int xfrm6_garbage_collect(void)
251249
{
252-
read_lock(&xfrm6_policy_afinfo.lock);
253250
xfrm6_policy_afinfo.garbage_collect();
254-
read_unlock(&xfrm6_policy_afinfo.lock);
255251
return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
256252
}
257253

@@ -311,8 +307,6 @@ static struct dst_ops xfrm6_dst_ops = {
311307

312308
static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
313309
.family = AF_INET6,
314-
.lock = RW_LOCK_UNLOCKED,
315-
.type_map = &xfrm6_type_map,
316310
.dst_ops = &xfrm6_dst_ops,
317311
.dst_lookup = xfrm6_dst_lookup,
318312
.find_bundle = __xfrm6_find_bundle,

‎net/ipv6/xfrm6_state.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ __xfrm6_find_acq(u8 mode, u32 reqid, u8 proto,
135135

136136
static struct xfrm_state_afinfo xfrm6_state_afinfo = {
137137
.family = AF_INET6,
138-
.lock = RW_LOCK_UNLOCKED,
139138
.init_tempsel = __xfrm6_init_tempsel,
140139
.state_lookup = __xfrm6_state_lookup,
141140
.find_acq = __xfrm6_find_acq,

‎net/xfrm/xfrm_policy.c‎

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,53 +46,51 @@ static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
4646

4747
static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
4848
static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
49+
static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
50+
static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
4951

5052
int xfrm_register_type(struct xfrm_type *type, unsigned short family)
5153
{
52-
struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
53-
struct xfrm_type_map *typemap;
54+
struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
55+
struct xfrm_type **typemap;
5456
int err = 0;
5557

5658
if (unlikely(afinfo == NULL))
5759
return -EAFNOSUPPORT;
5860
typemap = afinfo->type_map;
5961

60-
write_lock_bh(&typemap->lock);
61-
if (likely(typemap->map[type->proto] == NULL))
62-
typemap->map[type->proto] = type;
62+
if (likely(typemap[type->proto] == NULL))
63+
typemap[type->proto] = type;
6364
else
6465
err = -EEXIST;
65-
write_unlock_bh(&typemap->lock);
66-
xfrm_policy_put_afinfo(afinfo);
66+
xfrm_policy_unlock_afinfo(afinfo);
6767
return err;
6868
}
6969
EXPORT_SYMBOL(xfrm_register_type);
7070

7171
int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
7272
{
73-
struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
74-
struct xfrm_type_map *typemap;
73+
struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
74+
struct xfrm_type **typemap;
7575
int err = 0;
7676

7777
if (unlikely(afinfo == NULL))
7878
return -EAFNOSUPPORT;
7979
typemap = afinfo->type_map;
8080

81-
write_lock_bh(&typemap->lock);
82-
if (unlikely(typemap->map[type->proto] != type))
81+
if (unlikely(typemap[type->proto] != type))
8382
err = -ENOENT;
8483
else
85-
typemap->map[type->proto] = NULL;
86-
write_unlock_bh(&typemap->lock);
87-
xfrm_policy_put_afinfo(afinfo);
84+
typemap[type->proto] = NULL;
85+
xfrm_policy_unlock_afinfo(afinfo);
8886
return err;
8987
}
9088
EXPORT_SYMBOL(xfrm_unregister_type);
9189

9290
struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
9391
{
9492
struct xfrm_policy_afinfo *afinfo;
95-
struct xfrm_type_map *typemap;
93+
struct xfrm_type **typemap;
9694
struct xfrm_type *type;
9795
int modload_attempted = 0;
9896

@@ -102,11 +100,9 @@ struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
102100
return NULL;
103101
typemap = afinfo->type_map;
104102

105-
read_lock(&typemap->lock);
106-
type = typemap->map[proto];
103+
type = typemap[proto];
107104
if (unlikely(type && !try_module_get(type->owner)))
108105
type = NULL;
109-
read_unlock(&typemap->lock);
110106
if (!type && !modload_attempted) {
111107
xfrm_policy_put_afinfo(afinfo);
112108
request_module("xfrm-type-%d-%d",
@@ -1306,17 +1302,31 @@ static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
13061302
return NULL;
13071303
read_lock(&xfrm_policy_afinfo_lock);
13081304
afinfo = xfrm_policy_afinfo[family];
1309-
if (likely(afinfo != NULL))
1310-
read_lock(&afinfo->lock);
1311-
read_unlock(&xfrm_policy_afinfo_lock);
1305+
if (unlikely(!afinfo))
1306+
read_unlock(&xfrm_policy_afinfo_lock);
13121307
return afinfo;
13131308
}
13141309

13151310
static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
13161311
{
1317-
if (unlikely(afinfo == NULL))
1318-
return;
1319-
read_unlock(&afinfo->lock);
1312+
read_unlock(&xfrm_policy_afinfo_lock);
1313+
}
1314+
1315+
static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
1316+
{
1317+
struct xfrm_policy_afinfo *afinfo;
1318+
if (unlikely(family >= NPROTO))
1319+
return NULL;
1320+
write_lock_bh(&xfrm_policy_afinfo_lock);
1321+
afinfo = xfrm_policy_afinfo[family];
1322+
if (unlikely(!afinfo))
1323+
write_unlock_bh(&xfrm_policy_afinfo_lock);
1324+
return afinfo;
1325+
}
1326+
1327+
static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
1328+
{
1329+
write_unlock_bh(&xfrm_policy_afinfo_lock);
13201330
}
13211331

13221332
static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)

‎net/xfrm/xfrm_state.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,17 +1103,14 @@ static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned short family)
11031103
return NULL;
11041104
read_lock(&xfrm_state_afinfo_lock);
11051105
afinfo = xfrm_state_afinfo[family];
1106-
if (likely(afinfo != NULL))
1107-
read_lock(&afinfo->lock);
1108-
read_unlock(&xfrm_state_afinfo_lock);
1106+
if (unlikely(!afinfo))
1107+
read_unlock(&xfrm_state_afinfo_lock);
11091108
return afinfo;
11101109
}
11111110

11121111
static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
11131112
{
1114-
if (unlikely(afinfo == NULL))
1115-
return;
1116-
read_unlock(&afinfo->lock);
1113+
read_unlock(&xfrm_state_afinfo_lock);
11171114
}
11181115

11191116
/* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */

0 commit comments

Comments
 (0)