From: Cheng Renquan <crquan@gmail.com>
To: "David S. Miller" <davem@davemloft.net>, netdev@vger.kernel.org
Subject: [PATCH] dev-alloc-name-by-check-name-existence.patch
Date: Sat, 24 May 2014 14:46:51 -0700 [thread overview]
Message-ID: <1400968011-13023-1-git-send-email-crquan@gmail.com> (raw)
Nowadays the net_device names have been mounted on a hash table,
check name existence should be much faster than ever before, hence
the extra page of memory for bitmap scanning is no longer needed.
Signed-off-by: Cheng Renquan <crquan@gmail.com>
---
net/core/dev.c | 33 ++++++---------------------------
1 file changed, 6 insertions(+), 27 deletions(-)
Index: linux-3.14.3/net/core/dev.c
===================================================================
--- linux-3.14.3.orig/net/core/dev.c 2014-05-06 07:59:58.000000000 -0700
+++ linux-3.14.3/net/core/dev.c 2014-05-24 13:56:02.592865482 -0700
@@ -960,54 +960,33 @@
* Returns the number of the unit assigned or a negative errno code.
*/
static int __dev_alloc_name(struct net *net, const char *name, char *buf)
{
int i = 0;
const char *p;
- const int max_netdevices = 8*PAGE_SIZE;
- unsigned long *inuse;
- struct net_device *d;
p = strnchr(name, IFNAMSIZ-1, '%');
if (p) {
/*
* Verify the string as this thing may have come from
* the user. There must be either one "%d" and no other "%"
* characters.
*/
if (p[1] != 'd' || strchr(p + 2, '%'))
return -EINVAL;
- /* Use one page as a bit array of possible slots */
- inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
- if (!inuse)
- return -ENOMEM;
-
- for_each_netdev(net, d) {
- if (!sscanf(d->name, name, &i))
- continue;
- if (i < 0 || i >= max_netdevices)
- continue;
-
- /* avoid cases where sscanf is not exact inverse of printf */
- snprintf(buf, IFNAMSIZ, name, i);
- if (!strncmp(buf, d->name, IFNAMSIZ))
- set_bit(i, inuse);
- }
-
- i = find_first_zero_bit(inuse, max_netdevices);
- free_page((unsigned long) inuse);
+ do {
+ if (snprintf(buf, IFNAMSIZ, name, i) >= IFNAMSIZ)
+ break;
+ if (!__dev_get_by_name(net, buf))
+ return i;
+ } while (++i > 0);
}
- if (buf != name)
- snprintf(buf, IFNAMSIZ, name, i);
- if (!__dev_get_by_name(net, buf))
- return i;
-
/* It is possible to run out of possible slots
* when the name is long and there isn't enough space left
* for the digits, or if all bits are used.
*/
return -ENFILE;
}
next reply other threads:[~2014-05-24 21:45 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-24 21:46 Cheng Renquan [this message]
2014-05-24 21:57 ` [PATCH] dev-alloc-name-by-check-name-existence.patch David Miller
2014-05-25 2:58 ` crquan
2014-05-25 3:03 ` David Miller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1400968011-13023-1-git-send-email-crquan@gmail.com \
--to=crquan@gmail.com \
--cc=davem@davemloft.net \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).