linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] char_dev: Fix off-by-one bugs in find_dynamic_major()
@ 2018-02-06  2:25 Srivatsa S. Bhat
  2018-02-06  2:25 ` [PATCH 2/2] block, char_dev: Use correct format specifier for unsigned ints Srivatsa S. Bhat
  2018-02-06 17:14 ` [PATCH 1/2] char_dev: Fix off-by-one bugs in find_dynamic_major() Logan Gunthorpe
  0 siblings, 2 replies; 6+ messages in thread
From: Srivatsa S. Bhat @ 2018-02-06  2:25 UTC (permalink / raw)
  To: gregkh, logang
  Cc: axboe, jlayton, bfields, viro, linux-fsdevel, linux-block,
	linux-kernel, srivatsa

From: Srivatsa S. Bhat <srivatsa@csail.mit.edu>

CHRDEV_MAJOR_DYN_END and CHRDEV_MAJOR_DYN_EXT_END are valid major
numbers. So fix the loop iteration to include them in the search for
free major numbers.

While at it, also remove a redundant if condition ("cd->major != i"),
as it will never be true.

Signed-off-by: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
---

 fs/char_dev.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/char_dev.c b/fs/char_dev.c
index a65e4a5..33c9385 100644
--- a/fs/char_dev.c
+++ b/fs/char_dev.c
@@ -67,18 +67,18 @@ static int find_dynamic_major(void)
 	int i;
 	struct char_device_struct *cd;
 
-	for (i = ARRAY_SIZE(chrdevs)-1; i > CHRDEV_MAJOR_DYN_END; i--) {
+	for (i = ARRAY_SIZE(chrdevs)-1; i >= CHRDEV_MAJOR_DYN_END; i--) {
 		if (chrdevs[i] == NULL)
 			return i;
 	}
 
 	for (i = CHRDEV_MAJOR_DYN_EXT_START;
-	     i > CHRDEV_MAJOR_DYN_EXT_END; i--) {
+	     i >= CHRDEV_MAJOR_DYN_EXT_END; i--) {
 		for (cd = chrdevs[major_to_index(i)]; cd; cd = cd->next)
 			if (cd->major == i)
 				break;
 
-		if (cd == NULL || cd->major != i)
+		if (cd == NULL)
 			return i;
 	}
 

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/2] block, char_dev: Use correct format specifier for unsigned ints
  2018-02-06  2:25 [PATCH 1/2] char_dev: Fix off-by-one bugs in find_dynamic_major() Srivatsa S. Bhat
@ 2018-02-06  2:25 ` Srivatsa S. Bhat
  2018-02-06 10:24   ` Greg KH
  2018-02-06 17:15   ` Logan Gunthorpe
  2018-02-06 17:14 ` [PATCH 1/2] char_dev: Fix off-by-one bugs in find_dynamic_major() Logan Gunthorpe
  1 sibling, 2 replies; 6+ messages in thread
From: Srivatsa S. Bhat @ 2018-02-06  2:25 UTC (permalink / raw)
  To: gregkh, logang
  Cc: axboe, jlayton, bfields, viro, linux-fsdevel, linux-block,
	linux-kernel, srivatsa

From: Srivatsa S. Bhat <srivatsa@csail.mit.edu>

register_blkdev() and __register_chrdev_region() treat the major
number as an unsigned int. So print it the same way to avoid
absurd error statements such as:
"... major requested (-1) is greater than the maximum (511) ..."
(and also fix off-by-one bugs in the error prints).

While at it, also update the comment describing register_blkdev().

Signed-off-by: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
---

 block/genhd.c |   19 +++++++++++--------
 fs/char_dev.c |    4 ++--
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/block/genhd.c b/block/genhd.c
index 88a53c1..21a18e5 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -308,19 +308,22 @@ void blkdev_show(struct seq_file *seqf, off_t offset)
 /**
  * register_blkdev - register a new block device
  *
- * @major: the requested major device number [1..255]. If @major = 0, try to
- *         allocate any unused major number.
+ * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
+ *         @major = 0, try to allocate any unused major number.
  * @name: the name of the new block device as a zero terminated string
  *
  * The @name must be unique within the system.
  *
  * The return value depends on the @major input parameter:
  *
- *  - if a major device number was requested in range [1..255] then the
- *    function returns zero on success, or a negative error code
+ *  - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
+ *    then the function returns zero on success, or a negative error code
  *  - if any unused major number was requested with @major = 0 parameter
  *    then the return value is the allocated major number in range
- *    [1..255] or a negative error code otherwise
+ *    [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
+ *
+ * See Documentation/admin-guide/devices.txt for the list of allocated
+ * major numbers.
  */
 int register_blkdev(unsigned int major, const char *name)
 {
@@ -347,8 +350,8 @@ int register_blkdev(unsigned int major, const char *name)
 	}
 
 	if (major >= BLKDEV_MAJOR_MAX) {
-		pr_err("register_blkdev: major requested (%d) is greater than the maximum (%d) for %s\n",
-		       major, BLKDEV_MAJOR_MAX, name);
+		pr_err("register_blkdev: major requested (%u) is greater than the maximum (%u) for %s\n",
+		       major, BLKDEV_MAJOR_MAX-1, name);
 
 		ret = -EINVAL;
 		goto out;
@@ -375,7 +378,7 @@ int register_blkdev(unsigned int major, const char *name)
 		ret = -EBUSY;
 
 	if (ret < 0) {
-		printk("register_blkdev: cannot get major %d for %s\n",
+		printk("register_blkdev: cannot get major %u for %s\n",
 		       major, name);
 		kfree(p);
 	}
diff --git a/fs/char_dev.c b/fs/char_dev.c
index 33c9385..a279c58 100644
--- a/fs/char_dev.c
+++ b/fs/char_dev.c
@@ -121,8 +121,8 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
 	}
 
 	if (major >= CHRDEV_MAJOR_MAX) {
-		pr_err("CHRDEV \"%s\" major requested (%d) is greater than the maximum (%d)\n",
-		       name, major, CHRDEV_MAJOR_MAX);
+		pr_err("CHRDEV \"%s\" major requested (%u) is greater than the maximum (%u)\n",
+		       name, major, CHRDEV_MAJOR_MAX-1);
 		ret = -EINVAL;
 		goto out;
 	}

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] block, char_dev: Use correct format specifier for unsigned ints
  2018-02-06  2:25 ` [PATCH 2/2] block, char_dev: Use correct format specifier for unsigned ints Srivatsa S. Bhat
@ 2018-02-06 10:24   ` Greg KH
  2018-02-06 19:58     ` Srivatsa S. Bhat
  2018-02-06 17:15   ` Logan Gunthorpe
  1 sibling, 1 reply; 6+ messages in thread
From: Greg KH @ 2018-02-06 10:24 UTC (permalink / raw)
  To: Srivatsa S. Bhat
  Cc: logang, axboe, jlayton, bfields, viro, linux-fsdevel,
	linux-block, linux-kernel

On Mon, Feb 05, 2018 at 06:25:27PM -0800, Srivatsa S. Bhat wrote:
> From: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
> 
> register_blkdev() and __register_chrdev_region() treat the major
> number as an unsigned int. So print it the same way to avoid
> absurd error statements such as:
> "... major requested (-1) is greater than the maximum (511) ..."
> (and also fix off-by-one bugs in the error prints).
> 
> While at it, also update the comment describing register_blkdev().
> 
> Signed-off-by: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
> ---
> 
>  block/genhd.c |   19 +++++++++++--------
>  fs/char_dev.c |    4 ++--
>  2 files changed, 13 insertions(+), 10 deletions(-)
> 
> diff --git a/block/genhd.c b/block/genhd.c
> index 88a53c1..21a18e5 100644
> --- a/block/genhd.c
> +++ b/block/genhd.c
> @@ -308,19 +308,22 @@ void blkdev_show(struct seq_file *seqf, off_t offset)
>  /**
>   * register_blkdev - register a new block device
>   *
> - * @major: the requested major device number [1..255]. If @major = 0, try to
> - *         allocate any unused major number.
> + * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
> + *         @major = 0, try to allocate any unused major number.
>   * @name: the name of the new block device as a zero terminated string
>   *
>   * The @name must be unique within the system.
>   *
>   * The return value depends on the @major input parameter:
>   *
> - *  - if a major device number was requested in range [1..255] then the
> - *    function returns zero on success, or a negative error code
> + *  - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
> + *    then the function returns zero on success, or a negative error code
>   *  - if any unused major number was requested with @major = 0 parameter
>   *    then the return value is the allocated major number in range
> - *    [1..255] or a negative error code otherwise
> + *    [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
> + *
> + * See Documentation/admin-guide/devices.txt for the list of allocated
> + * major numbers.
>   */
>  int register_blkdev(unsigned int major, const char *name)
>  {
> @@ -347,8 +350,8 @@ int register_blkdev(unsigned int major, const char *name)
>  	}
>  
>  	if (major >= BLKDEV_MAJOR_MAX) {
> -		pr_err("register_blkdev: major requested (%d) is greater than the maximum (%d) for %s\n",
> -		       major, BLKDEV_MAJOR_MAX, name);
> +		pr_err("register_blkdev: major requested (%u) is greater than the maximum (%u) for %s\n",
> +		       major, BLKDEV_MAJOR_MAX-1, name);
>  
>  		ret = -EINVAL;
>  		goto out;
> @@ -375,7 +378,7 @@ int register_blkdev(unsigned int major, const char *name)
>  		ret = -EBUSY;
>  
>  	if (ret < 0) {
> -		printk("register_blkdev: cannot get major %d for %s\n",
> +		printk("register_blkdev: cannot get major %u for %s\n",
>  		       major, name);
>  		kfree(p);
>  	}
> diff --git a/fs/char_dev.c b/fs/char_dev.c
> index 33c9385..a279c58 100644
> --- a/fs/char_dev.c
> +++ b/fs/char_dev.c
> @@ -121,8 +121,8 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
>  	}
>  
>  	if (major >= CHRDEV_MAJOR_MAX) {
> -		pr_err("CHRDEV \"%s\" major requested (%d) is greater than the maximum (%d)\n",
> -		       name, major, CHRDEV_MAJOR_MAX);
> +		pr_err("CHRDEV \"%s\" major requested (%u) is greater than the maximum (%u)\n",
> +		       name, major, CHRDEV_MAJOR_MAX-1);
>  		ret = -EINVAL;
>  		goto out;
>  	}

Thanks for both of these patches, if Al doesn't grab them, I will after
4.16-rc1 comes out.

greg k-h

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/2] char_dev: Fix off-by-one bugs in find_dynamic_major()
  2018-02-06  2:25 [PATCH 1/2] char_dev: Fix off-by-one bugs in find_dynamic_major() Srivatsa S. Bhat
  2018-02-06  2:25 ` [PATCH 2/2] block, char_dev: Use correct format specifier for unsigned ints Srivatsa S. Bhat
@ 2018-02-06 17:14 ` Logan Gunthorpe
  1 sibling, 0 replies; 6+ messages in thread
From: Logan Gunthorpe @ 2018-02-06 17:14 UTC (permalink / raw)
  To: Srivatsa S. Bhat, gregkh
  Cc: axboe, jlayton, bfields, viro, linux-fsdevel, linux-block, linux-kernel

Thanks!

On 05/02/18 07:25 PM, Srivatsa S. Bhat wrote:
> From: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
> 
> CHRDEV_MAJOR_DYN_END and CHRDEV_MAJOR_DYN_EXT_END are valid major
> numbers. So fix the loop iteration to include them in the search for
> free major numbers.
> 
> While at it, also remove a redundant if condition ("cd->major != i"),
> as it will never be true.
> 
> Signed-off-by: Srivatsa S. Bhat <srivatsa@csail.mit.edu>

Reviewed-by: Logan Gunthorpe <logang@deltatee.com>


Logan

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] block, char_dev: Use correct format specifier for unsigned ints
  2018-02-06  2:25 ` [PATCH 2/2] block, char_dev: Use correct format specifier for unsigned ints Srivatsa S. Bhat
  2018-02-06 10:24   ` Greg KH
@ 2018-02-06 17:15   ` Logan Gunthorpe
  1 sibling, 0 replies; 6+ messages in thread
From: Logan Gunthorpe @ 2018-02-06 17:15 UTC (permalink / raw)
  To: Srivatsa S. Bhat, gregkh
  Cc: axboe, jlayton, bfields, viro, linux-fsdevel, linux-block, linux-kernel



On 05/02/18 07:25 PM, Srivatsa S. Bhat wrote:
> From: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
> 
> register_blkdev() and __register_chrdev_region() treat the major
> number as an unsigned int. So print it the same way to avoid
> absurd error statements such as:
> "... major requested (-1) is greater than the maximum (511) ..."
> (and also fix off-by-one bugs in the error prints).
> 
> While at it, also update the comment describing register_blkdev().
> 
> Signed-off-by: Srivatsa S. Bhat <srivatsa@csail.mit.edu>

Reviewed-by: Logan Gunthorpe <logang@deltatee.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/2] block, char_dev: Use correct format specifier for unsigned ints
  2018-02-06 10:24   ` Greg KH
@ 2018-02-06 19:58     ` Srivatsa S. Bhat
  0 siblings, 0 replies; 6+ messages in thread
From: Srivatsa S. Bhat @ 2018-02-06 19:58 UTC (permalink / raw)
  To: Greg KH
  Cc: logang, axboe, jlayton, bfields, viro, linux-fsdevel,
	linux-block, linux-kernel

On 2/6/18 2:24 AM, Greg KH wrote:
> On Mon, Feb 05, 2018 at 06:25:27PM -0800, Srivatsa S. Bhat wrote:
>> From: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
>>
>> register_blkdev() and __register_chrdev_region() treat the major
>> number as an unsigned int. So print it the same way to avoid
>> absurd error statements such as:
>> "... major requested (-1) is greater than the maximum (511) ..."
>> (and also fix off-by-one bugs in the error prints).
>>
>> While at it, also update the comment describing register_blkdev().
>>
>> Signed-off-by: Srivatsa S. Bhat <srivatsa@csail.mit.edu>
>> ---
>>
>>  block/genhd.c |   19 +++++++++++--------
>>  fs/char_dev.c |    4 ++--
>>  2 files changed, 13 insertions(+), 10 deletions(-)
>>
>> diff --git a/block/genhd.c b/block/genhd.c
>> index 88a53c1..21a18e5 100644
>> --- a/block/genhd.c
>> +++ b/block/genhd.c
>> @@ -308,19 +308,22 @@ void blkdev_show(struct seq_file *seqf, off_t offset)
>>  /**
>>   * register_blkdev - register a new block device
>>   *
>> - * @major: the requested major device number [1..255]. If @major = 0, try to
>> - *         allocate any unused major number.
>> + * @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
>> + *         @major = 0, try to allocate any unused major number.
>>   * @name: the name of the new block device as a zero terminated string
>>   *
>>   * The @name must be unique within the system.
>>   *
>>   * The return value depends on the @major input parameter:
>>   *
>> - *  - if a major device number was requested in range [1..255] then the
>> - *    function returns zero on success, or a negative error code
>> + *  - if a major device number was requested in range [1..BLKDEV_MAJOR_MAX-1]
>> + *    then the function returns zero on success, or a negative error code
>>   *  - if any unused major number was requested with @major = 0 parameter
>>   *    then the return value is the allocated major number in range
>> - *    [1..255] or a negative error code otherwise
>> + *    [1..BLKDEV_MAJOR_MAX-1] or a negative error code otherwise
>> + *
>> + * See Documentation/admin-guide/devices.txt for the list of allocated
>> + * major numbers.
>>   */
>>  int register_blkdev(unsigned int major, const char *name)
>>  {
>> @@ -347,8 +350,8 @@ int register_blkdev(unsigned int major, const char *name)
>>  	}
>>  
>>  	if (major >= BLKDEV_MAJOR_MAX) {
>> -		pr_err("register_blkdev: major requested (%d) is greater than the maximum (%d) for %s\n",
>> -		       major, BLKDEV_MAJOR_MAX, name);
>> +		pr_err("register_blkdev: major requested (%u) is greater than the maximum (%u) for %s\n",
>> +		       major, BLKDEV_MAJOR_MAX-1, name);
>>  
>>  		ret = -EINVAL;
>>  		goto out;
>> @@ -375,7 +378,7 @@ int register_blkdev(unsigned int major, const char *name)
>>  		ret = -EBUSY;
>>  
>>  	if (ret < 0) {
>> -		printk("register_blkdev: cannot get major %d for %s\n",
>> +		printk("register_blkdev: cannot get major %u for %s\n",
>>  		       major, name);
>>  		kfree(p);
>>  	}
>> diff --git a/fs/char_dev.c b/fs/char_dev.c
>> index 33c9385..a279c58 100644
>> --- a/fs/char_dev.c
>> +++ b/fs/char_dev.c
>> @@ -121,8 +121,8 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor,
>>  	}
>>  
>>  	if (major >= CHRDEV_MAJOR_MAX) {
>> -		pr_err("CHRDEV \"%s\" major requested (%d) is greater than the maximum (%d)\n",
>> -		       name, major, CHRDEV_MAJOR_MAX);
>> +		pr_err("CHRDEV \"%s\" major requested (%u) is greater than the maximum (%u)\n",
>> +		       name, major, CHRDEV_MAJOR_MAX-1);
>>  		ret = -EINVAL;
>>  		goto out;
>>  	}
> 
> Thanks for both of these patches, if Al doesn't grab them, I will after
> 4.16-rc1 comes out.
> 

Sounds great! Thank you!

Regards,
Srivatsa

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-02-06 19:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-06  2:25 [PATCH 1/2] char_dev: Fix off-by-one bugs in find_dynamic_major() Srivatsa S. Bhat
2018-02-06  2:25 ` [PATCH 2/2] block, char_dev: Use correct format specifier for unsigned ints Srivatsa S. Bhat
2018-02-06 10:24   ` Greg KH
2018-02-06 19:58     ` Srivatsa S. Bhat
2018-02-06 17:15   ` Logan Gunthorpe
2018-02-06 17:14 ` [PATCH 1/2] char_dev: Fix off-by-one bugs in find_dynamic_major() Logan Gunthorpe

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).