Skip to content

Commit 2f9f28b

Browse files
Jozsef Kadlecsikkaber
authored andcommitted
netfilter: ipset: references are protected by rwlock instead of mutex
The timeout variant of the list:set type must reference the member sets. However, its garbage collector runs at timer interrupt so the mutex protection of the references is a no go. Therefore the reference protection is converted to rwlock. Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> Signed-off-by: Patrick McHardy <kaber@trash.net>
1 parent 512d06b commit 2f9f28b

7 files changed

Lines changed: 73 additions & 56 deletions

File tree

‎include/linux/netfilter/ipset/ip_set.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ struct ip_set {
293293
/* Lock protecting the set data */
294294
rwlock_t lock;
295295
/* References to the set */
296-
atomic_t ref;
296+
u32 ref;
297297
/* The core set type */
298298
struct ip_set_type *type;
299299
/* The type variant doing the real job */

‎include/linux/netfilter/ipset/ip_set_ahash.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,7 @@ type_pf_head(struct ip_set *set, struct sk_buff *skb)
515515
if (h->netmask != HOST_MASK)
516516
NLA_PUT_U8(skb, IPSET_ATTR_NETMASK, h->netmask);
517517
#endif
518-
NLA_PUT_NET32(skb, IPSET_ATTR_REFERENCES,
519-
htonl(atomic_read(&set->ref) - 1));
518+
NLA_PUT_NET32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1));
520519
NLA_PUT_NET32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize));
521520
if (with_timeout(h->timeout))
522521
NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT, htonl(h->timeout));

‎net/netfilter/ipset/ip_set_bitmap_ip.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ bitmap_ip_head(struct ip_set *set, struct sk_buff *skb)
338338
NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip));
339339
if (map->netmask != 32)
340340
NLA_PUT_U8(skb, IPSET_ATTR_NETMASK, map->netmask);
341-
NLA_PUT_NET32(skb, IPSET_ATTR_REFERENCES,
342-
htonl(atomic_read(&set->ref) - 1));
341+
NLA_PUT_NET32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1));
343342
NLA_PUT_NET32(skb, IPSET_ATTR_MEMSIZE,
344343
htonl(sizeof(*map) + map->memsize));
345344
if (with_timeout(map->timeout))

‎net/netfilter/ipset/ip_set_bitmap_ipmac.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,7 @@ bitmap_ipmac_head(struct ip_set *set, struct sk_buff *skb)
434434
goto nla_put_failure;
435435
NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, htonl(map->first_ip));
436436
NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip));
437-
NLA_PUT_NET32(skb, IPSET_ATTR_REFERENCES,
438-
htonl(atomic_read(&set->ref) - 1));
437+
NLA_PUT_NET32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1));
439438
NLA_PUT_NET32(skb, IPSET_ATTR_MEMSIZE,
440439
htonl(sizeof(*map)
441440
+ (map->last_ip - map->first_ip + 1) * map->dsize));

‎net/netfilter/ipset/ip_set_bitmap_port.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,7 @@ bitmap_port_head(struct ip_set *set, struct sk_buff *skb)
320320
goto nla_put_failure;
321321
NLA_PUT_NET16(skb, IPSET_ATTR_PORT, htons(map->first_port));
322322
NLA_PUT_NET16(skb, IPSET_ATTR_PORT_TO, htons(map->last_port));
323-
NLA_PUT_NET32(skb, IPSET_ATTR_REFERENCES,
324-
htonl(atomic_read(&set->ref) - 1));
323+
NLA_PUT_NET32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1));
325324
NLA_PUT_NET32(skb, IPSET_ATTR_MEMSIZE,
326325
htonl(sizeof(*map) + map->memsize));
327326
if (with_timeout(map->timeout))

‎net/netfilter/ipset/ip_set_core.c‎

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
static LIST_HEAD(ip_set_type_list); /* all registered set types */
2828
static DEFINE_MUTEX(ip_set_type_mutex); /* protects ip_set_type_list */
29+
static DEFINE_RWLOCK(ip_set_ref_lock); /* protects the set refs */
2930

3031
static struct ip_set **ip_set_list; /* all individual sets */
3132
static ip_set_id_t ip_set_max = CONFIG_IP_SET_MAX; /* max number of sets */
@@ -301,13 +302,18 @@ EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
301302
static inline void
302303
__ip_set_get(ip_set_id_t index)
303304
{
304-
atomic_inc(&ip_set_list[index]->ref);
305+
write_lock_bh(&ip_set_ref_lock);
306+
ip_set_list[index]->ref++;
307+
write_unlock_bh(&ip_set_ref_lock);
305308
}
306309

307310
static inline void
308311
__ip_set_put(ip_set_id_t index)
309312
{
310-
atomic_dec(&ip_set_list[index]->ref);
313+
write_lock_bh(&ip_set_ref_lock);
314+
BUG_ON(ip_set_list[index]->ref == 0);
315+
ip_set_list[index]->ref--;
316+
write_unlock_bh(&ip_set_ref_lock);
311317
}
312318

313319
/*
@@ -324,7 +330,7 @@ ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
324330
struct ip_set *set = ip_set_list[index];
325331
int ret = 0;
326332

327-
BUG_ON(set == NULL || atomic_read(&set->ref) == 0);
333+
BUG_ON(set == NULL);
328334
pr_debug("set %s, index %u\n", set->name, index);
329335

330336
if (dim < set->type->dimension ||
@@ -356,7 +362,7 @@ ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
356362
struct ip_set *set = ip_set_list[index];
357363
int ret;
358364

359-
BUG_ON(set == NULL || atomic_read(&set->ref) == 0);
365+
BUG_ON(set == NULL);
360366
pr_debug("set %s, index %u\n", set->name, index);
361367

362368
if (dim < set->type->dimension ||
@@ -378,7 +384,7 @@ ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
378384
struct ip_set *set = ip_set_list[index];
379385
int ret = 0;
380386

381-
BUG_ON(set == NULL || atomic_read(&set->ref) == 0);
387+
BUG_ON(set == NULL);
382388
pr_debug("set %s, index %u\n", set->name, index);
383389

384390
if (dim < set->type->dimension ||
@@ -397,7 +403,6 @@ EXPORT_SYMBOL_GPL(ip_set_del);
397403
* Find set by name, reference it once. The reference makes sure the
398404
* thing pointed to, does not go away under our feet.
399405
*
400-
* The nfnl mutex must already be activated.
401406
*/
402407
ip_set_id_t
403408
ip_set_get_byname(const char *name, struct ip_set **set)
@@ -423,15 +428,12 @@ EXPORT_SYMBOL_GPL(ip_set_get_byname);
423428
* reference count by 1. The caller shall not assume the index
424429
* to be valid, after calling this function.
425430
*
426-
* The nfnl mutex must already be activated.
427431
*/
428432
void
429433
ip_set_put_byindex(ip_set_id_t index)
430434
{
431-
if (ip_set_list[index] != NULL) {
432-
BUG_ON(atomic_read(&ip_set_list[index]->ref) == 0);
435+
if (ip_set_list[index] != NULL)
433436
__ip_set_put(index);
434-
}
435437
}
436438
EXPORT_SYMBOL_GPL(ip_set_put_byindex);
437439

@@ -441,15 +443,14 @@ EXPORT_SYMBOL_GPL(ip_set_put_byindex);
441443
* can't be destroyed. The set cannot be renamed due to
442444
* the referencing either.
443445
*
444-
* The nfnl mutex must already be activated.
445446
*/
446447
const char *
447448
ip_set_name_byindex(ip_set_id_t index)
448449
{
449450
const struct ip_set *set = ip_set_list[index];
450451

451452
BUG_ON(set == NULL);
452-
BUG_ON(atomic_read(&set->ref) == 0);
453+
BUG_ON(set->ref == 0);
453454

454455
/* Referenced, so it's safe */
455456
return set->name;
@@ -515,18 +516,15 @@ void
515516
ip_set_nfnl_put(ip_set_id_t index)
516517
{
517518
nfnl_lock();
518-
if (ip_set_list[index] != NULL) {
519-
BUG_ON(atomic_read(&ip_set_list[index]->ref) == 0);
520-
__ip_set_put(index);
521-
}
519+
ip_set_put_byindex(index);
522520
nfnl_unlock();
523521
}
524522
EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
525523

526524
/*
527525
* Communication protocol with userspace over netlink.
528526
*
529-
* We already locked by nfnl_lock.
527+
* The commands are serialized by the nfnl mutex.
530528
*/
531529

532530
static inline bool
@@ -657,7 +655,6 @@ ip_set_create(struct sock *ctnl, struct sk_buff *skb,
657655
return -ENOMEM;
658656
rwlock_init(&set->lock);
659657
strlcpy(set->name, name, IPSET_MAXNAMELEN);
660-
atomic_set(&set->ref, 0);
661658
set->family = family;
662659

663660
/*
@@ -690,8 +687,8 @@ ip_set_create(struct sock *ctnl, struct sk_buff *skb,
690687

691688
/*
692689
* Here, we have a valid, constructed set and we are protected
693-
* by nfnl_lock. Find the first free index in ip_set_list and
694-
* check clashing.
690+
* by the nfnl mutex. Find the first free index in ip_set_list
691+
* and check clashing.
695692
*/
696693
if ((ret = find_free_id(set->name, &index, &clash)) != 0) {
697694
/* If this is the same set and requested, ignore error */
@@ -751,31 +748,51 @@ ip_set_destroy(struct sock *ctnl, struct sk_buff *skb,
751748
const struct nlattr * const attr[])
752749
{
753750
ip_set_id_t i;
751+
int ret = 0;
754752

755753
if (unlikely(protocol_failed(attr)))
756754
return -IPSET_ERR_PROTOCOL;
757755

758-
/* References are protected by the nfnl mutex */
756+
/* Commands are serialized and references are
757+
* protected by the ip_set_ref_lock.
758+
* External systems (i.e. xt_set) must call
759+
* ip_set_put|get_nfnl_* functions, that way we
760+
* can safely check references here.
761+
*
762+
* list:set timer can only decrement the reference
763+
* counter, so if it's already zero, we can proceed
764+
* without holding the lock.
765+
*/
766+
read_lock_bh(&ip_set_ref_lock);
759767
if (!attr[IPSET_ATTR_SETNAME]) {
760768
for (i = 0; i < ip_set_max; i++) {
761-
if (ip_set_list[i] != NULL &&
762-
(atomic_read(&ip_set_list[i]->ref)))
763-
return -IPSET_ERR_BUSY;
769+
if (ip_set_list[i] != NULL && ip_set_list[i]->ref) {
770+
ret = IPSET_ERR_BUSY;
771+
goto out;
772+
}
764773
}
774+
read_unlock_bh(&ip_set_ref_lock);
765775
for (i = 0; i < ip_set_max; i++) {
766776
if (ip_set_list[i] != NULL)
767777
ip_set_destroy_set(i);
768778
}
769779
} else {
770780
i = find_set_id(nla_data(attr[IPSET_ATTR_SETNAME]));
771-
if (i == IPSET_INVALID_ID)
772-
return -ENOENT;
773-
else if (atomic_read(&ip_set_list[i]->ref))
774-
return -IPSET_ERR_BUSY;
781+
if (i == IPSET_INVALID_ID) {
782+
ret = -ENOENT;
783+
goto out;
784+
} else if (ip_set_list[i]->ref) {
785+
ret = -IPSET_ERR_BUSY;
786+
goto out;
787+
}
788+
read_unlock_bh(&ip_set_ref_lock);
775789

776790
ip_set_destroy_set(i);
777791
}
778792
return 0;
793+
out:
794+
read_unlock_bh(&ip_set_ref_lock);
795+
return ret;
779796
}
780797

781798
/* Flush sets */
@@ -834,6 +851,7 @@ ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
834851
struct ip_set *set;
835852
const char *name2;
836853
ip_set_id_t i;
854+
int ret = 0;
837855

838856
if (unlikely(protocol_failed(attr) ||
839857
attr[IPSET_ATTR_SETNAME] == NULL ||
@@ -843,25 +861,33 @@ ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
843861
set = find_set(nla_data(attr[IPSET_ATTR_SETNAME]));
844862
if (set == NULL)
845863
return -ENOENT;
846-
if (atomic_read(&set->ref) != 0)
847-
return -IPSET_ERR_REFERENCED;
864+
865+
read_lock_bh(&ip_set_ref_lock);
866+
if (set->ref != 0) {
867+
ret = -IPSET_ERR_REFERENCED;
868+
goto out;
869+
}
848870

849871
name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
850872
for (i = 0; i < ip_set_max; i++) {
851873
if (ip_set_list[i] != NULL &&
852-
STREQ(ip_set_list[i]->name, name2))
853-
return -IPSET_ERR_EXIST_SETNAME2;
874+
STREQ(ip_set_list[i]->name, name2)) {
875+
ret = -IPSET_ERR_EXIST_SETNAME2;
876+
goto out;
877+
}
854878
}
855879
strncpy(set->name, name2, IPSET_MAXNAMELEN);
856880

857-
return 0;
881+
out:
882+
read_unlock_bh(&ip_set_ref_lock);
883+
return ret;
858884
}
859885

860886
/* Swap two sets so that name/index points to the other.
861887
* References and set names are also swapped.
862888
*
863-
* We are protected by the nfnl mutex and references are
864-
* manipulated only by holding the mutex. The kernel interfaces
889+
* The commands are serialized by the nfnl mutex and references are
890+
* protected by the ip_set_ref_lock. The kernel interfaces
865891
* do not hold the mutex but the pointer settings are atomic
866892
* so the ip_set_list always contains valid pointers to the sets.
867893
*/
@@ -874,7 +900,6 @@ ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
874900
struct ip_set *from, *to;
875901
ip_set_id_t from_id, to_id;
876902
char from_name[IPSET_MAXNAMELEN];
877-
u32 from_ref;
878903

879904
if (unlikely(protocol_failed(attr) ||
880905
attr[IPSET_ATTR_SETNAME] == NULL ||
@@ -899,17 +924,15 @@ ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
899924
from->type->family == to->type->family))
900925
return -IPSET_ERR_TYPE_MISMATCH;
901926

902-
/* No magic here: ref munging protected by the nfnl_lock */
903927
strncpy(from_name, from->name, IPSET_MAXNAMELEN);
904-
from_ref = atomic_read(&from->ref);
905-
906928
strncpy(from->name, to->name, IPSET_MAXNAMELEN);
907-
atomic_set(&from->ref, atomic_read(&to->ref));
908929
strncpy(to->name, from_name, IPSET_MAXNAMELEN);
909-
atomic_set(&to->ref, from_ref);
910930

931+
write_lock_bh(&ip_set_ref_lock);
932+
swap(from->ref, to->ref);
911933
ip_set_list[from_id] = to;
912934
ip_set_list[to_id] = from;
935+
write_unlock_bh(&ip_set_ref_lock);
913936

914937
return 0;
915938
}
@@ -926,7 +949,7 @@ ip_set_dump_done(struct netlink_callback *cb)
926949
{
927950
if (cb->args[2]) {
928951
pr_debug("release set %s\n", ip_set_list[cb->args[1]]->name);
929-
__ip_set_put((ip_set_id_t) cb->args[1]);
952+
ip_set_put_byindex((ip_set_id_t) cb->args[1]);
930953
}
931954
return 0;
932955
}
@@ -1068,7 +1091,7 @@ ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
10681091
/* If there was an error or set is done, release set */
10691092
if (ret || !cb->args[2]) {
10701093
pr_debug("release set %s\n", ip_set_list[index]->name);
1071-
__ip_set_put(index);
1094+
ip_set_put_byindex(index);
10721095
}
10731096

10741097
/* If we dump all sets, continue with dumping last ones */

‎net/netfilter/ipset/ip_set_list_set.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ list_set_head(struct ip_set *set, struct sk_buff *skb)
366366
NLA_PUT_NET32(skb, IPSET_ATTR_SIZE, htonl(map->size));
367367
if (with_timeout(map->timeout))
368368
NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT, htonl(map->timeout));
369-
NLA_PUT_NET32(skb, IPSET_ATTR_REFERENCES,
370-
htonl(atomic_read(&set->ref) - 1));
369+
NLA_PUT_NET32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1));
371370
NLA_PUT_NET32(skb, IPSET_ATTR_MEMSIZE,
372371
htonl(sizeof(*map) + map->size * map->dsize));
373372
ipset_nest_end(skb, nested);
@@ -457,8 +456,7 @@ list_set_gc(unsigned long ul_set)
457456
struct list_set *map = set->data;
458457
struct set_telem *e;
459458
u32 i;
460-
461-
/* nfnl_lock should be called */
459+
462460
write_lock_bh(&set->lock);
463461
for (i = 0; i < map->size; i++) {
464462
e = list_set_telem(map, i);

0 commit comments

Comments
 (0)