@@ -60,30 +60,19 @@ static struct cdev macvtap_cdev;
6060
6161/*
6262 * RCU usage:
63- * The macvtap_queue is referenced both from the chardev struct file
64- * and from the struct macvlan_dev using rcu_read_lock.
63+ * The macvtap_queue and the macvlan_dev are loosely coupled, the
64+ * pointers from one to the other can only be read while rcu_read_lock
65+ * or macvtap_lock is held.
6566 *
66- * We never actually update the contents of a macvtap_queue atomically
67- * with RCU but it is used for race-free destruction of a queue when
68- * either the file or the macvlan_dev goes away. Pointers back to
69- * the dev and the file are implicitly valid as long as the queue
70- * exists.
67+ * Both the file and the macvlan_dev hold a reference on the macvtap_queue
68+ * through sock_hold(&q->sk). When the macvlan_dev goes away first,
69+ * q->vlan becomes inaccessible. When the files gets closed,
70+ * macvtap_get_queue() fails.
7171 *
72- * The callbacks from macvlan are always done with rcu_read_lock held
73- * already. For calls from file_operations, we use the rcu_read_lock_bh
74- * to get a reference count on the socket and the device.
75- *
76- * When destroying a queue, we remove the pointers from the file and
77- * from the dev and then synchronize_rcu to make sure no thread is
78- * still using the queue. There may still be references to the struct
79- * sock inside of the queue from outbound SKBs, but these never
80- * reference back to the file or the dev. The data structure is freed
81- * through __sk_free when both our references and any pending SKBs
82- * are gone.
83- *
84- * macvtap_lock is only used to prevent multiple concurrent open()
85- * calls to assign a new vlan->tap pointer. It could be moved into
86- * the macvlan_dev itself but is extremely rarely used.
72+ * There may still be references to the struct sock inside of the
73+ * queue from outbound SKBs, but these never reference back to the
74+ * file or the dev. The data structure is freed through __sk_free
75+ * when both our references and any pending SKBs are gone.
8776 */
8877static DEFINE_SPINLOCK (macvtap_lock );
8978
@@ -101,40 +90,38 @@ static int macvtap_set_queue(struct net_device *dev, struct file *file,
10190 goto out ;
10291
10392 err = 0 ;
104- q -> vlan = vlan ;
93+ rcu_assign_pointer ( q -> vlan , vlan ) ;
10594 rcu_assign_pointer (vlan -> tap , q );
95+ sock_hold (& q -> sk );
10696
10797 q -> file = file ;
108- rcu_assign_pointer ( file -> private_data , q ) ;
98+ file -> private_data = q ;
10999
110100out :
111101 spin_unlock (& macvtap_lock );
112102 return err ;
113103}
114104
115105/*
116- * We must destroy each queue exactly once, when either
117- * the netdev or the file go away.
106+ * The file owning the queue got closed, give up both
107+ * the reference that the files holds as well as the
108+ * one from the macvlan_dev if that still exists.
118109 *
119110 * Using the spinlock makes sure that we don't get
120111 * to the queue again after destroying it.
121- *
122- * synchronize_rcu serializes with the packet flow
123- * that uses rcu_read_lock.
124112 */
125- static void macvtap_del_queue (struct macvtap_queue * * qp )
113+ static void macvtap_put_queue (struct macvtap_queue * q )
126114{
127- struct macvtap_queue * q ;
115+ struct macvlan_dev * vlan ;
128116
129117 spin_lock (& macvtap_lock );
130- q = rcu_dereference (* qp );
131- if (!q ) {
132- spin_unlock (& macvtap_lock );
133- return ;
118+ vlan = rcu_dereference (q -> vlan );
119+ if (vlan ) {
120+ rcu_assign_pointer (vlan -> tap , NULL );
121+ rcu_assign_pointer (q -> vlan , NULL );
122+ sock_put (& q -> sk );
134123 }
135124
136- rcu_assign_pointer (q -> vlan -> tap , NULL );
137- rcu_assign_pointer (q -> file -> private_data , NULL );
138125 spin_unlock (& macvtap_lock );
139126
140127 synchronize_rcu ();
@@ -152,29 +139,29 @@ static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
152139 return rcu_dereference (vlan -> tap );
153140}
154141
142+ /*
143+ * The net_device is going away, give up the reference
144+ * that it holds on the queue (all the queues one day)
145+ * and safely set the pointer from the queues to NULL.
146+ */
155147static void macvtap_del_queues (struct net_device * dev )
156148{
157149 struct macvlan_dev * vlan = netdev_priv (dev );
158- macvtap_del_queue (& vlan -> tap );
159- }
160-
161- static inline struct macvtap_queue * macvtap_file_get_queue (struct file * file )
162- {
163150 struct macvtap_queue * q ;
164- rcu_read_lock_bh ();
165- q = rcu_dereference (file -> private_data );
166- if (q ) {
167- sock_hold (& q -> sk );
168- dev_hold (q -> vlan -> dev );
151+
152+ spin_lock (& macvtap_lock );
153+ q = rcu_dereference (vlan -> tap );
154+ if (!q ) {
155+ spin_unlock (& macvtap_lock );
156+ return ;
169157 }
170- rcu_read_unlock_bh ();
171- return q ;
172- }
173158
174- static inline void macvtap_file_put_queue (struct macvtap_queue * q )
175- {
159+ rcu_assign_pointer (vlan -> tap , NULL );
160+ rcu_assign_pointer (q -> vlan , NULL );
161+ spin_unlock (& macvtap_lock );
162+
163+ synchronize_rcu ();
176164 sock_put (& q -> sk );
177- dev_put (q -> vlan -> dev );
178165}
179166
180167/*
@@ -284,7 +271,6 @@ static int macvtap_open(struct inode *inode, struct file *file)
284271 q -> sock .type = SOCK_RAW ;
285272 q -> sock .state = SS_CONNECTED ;
286273 sock_init_data (& q -> sock , & q -> sk );
287- q -> sk .sk_allocation = GFP_ATOMIC ; /* for now */
288274 q -> sk .sk_write_space = macvtap_sock_write_space ;
289275
290276 err = macvtap_set_queue (dev , file , q );
@@ -300,13 +286,14 @@ static int macvtap_open(struct inode *inode, struct file *file)
300286
301287static int macvtap_release (struct inode * inode , struct file * file )
302288{
303- macvtap_del_queue ((struct macvtap_queue * * )& file -> private_data );
289+ struct macvtap_queue * q = file -> private_data ;
290+ macvtap_put_queue (q );
304291 return 0 ;
305292}
306293
307294static unsigned int macvtap_poll (struct file * file , poll_table * wait )
308295{
309- struct macvtap_queue * q = macvtap_file_get_queue ( file ) ;
296+ struct macvtap_queue * q = file -> private_data ;
310297 unsigned int mask = POLLERR ;
311298
312299 if (!q )
@@ -323,7 +310,6 @@ static unsigned int macvtap_poll(struct file *file, poll_table * wait)
323310 sock_writeable (& q -> sk )))
324311 mask |= POLLOUT | POLLWRNORM ;
325312
326- macvtap_file_put_queue (q );
327313out :
328314 return mask ;
329315}
@@ -334,49 +320,56 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q,
334320 int noblock )
335321{
336322 struct sk_buff * skb ;
323+ struct macvlan_dev * vlan ;
337324 size_t len = count ;
338325 int err ;
339326
340327 if (unlikely (len < ETH_HLEN ))
341328 return - EINVAL ;
342329
343330 skb = sock_alloc_send_skb (& q -> sk , NET_IP_ALIGN + len , noblock , & err );
344-
345- if (!skb ) {
346- macvlan_count_rx (q -> vlan , 0 , false, false);
347- return err ;
348- }
331+ if (!skb )
332+ goto err ;
349333
350334 skb_reserve (skb , NET_IP_ALIGN );
351335 skb_put (skb , count );
352336
353- if (skb_copy_datagram_from_iovec (skb , 0 , iv , 0 , len )) {
354- macvlan_count_rx (q -> vlan , 0 , false, false);
355- kfree_skb (skb );
356- return - EFAULT ;
357- }
337+ err = skb_copy_datagram_from_iovec (skb , 0 , iv , 0 , len );
338+ if (err )
339+ goto err ;
358340
359341 skb_set_network_header (skb , ETH_HLEN );
360-
361- macvlan_start_xmit (skb , q -> vlan -> dev );
342+ rcu_read_lock_bh ();
343+ vlan = rcu_dereference (q -> vlan );
344+ if (vlan )
345+ macvlan_start_xmit (skb , vlan -> dev );
346+ else
347+ kfree_skb (skb );
348+ rcu_read_unlock_bh ();
362349
363350 return count ;
351+
352+ err :
353+ rcu_read_lock_bh ();
354+ vlan = rcu_dereference (q -> vlan );
355+ if (vlan )
356+ macvlan_count_rx (q -> vlan , 0 , false, false);
357+ rcu_read_unlock_bh ();
358+
359+ kfree_skb (skb );
360+
361+ return err ;
364362}
365363
366364static ssize_t macvtap_aio_write (struct kiocb * iocb , const struct iovec * iv ,
367365 unsigned long count , loff_t pos )
368366{
369367 struct file * file = iocb -> ki_filp ;
370368 ssize_t result = - ENOLINK ;
371- struct macvtap_queue * q = macvtap_file_get_queue (file );
372-
373- if (!q )
374- goto out ;
369+ struct macvtap_queue * q = file -> private_data ;
375370
376371 result = macvtap_get_user (q , iv , iov_length (iv , count ),
377372 file -> f_flags & O_NONBLOCK );
378- macvtap_file_put_queue (q );
379- out :
380373 return result ;
381374}
382375
@@ -385,14 +378,17 @@ static ssize_t macvtap_put_user(struct macvtap_queue *q,
385378 const struct sk_buff * skb ,
386379 const struct iovec * iv , int len )
387380{
388- struct macvlan_dev * vlan = q -> vlan ;
381+ struct macvlan_dev * vlan ;
389382 int ret ;
390383
391384 len = min_t (int , skb -> len , len );
392385
393386 ret = skb_copy_datagram_const_iovec (skb , 0 , iv , 0 , len );
394387
388+ rcu_read_lock_bh ();
389+ vlan = rcu_dereference (q -> vlan );
395390 macvlan_count_rx (vlan , len , ret == 0 , 0 );
391+ rcu_read_unlock_bh ();
396392
397393 return ret ? ret : len ;
398394}
@@ -401,14 +397,16 @@ static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
401397 unsigned long count , loff_t pos )
402398{
403399 struct file * file = iocb -> ki_filp ;
404- struct macvtap_queue * q = macvtap_file_get_queue ( file ) ;
400+ struct macvtap_queue * q = file -> private_data ;
405401
406402 DECLARE_WAITQUEUE (wait , current );
407403 struct sk_buff * skb ;
408404 ssize_t len , ret = 0 ;
409405
410- if (!q )
411- return - ENOLINK ;
406+ if (!q ) {
407+ ret = - ENOLINK ;
408+ goto out ;
409+ }
412410
413411 len = iov_length (iv , count );
414412 if (len < 0 ) {
@@ -444,7 +442,6 @@ static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
444442 remove_wait_queue (q -> sk .sk_sleep , & wait );
445443
446444out :
447- macvtap_file_put_queue (q );
448445 return ret ;
449446}
450447
@@ -454,12 +451,13 @@ static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
454451static long macvtap_ioctl (struct file * file , unsigned int cmd ,
455452 unsigned long arg )
456453{
457- struct macvtap_queue * q ;
454+ struct macvtap_queue * q = file -> private_data ;
455+ struct macvlan_dev * vlan ;
458456 void __user * argp = (void __user * )arg ;
459457 struct ifreq __user * ifr = argp ;
460458 unsigned int __user * up = argp ;
461459 unsigned int u ;
462- char devname [ IFNAMSIZ ] ;
460+ int ret ;
463461
464462 switch (cmd ) {
465463 case TUNSETIFF :
@@ -471,16 +469,21 @@ static long macvtap_ioctl(struct file *file, unsigned int cmd,
471469 return 0 ;
472470
473471 case TUNGETIFF :
474- q = macvtap_file_get_queue (file );
475- if (!q )
472+ rcu_read_lock_bh ();
473+ vlan = rcu_dereference (q -> vlan );
474+ if (vlan )
475+ dev_hold (vlan -> dev );
476+ rcu_read_unlock_bh ();
477+
478+ if (!vlan )
476479 return - ENOLINK ;
477- memcpy (devname , q -> vlan -> dev -> name , sizeof (devname ));
478- macvtap_file_put_queue (q );
479480
481+ ret = 0 ;
480482 if (copy_to_user (& ifr -> ifr_name , q -> vlan -> dev -> name , IFNAMSIZ ) ||
481483 put_user ((TUN_TAP_DEV | TUN_NO_PI ), & ifr -> ifr_flags ))
482- return - EFAULT ;
483- return 0 ;
484+ ret = - EFAULT ;
485+ dev_put (vlan -> dev );
486+ return ret ;
484487
485488 case TUNGETFEATURES :
486489 if (put_user ((IFF_TAP | IFF_NO_PI ), up ))
@@ -491,11 +494,7 @@ static long macvtap_ioctl(struct file *file, unsigned int cmd,
491494 if (get_user (u , up ))
492495 return - EFAULT ;
493496
494- q = macvtap_file_get_queue (file );
495- if (!q )
496- return - ENOLINK ;
497497 q -> sk .sk_sndbuf = u ;
498- macvtap_file_put_queue (q );
499498 return 0 ;
500499
501500 case TUNSETOFFLOAD :
0 commit comments