Skip to content

Commit b57b2d1

Browse files
author
Jozsef Kadlecsik
committed
netfilter: ipset: Prepare the ipset core to use RCU at set level
Replace rwlock_t with spinlock_t in "struct ip_set" and change the locking accordingly. Convert the comment extension into an rcu-avare object. Also, simplify the timeout routines. Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
1 parent bd55389 commit b57b2d1

4 files changed

Lines changed: 66 additions & 50 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ struct ip_set_counter {
108108
atomic64_t packets;
109109
};
110110

111+
struct ip_set_comment_rcu {
112+
struct rcu_head rcu;
113+
char str[0];
114+
};
115+
111116
struct ip_set_comment {
112-
char *str;
117+
struct ip_set_comment_rcu __rcu *c;
113118
};
114119

115120
struct ip_set_skbinfo {
@@ -226,7 +231,7 @@ struct ip_set {
226231
/* The name of the set */
227232
char name[IPSET_MAXNAMELEN];
228233
/* Lock protecting the set data */
229-
rwlock_t lock;
234+
spinlock_t lock;
230235
/* References to the set */
231236
u32 ref;
232237
/* The core set type */

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,57 @@ ip_set_comment_uget(struct nlattr *tb)
1616
return nla_data(tb);
1717
}
1818

19+
/* Called from uadd only, protected by the set spinlock.
20+
* The kadt functions don't use the comment extensions in any way.
21+
*/
1922
static inline void
2023
ip_set_init_comment(struct ip_set_comment *comment,
2124
const struct ip_set_ext *ext)
2225
{
26+
struct ip_set_comment_rcu *c = rcu_dereference_protected(comment->c, 1);
2327
size_t len = ext->comment ? strlen(ext->comment) : 0;
2428

25-
if (unlikely(comment->str)) {
26-
kfree(comment->str);
27-
comment->str = NULL;
29+
if (unlikely(c)) {
30+
kfree_rcu(c, rcu);
31+
rcu_assign_pointer(comment->c, NULL);
2832
}
2933
if (!len)
3034
return;
3135
if (unlikely(len > IPSET_MAX_COMMENT_SIZE))
3236
len = IPSET_MAX_COMMENT_SIZE;
33-
comment->str = kzalloc(len + 1, GFP_ATOMIC);
34-
if (unlikely(!comment->str))
37+
c = kzalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
38+
if (unlikely(!c))
3539
return;
36-
strlcpy(comment->str, ext->comment, len + 1);
40+
strlcpy(c->str, ext->comment, len + 1);
41+
rcu_assign_pointer(comment->c, c);
3742
}
3843

44+
/* Used only when dumping a set, protected by rcu_read_lock_bh() */
3945
static inline int
4046
ip_set_put_comment(struct sk_buff *skb, struct ip_set_comment *comment)
4147
{
42-
if (!comment->str)
48+
struct ip_set_comment_rcu *c = rcu_dereference_bh(comment->c);
49+
50+
if (!c)
4351
return 0;
44-
return nla_put_string(skb, IPSET_ATTR_COMMENT, comment->str);
52+
return nla_put_string(skb, IPSET_ATTR_COMMENT, c->str);
4553
}
4654

55+
/* Called from uadd/udel, flush or the garbage collectors protected
56+
* by the set spinlock.
57+
* Called when the set is destroyed and when there can't be any user
58+
* of the set data anymore.
59+
*/
4760
static inline void
4861
ip_set_comment_free(struct ip_set_comment *comment)
4962
{
50-
if (unlikely(!comment->str))
63+
struct ip_set_comment_rcu *c;
64+
65+
c = rcu_dereference_protected(comment->c, 1);
66+
if (unlikely(!c))
5167
return;
52-
kfree(comment->str);
53-
comment->str = NULL;
68+
kfree_rcu(c, rcu);
69+
rcu_assign_pointer(comment->c, NULL);
5470
}
5571

5672
#endif

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,26 @@ ip_set_timeout_uget(struct nlattr *tb)
4040
}
4141

4242
static inline bool
43-
ip_set_timeout_test(unsigned long timeout)
43+
ip_set_timeout_expired(unsigned long *t)
4444
{
45-
return timeout == IPSET_ELEM_PERMANENT ||
46-
time_is_after_jiffies(timeout);
47-
}
48-
49-
static inline bool
50-
ip_set_timeout_expired(unsigned long *timeout)
51-
{
52-
return *timeout != IPSET_ELEM_PERMANENT &&
53-
time_is_before_jiffies(*timeout);
45+
return *t != IPSET_ELEM_PERMANENT && time_is_before_jiffies(*t);
5446
}
5547

5648
static inline void
57-
ip_set_timeout_set(unsigned long *timeout, u32 t)
49+
ip_set_timeout_set(unsigned long *timeout, u32 value)
5850
{
59-
if (!t) {
51+
unsigned long t;
52+
53+
if (!value) {
6054
*timeout = IPSET_ELEM_PERMANENT;
6155
return;
6256
}
6357

64-
*timeout = msecs_to_jiffies(t * MSEC_PER_SEC) + jiffies;
65-
if (*timeout == IPSET_ELEM_PERMANENT)
58+
t = msecs_to_jiffies(value * MSEC_PER_SEC) + jiffies;
59+
if (t == IPSET_ELEM_PERMANENT)
6660
/* Bingo! :-) */
67-
(*timeout)--;
61+
t--;
62+
*timeout = t;
6863
}
6964

7065
static inline u32

‎net/netfilter/ipset/ip_set_core.c‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,15 @@ ip_set_type_register(struct ip_set_type *type)
209209
pr_warn("ip_set type %s, family %s with revision min %u already registered!\n",
210210
type->name, family_name(type->family),
211211
type->revision_min);
212-
ret = -EINVAL;
213-
goto unlock;
212+
ip_set_type_unlock();
213+
return -EINVAL;
214214
}
215215
list_add_rcu(&type->list, &ip_set_type_list);
216216
pr_debug("type %s, family %s, revision %u:%u registered.\n",
217217
type->name, family_name(type->family),
218218
type->revision_min, type->revision_max);
219-
unlock:
220219
ip_set_type_unlock();
220+
221221
return ret;
222222
}
223223
EXPORT_SYMBOL_GPL(ip_set_type_register);
@@ -231,12 +231,12 @@ ip_set_type_unregister(struct ip_set_type *type)
231231
pr_warn("ip_set type %s, family %s with revision min %u not registered\n",
232232
type->name, family_name(type->family),
233233
type->revision_min);
234-
goto unlock;
234+
ip_set_type_unlock();
235+
return;
235236
}
236237
list_del_rcu(&type->list);
237238
pr_debug("type %s, family %s with revision min %u unregistered.\n",
238239
type->name, family_name(type->family), type->revision_min);
239-
unlock:
240240
ip_set_type_unlock();
241241

242242
synchronize_rcu();
@@ -531,16 +531,16 @@ ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
531531
!(opt->family == set->family || set->family == NFPROTO_UNSPEC))
532532
return 0;
533533

534-
read_lock_bh(&set->lock);
534+
rcu_read_lock_bh();
535535
ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
536-
read_unlock_bh(&set->lock);
536+
rcu_read_unlock_bh();
537537

538538
if (ret == -EAGAIN) {
539539
/* Type requests element to be completed */
540540
pr_debug("element must be completed, ADD is triggered\n");
541-
write_lock_bh(&set->lock);
541+
spin_lock_bh(&set->lock);
542542
set->variant->kadt(set, skb, par, IPSET_ADD, opt);
543-
write_unlock_bh(&set->lock);
543+
spin_unlock_bh(&set->lock);
544544
ret = 1;
545545
} else {
546546
/* --return-nomatch: invert matched element */
@@ -570,9 +570,9 @@ ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
570570
!(opt->family == set->family || set->family == NFPROTO_UNSPEC))
571571
return -IPSET_ERR_TYPE_MISMATCH;
572572

573-
write_lock_bh(&set->lock);
573+
spin_lock_bh(&set->lock);
574574
ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
575-
write_unlock_bh(&set->lock);
575+
spin_unlock_bh(&set->lock);
576576

577577
return ret;
578578
}
@@ -593,9 +593,9 @@ ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
593593
!(opt->family == set->family || set->family == NFPROTO_UNSPEC))
594594
return -IPSET_ERR_TYPE_MISMATCH;
595595

596-
write_lock_bh(&set->lock);
596+
spin_lock_bh(&set->lock);
597597
ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
598-
write_unlock_bh(&set->lock);
598+
spin_unlock_bh(&set->lock);
599599

600600
return ret;
601601
}
@@ -880,7 +880,7 @@ ip_set_create(struct sock *ctnl, struct sk_buff *skb,
880880
set = kzalloc(sizeof(struct ip_set), GFP_KERNEL);
881881
if (!set)
882882
return -ENOMEM;
883-
rwlock_init(&set->lock);
883+
spin_lock_init(&set->lock);
884884
strlcpy(set->name, name, IPSET_MAXNAMELEN);
885885
set->family = family;
886886
set->revision = revision;
@@ -1062,9 +1062,9 @@ ip_set_flush_set(struct ip_set *set)
10621062
{
10631063
pr_debug("set: %s\n", set->name);
10641064

1065-
write_lock_bh(&set->lock);
1065+
spin_lock_bh(&set->lock);
10661066
set->variant->flush(set);
1067-
write_unlock_bh(&set->lock);
1067+
spin_unlock_bh(&set->lock);
10681068
}
10691069

10701070
static int
@@ -1377,9 +1377,9 @@ ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
13771377
set->variant->uref(set, cb, true);
13781378
/* Fall through and add elements */
13791379
default:
1380-
read_lock_bh(&set->lock);
1380+
rcu_read_lock_bh();
13811381
ret = set->variant->list(set, skb, cb);
1382-
read_unlock_bh(&set->lock);
1382+
rcu_read_unlock_bh();
13831383
if (!cb->args[IPSET_CB_ARG0])
13841384
/* Set is done, proceed with next one */
13851385
goto next_set;
@@ -1462,9 +1462,9 @@ call_ad(struct sock *ctnl, struct sk_buff *skb, struct ip_set *set,
14621462
bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
14631463

14641464
do {
1465-
write_lock_bh(&set->lock);
1465+
spin_lock_bh(&set->lock);
14661466
ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
1467-
write_unlock_bh(&set->lock);
1467+
spin_unlock_bh(&set->lock);
14681468
retried = true;
14691469
} while (ret == -EAGAIN &&
14701470
set->variant->resize &&
@@ -1644,9 +1644,9 @@ ip_set_utest(struct sock *ctnl, struct sk_buff *skb,
16441644
set->type->adt_policy))
16451645
return -IPSET_ERR_PROTOCOL;
16461646

1647-
read_lock_bh(&set->lock);
1647+
rcu_read_lock_bh();
16481648
ret = set->variant->uadt(set, tb, IPSET_TEST, NULL, 0, 0);
1649-
read_unlock_bh(&set->lock);
1649+
rcu_read_unlock_bh();
16501650
/* Userspace can't trigger element to be re-added */
16511651
if (ret == -EAGAIN)
16521652
ret = 1;

0 commit comments

Comments
 (0)