Skip to content

Commit e09eff7

Browse files
ebiedermdavem330
authored andcommitted
macvtap: Fix the minor device number allocation
On systems that create and delete lots of dynamic devices the 31bit linux ifindex fails to fit in the 16bit macvtap minor, resulting in unusable macvtap devices. I have systems running automated tests that that hit this condition in just a few days. Use a linux idr allocator to track which mavtap minor numbers are available and and to track the association between macvtap minor numbers and macvtap network devices. Remove the unnecessary unneccessary check to see if the network device we have found is indeed a macvtap device. With macvtap specific data structures it is impossible to find any other kind of networking device. Increase the macvtap minor range from 65536 to the full 20 bits that is supported by linux device numbers. It doesn't solve the original problem but there is no penalty for a larger minor device range. Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 9bf1907 commit e09eff7

2 files changed

Lines changed: 72 additions & 16 deletions

File tree

‎drivers/net/macvtap.c‎

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ static struct proto macvtap_proto = {
5151
};
5252

5353
/*
54-
* Minor number matches netdev->ifindex, so need a potentially
55-
* large value. This also makes it possible to split the
56-
* tap functionality out again in the future by offering it
57-
* from other drivers besides macvtap. As long as every device
58-
* only has one tap, the interface numbers assure that the
59-
* device nodes are unique.
54+
* Variables for dealing with macvtaps device numbers.
6055
*/
6156
static dev_t macvtap_major;
62-
#define MACVTAP_NUM_DEVS 65536
57+
#define MACVTAP_NUM_DEVS (1U << MINORBITS)
58+
static DEFINE_MUTEX(minor_lock);
59+
static DEFINE_IDR(minor_idr);
60+
6361
#define GOODCOPY_LEN 128
6462
static struct class *macvtap_class;
6563
static struct cdev macvtap_cdev;
@@ -275,6 +273,58 @@ static int macvtap_receive(struct sk_buff *skb)
275273
return macvtap_forward(skb->dev, skb);
276274
}
277275

276+
static int macvtap_get_minor(struct macvlan_dev *vlan)
277+
{
278+
int retval = -ENOMEM;
279+
int id;
280+
281+
mutex_lock(&minor_lock);
282+
if (idr_pre_get(&minor_idr, GFP_KERNEL) == 0)
283+
goto exit;
284+
285+
retval = idr_get_new_above(&minor_idr, vlan, 1, &id);
286+
if (retval < 0) {
287+
if (retval == -EAGAIN)
288+
retval = -ENOMEM;
289+
goto exit;
290+
}
291+
if (id < MACVTAP_NUM_DEVS) {
292+
vlan->minor = id;
293+
} else {
294+
printk(KERN_ERR "too many macvtap devices\n");
295+
retval = -EINVAL;
296+
idr_remove(&minor_idr, id);
297+
}
298+
exit:
299+
mutex_unlock(&minor_lock);
300+
return retval;
301+
}
302+
303+
static void macvtap_free_minor(struct macvlan_dev *vlan)
304+
{
305+
mutex_lock(&minor_lock);
306+
if (vlan->minor) {
307+
idr_remove(&minor_idr, vlan->minor);
308+
vlan->minor = 0;
309+
}
310+
mutex_unlock(&minor_lock);
311+
}
312+
313+
static struct net_device *dev_get_by_macvtap_minor(int minor)
314+
{
315+
struct net_device *dev = NULL;
316+
struct macvlan_dev *vlan;
317+
318+
mutex_lock(&minor_lock);
319+
vlan = idr_find(&minor_idr, minor);
320+
if (vlan) {
321+
dev = vlan->dev;
322+
dev_hold(dev);
323+
}
324+
mutex_unlock(&minor_lock);
325+
return dev;
326+
}
327+
278328
static int macvtap_newlink(struct net *src_net,
279329
struct net_device *dev,
280330
struct nlattr *tb[],
@@ -329,19 +379,14 @@ static void macvtap_sock_destruct(struct sock *sk)
329379
static int macvtap_open(struct inode *inode, struct file *file)
330380
{
331381
struct net *net = current->nsproxy->net_ns;
332-
struct net_device *dev = dev_get_by_index(net, iminor(inode));
382+
struct net_device *dev = dev_get_by_macvtap_minor(iminor(inode));
333383
struct macvtap_queue *q;
334384
int err;
335385

336386
err = -ENODEV;
337387
if (!dev)
338388
goto out;
339389

340-
/* check if this is a macvtap device */
341-
err = -EINVAL;
342-
if (dev->rtnl_link_ops != &macvtap_link_ops)
343-
goto out;
344-
345390
err = -ENOMEM;
346391
q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
347392
&macvtap_proto);
@@ -961,28 +1006,38 @@ static int macvtap_device_event(struct notifier_block *unused,
9611006
unsigned long event, void *ptr)
9621007
{
9631008
struct net_device *dev = ptr;
1009+
struct macvlan_dev *vlan;
9641010
struct device *classdev;
9651011
dev_t devt;
1012+
int err;
9661013

9671014
if (dev->rtnl_link_ops != &macvtap_link_ops)
9681015
return NOTIFY_DONE;
9691016

1017+
vlan = netdev_priv(dev);
9701018

9711019
switch (event) {
9721020
case NETDEV_REGISTER:
9731021
/* Create the device node here after the network device has
9741022
* been registered but before register_netdevice has
9751023
* finished running.
9761024
*/
977-
devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
1025+
err = macvtap_get_minor(vlan);
1026+
if (err)
1027+
return notifier_from_errno(err);
1028+
1029+
devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
9781030
classdev = device_create(macvtap_class, &dev->dev, devt,
9791031
dev, "tap%d", dev->ifindex);
980-
if (IS_ERR(classdev))
1032+
if (IS_ERR(classdev)) {
1033+
macvtap_free_minor(vlan);
9811034
return notifier_from_errno(PTR_ERR(classdev));
1035+
}
9821036
break;
9831037
case NETDEV_UNREGISTER:
984-
devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
1038+
devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
9851039
device_destroy(macvtap_class, devt);
1040+
macvtap_free_minor(vlan);
9861041
break;
9871042
}
9881043

‎include/linux/if_macvlan.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct macvlan_dev {
6464
int (*forward)(struct net_device *dev, struct sk_buff *skb);
6565
struct macvtap_queue *taps[MAX_MACVTAP_QUEUES];
6666
int numvtaps;
67+
int minor;
6768
};
6869

6970
static inline void macvlan_count_rx(const struct macvlan_dev *vlan,

0 commit comments

Comments
 (0)