]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/3.18.137/netlink-trim-skb-to-alloc-size-to-avoid-msg_trunc.patch
4.9-stable patches
[thirdparty/kernel/stable-queue.git] / releases / 3.18.137 / netlink-trim-skb-to-alloc-size-to-avoid-msg_trunc.patch
1 From db65a3aaf29ecce2e34271d52e8d2336b97bd9fe Mon Sep 17 00:00:00 2001
2 From: "Arad, Ronen" <ronen.arad@intel.com>
3 Date: Thu, 15 Oct 2015 01:55:17 -0700
4 Subject: netlink: Trim skb to alloc size to avoid MSG_TRUNC
5
6 From: Arad, Ronen <ronen.arad@intel.com>
7
8 commit db65a3aaf29ecce2e34271d52e8d2336b97bd9fe upstream.
9
10 netlink_dump() allocates skb based on the calculated min_dump_alloc or
11 a per socket max_recvmsg_len.
12 min_alloc_size is maximum space required for any single netdev
13 attributes as calculated by rtnl_calcit().
14 max_recvmsg_len tracks the user provided buffer to netlink_recvmsg.
15 It is capped at 16KiB.
16 The intention is to avoid small allocations and to minimize the number
17 of calls required to obtain dump information for all net devices.
18
19 netlink_dump packs as many small messages as could fit within an skb
20 that was sized for the largest single netdev information. The actual
21 space available within an skb is larger than what is requested. It could
22 be much larger and up to near 2x with align to next power of 2 approach.
23
24 Allowing netlink_dump to use all the space available within the
25 allocated skb increases the buffer size a user has to provide to avoid
26 truncaion (i.e. MSG_TRUNG flag set).
27
28 It was observed that with many VLANs configured on at least one netdev,
29 a larger buffer of near 64KiB was necessary to avoid "Message truncated"
30 error in "ip link" or "bridge [-c[ompressvlans]] vlan show" when
31 min_alloc_size was only little over 32KiB.
32
33 This patch trims skb to allocated size in order to allow the user to
34 avoid truncation with more reasonable buffer size.
35
36 Signed-off-by: Ronen Arad <ronen.arad@intel.com>
37 Signed-off-by: David S. Miller <davem@davemloft.net>
38 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
39 Signed-off-by: Mark Salyzyn <salyzyn@android.com>
40
41 ---
42 net/netlink/af_netlink.c | 34 ++++++++++++++++++++++------------
43 1 file changed, 22 insertions(+), 12 deletions(-)
44
45 --- a/net/netlink/af_netlink.c
46 +++ b/net/netlink/af_netlink.c
47 @@ -1977,6 +1977,7 @@ static int netlink_dump(struct sock *sk)
48 struct nlmsghdr *nlh;
49 struct module *module;
50 int err = -ENOBUFS;
51 + int alloc_min_size;
52 int alloc_size;
53
54 mutex_lock(nlk->cb_mutex);
55 @@ -1985,9 +1986,6 @@ static int netlink_dump(struct sock *sk)
56 goto errout_skb;
57 }
58
59 - cb = &nlk->cb;
60 - alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
61 -
62 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
63 goto errout_skb;
64
65 @@ -1996,22 +1994,34 @@ static int netlink_dump(struct sock *sk)
66 * to reduce number of system calls on dump operations, if user
67 * ever provided a big enough buffer.
68 */
69 - if (alloc_size < nlk->max_recvmsg_len) {
70 - skb = netlink_alloc_skb(sk,
71 - nlk->max_recvmsg_len,
72 - nlk->portid,
73 + cb = &nlk->cb;
74 + alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
75 +
76 + if (alloc_min_size < nlk->max_recvmsg_len) {
77 + alloc_size = nlk->max_recvmsg_len;
78 + skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
79 (GFP_KERNEL & ~__GFP_WAIT) |
80 __GFP_NOWARN | __GFP_NORETRY);
81 - /* available room should be exact amount to avoid MSG_TRUNC */
82 - if (skb)
83 - skb_reserve(skb, skb_tailroom(skb) -
84 - nlk->max_recvmsg_len);
85 }
86 - if (!skb)
87 + if (!skb) {
88 + alloc_size = alloc_min_size;
89 skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
90 (GFP_KERNEL & ~__GFP_WAIT));
91 + }
92 if (!skb)
93 goto errout_skb;
94 +
95 + /* Trim skb to allocated size. User is expected to provide buffer as
96 + * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at
97 + * netlink_recvmsg())). dump will pack as many smaller messages as
98 + * could fit within the allocated skb. skb is typically allocated
99 + * with larger space than required (could be as much as near 2x the
100 + * requested size with align to next power of 2 approach). Allowing
101 + * dump to use the excess space makes it difficult for a user to have a
102 + * reasonable static buffer based on the expected largest dump of a
103 + * single netdev. The outcome is MSG_TRUNC error.
104 + */
105 + skb_reserve(skb, skb_tailroom(skb) - alloc_size);
106 netlink_skb_set_owner_r(skb, sk);
107
108 if (nlk->dump_done_errno > 0)