From patchwork Sat Jul 26 04:27:06 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 373887 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id A682A1400F4 for ; Sat, 26 Jul 2014 14:27:33 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750836AbaGZE13 (ORCPT ); Sat, 26 Jul 2014 00:27:29 -0400 Received: from mail-oa0-f54.google.com ([209.85.219.54]:47036 "EHLO mail-oa0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750705AbaGZE12 (ORCPT ); Sat, 26 Jul 2014 00:27:28 -0400 Received: by mail-oa0-f54.google.com with SMTP id n16so6509595oag.41 for ; Fri, 25 Jul 2014 21:27:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:subject:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-type:content-transfer-encoding; bh=EJz02DrDyGyyzagbWMMUJ50Fbc8h/PUpAq9roG/S9d8=; b=Q/S/WYNIvqmiZeE92Ny8JzSHbL7LSooMztFhbPicnpsZeTbaCSLuvFoI2IEnsP4pHw Nywqxd+cBM4mn0xoCfgtU6jMfnQhjKnCyZqVsmOA3VDuElVpva9XH82FQcndrWHzsld8 SeFhdSJRMonRorgtimwcsH+WQMRJYKzx1XwmQWsWC2lJKK0cuQTicjK3TraMcI2BhXeE J9Ws+0Rua3Urkgxxqp+L9tqQwJgh7gR2R3v0r1PQTnESQ28743+gVTgeRRR1OtTsavOK TcKKmvgUEbyBO9zXxYJrCMU1sKl2peL80JW2X3uXE05axT8lFVS+u1CAAhEtnDcfiftb 2P5A== X-Received: by 10.60.43.196 with SMTP id y4mr29355987oel.44.1406348848349; Fri, 25 Jul 2014 21:27:28 -0700 (PDT) Received: from nitbit.x32 ([72.168.134.246]) by mx.google.com with ESMTPSA id ej4sm23382528obb.28.2014.07.25.21.27.16 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 25 Jul 2014 21:27:27 -0700 (PDT) From: John Fastabend X-Google-Original-From: John Fastabend Subject: [net-next PATCH v1 04/15] net: sched: cls_cgroup use RCU To: xiyou.wangcong@gmail.com, jhs@mojatatu.com, eric.dumazet@gmail.com Cc: netdev@vger.kernel.org, paulmck@linux.vnet.ibm.com, brouer@redhat.com Date: Fri, 25 Jul 2014 21:27:06 -0700 Message-ID: <20140726042704.21036.42754.stgit@nitbit.x32> In-Reply-To: <20140726042439.21036.57721.stgit@nitbit.x32> References: <20140726042439.21036.57721.stgit@nitbit.x32> User-Agent: StGit/0.16 MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Make cgroup classifier safe for RCU. Also drops the calls in the classify routine that were doing a rcu_read_lock()/rcu_read_unlock(). If the rcu_read_lock() isn't held entering this routine we have issues with deleting the classifier chain so remove the unnecessary rcu_read_lock()/rcu_read_unlock() pair noting all paths AFAIK hold rcu_read_lock. If there is a case where classify is called without the rcu read lock then an rcu splat will occur and we can correct it. Signed-off-by: John Fastabend --- net/sched/cls_cgroup.c | 63 ++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 24 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/net/sched/cls_cgroup.c b/net/sched/cls_cgroup.c index cacf01b..3b75487 100644 --- a/net/sched/cls_cgroup.c +++ b/net/sched/cls_cgroup.c @@ -22,17 +22,17 @@ struct cls_cgroup_head { u32 handle; struct tcf_exts exts; struct tcf_ematch_tree ematches; + struct tcf_proto *tp; + struct rcu_head rcu; }; static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp, struct tcf_result *res) { - struct cls_cgroup_head *head = tp->root; + struct cls_cgroup_head *head = rcu_dereference_bh(tp->root); u32 classid; - rcu_read_lock(); classid = task_cls_state(current)->classid; - rcu_read_unlock(); /* * Due to the nature of the classifier it is required to ignore all @@ -80,13 +80,25 @@ static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = { [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED }, }; +static void cls_cgroup_destroy_rcu(struct rcu_head *root) +{ + struct cls_cgroup_head *head = container_of(root, + struct cls_cgroup_head, + rcu); + + tcf_exts_destroy(head->tp, &head->exts); + tcf_em_tree_destroy(head->tp, &head->ematches); + kfree(head); +} + static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb, struct tcf_proto *tp, unsigned long base, u32 handle, struct nlattr **tca, unsigned long *arg, bool ovr) { struct nlattr *tb[TCA_CGROUP_MAX + 1]; - struct cls_cgroup_head *head = tp->root; + struct cls_cgroup_head *head = rtnl_dereference(tp->root); + struct cls_cgroup_head *new; struct tcf_ematch_tree t; struct tcf_exts e; int err; @@ -94,25 +106,24 @@ static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb, if (!tca[TCA_OPTIONS]) return -EINVAL; - if (head == NULL) { - if (!handle) - return -EINVAL; + if (!head && !handle) + return -EINVAL; - head = kzalloc(sizeof(*head), GFP_KERNEL); - if (head == NULL) - return -ENOBUFS; + if (head && handle != head->handle) + return -ENOENT; - tcf_exts_init(&head->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE); - head->handle = handle; + new = kzalloc(sizeof(*head), GFP_KERNEL); + if (!new) + return -ENOBUFS; - tcf_tree_lock(tp); - tp->root = head; - tcf_tree_unlock(tp); + if (head) { + new->handle = head->handle; + } else { + tcf_exts_init(&new->exts, TCA_CGROUP_ACT, TCA_CGROUP_POLICE); + new->handle = handle; } - if (handle != head->handle) - return -ENOENT; - + new->tp = tp; err = nla_parse_nested(tb, TCA_CGROUP_MAX, tca[TCA_OPTIONS], cgroup_policy); if (err < 0) @@ -127,20 +138,24 @@ static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb, if (err < 0) return err; - tcf_exts_change(tp, &head->exts, &e); - tcf_em_tree_change(tp, &head->ematches, &t); + tcf_exts_change(tp, &new->exts, &e); + tcf_em_tree_change(tp, &new->ematches, &t); + rcu_assign_pointer(tp->root, new); + if (head) + call_rcu(&head->rcu, cls_cgroup_destroy_rcu); return 0; } static void cls_cgroup_destroy(struct tcf_proto *tp) { - struct cls_cgroup_head *head = tp->root; + struct cls_cgroup_head *head = rtnl_dereference(tp->root); if (head) { tcf_exts_destroy(tp, &head->exts); tcf_em_tree_destroy(tp, &head->ematches); - kfree(head); + RCU_INIT_POINTER(tp->root, NULL); + kfree_rcu(head, rcu); } } @@ -151,7 +166,7 @@ static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg) static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg) { - struct cls_cgroup_head *head = tp->root; + struct cls_cgroup_head *head = rtnl_dereference(tp->root); if (arg->count < arg->skip) goto skip; @@ -167,7 +182,7 @@ skip: static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, unsigned long fh, struct sk_buff *skb, struct tcmsg *t) { - struct cls_cgroup_head *head = tp->root; + struct cls_cgroup_head *head = rtnl_dereference(tp->root); unsigned char *b = skb_tail_pointer(skb); struct nlattr *nest;