Skip to content

Commit bdc712b

Browse files
committed
inet: Decrease overhead of on-stack inet_cork.
When we fast path datagram sends to avoid locking by putting the inet_cork on the stack we use up lots of space that isn't necessary. This is because inet_cork contains a "struct flowi" which isn't used in these code paths. Split inet_cork to two parts, "inet_cork" and "inet_cork_full". Only the latter of which has the "struct flowi" and is what is stored in inet_sock. Signed-off-by: David S. Miller <davem@davemloft.net> Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
1 parent ad638bd commit bdc712b

5 files changed

Lines changed: 41 additions & 33 deletions

File tree

‎include/net/inet_sock.h‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,21 @@ static inline struct inet_request_sock *inet_rsk(const struct request_sock *sk)
9696

9797
struct inet_cork {
9898
unsigned int flags;
99-
unsigned int fragsize;
99+
__be32 addr;
100100
struct ip_options *opt;
101+
unsigned int fragsize;
101102
struct dst_entry *dst;
102103
int length; /* Total length of all frames */
103-
__be32 addr;
104-
struct flowi fl;
105104
struct page *page;
106105
u32 off;
107106
u8 tx_flags;
108107
};
109108

109+
struct inet_cork_full {
110+
struct inet_cork base;
111+
struct flowi fl;
112+
};
113+
110114
struct ip_mc_socklist;
111115
struct ipv6_pinfo;
112116
struct rtable;
@@ -164,7 +168,7 @@ struct inet_sock {
164168
int mc_index;
165169
__be32 mc_addr;
166170
struct ip_mc_socklist __rcu *mc_list;
167-
struct inet_cork cork;
171+
struct inet_cork_full cork;
168172
};
169173

170174
#define IPCORK_OPT 1 /* ip-options has been held in ipcork.opt */

‎include/net/ip.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ extern struct sk_buff *ip_make_skb(struct sock *sk,
132132

133133
static inline struct sk_buff *ip_finish_skb(struct sock *sk)
134134
{
135-
return __ip_make_skb(sk, &sk->sk_write_queue, &inet_sk(sk)->cork);
135+
return __ip_make_skb(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
136136
}
137137

138138
/* datagram.c */

‎net/ipv4/ip_output.c‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,14 +1096,14 @@ int ip_append_data(struct sock *sk,
10961096
return 0;
10971097

10981098
if (skb_queue_empty(&sk->sk_write_queue)) {
1099-
err = ip_setup_cork(sk, &inet->cork, ipc, rtp);
1099+
err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
11001100
if (err)
11011101
return err;
11021102
} else {
11031103
transhdrlen = 0;
11041104
}
11051105

1106-
return __ip_append_data(sk, &sk->sk_write_queue, &inet->cork, getfrag,
1106+
return __ip_append_data(sk, &sk->sk_write_queue, &inet->cork.base, getfrag,
11071107
from, length, transhdrlen, flags);
11081108
}
11091109

@@ -1114,6 +1114,7 @@ ssize_t ip_append_page(struct sock *sk, struct page *page,
11141114
struct sk_buff *skb;
11151115
struct rtable *rt;
11161116
struct ip_options *opt = NULL;
1117+
struct inet_cork *cork;
11171118
int hh_len;
11181119
int mtu;
11191120
int len;
@@ -1129,28 +1130,29 @@ ssize_t ip_append_page(struct sock *sk, struct page *page,
11291130
if (skb_queue_empty(&sk->sk_write_queue))
11301131
return -EINVAL;
11311132

1132-
rt = (struct rtable *)inet->cork.dst;
1133-
if (inet->cork.flags & IPCORK_OPT)
1134-
opt = inet->cork.opt;
1133+
cork = &inet->cork.base;
1134+
rt = (struct rtable *)cork->dst;
1135+
if (cork->flags & IPCORK_OPT)
1136+
opt = cork->opt;
11351137

11361138
if (!(rt->dst.dev->features&NETIF_F_SG))
11371139
return -EOPNOTSUPP;
11381140

11391141
hh_len = LL_RESERVED_SPACE(rt->dst.dev);
1140-
mtu = inet->cork.fragsize;
1142+
mtu = cork->fragsize;
11411143

11421144
fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
11431145
maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
11441146

1145-
if (inet->cork.length + size > 0xFFFF - fragheaderlen) {
1147+
if (cork->length + size > 0xFFFF - fragheaderlen) {
11461148
ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->inet_dport, mtu);
11471149
return -EMSGSIZE;
11481150
}
11491151

11501152
if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
11511153
return -EINVAL;
11521154

1153-
inet->cork.length += size;
1155+
cork->length += size;
11541156
if ((size + skb->len > mtu) &&
11551157
(sk->sk_protocol == IPPROTO_UDP) &&
11561158
(rt->dst.dev->features & NETIF_F_UFO)) {
@@ -1245,7 +1247,7 @@ ssize_t ip_append_page(struct sock *sk, struct page *page,
12451247
return 0;
12461248

12471249
error:
1248-
inet->cork.length -= size;
1250+
cork->length -= size;
12491251
IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
12501252
return err;
12511253
}
@@ -1396,7 +1398,7 @@ static void __ip_flush_pending_frames(struct sock *sk,
13961398

13971399
void ip_flush_pending_frames(struct sock *sk)
13981400
{
1399-
__ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork);
1401+
__ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
14001402
}
14011403

14021404
struct sk_buff *ip_make_skb(struct sock *sk,

‎net/ipv6/ip6_output.c‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,7 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
11501150
{
11511151
struct inet_sock *inet = inet_sk(sk);
11521152
struct ipv6_pinfo *np = inet6_sk(sk);
1153+
struct inet_cork *cork;
11531154
struct sk_buff *skb;
11541155
unsigned int maxfraglen, fragheaderlen;
11551156
int exthdrlen;
@@ -1163,6 +1164,7 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
11631164

11641165
if (flags&MSG_PROBE)
11651166
return 0;
1167+
cork = &inet->cork.base;
11661168
if (skb_queue_empty(&sk->sk_write_queue)) {
11671169
/*
11681170
* setup for corking
@@ -1202,7 +1204,7 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
12021204
/* need source address above miyazawa*/
12031205
}
12041206
dst_hold(&rt->dst);
1205-
inet->cork.dst = &rt->dst;
1207+
cork->dst = &rt->dst;
12061208
inet->cork.fl.u.ip6 = *fl6;
12071209
np->cork.hop_limit = hlimit;
12081210
np->cork.tclass = tclass;
@@ -1212,23 +1214,23 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
12121214
if (np->frag_size)
12131215
mtu = np->frag_size;
12141216
}
1215-
inet->cork.fragsize = mtu;
1217+
cork->fragsize = mtu;
12161218
if (dst_allfrag(rt->dst.path))
1217-
inet->cork.flags |= IPCORK_ALLFRAG;
1218-
inet->cork.length = 0;
1219+
cork->flags |= IPCORK_ALLFRAG;
1220+
cork->length = 0;
12191221
sk->sk_sndmsg_page = NULL;
12201222
sk->sk_sndmsg_off = 0;
12211223
exthdrlen = rt->dst.header_len + (opt ? opt->opt_flen : 0) -
12221224
rt->rt6i_nfheader_len;
12231225
length += exthdrlen;
12241226
transhdrlen += exthdrlen;
12251227
} else {
1226-
rt = (struct rt6_info *)inet->cork.dst;
1228+
rt = (struct rt6_info *)cork->dst;
12271229
fl6 = &inet->cork.fl.u.ip6;
12281230
opt = np->cork.opt;
12291231
transhdrlen = 0;
12301232
exthdrlen = 0;
1231-
mtu = inet->cork.fragsize;
1233+
mtu = cork->fragsize;
12321234
}
12331235

12341236
hh_len = LL_RESERVED_SPACE(rt->dst.dev);
@@ -1238,7 +1240,7 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
12381240
maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
12391241

12401242
if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
1241-
if (inet->cork.length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
1243+
if (cork->length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
12421244
ipv6_local_error(sk, EMSGSIZE, fl6, mtu-exthdrlen);
12431245
return -EMSGSIZE;
12441246
}
@@ -1267,7 +1269,7 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
12671269
* --yoshfuji
12681270
*/
12691271

1270-
inet->cork.length += length;
1272+
cork->length += length;
12711273
if (length > mtu) {
12721274
int proto = sk->sk_protocol;
12731275
if (dontfrag && (proto == IPPROTO_UDP || proto == IPPROTO_RAW)){
@@ -1292,7 +1294,7 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
12921294

12931295
while (length > 0) {
12941296
/* Check if the remaining data fits into current packet. */
1295-
copy = (inet->cork.length <= mtu && !(inet->cork.flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
1297+
copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
12961298
if (copy < length)
12971299
copy = maxfraglen - skb->len;
12981300

@@ -1317,7 +1319,7 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
13171319
* we know we need more fragment(s).
13181320
*/
13191321
datalen = length + fraggap;
1320-
if (datalen > (inet->cork.length <= mtu && !(inet->cork.flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
1322+
if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
13211323
datalen = maxfraglen - fragheaderlen;
13221324

13231325
fraglen = datalen + fragheaderlen;
@@ -1481,7 +1483,7 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
14811483
}
14821484
return 0;
14831485
error:
1484-
inet->cork.length -= length;
1486+
cork->length -= length;
14851487
IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
14861488
return err;
14871489
}
@@ -1497,10 +1499,10 @@ static void ip6_cork_release(struct inet_sock *inet, struct ipv6_pinfo *np)
14971499
np->cork.opt = NULL;
14981500
}
14991501

1500-
if (inet->cork.dst) {
1501-
dst_release(inet->cork.dst);
1502-
inet->cork.dst = NULL;
1503-
inet->cork.flags &= ~IPCORK_ALLFRAG;
1502+
if (inet->cork.base.dst) {
1503+
dst_release(inet->cork.base.dst);
1504+
inet->cork.base.dst = NULL;
1505+
inet->cork.base.flags &= ~IPCORK_ALLFRAG;
15041506
}
15051507
memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
15061508
}
@@ -1515,7 +1517,7 @@ int ip6_push_pending_frames(struct sock *sk)
15151517
struct net *net = sock_net(sk);
15161518
struct ipv6hdr *hdr;
15171519
struct ipv6_txoptions *opt = np->cork.opt;
1518-
struct rt6_info *rt = (struct rt6_info *)inet->cork.dst;
1520+
struct rt6_info *rt = (struct rt6_info *)inet->cork.base.dst;
15191521
struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
15201522
unsigned char proto = fl6->flowi6_proto;
15211523
int err = 0;

‎net/ipv6/raw.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
542542
goto out;
543543

544544
offset = rp->offset;
545-
total_len = inet_sk(sk)->cork.length - (skb_network_header(skb) -
546-
skb->data);
545+
total_len = inet_sk(sk)->cork.base.length - (skb_network_header(skb) -
546+
skb->data);
547547
if (offset >= total_len - 1) {
548548
err = -EINVAL;
549549
ip6_flush_pending_frames(sk);

0 commit comments

Comments
 (0)