From patchwork Mon Feb 22 13:07:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 62565 Delivered-To: patch@linaro.org Received: by 10.112.43.199 with SMTP id y7csp1224041lbl; Mon, 22 Feb 2016 05:07:44 -0800 (PST) X-Received: by 10.98.33.131 with SMTP id o3mr37846375pfj.97.1456146464499; Mon, 22 Feb 2016 05:07:44 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id lu9si39647666pab.215.2016.02.22.05.07.44; Mon, 22 Feb 2016 05:07:44 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dkim=pass header.i=@linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754932AbcBVNHn (ORCPT + 30 others); Mon, 22 Feb 2016 08:07:43 -0500 Received: from mail-lb0-f181.google.com ([209.85.217.181]:33586 "EHLO mail-lb0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751176AbcBVNHl (ORCPT ); Mon, 22 Feb 2016 08:07:41 -0500 Received: by mail-lb0-f181.google.com with SMTP id x4so82166837lbm.0 for ; Mon, 22 Feb 2016 05:07:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=0SMRfUF/fqh1gTWXlG5aqXtIcSatpYX7Ckqxpg0nQjc=; b=LORIXeZZKmUbG8iG5ggmeaVIUslH6O4QHoATg1czDmlGbiYkSFyROSnFuC6+N7mVXs R1TKc7aSO+XEVunoxOCVwB9us0gM/0x5PfzyRYeDP/xERUH4XxDg8i6PdvJQnj8sXW9E DNix9I6npiLrfi5vEw3ahizH5CP1wCpnqbUAw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=0SMRfUF/fqh1gTWXlG5aqXtIcSatpYX7Ckqxpg0nQjc=; b=QAmiPzzEajuOBKgqXzQrbnmqQe0rUJhJ6ryN7/TDIpvvekWRnO821j0HEFtFUlGfzO Z2/CcrzF//qQS7wQ8Ne6a92/UUzsnUMx0Wi+ioFAdTignTUeDuMVWIhEywJOh8BPhfta JccUPHk9NkKfq53Ms+mm9z0VyxE/YZpUSl5GEPZ9lBdgkQfn2mbwt/mwGmzyr5C5e0PL keFKUt/ouPYOeEv4lqCdjzlbnkDfX2HF37zrYyemx1W/O3DAiAl393F3455xmFsWPC7l PrD7xBwFjNblirf5Xun3iopjQZB1+w55NcxDFK1aXbGg2eZQUSmiFYaYPtYaY+xcStO1 aQ/Q== X-Gm-Message-State: AG10YOQOSmlxudwFVNTH4B28Dc+Bd8Uk0aj32+iO9+k9ySb4KDAK/s4wmEEJyY10C0RTMkyV X-Received: by 10.112.150.133 with SMTP id ui5mr10011107lbb.12.1456146460018; Mon, 22 Feb 2016 05:07:40 -0800 (PST) Received: from localhost.localdomain ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id jx8sm3319677lbc.29.2016.02.22.05.07.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Feb 2016 05:07:39 -0800 (PST) From: Linus Walleij To: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Alan Cox , Arnd Bergmann Cc: Linus Walleij , Linus Torvalds Subject: [PATCH 2/2 v3] chrdev: allocate dynamic chardevs in all unused holes Date: Mon, 22 Feb 2016 14:07:32 +0100 Message-Id: <1456146452-1268-1-git-send-email-linus.walleij@linaro.org> X-Mailer: git-send-email 2.4.3 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a duct-tape-and-chewing-gum solution to the problem with the major numbers running out when allocating major numbers dynamically. To avoid collisions in the major space, we supply a bitmap with "holes" that exist in the lower range of major numbers [0-254] and pick numbers from there, beginning with the unused char device 8 and moving up through 26, 40, 60-63, 93-94, 102, 120-127, 159, 213-215, 222-223 and 234-254. It will also FAIL if we actually fill up all free major numbers. This seems to me like the reasonable thing to do since the other numbers are, after all, reserved. This also adds the macro BITS() to so we can define the static bitmap at compiletime in a reasonable way. If someone prefer that I just put opaque hex numbers in there, then tell me, that works too, it's just not elegant IMO. The macro with the same name in select.c is consequently renamed FDS_BITS(). This also deletes the comment /* temporary */ which must be one of the biggest lies ever. This also updates the Documentation/devices.txt document to reflect that all these numbers are used for dynamic assignment. Reported-by: Ying Huang Cc: Linus Torvalds Cc: Greg Kroah-Hartman Cc: Alan Cox Cc: Arnd Bergmann Signed-off-by: Linus Walleij --- ChangeLog v2->v3: - Of course I had a dangling hunk for pr_dbg()->pr_debug() in my working tree. Mea culpa. - Fix the redefinition of BITS() in select.c to use the more precise name FDS_BITS() over the generic plural. ChangeLog v1->v2: - Follow-up on the previous RFC patch, this uses Torvald's suggested bitmap approach to allocate devices instead of a list of free numbers. - As a result of using find_first_zero_bit(), the major numbers are assigned from low to high instead from high to low. It's a bit scarier but I guess drivers using dynamic numbers should be all right with it, I'm more worried about userspaces expecting dynamic majors to be in the [234,254] range. Input welcome, maybe I'm just chicken. - This still needs to be applied on top of the previous fix to start warning about going below major 234. If you prefer to just get this patch and get rid of the problem then tell me. --- Documentation/devices.txt | 22 ++++++++++++++-------- fs/char_dev.c | 46 ++++++++++++++++++++++++++++++++++------------ fs/select.c | 6 +++--- include/linux/bitops.h | 1 + include/linux/fs.h | 2 -- 5 files changed, 52 insertions(+), 25 deletions(-) -- 2.4.3 diff --git a/Documentation/devices.txt b/Documentation/devices.txt index 4035eca87144..2a4242be2fcd 100644 --- a/Documentation/devices.txt +++ b/Documentation/devices.txt @@ -248,6 +248,8 @@ Your cooperation is appreciated. associated with block devices. The binding to the loop devices is handled by mount(8) or losetup(8). + 8 char RESERVED FOR DYNAMIC ALLOCATION OF MAJOR NUMBERS + 8 block SCSI disk devices (0-15) 0 = /dev/sda First SCSI disk whole disk 16 = /dev/sdb Second SCSI disk whole disk @@ -620,7 +622,7 @@ Your cooperation is appreciated. 2 = /dev/sbpcd2 Panasonic CD-ROM controller 0 unit 2 3 = /dev/sbpcd3 Panasonic CD-ROM controller 0 unit 3 - 26 char + 26 char RESERVED FOR DYNAMIC ALLOCATION OF MAJOR NUMBERS 26 block Second Matsushita (Panasonic/SoundBlaster) CD-ROM 0 = /dev/sbpcd4 Panasonic CD-ROM controller 1 unit 0 @@ -863,7 +865,7 @@ Your cooperation is appreciated. ... 39 block - 40 char + 40 char RESERVED FOR DYNAMIC ALLOCATION OF MAJOR NUMBERS 40 block @@ -1127,7 +1129,7 @@ Your cooperation is appreciated. NAMING CONFLICT -- PROPOSED REVISED NAME /dev/rpda0 etc - 60-63 char LOCAL/EXPERIMENTAL USE + 60-63 char RESERVED FOR DYNAMIC ALLOCATION OF MAJOR NUMBERS 60-63 block LOCAL/EXPERIMENTAL USE Allocated for local/experimental use. For devices not @@ -1641,7 +1643,7 @@ Your cooperation is appreciated. disks (see major number 3) except that the limit on partitions is 15. - 93 char + 93 char RESERVED FOR DYNAMIC ALLOCATION OF MAJOR NUMBERS 93 block NAND Flash Translation Layer filesystem 0 = /dev/nftla First NFTL layer @@ -1649,7 +1651,7 @@ Your cooperation is appreciated. ... 240 = /dev/nftlp 16th NTFL layer - 94 char + 94 char RESERVED FOR DYNAMIC ALLOCATION OF MAJOR NUMBERS 94 block IBM S/390 DASD block storage 0 = /dev/dasda First DASD device, major @@ -1742,7 +1744,7 @@ Your cooperation is appreciated. ... 15 = /dev/amiraid/ar?p15 15th partition -102 char +102 char RESERVED FOR DYNAMIC ALLOCATION OF MAJOR NUMBERS 102 block Compressed block device 0 = /dev/cbd/a First compressed block device, whole device @@ -2027,7 +2029,7 @@ Your cooperation is appreciated. 1 = /dev/vnet1 2nd virtual network ... -120-127 char LOCAL/EXPERIMENTAL USE +120-127 char RESERVED FOR DYNAMIC ALLOCATION OF MAJOR NUMBERS 120-127 block LOCAL/EXPERIMENTAL USE Allocated for local/experimental use. For devices not @@ -2341,7 +2343,7 @@ Your cooperation is appreciated. 1 = /dev/gfax1 GammaLink channel 1 ... -159 char RESERVED +159 char RESERVED FOR DYNAMIC ALLOCATION OF MAJOR NUMBERS 159 block RESERVED @@ -2929,6 +2931,8 @@ Your cooperation is appreciated. ... 196 = /dev/dvb/adapter3/video0 first video decoder of fourth card +213-215 char RESERVED FOR DYNAMIC ALLOCATION OF MAJOR NUMBERS + 216 char Bluetooth RFCOMM TTY devices 0 = /dev/rfcomm0 First Bluetooth RFCOMM TTY device 1 = /dev/rfcomm1 Second Bluetooth RFCOMM TTY device @@ -2971,6 +2975,8 @@ Your cooperation is appreciated. same interface. For interface documentation see http://www.vmelinux.org/. +222-223 char RESERVED FOR DYNAMIC ALLOCATION OF MAJOR NUMBERS + 224 char A2232 serial card 0 = /dev/ttyY0 First A2232 port 1 = /dev/ttyY1 Second A2232 port diff --git a/fs/char_dev.c b/fs/char_dev.c index 687471dc04a0..721d565c4640 100644 --- a/fs/char_dev.c +++ b/fs/char_dev.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include "internal.h" @@ -37,6 +39,31 @@ static struct char_device_struct { struct cdev *cdev; /* will die */ } *chrdevs[CHRDEV_MAJOR_HASH_SIZE]; +/* + * A bitmap for the 255 lower device numbers. A "1" means the + * major number is taken, a "0" means it is available. We first + * need to define all the assigned devices as taken so that + * dynamic device allocation will not go in and steal them. + */ +static u32 majors_map[] = { + /* 8 and 26 are free */ + BITS(0, 7) | BITS(9, 25) | BITS(27, 31), + /* 40 and 60-63 are free */ + BITS(0, 7) | BITS(9, 27), + /* 93 and 94 are free */ + BITS(0, 28) | BIT(31), + /* 102 and 120-127 are free */ + BITS(0, 5) | BITS(7, 23), + /* 159 is free */ + BITS(0, 30), + /* No free numbers */ + ~0x0U, + /* 213-215 and 222-223 are free */ + BITS(0, 20) | BITS(24, 29), + /* 234-254 are free */ + BITS(0, 9) | BIT(31) +}; + /* index in the above */ static inline int major_to_index(unsigned major) { @@ -84,22 +111,17 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor, mutex_lock(&chrdevs_lock); - /* temporary */ if (major == 0) { - for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) { - if (chrdevs[i] == NULL) - break; - } - - if (i < CHRDEV_MAJOR_DYN_END) - pr_warn("CHRDEV \"%s\" major number %d goes below the dynamic allocation range", - name, i); - - if (i == 0) { + major = find_first_zero_bit(majors_map, CHRDEV_MAJOR_HASH_SIZE); + if (major == CHRDEV_MAJOR_HASH_SIZE) { + pr_warn("CHRDEV: \"%s\" out of major numbers", + name); ret = -EBUSY; goto out; } - major = i; + set_bit(major, (unsigned long *)majors_map); + pr_debug("CHRDEV: \"%s\" using major %d\n", + name, major); } cd->major = major; diff --git a/fs/select.c b/fs/select.c index 79d0d4953cad..9161491a1d28 100644 --- a/fs/select.c +++ b/fs/select.c @@ -337,7 +337,7 @@ sticky: #define FDS_OUT(fds, n) (fds->out + n) #define FDS_EX(fds, n) (fds->ex + n) -#define BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n)) +#define FDS_BITS(fds, n) (*FDS_IN(fds, n)|*FDS_OUT(fds, n)|*FDS_EX(fds, n)) static int max_select_fd(unsigned long n, fd_set_bits *fds) { @@ -353,7 +353,7 @@ static int max_select_fd(unsigned long n, fd_set_bits *fds) open_fds = fdt->open_fds + n; max = 0; if (set) { - set &= BITS(fds, n); + set &= FDS_BITS(fds, n); if (set) { if (!(set & ~*open_fds)) goto get_max; @@ -363,7 +363,7 @@ static int max_select_fd(unsigned long n, fd_set_bits *fds) while (n) { open_fds--; n--; - set = BITS(fds, n); + set = FDS_BITS(fds, n); if (!set) continue; if (set & ~*open_fds) diff --git a/include/linux/bitops.h b/include/linux/bitops.h index defeaac0745f..73301256b474 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -4,6 +4,7 @@ #ifdef __KERNEL__ #define BIT(nr) (1UL << (nr)) +#define BITS(_start, _end) ((BIT(_end) - BIT(_start)) + BIT(_end)) #define BIT_ULL(nr) (1ULL << (nr)) #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) diff --git a/include/linux/fs.h b/include/linux/fs.h index 6301ac091e54..1a2046275cdf 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2384,8 +2384,6 @@ static inline void bd_unlink_disk_holder(struct block_device *bdev, /* fs/char_dev.c */ #define CHRDEV_MAJOR_HASH_SIZE 255 -/* Marks the bottom of the first segment of free char majors */ -#define CHRDEV_MAJOR_DYN_END 234 extern int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *); extern int register_chrdev_region(dev_t, unsigned, const char *); extern int __register_chrdev(unsigned int major, unsigned int baseminor,