Skip to contentSkip to conversation

Problems with read() on /proc/devices with x86_64 system

DISCUSSION Martin Schlemmer 4 participants 5 messages Feb 22, 2006 – Apr 21, 2006

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi,

Not sure when it started, but 2.6.16-rc[1234] at least have problems with unbuffered read() and /proc/devices on my x86_64 box. I first picked it up with dmsetup that did not want to work properly built against klibc (glibc with fread() worked fine though, as it mmap()'d the file).

Following code (from HPA and klibc mailing lists), when compiled and run with /proc/devices only reads the first two lines and then exits normally, where with any other file works as expected.

-----
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
char c;
int i, fd, rv;

  for ( i = 1 ; i < argc ; i++ ) {
    fd = open(argv[i], O_RDONLY);
    if ( fd < 0 ) {
      perror(argv[i]);
      exit(1);
    }
    while ( (rv = read(fd, &c, 1)) ) {
      if ( rv == -1 ) {
        if ( errno == EINTR || errno == EAGAIN )
          continue;

        perror(argv[i]);
        exit(1);
      }
      putchar(c);
    }

    close(fd);

}

return 0;
}
-----

Output over here:

-----

 # ./readbychar.klibc /proc/devices
Character devices:
  1 mem
 #
-----

Thanks,

Signature · 1 line
Martin Schlemmer
#1 lore raw
Quoted 51 lines · Martin Schlemmer

Hi,

Not sure when it started, but 2.6.16-rc[1234] at least have problems with unbuffered read() and /proc/devices on my x86_64 box. I first picked it up with dmsetup that did not want to work properly built against klibc (glibc with fread() worked fine though, as it mmap()'d the file).

Following code (from HPA and klibc mailing lists), when compiled and run with /proc/devices only reads the first two lines and then exits normally, where with any other file works as expected.

-----
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
char c;
int i, fd, rv;

 for ( i = 1 ; i < argc ; i++ ) {
   fd = open(argv[i], O_RDONLY);
   if ( fd < 0 ) {
     perror(argv[i]);
     exit(1);
   }

   while ( (rv = read(fd, &c, 1)) ) {
     if ( rv == -1 ) {
       if ( errno == EINTR || errno == EAGAIN )
         continue;

       perror(argv[i]);
       exit(1);
     }
     putchar(c);
   }

   close(fd);

}
return 0;
}
-----

Output over here:

Quoted 8 lines

-----

# ./readbychar.klibc /proc/devices
Character devices:
 1 mem
#
-----
Thanks,
Martin Schlemmer

If your code ever worked, it's probably because of some fortuitous buffering in the 'C' runtime library. Most of the 'read' code in drivers that have a /proc interface is not designed for 1-character-at-a-time I/O. It's expected that it will be accessed like cat or more or other such tools access it, -- one read with 4096-byte buffer --

read(3, "MemTotal: 773860 kB\nMemFre"..., 4096) = 670
write(1, "MemTotal: 773860 kB\nMemFre"..., 670) = 670

The read code uses sprintf to write all the parameters to a buffer, then it copies the parameters to the user. The next read will return 0 for EOF and reset the interface for the next access.

If your code read /proc without any help from the 'C' runtime library, you would read the same first character, every time you attempted to read a character. Don't do that! Your code should do (with some error-checking):

         fd = open(argv[1], O_RDONLY);
 	buffer = malloc(LEN);
         read(fd, buffer, len);
         puts(buffer);

Also, something seems somewhat strange because it is not commonplace to provide a mmap() interface to /proc file-system capability in drivers and the /proc base code doesn't provide memory-map capability at least on 2.6.15.4. So, your reference to memory-mapping the file seems to be incorrect.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.15.4 on an i686 machine (5589.54 BogoMips). Warning : 98.36% of all statistics are fiction.
_


****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

#2 lore raw
Quoted 62 lines · linux-os (Dick Johnson)
Quoted 51 lines · Martin Schlemmer

Hi,

Not sure when it started, but 2.6.16-rc[1234] at least have problems with unbuffered read() and /proc/devices on my x86_64 box. I first picked it up with dmsetup that did not want to work properly built against klibc (glibc with fread() worked fine though, as it mmap()'d the file).

Following code (from HPA and klibc mailing lists), when compiled and run with /proc/devices only reads the first two lines and then exits normally, where with any other file works as expected.

-----
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
char c;
int i, fd, rv;

 for ( i = 1 ; i < argc ; i++ ) {
   fd = open(argv[i], O_RDONLY);
   if ( fd < 0 ) {
     perror(argv[i]);
     exit(1);
   }

   while ( (rv = read(fd, &c, 1)) ) {
     if ( rv == -1 ) {
       if ( errno == EINTR || errno == EAGAIN )
         continue;

       perror(argv[i]);
       exit(1);
     }
     putchar(c);
   }

   close(fd);

}
return 0;
}
-----

Output over here:

Quoted 8 lines

-----

# ./readbychar.klibc /proc/devices
Character devices:
 1 mem
#
-----
Thanks,
Martin Schlemmer
Quoted 2 lines

If your code ever worked, it's probably because of some fortuitous buffering in the 'C' runtime library.

Not my code .. I just did a minimal hack to get it to build with klibc (klibc do not support fscanf(), so used fread() and sscanf() ..).

Quoted 9 lines

Most
of the 'read' code in drivers that have a /proc interface is not designed for 1-character-at-a-time I/O. It's expected that it will be accessed like cat or more or other such tools access it, -- one read with 4096-byte buffer --

read(3, "MemTotal: 773860 kB\nMemFre"..., 4096) = 670
write(1, "MemTotal: 773860 kB\nMemFre"..., 670) = 670

Maybe, but the same code I posted works fine with any other file in /proc, and works fine on my small p3 server with 2.6.14.

Quoted 15 lines

The read code uses sprintf to write all the parameters to a buffer, then it copies the parameters to the user. The next read will return 0 for EOF and reset the interface for the next access.

If your code read /proc without any help from the 'C' runtime library, you would read the same first character, every time you attempted to read a character. Don't do that! Your code should do (with some error-checking):

         fd = open(argv[1], O_RDONLY);
 	buffer = malloc(LEN);
         read(fd, buffer, len);
         puts(buffer);
Quoted 7 lines

Also, something seems somewhat strange because it is not commonplace to provide a mmap() interface to /proc file-system capability in drivers and the /proc base code doesn't provide memory-map capability at least on 2.6.15.4. So, your reference to memory-mapping the file seems to be incorrect.

Might have misread the strace there.

Thanks,

Signature · 1 line
Martin Schlemmer
#3 lore raw
Quoted 6 lines
Quoted 2 lines

If your code ever worked, it's probably because of some fortuitous buffering in the 'C' runtime library.

Not my code .. I just did a minimal hack to get it to build with klibc (klibc do not support fscanf(), so used fread() and sscanf() ..).

Ah, you can't do fread(ptr, 4096, 1, fp), because it would probably never return 1 if the file was smaller than 4096. Bad luck with stdio, I'd say. Back to read(2).

Quoted 8 lines
Quoted 8 lines

Most
of the 'read' code in drivers that have a /proc interface is not designed for 1-character-at-a-time I/O. It's expected that it will be accessed like cat or more or other such tools access it, -- one read with 4096-byte buffer --

read(3, "MemTotal: 773860 kB\nMemFre"..., 4096) = 670
write(1, "MemTotal: 773860 kB\nMemFre"..., 670) = 670

Jan Engelhardt

#4 lore raw
In reply to Martin Schlemmer
Quoted 9 lines · Martin Schlemmer

Not sure when it started, but 2.6.16-rc[1234] at least have problems with unbuffered read() and /proc/devices on my x86_64 box. I first picked it up with dmsetup that did not want to work properly built against klibc (glibc with fread() worked fine though, as it mmap()'d the file).

Following code (from HPA and klibc mailing lists), when compiled and run with /proc/devices only reads the first two lines and then exits normally, where with any other file works as expected.

Hi Martin,
I case you haven't found out yet, this is fixed in 2.6.17 by:

[PATCH] Simplify proc/devices and fix early termination regression

Attached is the version for 2.6.16.

Regards,
Joe

Repair /proc/devices early-termination regression.

2.6.16 broke /proc/devices. An application often gets an EOF before the end of data is reached, if that application uses a series of short read(2)s to access the data. I have used read buffers of varying sizes with varying degrees of unsuccess (larger sizes get further into the data than smaller sizes, following a simple pattern). It appears that the only safe way to get the data is to use a single read buffer larger than all the data in /proc/devices.

The following example demonstates the problem:

    # dd if=/proc/devices bs=1
    Character devices:
      1 mem
    27+0 records in
    27+0 records out

This patch is a backport of the fix recently accepted to Linus's tree:

commit 68eef3b4791572ecb70249c7fb145bb3742dd899
[PATCH] Simplify proc/devices and fix early termination regression

It replaces the complex, state-machine algorithm introduced in 2.6.16 with a simple algorithm, modeled on the implementation of /proc/interrupts.

[[email protected]: cleanups, simplifications]

 2.6.16.1-jak/block/genhd.c       |  103 ++----------------------
 2.6.16.1-jak/fs/char_dev.c       |   87 ++-------------------
 2.6.16.1-jak/fs/proc/proc_misc.c |  161 ++++++++-------------------------------
 2.6.16.1-jak/include/linux/fs.h  |   15 ---
 4 files changed, 61 insertions(+), 305 deletions(-)
block/genhd.c renamed from block/genhd.c~patch +11 -92
diff -puNa block/genhd.c~patch block/genhd.c
@@ -16,8 +16,6 @@
 #include <linux/kobj_map.h>
 #include <linux/buffer_head.h>
 
-#define MAX_PROBE_HASH 255	/* random */
-
 static struct subsystem block_subsys;
 
 static DECLARE_MUTEX(block_subsys_sem);
@@ -30,108 +28,29 @@ static struct blk_major_name {
 	struct blk_major_name *next;
 	int major;
 	char name[16];
-} *major_names[MAX_PROBE_HASH];
+} *major_names[BLKDEV_MAJOR_HASH_SIZE];
 
 /* index in the above - for now: assume no multimajor ranges */
 static inline int major_to_index(int major)
 {
-	return major % MAX_PROBE_HASH;
-}
-
-struct blkdev_info {
-        int index;
-        struct blk_major_name *bd;
-};
-
-/*
- * iterate over a list of blkdev_info structures.  allows
- * the major_names array to be iterated over from outside this file
- * must be called with the block_subsys_sem held
- */
-void *get_next_blkdev(void *dev)
-{
-        struct blkdev_info *info;
-
-        if (dev == NULL) {
-                info = kmalloc(sizeof(*info), GFP_KERNEL);
-                if (!info)
-                        goto out;
-                info->index=0;
-                info->bd = major_names[info->index];
-                if (info->bd)
-                        goto out;
-        } else {
-                info = dev;
-        }
-
-        while (info->index < ARRAY_SIZE(major_names)) {
-                if (info->bd)
-                        info->bd = info->bd->next;
-                if (info->bd)
-                        goto out;
-                /*
-                 * No devices on this chain, move to the next
-                 */
-                info->index++;
-                info->bd = (info->index < ARRAY_SIZE(major_names)) ?
-			major_names[info->index] : NULL;
-                if (info->bd)
-                        goto out;
-        }
-
-out:
-        return info;
-}
-
-void *acquire_blkdev_list(void)
-{
-        down(&block_subsys_sem);
-        return get_next_blkdev(NULL);
-}
-
-void release_blkdev_list(void *dev)
-{
-        up(&block_subsys_sem);
-        kfree(dev);
+	return major % BLKDEV_MAJOR_HASH_SIZE;
 }
 
+#ifdef CONFIG_PROC_FS
 
-/*
- * Count the number of records in the blkdev_list.
- * must be called with the block_subsys_sem held
- */
-int count_blkdev_list(void)
+void blkdev_show(struct seq_file *f, off_t offset)
 {
-	struct blk_major_name *n;
-	int i, count;
-
-	count = 0;
+	struct blk_major_name *dp;
 
-	for (i = 0; i < ARRAY_SIZE(major_names); i++) {
-		for (n = major_names[i]; n; n = n->next)
-				count++;
+	if (offset < BLKDEV_MAJOR_HASH_SIZE) {
+		down(&block_subsys_sem);
+		for (dp = major_names[offset]; dp; dp = dp->next)
+			seq_printf(f, "%3d %s\n", dp->major, dp->name);
+		up(&block_subsys_sem);
 	}
-
-	return count;
-}
-
-/*
- * extract the major and name values from a blkdev_info struct
- * passed in as a void to *dev.  Must be called with
- * block_subsys_sem held
- */
-int get_blkdev_info(void *dev, int *major, char **name)
-{
-        struct blkdev_info *info = dev;
-
-        if (info->bd == NULL)
-                return 1;
-
-        *major = info->bd->major;
-        *name = info->bd->name;
-        return 0;
 }
 
+#endif /* CONFIG_PROC_FS */
 
 int register_blkdev(unsigned int major, const char *name)
 {
fs/char_dev.c renamed from fs/char_dev.c~patch +11 -76

118 lines not on this page · raw message

fs/proc/proc_misc.c renamed from fs/proc/proc_misc.c~patch +35 -126

192 lines not on this page · raw message

include/linux/fs.h renamed from include/linux/fs.h~patch +4 -11
diff -puNa include/linux/fs.h~patch include/linux/fs.h
@@ -1383,6 +1383,7 @@ extern int bd_claim(struct block_device 
 extern void bd_release(struct block_device *);
 
 /* fs/char_dev.c */
+#define CHRDEV_MAJOR_HASH_SIZE	255
 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, const char *,
@@ -1390,25 +1391,17 @@ extern int register_chrdev(unsigned int,
 extern int unregister_chrdev(unsigned int, const char *);
 extern void unregister_chrdev_region(dev_t, unsigned);
 extern int chrdev_open(struct inode *, struct file *);
-extern int get_chrdev_list(char *);
-extern void *acquire_chrdev_list(void);
-extern int count_chrdev_list(void);
-extern void *get_next_chrdev(void *);
-extern int get_chrdev_info(void *, int *, char **);
-extern void release_chrdev_list(void *);
+extern void chrdev_show(struct seq_file *,off_t);
 
 /* fs/block_dev.c */
+#define BLKDEV_MAJOR_HASH_SIZE	255
 #define BDEVNAME_SIZE	32	/* Largest string for a blockdev identifier */
 extern const char *__bdevname(dev_t, char *buffer);
 extern const char *bdevname(struct block_device *bdev, char *buffer);
 extern struct block_device *lookup_bdev(const char *);
 extern struct block_device *open_bdev_excl(const char *, int, void *);
 extern void close_bdev_excl(struct block_device *);
-extern void *acquire_blkdev_list(void);
-extern int count_blkdev_list(void);
-extern void *get_next_blkdev(void *);
-extern int get_blkdev_info(void *, int *, char **);
-extern void release_blkdev_list(void *);
+extern void blkdev_show(struct seq_file *,off_t);
 
 extern void init_special_inode(struct inode *, umode_t, dev_t);
 

_