Skip to content

Commit 7fee226

Browse files
Eric Dumazetdavem330
authored andcommitted
net: add a noref bit on skb dst
Use low order bit of skb->_skb_dst to tell dst is not refcounted. Change _skb_dst to _skb_refdst to make sure all uses are catched. skb_dst() returns the dst, regardless of noref bit set or not, but with a lockdep check to make sure a noref dst is not given if current user is not rcu protected. New skb_dst_set_noref() helper to set an notrefcounted dst on a skb. (with lockdep check) skb_dst_drop() drops a reference only if skb dst was refcounted. skb_dst_force() helper is used to force a refcount on dst, when skb is queued and not anymore RCU protected. Use skb_dst_force() in __sk_add_backlog(), __dev_xmit_skb() if !IFF_XMIT_DST_RELEASE or skb enqueued on qdisc queue, in sock_queue_rcv_skb(), in __nf_queue(). Use skb_dst_force() in dev_requeue_skb(). Note: dst_use_noref() still dirties dst, we might transform it later to do one dirtying per jiffies. Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ebda37c commit 7fee226

12 files changed

Lines changed: 134 additions & 25 deletions

File tree

‎include/linux/skbuff.h‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ typedef unsigned char *sk_buff_data_t;
264264
* @transport_header: Transport layer header
265265
* @network_header: Network layer header
266266
* @mac_header: Link layer header
267-
* @_skb_dst: destination entry
267+
* @_skb_refdst: destination entry (with norefcount bit)
268268
* @sp: the security path, used for xfrm
269269
* @cb: Control buffer. Free for use by every layer. Put private vars here
270270
* @len: Length of actual data
@@ -328,7 +328,7 @@ struct sk_buff {
328328
*/
329329
char cb[48] __aligned(8);
330330

331-
unsigned long _skb_dst;
331+
unsigned long _skb_refdst;
332332
#ifdef CONFIG_XFRM
333333
struct sec_path *sp;
334334
#endif
@@ -419,14 +419,64 @@ struct sk_buff {
419419

420420
#include <asm/system.h>
421421

422+
/*
423+
* skb might have a dst pointer attached, refcounted or not.
424+
* _skb_refdst low order bit is set if refcount was _not_ taken
425+
*/
426+
#define SKB_DST_NOREF 1UL
427+
#define SKB_DST_PTRMASK ~(SKB_DST_NOREF)
428+
429+
/**
430+
* skb_dst - returns skb dst_entry
431+
* @skb: buffer
432+
*
433+
* Returns skb dst_entry, regardless of reference taken or not.
434+
*/
422435
static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
423436
{
424-
return (struct dst_entry *)skb->_skb_dst;
437+
/* If refdst was not refcounted, check we still are in a
438+
* rcu_read_lock section
439+
*/
440+
WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) &&
441+
!rcu_read_lock_held() &&
442+
!rcu_read_lock_bh_held());
443+
return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
425444
}
426445

446+
/**
447+
* skb_dst_set - sets skb dst
448+
* @skb: buffer
449+
* @dst: dst entry
450+
*
451+
* Sets skb dst, assuming a reference was taken on dst and should
452+
* be released by skb_dst_drop()
453+
*/
427454
static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
428455
{
429-
skb->_skb_dst = (unsigned long)dst;
456+
skb->_skb_refdst = (unsigned long)dst;
457+
}
458+
459+
/**
460+
* skb_dst_set_noref - sets skb dst, without a reference
461+
* @skb: buffer
462+
* @dst: dst entry
463+
*
464+
* Sets skb dst, assuming a reference was not taken on dst
465+
* skb_dst_drop() should not dst_release() this dst
466+
*/
467+
static inline void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst)
468+
{
469+
WARN_ON(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
470+
skb->_skb_refdst = (unsigned long)dst | SKB_DST_NOREF;
471+
}
472+
473+
/**
474+
* skb_dst_is_noref - Test if skb dst isnt refcounted
475+
* @skb: buffer
476+
*/
477+
static inline bool skb_dst_is_noref(const struct sk_buff *skb)
478+
{
479+
return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
430480
}
431481

432482
static inline struct rtable *skb_rtable(const struct sk_buff *skb)

‎include/net/dst.h‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ static inline void dst_use(struct dst_entry *dst, unsigned long time)
168168
dst->lastuse = time;
169169
}
170170

171+
static inline void dst_use_noref(struct dst_entry *dst, unsigned long time)
172+
{
173+
dst->__use++;
174+
dst->lastuse = time;
175+
}
176+
171177
static inline
172178
struct dst_entry * dst_clone(struct dst_entry * dst)
173179
{
@@ -177,11 +183,47 @@ struct dst_entry * dst_clone(struct dst_entry * dst)
177183
}
178184

179185
extern void dst_release(struct dst_entry *dst);
186+
187+
static inline void refdst_drop(unsigned long refdst)
188+
{
189+
if (!(refdst & SKB_DST_NOREF))
190+
dst_release((struct dst_entry *)(refdst & SKB_DST_PTRMASK));
191+
}
192+
193+
/**
194+
* skb_dst_drop - drops skb dst
195+
* @skb: buffer
196+
*
197+
* Drops dst reference count if a reference was taken.
198+
*/
180199
static inline void skb_dst_drop(struct sk_buff *skb)
181200
{
182-
if (skb->_skb_dst)
183-
dst_release(skb_dst(skb));
184-
skb->_skb_dst = 0UL;
201+
if (skb->_skb_refdst) {
202+
refdst_drop(skb->_skb_refdst);
203+
skb->_skb_refdst = 0UL;
204+
}
205+
}
206+
207+
static inline void skb_dst_copy(struct sk_buff *nskb, const struct sk_buff *oskb)
208+
{
209+
nskb->_skb_refdst = oskb->_skb_refdst;
210+
if (!(nskb->_skb_refdst & SKB_DST_NOREF))
211+
dst_clone(skb_dst(nskb));
212+
}
213+
214+
/**
215+
* skb_dst_force - makes sure skb dst is refcounted
216+
* @skb: buffer
217+
*
218+
* If dst is not yet refcounted, let's do it
219+
*/
220+
static inline void skb_dst_force(struct sk_buff *skb)
221+
{
222+
if (skb_dst_is_noref(skb)) {
223+
WARN_ON(!rcu_read_lock_held());
224+
skb->_skb_refdst &= ~SKB_DST_NOREF;
225+
dst_clone(skb_dst(skb));
226+
}
185227
}
186228

187229
/* Children define the path of the packet through the

‎include/net/sock.h‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,15 @@ static inline int sk_stream_memory_free(struct sock *sk)
600600
/* OOB backlog add */
601601
static inline void __sk_add_backlog(struct sock *sk, struct sk_buff *skb)
602602
{
603-
if (!sk->sk_backlog.tail) {
604-
sk->sk_backlog.head = sk->sk_backlog.tail = skb;
605-
} else {
603+
/* dont let skb dst not refcounted, we are going to leave rcu lock */
604+
skb_dst_force(skb);
605+
606+
if (!sk->sk_backlog.tail)
607+
sk->sk_backlog.head = skb;
608+
else
606609
sk->sk_backlog.tail->next = skb;
607-
sk->sk_backlog.tail = skb;
608-
}
610+
611+
sk->sk_backlog.tail = skb;
609612
skb->next = NULL;
610613
}
611614

‎net/core/dev.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,8 @@ static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
20522052
* waiting to be sent out; and the qdisc is not running -
20532053
* xmit the skb directly.
20542054
*/
2055+
if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE))
2056+
skb_dst_force(skb);
20552057
__qdisc_update_bstats(q, skb->len);
20562058
if (sch_direct_xmit(skb, q, dev, txq, root_lock))
20572059
__qdisc_run(q);
@@ -2060,6 +2062,7 @@ static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
20602062

20612063
rc = NET_XMIT_SUCCESS;
20622064
} else {
2065+
skb_dst_force(skb);
20632066
rc = qdisc_enqueue_root(skb, q);
20642067
qdisc_run(q);
20652068
}

‎net/core/skbuff.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
520520
new->transport_header = old->transport_header;
521521
new->network_header = old->network_header;
522522
new->mac_header = old->mac_header;
523-
skb_dst_set(new, dst_clone(skb_dst(old)));
523+
skb_dst_copy(new, old);
524524
new->rxhash = old->rxhash;
525525
#ifdef CONFIG_XFRM
526526
new->sp = secpath_get(old->sp);

‎net/core/sock.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
307307
*/
308308
skb_len = skb->len;
309309

310+
/* we escape from rcu protected region, make sure we dont leak
311+
* a norefcounted dst
312+
*/
313+
skb_dst_force(skb);
314+
310315
spin_lock_irqsave(&list->lock, flags);
311316
skb->dropcount = atomic_read(&sk->sk_drops);
312317
__skb_queue_tail(list, skb);
@@ -1536,6 +1541,7 @@ static void __release_sock(struct sock *sk)
15361541
do {
15371542
struct sk_buff *next = skb->next;
15381543

1544+
WARN_ON_ONCE(skb_dst_is_noref(skb));
15391545
skb->next = NULL;
15401546
sk_backlog_rcv(sk, skb);
15411547

‎net/ipv4/icmp.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,20 +587,20 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
587587
err = __ip_route_output_key(net, &rt2, &fl);
588588
else {
589589
struct flowi fl2 = {};
590-
struct dst_entry *odst;
590+
unsigned long orefdst;
591591

592592
fl2.fl4_dst = fl.fl4_src;
593593
if (ip_route_output_key(net, &rt2, &fl2))
594594
goto relookup_failed;
595595

596596
/* Ugh! */
597-
odst = skb_dst(skb_in);
597+
orefdst = skb_in->_skb_refdst; /* save old refdst */
598598
err = ip_route_input(skb_in, fl.fl4_dst, fl.fl4_src,
599599
RT_TOS(tos), rt2->u.dst.dev);
600600

601601
dst_release(&rt2->u.dst);
602602
rt2 = skb_rtable(skb_in);
603-
skb_dst_set(skb_in, odst);
603+
skb_in->_skb_refdst = orefdst; /* restore old refdst */
604604
}
605605

606606
if (err)

‎net/ipv4/ip_options.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ int ip_options_rcv_srr(struct sk_buff *skb)
601601
unsigned char *optptr = skb_network_header(skb) + opt->srr;
602602
struct rtable *rt = skb_rtable(skb);
603603
struct rtable *rt2;
604+
unsigned long orefdst;
604605
int err;
605606

606607
if (!opt->srr)
@@ -624,16 +625,16 @@ int ip_options_rcv_srr(struct sk_buff *skb)
624625
}
625626
memcpy(&nexthop, &optptr[srrptr-1], 4);
626627

627-
rt = skb_rtable(skb);
628+
orefdst = skb->_skb_refdst;
628629
skb_dst_set(skb, NULL);
629630
err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev);
630631
rt2 = skb_rtable(skb);
631632
if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
632-
ip_rt_put(rt2);
633-
skb_dst_set(skb, &rt->u.dst);
633+
skb_dst_drop(skb);
634+
skb->_skb_refdst = orefdst;
634635
return -EINVAL;
635636
}
636-
ip_rt_put(rt);
637+
refdst_drop(orefdst);
637638
if (rt2->rt_type != RTN_LOCAL)
638639
break;
639640
/* Superfast 8) loopback forward */

‎net/ipv4/netfilter.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int ip_route_me_harder(struct sk_buff *skb, unsigned addr_type)
1717
const struct iphdr *iph = ip_hdr(skb);
1818
struct rtable *rt;
1919
struct flowi fl = {};
20-
struct dst_entry *odst;
20+
unsigned long orefdst;
2121
unsigned int hh_len;
2222
unsigned int type;
2323

@@ -51,14 +51,14 @@ int ip_route_me_harder(struct sk_buff *skb, unsigned addr_type)
5151
if (ip_route_output_key(net, &rt, &fl) != 0)
5252
return -1;
5353

54-
odst = skb_dst(skb);
54+
orefdst = skb->_skb_refdst;
5555
if (ip_route_input(skb, iph->daddr, iph->saddr,
5656
RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
5757
dst_release(&rt->u.dst);
5858
return -1;
5959
}
6060
dst_release(&rt->u.dst);
61-
dst_release(odst);
61+
refdst_drop(orefdst);
6262
}
6363

6464
if (skb_dst(skb)->error)

‎net/ipv4/route.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3033,7 +3033,7 @@ int ip_rt_dump(struct sk_buff *skb, struct netlink_callback *cb)
30333033
continue;
30343034
if (rt_is_expired(rt))
30353035
continue;
3036-
skb_dst_set(skb, dst_clone(&rt->u.dst));
3036+
skb_dst_set_noref(skb, &rt->u.dst);
30373037
if (rt_fill_info(net, skb, NETLINK_CB(cb->skb).pid,
30383038
cb->nlh->nlmsg_seq, RTM_NEWROUTE,
30393039
1, NLM_F_MULTI) <= 0) {

0 commit comments

Comments
 (0)