Skip to contentSkip to conversation

procfs: Fix /proc/self/maps output for 32-bit kernel and compat tasks

PATCH Superseded Helge Deller 3 participants 4 messages Aug 19, 2023 – Aug 22, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 procfs: Fix /proc/self/maps output for 32-bit kernel and compat tasks

On a 32-bit kernel addresses should be shown with 8 hex digits, e.g.:

root@debian:~# cat /proc/self/maps
00010000-00019000 r-xp 00000000 08:05 787324     /usr/bin/cat
00019000-0001a000 rwxp 00009000 08:05 787324     /usr/bin/cat
0001a000-0003b000 rwxp 00000000 00:00 0          [heap]
f7551000-f770d000 r-xp 00000000 08:05 794765     /usr/lib/hppa-linux-gnu/libc.so.6
f770d000-f770f000 r--p 001bc000 08:05 794765     /usr/lib/hppa-linux-gnu/libc.so.6
f770f000-f7714000 rwxp 001be000 08:05 794765     /usr/lib/hppa-linux-gnu/libc.so.6
f7d39000-f7d68000 r-xp 00000000 08:05 794759     /usr/lib/hppa-linux-gnu/ld.so.1
f7d68000-f7d69000 r--p 0002f000 08:05 794759     /usr/lib/hppa-linux-gnu/ld.so.1
f7d69000-f7d6d000 rwxp 00030000 08:05 794759     /usr/lib/hppa-linux-gnu/ld.so.1
f7ea9000-f7eaa000 r-xp 00000000 00:00 0          [vdso]
f8565000-f8587000 rwxp 00000000 00:00 0          [stack]

But since commmit 0e3dc0191431 ("procfs: add seq_put_hex_ll to speed up /proc/pid/maps") even on native 32-bit kernels the output looks like this:

root@debian:~# cat /proc/self/maps
0000000010000-0000000019000 r-xp 00000000 000000008:000000005 787324  /usr/bin/cat
0000000019000-000000001a000 rwxp 000000009000 000000008:000000005 787324  /usr/bin/cat
000000001a000-000000003b000 rwxp 00000000 00:00 0  [heap]
00000000f73d1000-00000000f758d000 r-xp 00000000 000000008:000000005 794765  /usr/lib/hppa-linux-gnu/libc.so.6
00000000f758d000-00000000f758f000 r--p 000000001bc000 000000008:000000005 794765  /usr/lib/hppa-linux-gnu/libc.so.6
00000000f758f000-00000000f7594000 rwxp 000000001be000 000000008:000000005 794765  /usr/lib/hppa-linux-gnu/libc.so.6
00000000f7af9000-00000000f7b28000 r-xp 00000000 000000008:000000005 794759  /usr/lib/hppa-linux-gnu/ld.so.1
00000000f7b28000-00000000f7b29000 r--p 000000002f000 000000008:000000005 794759  /usr/lib/hppa-linux-gnu/ld.so.1
00000000f7b29000-00000000f7b2d000 rwxp 0000000030000 000000008:000000005 794759  /usr/lib/hppa-linux-gnu/ld.so.1
00000000f7e0c000-00000000f7e0d000 r-xp 00000000 00:00 0  [vdso]
00000000f9061000-00000000f9083000 rwxp 00000000 00:00 0  [stack]

This patch brings back the old default 8-hex digit output for 32-bit kernels and compat tasks.

Fixes 0e3dc0191431 ("procfs: add seq_put_hex_ll to speed up /proc/pid/maps")
Cc Andrei Vagin <[email protected]>
fs/seq_file.c +7 -1
index f5fdaf3b1572..1a15b531aede 100644
@@ -19,6 +19,7 @@
 #include <linux/printk.h>
 #include <linux/string_helpers.h>
 #include <linux/uio.h>
+#include <linux/compat.h>
 
 #include <linux/uaccess.h>
 #include <asm/page.h>
@@ -759,11 +760,16 @@ void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
 			seq_puts(m, delimiter);
 	}
 
+#ifdef CONFIG_64BIT
 	/* If x is 0, the result of __builtin_clzll is undefined */
-	if (v == 0)
+	if (v == 0 || is_compat_task())
 		len = 1;
 	else
 		len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
+#else
+	/* On 32-bit kernel always use provided width */
+	len = 1;
+#endif
 
 	if (len < width)
 		len = width;
#1 lore raw
Quoted 58 lines · Helge Deller

On a 32-bit kernel addresses should be shown with 8 hex digits, e.g.:

root@debian:~# cat /proc/self/maps
00010000-00019000 r-xp 00000000 08:05 787324     /usr/bin/cat
00019000-0001a000 rwxp 00009000 08:05 787324     /usr/bin/cat
0001a000-0003b000 rwxp 00000000 00:00 0          [heap]
f7551000-f770d000 r-xp 00000000 08:05 794765     /usr/lib/hppa-linux-gnu/libc.so.6
f770d000-f770f000 r--p 001bc000 08:05 794765     /usr/lib/hppa-linux-gnu/libc.so.6
f770f000-f7714000 rwxp 001be000 08:05 794765     /usr/lib/hppa-linux-gnu/libc.so.6
f7d39000-f7d68000 r-xp 00000000 08:05 794759     /usr/lib/hppa-linux-gnu/ld.so.1
f7d68000-f7d69000 r--p 0002f000 08:05 794759     /usr/lib/hppa-linux-gnu/ld.so.1
f7d69000-f7d6d000 rwxp 00030000 08:05 794759     /usr/lib/hppa-linux-gnu/ld.so.1
f7ea9000-f7eaa000 r-xp 00000000 00:00 0          [vdso]
f8565000-f8587000 rwxp 00000000 00:00 0          [stack]

But since commmit 0e3dc0191431 ("procfs: add seq_put_hex_ll to speed up /proc/pid/maps") even on native 32-bit kernels the output looks like this:

root@debian:~# cat /proc/self/maps
0000000010000-0000000019000 r-xp 00000000 000000008:000000005 787324  /usr/bin/cat
0000000019000-000000001a000 rwxp 000000009000 000000008:000000005 787324  /usr/bin/cat
000000001a000-000000003b000 rwxp 00000000 00:00 0  [heap]
00000000f73d1000-00000000f758d000 r-xp 00000000 000000008:000000005 794765  /usr/lib/hppa-linux-gnu/libc.so.6
00000000f758d000-00000000f758f000 r--p 000000001bc000 000000008:000000005 794765  /usr/lib/hppa-linux-gnu/libc.so.6
00000000f758f000-00000000f7594000 rwxp 000000001be000 000000008:000000005 794765  /usr/lib/hppa-linux-gnu/libc.so.6
00000000f7af9000-00000000f7b28000 r-xp 00000000 000000008:000000005 794759  /usr/lib/hppa-linux-gnu/ld.so.1
00000000f7b28000-00000000f7b29000 r--p 000000002f000 000000008:000000005 794759  /usr/lib/hppa-linux-gnu/ld.so.1
00000000f7b29000-00000000f7b2d000 rwxp 0000000030000 000000008:000000005 794759  /usr/lib/hppa-linux-gnu/ld.so.1
00000000f7e0c000-00000000f7e0d000 r-xp 00000000 00:00 0  [vdso]
00000000f9061000-00000000f9083000 rwxp 00000000 00:00 0  [stack]

This patch brings back the old default 8-hex digit output for 32-bit kernels and compat tasks.

Fixes 0e3dc0191431 ("procfs: add seq_put_hex_ll to speed up /proc/pid/maps")
Cc Andrei Vagin <[email protected]>
fs/seq_file.c +3 -1
index f5fdaf3b1572..1a15b531aede 100644
@@ -19,6 +19,7 @@
  #include <linux/printk.h>
  #include <linux/string_helpers.h>
  #include <linux/uio.h>
+#include <linux/compat.h>
 
  #include <linux/uaccess.h>
  #include <asm/page.h>
@@ -759,11 +760,16 @@ void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
  			seq_puts(m, delimiter);
  	}
 
+#ifdef CONFIG_64BIT
  	/* If x is 0, the result of __builtin_clzll is undefined */
-	if (v == 0)
+	if (v == 0 || is_compat_task())

fs/seq_file.c void seq_put_hex_ll(struct seq_file *m, const char *delimiter, · line 775

Quoted 10 lines
fs/seq_file.c +4 -0
  		len = 1;
  	else
  		len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
+#else
+	/* On 32-bit kernel always use provided width */
+	len = 1;
+#endif
 
  	if (len < width)
  		len = width;

Hi,

I think this is fixing the wrong thing.

seq_put_hex_ll() is a generic routine so the #ifdef/is_compat_task() doesn't seem to belong there.

Moreover, the kerneldoc comment on this function states:

* seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)

The seq_put_hex_ll() call from show_vma_header_prefix() is calling this with the last argument (minimum padding length) being 8, so why is it padding to more than that in the first place?

Look at your example:

Quoted 2 lines
root@debian:~# cat /proc/self/maps
0000000010000-0000000019000 r-xp 00000000 000000008:000000005 787324 /usr/bin/cat

that's padded to... 13 hex characters? Huh?

Even on my x86_64, short addresses are only padded to 8 bytes, as they should be in all cases. Could there possibly be a bug in parisc __builtin_clzll()...?

Vegard

#2 lore raw
In reply to Helge Deller
kernel test robot noticed the following build errors:

Hi Helge,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.5-rc7 next-20230821]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Helge-Deller/procfs-Fix-proc-self-maps-output-for-32-bit-kernel-and-compat-tasks/20230821-100823
base:   linus/master
patch link:    https://lore.kernel.org/r/ZOCJltW%2FeufPUc%2BT%40p100
patch subject: [PATCH] procfs: Fix /proc/self/maps output for 32-bit kernel and compat tasks
config: x86_64-buildonly-randconfig-006-20230822 (https://download.01.org/0day-ci/archive/20230822/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce: (https://download.01.org/0day-ci/archive/20230822/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

fs/seq_file.c: In function 'seq_put_hex_ll':

>> fs/seq_file.c:765:23: error: implicit declaration of function 'is_compat_task' [-Werror=implicit-function-declaration]
     765 |         if (v == 0 || is_compat_task())
         |                       ^~~~~~~~~~~~~~

cc1: some warnings being treated as errors

vim +/is_compat_task +765 fs/seq_file.c

737
738 /**
739 * seq_put_hex_ll - put a number in hexadecimal notation
740 * @m: seq_file identifying the buffer to which data should be written
741 * @delimiter: a string which is printed before the number
742 * @v: the number
743 * @width: a minimum field width
744 *
745 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
746 *
747 * This routine is very quick when you show lots of numbers.
748 * In usual cases, it will be better to use seq_printf(). It's easier to read.
749 */
750 void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
751 unsigned long long v, unsigned int width)
752 {
753 unsigned int len;
754 int i;
755
756 if (delimiter && delimiter[0]) {
757 if (delimiter[1] == 0)
758 seq_putc(m, delimiter[0]);
759 else
760 seq_puts(m, delimiter);
761 }
762
763 #ifdef CONFIG_64BIT
764 /* If x is 0, the result of __builtin_clzll is undefined */

 > 765		if (v == 0 || is_compat_task())

766 len = 1;
767 else
768 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
769 #else
770 /* On 32-bit kernel always use provided width */
771 len = 1;
772 #endif
773
774 if (len < width)
775 len = width;
776
777 if (m->count + len > m->size) {
778 seq_set_overflow(m);
779 return;
780 }
781
782 for (i = len - 1; i >= 0; i--) {
783 m->buf[m->count + i] = hex_asc[0xf & v];
784 v = v >> 4;
785 }
786 m->count += len;
787 }
788

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw
In reply to Helge Deller
kernel test robot noticed the following build errors:

Hi Helge,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.5-rc7 next-20230821]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Helge-Deller/procfs-Fix-proc-self-maps-output-for-32-bit-kernel-and-compat-tasks/20230821-100823
base:   linus/master
patch link:    https://lore.kernel.org/r/ZOCJltW%2FeufPUc%2BT%40p100
patch subject: [PATCH] procfs: Fix /proc/self/maps output for 32-bit kernel and compat tasks
config: x86_64-randconfig-013-20230821 (https://download.01.org/0day-ci/archive/20230822/[email protected]/config)
compiler: clang version 16.0.4 (https://github.com/llvm/llvm-project.git ae42196bc493ffe877a7e3dff8be32035dea4d07)
reproduce: (https://download.01.org/0day-ci/archive/20230822/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> fs/seq_file.c:765:16: error: call to undeclared function 'is_compat_task'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
           if (v == 0 || is_compat_task())
                         ^

1 error generated.

vim +/is_compat_task +765 fs/seq_file.c

737
738 /**
739 * seq_put_hex_ll - put a number in hexadecimal notation
740 * @m: seq_file identifying the buffer to which data should be written
741 * @delimiter: a string which is printed before the number
742 * @v: the number
743 * @width: a minimum field width
744 *
745 * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
746 *
747 * This routine is very quick when you show lots of numbers.
748 * In usual cases, it will be better to use seq_printf(). It's easier to read.
749 */
750 void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
751 unsigned long long v, unsigned int width)
752 {
753 unsigned int len;
754 int i;
755
756 if (delimiter && delimiter[0]) {
757 if (delimiter[1] == 0)
758 seq_putc(m, delimiter[0]);
759 else
760 seq_puts(m, delimiter);
761 }
762
763 #ifdef CONFIG_64BIT
764 /* If x is 0, the result of __builtin_clzll is undefined */

 > 765		if (v == 0 || is_compat_task())

766 len = 1;
767 else
768 len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
769 #else
770 /* On 32-bit kernel always use provided width */
771 len = 1;
772 #endif
773
774 if (len < width)
775 len = width;
776
777 if (m->count + len > m->size) {
778 seq_set_overflow(m);
779 return;
780 }
781
782 for (i = len - 1; i >= 0; i--) {
783 m->buf[m->count + i] = hex_asc[0xf & v];
784 v = v >> 4;
785 }
786 m->count += len;
787 }
788

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki