]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
netfilter: nf_tables: add GC synchronization helpers
authorPatrick McHardy <kaber@trash.net>
Thu, 26 Mar 2015 12:39:39 +0000 (12:39 +0000)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 1 Apr 2015 09:17:29 +0000 (11:17 +0200)
GC is expected to happen asynchrously to the netlink interface. In the
netlink path, both insertion and removal of elements consist of two
steps, insertion followed by activation or deactivation followed by
removal, during which the element must not be freed by GC.

The synchronization helpers use an unused bit in the genmask field to
atomically mark an element as "busy", meaning it is either currently
being handled through the netlink API or by GC.

Elements being processed by GC will never survive, netlink will simply
ignore them. Elements being currently processed through netlink will be
skipped by GC and reprocessed during the next run.

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/net/netfilter/nf_tables.h
net/netfilter/nf_tables_api.c

index 6fd44959bf870143f74fea9d492191027f8d7773..1ea13fcd388ec1079d8f436bd0748554f23b2788 100644 (file)
@@ -852,6 +852,41 @@ static inline void nft_set_elem_change_active(const struct nft_set *set,
        ext->genmask ^= nft_genmask_next(read_pnet(&set->pnet));
 }
 
+/*
+ * We use a free bit in the genmask field to indicate the element
+ * is busy, meaning it is currently being processed either by
+ * the netlink API or GC.
+ *
+ * Even though the genmask is only a single byte wide, this works
+ * because the extension structure if fully constant once initialized,
+ * so there are no non-atomic write accesses unless it is already
+ * marked busy.
+ */
+#define NFT_SET_ELEM_BUSY_MASK (1 << 2)
+
+#if defined(__LITTLE_ENDIAN_BITFIELD)
+#define NFT_SET_ELEM_BUSY_BIT  2
+#elif defined(__BIG_ENDIAN_BITFIELD)
+#define NFT_SET_ELEM_BUSY_BIT  (BITS_PER_LONG - BITS_PER_BYTE + 2)
+#else
+#error
+#endif
+
+static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
+{
+       unsigned long *word = (unsigned long *)ext;
+
+       BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
+       return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
+}
+
+static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
+{
+       unsigned long *word = (unsigned long *)ext;
+
+       clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
+}
+
 /**
  *     struct nft_trans - nf_tables object update in transaction
  *
index 138e47fddab773ee994584c2076f9e0932b1f2cc..3aa92b3f85fd075819bd1f34972a1f1c4de05244 100644 (file)
@@ -3338,7 +3338,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
        if (trans == NULL)
                goto err4;
 
-       ext->genmask = nft_genmask_cur(ctx->net);
+       ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
        err = set->ops->insert(set, &elem);
        if (err < 0)
                goto err5;