From patchwork Thu Oct 20 14:28:46 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Eric W. Biederman" X-Patchwork-Id: 120823 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 4A263B700E for ; Fri, 21 Oct 2011 01:28:20 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755703Ab1JTO2Q (ORCPT ); Thu, 20 Oct 2011 10:28:16 -0400 Received: from out02.mta.xmission.com ([166.70.13.232]:58260 "EHLO out02.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752983Ab1JTO2P (ORCPT ); Thu, 20 Oct 2011 10:28:15 -0400 Received: from in01.mta.xmission.com ([166.70.13.51]) by out02.mta.xmission.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1RGtbS-0004xT-Vm; Thu, 20 Oct 2011 08:28:14 -0600 Received: from c-98-207-153-68.hsd1.ca.comcast.net ([98.207.153.68] helo=x61.ebiederm.org) by in01.mta.xmission.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1RGtbS-0006y0-0g; Thu, 20 Oct 2011 08:28:14 -0600 Received: from fess.ebiederm.org (fess.int.ebiederm.org [192.168.4.7]) by x61.ebiederm.org (Postfix) with ESMTP id 8E74939D7A; Thu, 20 Oct 2011 07:26:58 -0700 (PDT) Received: by fess.ebiederm.org (Postfix, from userid 502) id 66FC2C04A1; Thu, 20 Oct 2011 07:28:46 -0700 (PDT) From: ebiederm@xmission.com (Eric W. Biederman) To: David Miller Cc: , Arnd Bergmann , Jason Wang , "Michael S. Tsirkin" , Ian Campbell , Shirly Ma References: Date: Thu, 20 Oct 2011 07:28:46 -0700 In-Reply-To: (Eric W. Biederman's message of "Thu, 20 Oct 2011 07:27:24 -0700") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) MIME-Version: 1.0 X-XM-SPF: eid=; ; ; mid=; ; ; hst=in01.mta.xmission.com; ; ; ip=98.207.153.68; ; ; frm=ebiederm@xmission.com; ; ; spf=neutral X-XM-AID: U2FsdGVkX1/Dz+cA1GSdKWikj+GlohhVWU14YzF+nJ4= X-SA-Exim-Connect-IP: 98.207.153.68 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on sa03.xmission.com X-Spam-Level: X-Spam-Status: No, score=-1.1 required=8.0 tests=BAYES_00, DCC_CHECK_NEGATIVE, T_TooManySym_01, T_TooManySym_02, UNTRUSTED_Relay, XMNoVowels autolearn=disabled version=3.3.1 X-Spam-Report: * 1.5 XMNoVowels Alpha-numberic number with no vowels * -3.0 BAYES_00 BODY: Bayes spam probability is 0 to 1% * [score: 0.0000] * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa03 1397; Body=1 Fuz1=1 Fuz2=1] * 0.0 T_TooManySym_01 4+ unique symbols in subject * 0.0 T_TooManySym_02 5+ unique symbols in subject * 0.4 UNTRUSTED_Relay Comes from a non-trusted relay X-Spam-DCC: XMission; sa03 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;David Miller X-Spam-Relay-Country: ** Subject: [PATCH 4/5] macvtap: Rewrite macvtap_newlink so the error handling works. X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Fri, 06 Aug 2010 16:31:04 -0600) X-SA-Exim-Scanned: Yes (on in01.mta.xmission.com) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Place macvlan_common_newlink at the end of macvtap_newlink because failing in newlink after registering your network device is not supported. Move device_create into a netdevice creation notifier. The network device notifier is the only hook that is called after the network device has been registered with the device layer and before register_network_device returns success. Signed-off-by: Eric W. Biederman --- drivers/net/macvtap.c | 73 +++++++++++++++++++++++++++++++++---------------- 1 files changed, 49 insertions(+), 24 deletions(-) diff --git a/drivers/net/macvtap.c b/drivers/net/macvtap.c index 515aa87..25689e9 100644 --- a/drivers/net/macvtap.c +++ b/drivers/net/macvtap.c @@ -280,34 +280,16 @@ static int macvtap_newlink(struct net *src_net, struct nlattr *tb[], struct nlattr *data[]) { - struct device *classdev; - dev_t devt; - int err; - - err = macvlan_common_newlink(src_net, dev, tb, data, - macvtap_receive, macvtap_forward); - if (err) - goto out; - - devt = MKDEV(MAJOR(macvtap_major), dev->ifindex); - - classdev = device_create(macvtap_class, &dev->dev, devt, - dev, "tap%d", dev->ifindex); - if (IS_ERR(classdev)) { - err = PTR_ERR(classdev); - macvtap_del_queues(dev); - } - -out: - return err; + /* Don't put anything that may fail after macvlan_common_newlink + * because we can't undo what it does. + */ + return macvlan_common_newlink(src_net, dev, tb, data, + macvtap_receive, macvtap_forward); } static void macvtap_dellink(struct net_device *dev, struct list_head *head) { - device_destroy(macvtap_class, - MKDEV(MAJOR(macvtap_major), dev->ifindex)); - macvtap_del_queues(dev); macvlan_dellink(dev, head); } @@ -975,6 +957,42 @@ struct socket *macvtap_get_socket(struct file *file) } EXPORT_SYMBOL_GPL(macvtap_get_socket); +static int macvtap_device_event(struct notifier_block *unused, + unsigned long event, void *ptr) +{ + struct net_device *dev = ptr; + struct device *classdev; + dev_t devt; + + if (dev->rtnl_link_ops != &macvtap_link_ops) + return NOTIFY_DONE; + + + switch (event) { + case NETDEV_REGISTER: + /* Create the device node here after the network device has + * been registered but before register_netdevice has + * finished running. + */ + devt = MKDEV(MAJOR(macvtap_major), dev->ifindex); + classdev = device_create(macvtap_class, &dev->dev, devt, + dev, "tap%d", dev->ifindex); + if (IS_ERR(classdev)) + return notifier_from_errno(PTR_ERR(classdev)); + break; + case NETDEV_UNREGISTER: + devt = MKDEV(MAJOR(macvtap_major), dev->ifindex); + device_destroy(macvtap_class, devt); + break; + } + + return NOTIFY_DONE; +} + +static struct notifier_block macvtap_notifier_block __read_mostly = { + .notifier_call = macvtap_device_event, +}; + static int macvtap_init(void) { int err; @@ -995,12 +1013,18 @@ static int macvtap_init(void) goto out3; } - err = macvlan_link_register(&macvtap_link_ops); + err = register_netdevice_notifier(&macvtap_notifier_block); if (err) goto out4; + err = macvlan_link_register(&macvtap_link_ops); + if (err) + goto out5; + return 0; +out5: + unregister_netdevice_notifier(&macvtap_notifier_block); out4: class_unregister(macvtap_class); out3: @@ -1015,6 +1039,7 @@ module_init(macvtap_init); static void macvtap_exit(void) { rtnl_link_unregister(&macvtap_link_ops); + unregister_netdevice_notifier(&macvtap_notifier_block); class_unregister(macvtap_class); cdev_del(&macvtap_cdev); unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);