From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Netfilter Development Mailinglist <netfilter-devel@lists.netfilter.org>
Cc: Harald Welte <laforge@netfilter.org>, Patrick McHardy <kaber@trash.net>
Subject: [PATCH 3/3][CONNTRACK] Fix race condition in early drop
Date: Mon, 21 Aug 2006 10:47:49 +0200 [thread overview]
Message-ID: <44E97335.1080105@netfilter.org> (raw)
[-- Attachment #1: Type: text/plain, Size: 705 bytes --]
[CONNTRACK] Fix race condition in early drop
On SMP environments the maximum number of conntracks can be overpassed
under heavy stress situations due to an existing race condition.
CPU A CPU B
atomic_read() ...
early_drop() ...
... atomic_read()
allocate conntrack allocate conntrack
atomic_inc() atomic_inc()
This patch uses an optimistic approach to solve the concurrency problem.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
--
The dawn of the fourth age of Linux firewalling is coming; a time of
great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
[-- Attachment #2: 09race.patch --]
[-- Type: text/plain, Size: 4111 bytes --]
[CONNTRACK] Fix race condition in early drop
On SMP environments the maximum number of conntracks can be overpassed
under heavy stress situations due to an existing race condition.
CPU A CPU B
atomic_read() ...
early_drop() ...
... atomic_read()
allocate conntrack allocate conntrack
atomic_inc() atomic_inc()
This patch uses an optimistic approach to solve the concurrency problem.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Index: net-2.6/net/ipv4/netfilter/ip_conntrack_core.c
===================================================================
--- net-2.6.orig/net/ipv4/netfilter/ip_conntrack_core.c 2006-08-17 15:50:33.000000000 +0200
+++ net-2.6/net/ipv4/netfilter/ip_conntrack_core.c 2006-08-17 17:52:27.000000000 +0200
@@ -642,21 +642,32 @@ struct ip_conntrack *ip_conntrack_alloc(
}
if (ip_conntrack_max
- && atomic_read(&ip_conntrack_count) >= ip_conntrack_max) {
+ && !atomic_add_unless(&ip_conntrack_count, 1, ip_conntrack_max)) {
unsigned int hash = hash_conntrack(orig);
/* Try dropping from this hash chain. */
- if (!early_drop(&ip_conntrack_hash[hash])) {
- if (net_ratelimit())
- printk(KERN_WARNING
- "ip_conntrack: table full, dropping"
- " packet.\n");
- return ERR_PTR(-ENOMEM);
- }
+ do {
+ if (!early_drop(&ip_conntrack_hash[hash])) {
+ if (net_ratelimit())
+ printk(KERN_WARNING
+ "ip_conntrack: table full, "
+ "dropping packet.\n");
+ return ERR_PTR(-ENOMEM);
+ }
+ /*
+ * On SMP environments, if the table is full and we
+ * early drop a conntrack to make some place for this
+ * new one then we have to ensure that no other
+ * conntrack slips through.
+ */
+ } while (!atomic_add_unless(&ip_conntrack_count,
+ 1,
+ ip_conntrack_max));
}
conntrack = kmem_cache_alloc(ip_conntrack_cachep, GFP_ATOMIC);
if (!conntrack) {
DEBUGP("Can't allocate conntrack.\n");
+ atomic_dec(&ip_conntrack_count);
return ERR_PTR(-ENOMEM);
}
@@ -670,8 +681,6 @@ struct ip_conntrack *ip_conntrack_alloc(
conntrack->timeout.data = (unsigned long)conntrack;
conntrack->timeout.function = death_by_timeout;
- atomic_inc(&ip_conntrack_count);
-
return conntrack;
}
Index: net-2.6/net/netfilter/nf_conntrack_core.c
===================================================================
--- net-2.6.orig/net/netfilter/nf_conntrack_core.c 2006-08-18 19:23:19.000000000 +0200
+++ net-2.6/net/netfilter/nf_conntrack_core.c 2006-08-18 20:20:08.000000000 +0200
@@ -868,16 +868,26 @@ __nf_conntrack_alloc(const struct nf_con
}
if (nf_conntrack_max
- && atomic_read(&nf_conntrack_count) >= nf_conntrack_max) {
+ && !atomic_add_unless(&nf_conntrack_count, 1, nf_conntrack_max)) {
unsigned int hash = hash_conntrack(orig);
/* Try dropping from this hash chain. */
- if (!early_drop(&nf_conntrack_hash[hash])) {
- if (net_ratelimit())
- printk(KERN_WARNING
- "nf_conntrack: table full, dropping"
- " packet.\n");
- return ERR_PTR(-ENOMEM);
- }
+ do {
+ if (!early_drop(&nf_conntrack_hash[hash])) {
+ if (net_ratelimit())
+ printk(KERN_WARNING
+ "ip_conntrack: table full, "
+ "dropping packet.\n");
+ return ERR_PTR(-ENOMEM);
+ }
+ /*
+ * On SMP environments, if the table is full and we
+ * early drop a conntrack to make some place for this
+ * new one then we have to ensure that no other
+ * conntrack slips through.
+ */
+ } while (!atomic_add_unless(&nf_conntrack_count,
+ 1,
+ nf_conntrack_max));
}
/* find features needed by this conntrack. */
@@ -923,9 +933,12 @@ __nf_conntrack_alloc(const struct nf_con
conntrack->timeout.data = (unsigned long)conntrack;
conntrack->timeout.function = death_by_timeout;
- atomic_inc(&nf_conntrack_count);
+ read_unlock_bh(&nf_ct_cache_lock);
+ return conntrack;
+
out:
read_unlock_bh(&nf_ct_cache_lock);
+ atomic_dec(&nf_conntrack_count);
return conntrack;
}
next reply other threads:[~2006-08-21 8:47 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-08-21 8:47 Pablo Neira Ayuso [this message]
2006-08-22 4:35 ` [PATCH 3/3][CONNTRACK] Fix race condition in early drop Yasuyuki KOZAKAI
[not found] ` <200608220435.k7M4ZSLf001686@toshiba.co.jp>
2006-08-22 13:46 ` Pablo Neira Ayuso
2006-08-22 14:39 ` Pablo Neira Ayuso
[not found] ` <200608230228.k7N2SDTf000802@toshiba.co.jp>
2006-08-23 4:38 ` Patrick McHardy
2006-08-23 2:28 ` Yasuyuki KOZAKAI
2006-08-24 11:47 ` Jarek Poplawski
2006-08-24 13:02 ` Jarek Poplawski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=44E97335.1080105@netfilter.org \
--to=pablo@netfilter.org \
--cc=kaber@trash.net \
--cc=laforge@netfilter.org \
--cc=netfilter-devel@lists.netfilter.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.