History

Merge tag 'nf-23-08-10' of git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf

Pablo Neira Ayuso says: ==================== Netfilter fixes for net The existing attempt to resolve races between control plane and GC work is error prone, as reported by Bien Pham <phamnnb@sea.com>, some places forgot to call nft_set_elem_mark_busy(), leading to double-deactivation of elements. This series contains the following patches: 1) Do not skip expired elements during walk otherwise elements might never decrement the reference counter on data, leading to memleak. 2) Add a GC transaction API to replace the former attempt to deal with races between control plane and GC. GC worker sets on NFT_SET_ELEM_DEAD_BIT on elements and it creates a GC transaction to remove the expired elements, GC transaction could abort in case of interference with control plane and retried later (GC async). Set backends such as rbtree and pipapo also perform GC from control plane (GC sync), in such case, element deactivation and removal is safe because mutex is held then collected elements are released via call_rcu(). 3) Adapt existing set backends to use the GC transaction API. 4) Update rhash set backend to set on _DEAD bit to report deleted elements from datapath for GC. 5) Remove old GC batch API and the NFT_SET_ELEM_BUSY_BIT. * tag 'nf-23-08-10' of git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf: netfilter: nf_tables: remove busy mark and gc batch API netfilter: nft_set_hash: mark set element as dead when deleting from packet path netfilter: nf_tables: adapt set backend to use GC transaction API netfilter: nf_tables: GC transaction API to avoid race with control plane netfilter: nf_tables: don't skip expired elements during walk ====================

Jakub Kicinski committed 3 years ago

Commit 3e91b0e2 parents 62d02fc a2dd0235 files · +476 −248Browse files

Showing the changes to include/net (1 of 5 files). Show all

Unified · Split

include/net/netfilter/nf_tables.h+45−75
@@ -512,6 +512,7 @@ struct nft_set_elem_expr { · 511 unmodified lines
512 *512 *
513 * @list: table set list node513 * @list: table set list node
514 * @bindings: list of set bindings514 * @bindings: list of set bindings
515 * @refs: internal refcounting for async set destruction
515 * @table: table this set belongs to516 * @table: table this set belongs to
516 * @net: netnamespace this set belongs to517 * @net: netnamespace this set belongs to
517 * @name: name of the set518 * @name: name of the set
@@ -541,6 +542,7 @@ struct nft_set_elem_expr { · 23 unmodified lines
541struct nft_set {542struct nft_set {
542 struct list_head list;543 struct list_head list;
543 struct list_head bindings;544 struct list_head bindings;
545 refcount_t refs;
544 struct nft_table *table;546 struct nft_table *table;
545 possible_net_t net;547 possible_net_t net;
546 char *name;548 char *name;
@@ -562,7 +564,8 @@ struct nft_set { · 15 unmodified lines
562 struct list_head pending_update;564 struct list_head pending_update;
563 /* runtime data below here */565 /* runtime data below here */
564 const struct nft_set_ops *ops ____cacheline_aligned;566 const struct nft_set_ops *ops ____cacheline_aligned;
565 u16 flags:14,567 u16 flags:13,
568 dead:1,
566 genmask:2;569 genmask:2;
567 u8 klen;570 u8 klen;
568 u8 dlen;571 u8 dlen;
@@ -596,7 +599,6 @@ struct nft_set *nft_set_lookup_global(const struct net *net, · 27 unmodified lines
596 599
597struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,600struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
598 const struct nft_set *set);601 const struct nft_set *set);
599void *nft_set_catchall_gc(const struct nft_set *set);
600 602
601static inline unsigned long nft_set_gc_interval(const struct nft_set *set)603static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
602{604{
@@ -813,62 +815,6 @@ void nft_set_elem_destroy(const struct nft_set *set, void *elem, · 210 unmodified lines
813void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,815void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
814 const struct nft_set *set, void *elem);816 const struct nft_set *set, void *elem);
815 817
816/**
817 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
818 *
819 * @rcu: rcu head
820 * @set: set the elements belong to
821 * @cnt: count of elements
822 */
823struct nft_set_gc_batch_head {
824 struct rcu_head rcu;
825 const struct nft_set *set;
826 unsigned int cnt;
827};
828
829#define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
830 sizeof(struct nft_set_gc_batch_head)) / \
831 sizeof(void *))
832
833/**
834 * struct nft_set_gc_batch - nf_tables set garbage collection batch
835 *
836 * @head: GC batch head
837 * @elems: garbage collection elements
838 */
839struct nft_set_gc_batch {
840 struct nft_set_gc_batch_head head;
841 void *elems[NFT_SET_GC_BATCH_SIZE];
842};
843
844struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
845 gfp_t gfp);
846void nft_set_gc_batch_release(struct rcu_head *rcu);
847
848static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
849{
850 if (gcb != NULL)
851 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
852}
853
854static inline struct nft_set_gc_batch *
855nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
856 gfp_t gfp)
857{
858 if (gcb != NULL) {
859 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
860 return gcb;
861 nft_set_gc_batch_complete(gcb);
862 }
863 return nft_set_gc_batch_alloc(set, gfp);
864}
865
866static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
867 void *elem)
868{
869 gcb->elems[gcb->head.cnt++] = elem;
870}
871
872struct nft_expr_ops;818struct nft_expr_ops;
873/**819/**
874 * struct nft_expr_type - nf_tables expression type820 * struct nft_expr_type - nf_tables expression type
@@ -1557,39 +1503,30 @@ static inline void nft_set_elem_change_active(const struct net *net, · 682 unmodified lines
1557 1503
1558#endif /* IS_ENABLED(CONFIG_NF_TABLES) */1504#endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1559 1505
1560/*1506#define NFT_SET_ELEM_DEAD_MASK (1 << 2)
1561 * We use a free bit in the genmask field to indicate the element
1562 * is busy, meaning it is currently being processed either by
1563 * the netlink API or GC.
1564 *
1565 * Even though the genmask is only a single byte wide, this works
1566 * because the extension structure if fully constant once initialized,
1567 * so there are no non-atomic write accesses unless it is already
1568 * marked busy.
1569 */
1570#define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1571 1507
1572#if defined(__LITTLE_ENDIAN_BITFIELD)1508#if defined(__LITTLE_ENDIAN_BITFIELD)
1573#define NFT_SET_ELEM_BUSY_BIT 21509#define NFT_SET_ELEM_DEAD_BIT 2
1574#elif defined(__BIG_ENDIAN_BITFIELD)1510#elif defined(__BIG_ENDIAN_BITFIELD)
1575#define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)1511#define NFT_SET_ELEM_DEAD_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1576#else1512#else
1577#error1513#error
1578#endif1514#endif
1579 1515
1580static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)1516static inline void nft_set_elem_dead(struct nft_set_ext *ext)
1581{1517{
1582 unsigned long *word = (unsigned long *)ext;1518 unsigned long *word = (unsigned long *)ext;
1583 1519
1584 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);1520 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1585 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);1521 set_bit(NFT_SET_ELEM_DEAD_BIT, word);
1586}1522}
1587 1523
1588static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)1524static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext)
1589{1525{
1590 unsigned long *word = (unsigned long *)ext;1526 unsigned long *word = (unsigned long *)ext;
1591 1527
1592 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);1528 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1529 return test_bit(NFT_SET_ELEM_DEAD_BIT, word);
1593}1530}
1594 1531
1595/**1532/**
@@ -1732,6 +1669,38 @@ struct nft_trans_flowtable { · 136 unmodified lines
1732#define nft_trans_flowtable_flags(trans) \1669#define nft_trans_flowtable_flags(trans) \
1733 (((struct nft_trans_flowtable *)trans->data)->flags)1670 (((struct nft_trans_flowtable *)trans->data)->flags)
1734 1671
1672#define NFT_TRANS_GC_BATCHCOUNT 256
1673
1674struct nft_trans_gc {
1675 struct list_head list;
1676 struct net *net;
1677 struct nft_set *set;
1678 u32 seq;
1679 u8 count;
1680 void *priv[NFT_TRANS_GC_BATCHCOUNT];
1681 struct rcu_head rcu;
1682};
1683
1684struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
1685 unsigned int gc_seq, gfp_t gfp);
1686void nft_trans_gc_destroy(struct nft_trans_gc *trans);
1687
1688struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
1689 unsigned int gc_seq, gfp_t gfp);
1690void nft_trans_gc_queue_async_done(struct nft_trans_gc *gc);
1691
1692struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp);
1693void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans);
1694
1695void nft_trans_gc_elem_add(struct nft_trans_gc *gc, void *priv);
1696
1697struct nft_trans_gc *nft_trans_gc_catchall(struct nft_trans_gc *gc,
1698 unsigned int gc_seq);
1699
1700void nft_setelem_data_deactivate(const struct net *net,
1701 const struct nft_set *set,
1702 struct nft_set_elem *elem);
1703
1735int __init nft_chain_filter_init(void);1704int __init nft_chain_filter_init(void);
1736void nft_chain_filter_fini(void);1705void nft_chain_filter_fini(void);
1737 1706
@@ -1758,6 +1727,7 @@ struct nftables_pernet { · 20 unmodified lines
1758 struct mutex commit_mutex;1727 struct mutex commit_mutex;
1759 u64 table_handle;1728 u64 table_handle;
1760 unsigned int base_seq;1729 unsigned int base_seq;
1730 unsigned int gc_seq;
1761};1731};
1762 1732
1763extern unsigned int nf_tables_net_id;1733extern unsigned int nf_tables_net_id;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t