2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-11-08 14:56:00 +08:00
|
|
|
/*
|
|
|
|
* net/sched/cls_cgroup.c Control Group Classifier
|
|
|
|
*
|
|
|
|
* Authors: Thomas Graf <tgraf@suug.ch>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2008-11-08 14:56:00 +08:00
|
|
|
#include <linux/skbuff.h>
|
cls_cgroup: Store classid in struct sock
Up until now cls_cgroup has relied on fetching the classid out of
the current executing thread. This runs into trouble when a packet
processing is delayed in which case it may execute out of another
thread's context.
Furthermore, even when a packet is not delayed we may fail to
classify it if soft IRQs have been disabled, because this scenario
is indistinguishable from one where a packet unrelated to the
current thread is processed by a real soft IRQ.
In fact, the current semantics is inherently broken, as a single
skb may be constructed out of the writes of two different tasks.
A different manifestation of this problem is when the TCP stack
transmits in response of an incoming ACK. This is currently
unclassified.
As we already have a concept of packet ownership for accounting
purposes in the skb->sk pointer, this is a natural place to store
the classid in a persistent manner.
This patch adds the cls_cgroup classid in struct sock, filling up
an existing hole on 64-bit :)
The value is set at socket creation time. So all sockets created
via socket(2) automatically gains the ID of the thread creating it.
Whenever another process touches the socket by either reading or
writing to it, we will change the socket classid to that of the
process if it has a valid (non-zero) classid.
For sockets created on inbound connections through accept(2), we
inherit the classid of the original listening socket through
sk_clone, possibly preceding the actual accept(2) call.
In order to minimise risks, I have not made this the authoritative
classid. For now it is only used as a backup when we execute
with soft IRQs disabled. Once we're completely happy with its
semantics we can use it as the sole classid.
Footnote: I have rearranged the error path on cls_group module
creation. If we didn't do this, then there is a window where
someone could create a tc rule using cls_group before the cgroup
subsystem has been registered.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-05-24 15:12:34 +08:00
|
|
|
#include <linux/rcupdate.h>
|
2008-11-08 14:56:00 +08:00
|
|
|
#include <net/rtnetlink.h>
|
|
|
|
#include <net/pkt_cls.h>
|
cls_cgroup: Store classid in struct sock
Up until now cls_cgroup has relied on fetching the classid out of
the current executing thread. This runs into trouble when a packet
processing is delayed in which case it may execute out of another
thread's context.
Furthermore, even when a packet is not delayed we may fail to
classify it if soft IRQs have been disabled, because this scenario
is indistinguishable from one where a packet unrelated to the
current thread is processed by a real soft IRQ.
In fact, the current semantics is inherently broken, as a single
skb may be constructed out of the writes of two different tasks.
A different manifestation of this problem is when the TCP stack
transmits in response of an incoming ACK. This is currently
unclassified.
As we already have a concept of packet ownership for accounting
purposes in the skb->sk pointer, this is a natural place to store
the classid in a persistent manner.
This patch adds the cls_cgroup classid in struct sock, filling up
an existing hole on 64-bit :)
The value is set at socket creation time. So all sockets created
via socket(2) automatically gains the ID of the thread creating it.
Whenever another process touches the socket by either reading or
writing to it, we will change the socket classid to that of the
process if it has a valid (non-zero) classid.
For sockets created on inbound connections through accept(2), we
inherit the classid of the original listening socket through
sk_clone, possibly preceding the actual accept(2) call.
In order to minimise risks, I have not made this the authoritative
classid. For now it is only used as a backup when we execute
with soft IRQs disabled. Once we're completely happy with its
semantics we can use it as the sole classid.
Footnote: I have rearranged the error path on cls_group module
creation. If we didn't do this, then there is a window where
someone could create a tc rule using cls_group before the cgroup
subsystem has been registered.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
2010-05-24 15:12:34 +08:00
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/cls_cgroup.h>
|
2022-12-06 21:55:13 +08:00
|
|
|
#include <net/tc_wrapper.h>
|
2008-11-08 14:56:00 +08:00
|
|
|
|
2011-01-20 03:26:56 +08:00
|
|
|
struct cls_cgroup_head {
|
2008-11-08 14:56:00 +08:00
|
|
|
u32 handle;
|
|
|
|
struct tcf_exts exts;
|
|
|
|
struct tcf_ematch_tree ematches;
|
2014-09-13 11:06:26 +08:00
|
|
|
struct tcf_proto *tp;
|
2018-05-24 06:26:53 +08:00
|
|
|
struct rcu_work rwork;
|
2008-11-08 14:56:00 +08:00
|
|
|
};
|
|
|
|
|
2022-12-06 21:55:13 +08:00
|
|
|
TC_INDIRECT_SCOPE int cls_cgroup_classify(struct sk_buff *skb,
|
|
|
|
const struct tcf_proto *tp,
|
|
|
|
struct tcf_result *res)
|
2008-11-08 14:56:00 +08:00
|
|
|
{
|
2014-09-13 11:06:26 +08:00
|
|
|
struct cls_cgroup_head *head = rcu_dereference_bh(tp->root);
|
2015-07-15 20:21:41 +08:00
|
|
|
u32 classid = task_get_classid(skb);
|
2008-11-08 14:56:00 +08:00
|
|
|
|
cls_cgroup: avoid panic when receiving a packet before filter set
When a cgroup classifier is added, there is a small time interval in
which tp->root is NULL. If we receive a packet in this small time slice
a NULL pointer dereference will happen, leading to a kernel panic:
# mkdir /sys/fs/cgroup/net_cls/0
# echo 0x100001 > /sys/fs/cgroup/net_cls/0/net_cls.classid
# echo $$ >/sys/fs/cgroup/net_cls/0/tasks
# ping -qfb 255.255.255.255 -I eth0 &>/dev/null &
# tc qdisc add dev eth0 root handle 10: htb
# while : ; do
> tc filter add dev eth0 parent 10: protocol ip prio 10 handle 1: cgroup
> tc filter delete dev eth0
> done
Unable to handle kernel NULL pointer dereference at virtual address 0000000000000028
Mem abort info:
ESR = 0x96000005
Exception class = DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
Data abort info:
ISV = 0, ISS = 0x00000005
CM = 0, WnR = 0
user pgtable: 4k pages, 39-bit VAs, pgdp = 0000000098a7ff91
[0000000000000028] pgd=0000000000000000, pud=0000000000000000
Internal error: Oops: 96000005 [#1] SMP
Modules linked in: sch_htb cls_cgroup algif_hash af_alg nls_iso8859_1 nls_cp437 vfat fat xhci_plat_hcd m25p80 spi_nor xhci_hcd mtd usbcore usb_common spi_orion sfp i2c_mv64xxx phy_generic mdio_i2c marvell10g i2c_core mvpp2 mvmdio phylink sbsa_gwdt ip_tables x_tables autofs4
Process ping (pid: 5421, stack limit = 0x00000000b20b1505)
CPU: 3 PID: 5421 Comm: ping Not tainted 5.1.0-rc6 #31
Hardware name: Marvell 8040 MACCHIATOBin Double-shot (DT)
pstate: 60000005 (nZCv daif -PAN -UAO)
pc : cls_cgroup_classify+0x80/0xec [cls_cgroup]
lr : cls_cgroup_classify+0x34/0xec [cls_cgroup]
sp : ffffff8012e6b850
x29: ffffff8012e6b850 x28: ffffffc423dd3c00
x27: ffffff801093ebc0 x26: ffffffc425a85b00
x25: 0000000020000000 x24: 0000000000000000
x23: ffffff8012e6b910 x22: ffffffc428db4900
x21: ffffff8012e6b910 x20: 0000000000100001
x19: 0000000000000000 x18: 0000000000000000
x17: 0000000000000000 x16: 0000000000000000
x15: 0000000000000000 x14: 0000000000000000
x13: 0000000000000000 x12: 000000000000001c
x11: 0000000000000018 x10: ffffff8012e6b840
x9 : 0000000000003580 x8 : 000000000000009d
x7 : 0000000000000002 x6 : ffffff8012e6b860
x5 : 000000007cd66ffe x4 : 000000009742a193
x3 : ffffff800865b4d8 x2 : ffffff8012e6b910
x1 : 0000000000000400 x0 : ffffffc42c38f300
Call trace:
cls_cgroup_classify+0x80/0xec [cls_cgroup]
tcf_classify+0x78/0x138
htb_enqueue+0x74/0x320 [sch_htb]
__dev_queue_xmit+0x3e4/0x9d0
dev_queue_xmit+0x24/0x30
ip_finish_output2+0x2e4/0x4d0
ip_finish_output+0x1d8/0x270
ip_mc_output+0xa8/0x240
ip_local_out+0x58/0x68
ip_send_skb+0x2c/0x88
ip_push_pending_frames+0x44/0x50
raw_sendmsg+0x458/0x830
inet_sendmsg+0x54/0xe8
sock_sendmsg+0x34/0x50
__sys_sendto+0xd0/0x120
__arm64_sys_sendto+0x30/0x40
el0_svc_common.constprop.0+0x88/0xf8
el0_svc_handler+0x2c/0x38
el0_svc+0x8/0xc
Code: 39496001 360002a1 b9425c14 34000274 (79405260)
Fixes: ed76f5edccc9 ("net: sched: protect filter_chain list with filter_chain_lock mutex")
Suggested-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: Matteo Croce <mcroce@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-05-02 23:13:18 +08:00
|
|
|
if (unlikely(!head))
|
|
|
|
return -1;
|
2009-05-27 11:47:02 +08:00
|
|
|
if (!classid)
|
|
|
|
return -1;
|
|
|
|
if (!tcf_em_tree_match(skb, &head->ematches, NULL))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
res->classid = classid;
|
|
|
|
res->class = 0;
|
2015-07-15 20:21:41 +08:00
|
|
|
|
2009-05-27 11:47:02 +08:00
|
|
|
return tcf_exts_exec(skb, &head->exts, res);
|
2008-11-08 14:56:00 +08:00
|
|
|
}
|
|
|
|
|
2017-08-05 12:31:43 +08:00
|
|
|
static void *cls_cgroup_get(struct tcf_proto *tp, u32 handle)
|
2008-11-08 14:56:00 +08:00
|
|
|
{
|
2017-08-05 12:31:43 +08:00
|
|
|
return NULL;
|
2008-11-08 14:56:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cls_cgroup_init(struct tcf_proto *tp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
|
|
|
|
[TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
|
|
|
|
};
|
|
|
|
|
2017-11-07 05:47:22 +08:00
|
|
|
static void __cls_cgroup_destroy(struct cls_cgroup_head *head)
|
|
|
|
{
|
|
|
|
tcf_exts_destroy(&head->exts);
|
|
|
|
tcf_em_tree_destroy(&head->ematches);
|
|
|
|
tcf_exts_put_net(&head->exts);
|
|
|
|
kfree(head);
|
|
|
|
}
|
|
|
|
|
2017-10-27 09:24:31 +08:00
|
|
|
static void cls_cgroup_destroy_work(struct work_struct *work)
|
|
|
|
{
|
2018-05-24 06:26:53 +08:00
|
|
|
struct cls_cgroup_head *head = container_of(to_rcu_work(work),
|
2017-10-27 09:24:31 +08:00
|
|
|
struct cls_cgroup_head,
|
2018-05-24 06:26:53 +08:00
|
|
|
rwork);
|
2017-10-27 09:24:31 +08:00
|
|
|
rtnl_lock();
|
2017-11-07 05:47:22 +08:00
|
|
|
__cls_cgroup_destroy(head);
|
2017-10-27 09:24:31 +08:00
|
|
|
rtnl_unlock();
|
|
|
|
}
|
|
|
|
|
2013-01-14 13:15:39 +08:00
|
|
|
static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
|
2012-05-26 03:42:45 +08:00
|
|
|
struct tcf_proto *tp, unsigned long base,
|
2008-11-08 14:56:00 +08:00
|
|
|
u32 handle, struct nlattr **tca,
|
2021-07-30 07:12:14 +08:00
|
|
|
void **arg, u32 flags,
|
2018-01-19 00:20:51 +08:00
|
|
|
struct netlink_ext_ack *extack)
|
2008-11-08 14:56:00 +08:00
|
|
|
{
|
2011-01-20 03:26:56 +08:00
|
|
|
struct nlattr *tb[TCA_CGROUP_MAX + 1];
|
2014-09-13 11:06:26 +08:00
|
|
|
struct cls_cgroup_head *head = rtnl_dereference(tp->root);
|
|
|
|
struct cls_cgroup_head *new;
|
2008-11-08 14:56:00 +08:00
|
|
|
int err;
|
|
|
|
|
2009-06-09 19:03:09 +08:00
|
|
|
if (!tca[TCA_OPTIONS])
|
|
|
|
return -EINVAL;
|
|
|
|
|
2014-09-13 11:06:26 +08:00
|
|
|
if (!head && !handle)
|
|
|
|
return -EINVAL;
|
2008-11-08 14:56:00 +08:00
|
|
|
|
2014-09-13 11:06:26 +08:00
|
|
|
if (head && handle != head->handle)
|
|
|
|
return -ENOENT;
|
2008-11-08 14:56:00 +08:00
|
|
|
|
2014-09-13 11:06:26 +08:00
|
|
|
new = kzalloc(sizeof(*head), GFP_KERNEL);
|
|
|
|
if (!new)
|
|
|
|
return -ENOBUFS;
|
2008-11-08 14:56:00 +08:00
|
|
|
|
2019-02-21 13:37:42 +08:00
|
|
|
err = tcf_exts_init(&new->exts, net, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
|
2016-08-20 03:36:54 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto errout;
|
2014-12-03 01:00:36 +08:00
|
|
|
new->handle = handle;
|
2014-09-13 11:06:26 +08:00
|
|
|
new->tp = tp;
|
netlink: make validation more configurable for future strictness
We currently have two levels of strict validation:
1) liberal (default)
- undefined (type >= max) & NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
- garbage at end of message accepted
2) strict (opt-in)
- NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
Split out parsing strictness into four different options:
* TRAILING - check that there's no trailing data after parsing
attributes (in message or nested)
* MAXTYPE - reject attrs > max known type
* UNSPEC - reject attributes with NLA_UNSPEC policy entries
* STRICT_ATTRS - strictly validate attribute size
The default for future things should be *everything*.
The current *_strict() is a combination of TRAILING and MAXTYPE,
and is renamed to _deprecated_strict().
The current regular parsing has none of this, and is renamed to
*_parse_deprecated().
Additionally it allows us to selectively set one of the new flags
even on old policies. Notably, the UNSPEC flag could be useful in
this case, since it can be arranged (by filling in the policy) to
not be an incompatible userspace ABI change, but would then going
forward prevent forgetting attribute entries. Similar can apply
to the POLICY flag.
We end up with the following renames:
* nla_parse -> nla_parse_deprecated
* nla_parse_strict -> nla_parse_deprecated_strict
* nlmsg_parse -> nlmsg_parse_deprecated
* nlmsg_parse_strict -> nlmsg_parse_deprecated_strict
* nla_parse_nested -> nla_parse_nested_deprecated
* nla_validate_nested -> nla_validate_nested_deprecated
Using spatch, of course:
@@
expression TB, MAX, HEAD, LEN, POL, EXT;
@@
-nla_parse(TB, MAX, HEAD, LEN, POL, EXT)
+nla_parse_deprecated(TB, MAX, HEAD, LEN, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression TB, MAX, NLA, POL, EXT;
@@
-nla_parse_nested(TB, MAX, NLA, POL, EXT)
+nla_parse_nested_deprecated(TB, MAX, NLA, POL, EXT)
@@
expression START, MAX, POL, EXT;
@@
-nla_validate_nested(START, MAX, POL, EXT)
+nla_validate_nested_deprecated(START, MAX, POL, EXT)
@@
expression NLH, HDRLEN, MAX, POL, EXT;
@@
-nlmsg_validate(NLH, HDRLEN, MAX, POL, EXT)
+nlmsg_validate_deprecated(NLH, HDRLEN, MAX, POL, EXT)
For this patch, don't actually add the strict, non-renamed versions
yet so that it breaks compile if I get it wrong.
Also, while at it, make nla_validate and nla_parse go down to a
common __nla_validate_parse() function to avoid code duplication.
Ultimately, this allows us to have very strict validation for every
new caller of nla_parse()/nlmsg_parse() etc as re-introduced in the
next patch, while existing things will continue to work as is.
In effect then, this adds fully strict validation for any new command.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-04-26 20:07:28 +08:00
|
|
|
err = nla_parse_nested_deprecated(tb, TCA_CGROUP_MAX,
|
|
|
|
tca[TCA_OPTIONS], cgroup_policy,
|
|
|
|
NULL);
|
2008-11-08 14:56:00 +08:00
|
|
|
if (err < 0)
|
2014-09-16 14:31:17 +08:00
|
|
|
goto errout;
|
2008-11-08 14:56:00 +08:00
|
|
|
|
2021-07-30 07:12:14 +08:00
|
|
|
err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &new->exts, flags,
|
|
|
|
extack);
|
2008-11-08 14:56:00 +08:00
|
|
|
if (err < 0)
|
2014-09-16 14:31:17 +08:00
|
|
|
goto errout;
|
2008-11-08 14:56:00 +08:00
|
|
|
|
2017-08-04 20:28:57 +08:00
|
|
|
err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &new->ematches);
|
2017-08-04 20:29:11 +08:00
|
|
|
if (err < 0)
|
2014-09-16 14:31:17 +08:00
|
|
|
goto errout;
|
2008-11-08 14:56:00 +08:00
|
|
|
|
2014-09-13 11:06:26 +08:00
|
|
|
rcu_assign_pointer(tp->root, new);
|
2017-11-07 05:47:22 +08:00
|
|
|
if (head) {
|
|
|
|
tcf_exts_get_net(&head->exts);
|
2018-05-24 06:26:53 +08:00
|
|
|
tcf_queue_work(&head->rwork, cls_cgroup_destroy_work);
|
2017-11-07 05:47:22 +08:00
|
|
|
}
|
2008-11-08 14:56:00 +08:00
|
|
|
return 0;
|
2014-09-16 14:31:17 +08:00
|
|
|
errout:
|
2016-08-20 03:36:54 +08:00
|
|
|
tcf_exts_destroy(&new->exts);
|
2014-09-16 14:31:17 +08:00
|
|
|
kfree(new);
|
|
|
|
return err;
|
2008-11-08 14:56:00 +08:00
|
|
|
}
|
|
|
|
|
2019-02-11 16:55:45 +08:00
|
|
|
static void cls_cgroup_destroy(struct tcf_proto *tp, bool rtnl_held,
|
2018-01-25 04:54:13 +08:00
|
|
|
struct netlink_ext_ack *extack)
|
2008-11-08 14:56:00 +08:00
|
|
|
{
|
2014-09-13 11:06:26 +08:00
|
|
|
struct cls_cgroup_head *head = rtnl_dereference(tp->root);
|
2008-11-08 14:56:00 +08:00
|
|
|
|
net, sched: respect rcu grace period on cls destruction
Roi reported a crash in flower where tp->root was NULL in ->classify()
callbacks. Reason is that in ->destroy() tp->root is set to NULL via
RCU_INIT_POINTER(). It's problematic for some of the classifiers, because
this doesn't respect RCU grace period for them, and as a result, still
outstanding readers from tc_classify() will try to blindly dereference
a NULL tp->root.
The tp->root object is strictly private to the classifier implementation
and holds internal data the core such as tc_ctl_tfilter() doesn't know
about. Within some classifiers, such as cls_bpf, cls_basic, etc, tp->root
is only checked for NULL in ->get() callback, but nowhere else. This is
misleading and seemed to be copied from old classifier code that was not
cleaned up properly. For example, d3fa76ee6b4a ("[NET_SCHED]: cls_basic:
fix NULL pointer dereference") moved tp->root initialization into ->init()
routine, where before it was part of ->change(), so ->get() had to deal
with tp->root being NULL back then, so that was indeed a valid case, after
d3fa76ee6b4a, not really anymore. We used to set tp->root to NULL long
ago in ->destroy(), see 47a1a1d4be29 ("pkt_sched: remove unnecessary xchg()
in packet classifiers"); but the NULLifying was reintroduced with the
RCUification, but it's not correct for every classifier implementation.
In the cases that are fixed here with one exception of cls_cgroup, tp->root
object is allocated and initialized inside ->init() callback, which is always
performed at a point in time after we allocate a new tp, which means tp and
thus tp->root was not globally visible in the tp chain yet (see tc_ctl_tfilter()).
Also, on destruction tp->root is strictly kfree_rcu()'ed in ->destroy()
handler, same for the tp which is kfree_rcu()'ed right when we return
from ->destroy() in tcf_destroy(). This means, the head object's lifetime
for such classifiers is always tied to the tp lifetime. The RCU callback
invocation for the two kfree_rcu() could be out of order, but that's fine
since both are independent.
Dropping the RCU_INIT_POINTER(tp->root, NULL) for these classifiers here
means that 1) we don't need a useless NULL check in fast-path and, 2) that
outstanding readers of that tp in tc_classify() can still execute under
respect with RCU grace period as it is actually expected.
Things that haven't been touched here: cls_fw and cls_route. They each
handle tp->root being NULL in ->classify() path for historic reasons, so
their ->destroy() implementation can stay as is. If someone actually
cares, they could get cleaned up at some point to avoid the test in fast
path. cls_u32 doesn't set tp->root to NULL. For cls_rsvp, I just added a
!head should anyone actually be using/testing it, so it at least aligns with
cls_fw and cls_route. For cls_flower we additionally need to defer rhashtable
destruction (to a sleepable context) after RCU grace period as concurrent
readers might still access it. (Note that in this case we need to hold module
reference to keep work callback address intact, since we only wait on module
unload for all call_rcu()s to finish.)
This fixes one race to bring RCU grace period guarantees back. Next step
as worked on by Cong however is to fix 1e052be69d04 ("net_sched: destroy
proto tp when all filters are gone") to get the order of unlinking the tp
in tc_ctl_tfilter() for the RTM_DELTFILTER case right by moving
RCU_INIT_POINTER() before tcf_destroy() and let the notification for
removal be done through the prior ->delete() callback. Both are independant
issues. Once we have that right, we can then clean tp->root up for a number
of classifiers by not making them RCU pointers, which requires a new callback
(->uninit) that is triggered from tp's RCU callback, where we just kfree()
tp->root from there.
Fixes: 1f947bf151e9 ("net: sched: rcu'ify cls_bpf")
Fixes: 9888faefe132 ("net: sched: cls_basic use RCU")
Fixes: 70da9f0bf999 ("net: sched: cls_flow use RCU")
Fixes: 77b9900ef53a ("tc: introduce Flower classifier")
Fixes: bf3994d2ed31 ("net/sched: introduce Match-all classifier")
Fixes: 952313bd6258 ("net: sched: cls_cgroup use RCU")
Reported-by: Roi Dayan <roid@mellanox.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Cc: Cong Wang <xiyou.wangcong@gmail.com>
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: Roi Dayan <roid@mellanox.com>
Cc: Jiri Pirko <jiri@mellanox.com>
Acked-by: John Fastabend <john.r.fastabend@intel.com>
Acked-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-11-27 08:18:01 +08:00
|
|
|
/* Head can still be NULL due to cls_cgroup_init(). */
|
2017-11-07 05:47:22 +08:00
|
|
|
if (head) {
|
|
|
|
if (tcf_exts_get_net(&head->exts))
|
2018-05-24 06:26:53 +08:00
|
|
|
tcf_queue_work(&head->rwork, cls_cgroup_destroy_work);
|
2017-11-07 05:47:22 +08:00
|
|
|
else
|
|
|
|
__cls_cgroup_destroy(head);
|
|
|
|
}
|
2008-11-08 14:56:00 +08:00
|
|
|
}
|
|
|
|
|
2018-01-19 00:20:53 +08:00
|
|
|
static int cls_cgroup_delete(struct tcf_proto *tp, void *arg, bool *last,
|
2019-02-11 16:55:45 +08:00
|
|
|
bool rtnl_held, struct netlink_ext_ack *extack)
|
2008-11-08 14:56:00 +08:00
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2019-02-11 16:55:45 +08:00
|
|
|
static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg,
|
|
|
|
bool rtnl_held)
|
2008-11-08 14:56:00 +08:00
|
|
|
{
|
2014-09-13 11:06:26 +08:00
|
|
|
struct cls_cgroup_head *head = rtnl_dereference(tp->root);
|
2008-11-08 14:56:00 +08:00
|
|
|
|
|
|
|
if (arg->count < arg->skip)
|
|
|
|
goto skip;
|
|
|
|
|
2019-02-15 23:18:44 +08:00
|
|
|
if (!head)
|
|
|
|
return;
|
2017-08-05 12:31:43 +08:00
|
|
|
if (arg->fn(tp, head, arg) < 0) {
|
2008-11-08 14:56:00 +08:00
|
|
|
arg->stop = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
skip:
|
|
|
|
arg->count++;
|
|
|
|
}
|
|
|
|
|
2017-08-05 12:31:43 +08:00
|
|
|
static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, void *fh,
|
2019-02-11 16:55:45 +08:00
|
|
|
struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
|
2008-11-08 14:56:00 +08:00
|
|
|
{
|
2014-09-13 11:06:26 +08:00
|
|
|
struct cls_cgroup_head *head = rtnl_dereference(tp->root);
|
2008-11-08 14:56:00 +08:00
|
|
|
struct nlattr *nest;
|
|
|
|
|
|
|
|
t->tcm_handle = head->handle;
|
|
|
|
|
2019-04-26 17:13:06 +08:00
|
|
|
nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
|
2008-11-08 14:56:00 +08:00
|
|
|
if (nest == NULL)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
2013-12-16 12:15:07 +08:00
|
|
|
if (tcf_exts_dump(skb, &head->exts) < 0 ||
|
2008-11-08 14:56:00 +08:00
|
|
|
tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
nla_nest_end(skb, nest);
|
|
|
|
|
2013-12-16 12:15:07 +08:00
|
|
|
if (tcf_exts_dump_stats(skb, &head->exts) < 0)
|
2008-11-08 14:56:00 +08:00
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
return skb->len;
|
|
|
|
|
|
|
|
nla_put_failure:
|
2014-12-10 05:23:29 +08:00
|
|
|
nla_nest_cancel(skb, nest);
|
2008-11-08 14:56:00 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
|
|
|
|
.kind = "cgroup",
|
|
|
|
.init = cls_cgroup_init,
|
|
|
|
.change = cls_cgroup_change,
|
|
|
|
.classify = cls_cgroup_classify,
|
|
|
|
.destroy = cls_cgroup_destroy,
|
|
|
|
.get = cls_cgroup_get,
|
|
|
|
.delete = cls_cgroup_delete,
|
|
|
|
.walk = cls_cgroup_walk,
|
|
|
|
.dump = cls_cgroup_dump,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init init_cgroup_cls(void)
|
|
|
|
{
|
2013-12-30 01:27:10 +08:00
|
|
|
return register_tcf_proto_ops(&cls_cgroup_ops);
|
2008-11-08 14:56:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit exit_cgroup_cls(void)
|
|
|
|
{
|
|
|
|
unregister_tcf_proto_ops(&cls_cgroup_ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(init_cgroup_cls);
|
|
|
|
module_exit(exit_cgroup_cls);
|
2023-10-27 23:50:44 +08:00
|
|
|
MODULE_DESCRIPTION("TC cgroup classifier");
|
2008-11-08 14:56:00 +08:00
|
|
|
MODULE_LICENSE("GPL");
|