From: mathieu.poirier@linaro.org
To: pablo@netfilter.org
Cc: netfilter-devel@vger.kernel.org, netfilter@vger.kernel.org,
fw@strlen.de, mathieu.poirier@linaro.org
Subject: [PATCH v4 1/2] netfilter: nfnetlink_acct: make shared objects SMP safe
Date: Mon, 10 Feb 2014 19:27:07 -0700 [thread overview]
Message-ID: <1392085628-24804-2-git-send-email-mathieu.poirier@linaro.org> (raw)
In-Reply-To: <1392085628-24804-1-git-send-email-mathieu.poirier@linaro.org>
From: Mathieu Poirier <mathieu.poirier@linaro.org>
Introducing locking mechanism to protect accounting objects seen
and shared by multiple processors. Under heavy traffic load
objects can be acted upon concurrently by more than one CPUs,
leading to inaccuracies between packet and byte counts.
Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>
---
net/netfilter/nfnetlink_acct.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/net/netfilter/nfnetlink_acct.c b/net/netfilter/nfnetlink_acct.c
index c7b6d46..225a886 100644
--- a/net/netfilter/nfnetlink_acct.c
+++ b/net/netfilter/nfnetlink_acct.c
@@ -36,6 +36,7 @@ struct nf_acct {
atomic_t refcnt;
char name[NFACCT_NAME_MAX];
struct rcu_head rcu_head;
+ spinlock_t lock;
};
static int
@@ -66,8 +67,10 @@ nfnl_acct_new(struct sock *nfnl, struct sk_buff *skb,
if (matching) {
if (nlh->nlmsg_flags & NLM_F_REPLACE) {
/* reset counters if you request a replacement. */
+ spin_lock_bh(&matching->lock);
atomic64_set(&matching->pkts, 0);
atomic64_set(&matching->bytes, 0);
+ spin_unlock_bh(&matching->lock);
return 0;
}
return -EBUSY;
@@ -88,6 +91,7 @@ nfnl_acct_new(struct sock *nfnl, struct sk_buff *skb,
be64_to_cpu(nla_get_be64(tb[NFACCT_PKTS])));
}
atomic_set(&nfacct->refcnt, 1);
+ spin_lock_init(&nfacct->lock);
list_add_tail_rcu(&nfacct->head, &nfnl_acct_list);
return 0;
}
@@ -114,6 +118,7 @@ nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
if (nla_put_string(skb, NFACCT_NAME, acct->name))
goto nla_put_failure;
+ spin_lock_bh(&acct->lock);
if (type == NFNL_MSG_ACCT_GET_CTRZERO) {
pkts = atomic64_xchg(&acct->pkts, 0);
bytes = atomic64_xchg(&acct->bytes, 0);
@@ -121,6 +126,8 @@ nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
pkts = atomic64_read(&acct->pkts);
bytes = atomic64_read(&acct->bytes);
}
+ spin_unlock_bh(&acct->lock);
+
if (nla_put_be64(skb, NFACCT_PKTS, cpu_to_be64(pkts)) ||
nla_put_be64(skb, NFACCT_BYTES, cpu_to_be64(bytes)) ||
nla_put_be32(skb, NFACCT_USE, htonl(atomic_read(&acct->refcnt))))
@@ -331,8 +338,10 @@ EXPORT_SYMBOL_GPL(nfnl_acct_put);
void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct)
{
+ spin_lock_bh(&nfacct->lock);
atomic64_inc(&nfacct->pkts);
atomic64_add(skb->len, &nfacct->bytes);
+ spin_unlock_bh(&nfacct->lock);
}
EXPORT_SYMBOL_GPL(nfnl_acct_update);
--
1.8.3.2
next prev parent reply other threads:[~2014-02-11 2:27 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-11 2:27 [PATCH v4 0/2] Add quota capabilities to nfacct mathieu.poirier
2014-02-11 2:27 ` mathieu.poirier [this message]
2014-02-11 2:27 ` [PATCH v4 2/2] netfilter: xtables: add quota support for nfacct mathieu.poirier
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=1392085628-24804-2-git-send-email-mathieu.poirier@linaro.org \
--to=mathieu.poirier@linaro.org \
--cc=fw@strlen.de \
--cc=netfilter-devel@vger.kernel.org \
--cc=netfilter@vger.kernel.org \
--cc=pablo@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).