Skip to contentSkip to conversation

linux: use stat instead of udevadm for partition lookup + cleanup

PATCH Superseded Petr Vorel 5 participants 18 messages Jul 8, 2021 – Jul 14, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

cover linux: use stat instead of udevadm for partition lookup + cleanup

Hi,

I'm backporting Jeff's patch from openSUSE + adding little cleanup.

Kind regards,
Petr

Jeff Mahoney (1):
grub2: use stat instead of udevadm for partition lookup

Petr Vorel (1):
osdep: Introduce major.h and use it

 grub-core/osdep/devmapper/getroot.c  |  7 +------
 grub-core/osdep/devmapper/hostdisk.c |  7 +------
 grub-core/osdep/linux/getroot.c      |  7 +------
 grub-core/osdep/linux/hostdisk.c     |  8 ++++++++
 grub-core/osdep/unix/getroot.c       |  7 +------
 include/grub/osdep/major.h           | 30 ++++++++++++++++++++++++++++
 6 files changed, 42 insertions(+), 24 deletions(-)
 create mode 100644 include/grub/osdep/major.h
Signature · 1 line
2.32.0
author
#1 lore raw

PATCH 1/2 osdep: Introduce major.h and use it

---
 grub-core/osdep/devmapper/getroot.c  |  7 +------
 grub-core/osdep/devmapper/hostdisk.c |  7 +------
 grub-core/osdep/linux/getroot.c      |  7 +------
 grub-core/osdep/unix/getroot.c       |  7 +------
 include/grub/osdep/major.h           | 30 ++++++++++++++++++++++++++++
 5 files changed, 34 insertions(+), 24 deletions(-)
 create mode 100644 include/grub/osdep/major.h
grub-core/osdep/devmapper/getroot.c +1 -6
index a13a39c96..9ba5c9865 100644
@@ -40,12 +40,7 @@
 #include <limits.h>
 #endif
 
-#if defined(MAJOR_IN_MKDEV)
-#include <sys/mkdev.h>
-#elif defined(MAJOR_IN_SYSMACROS)
-#include <sys/sysmacros.h>
-#endif
-
+#include <grub/osdep/major.h>
 #include <libdevmapper.h>
 
 #include <grub/types.h>
grub-core/osdep/devmapper/hostdisk.c +1 -6
index a8afc0c94..c8053728b 100644
@@ -11,6 +11,7 @@
 #include <grub/misc.h>
 #include <grub/i18n.h>
 #include <grub/list.h>
+#include <grub/osdep/major.h>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -24,12 +25,6 @@
 #include <errno.h>
 #include <limits.h>
 
-#if defined(MAJOR_IN_MKDEV)
-#include <sys/mkdev.h>
-#elif defined(MAJOR_IN_SYSMACROS)
-#include <sys/sysmacros.h>
-#endif
-
 #ifdef HAVE_DEVICE_MAPPER
 # include <libdevmapper.h>
 
grub-core/osdep/linux/getroot.c +1 -6
index 001b818fe..cd588588e 100644
@@ -35,12 +35,7 @@
 #include <limits.h>
 #endif
 
-#if defined(MAJOR_IN_MKDEV)
-#include <sys/mkdev.h>
-#elif defined(MAJOR_IN_SYSMACROS)
-#include <sys/sysmacros.h>
-#endif
-
+#include <grub/osdep/major.h>
 #include <grub/types.h>
 #include <sys/ioctl.h>         /* ioctl */
 #include <sys/mount.h>
grub-core/osdep/unix/getroot.c +1 -6
index 46d7116c6..74f69116d 100644
@@ -51,12 +51,7 @@
 #endif /* ! FLOPPY_MAJOR */
 #endif
 
-#include <sys/types.h>
-#if defined(MAJOR_IN_MKDEV)
-#include <sys/mkdev.h>
-#elif defined(MAJOR_IN_SYSMACROS)
-#include <sys/sysmacros.h>
-#endif
+#include <grub/osdep/major.h>
 
 #if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
 # include <grub/util/libzfs.h>
include/grub/osdep/major.h new file +30 -0
new file mode 100644
index 000000000..e3b545551
@@ -0,0 +1,30 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2021  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * Fix for glibc 2.25 is deprecating the namespace pollution of sys/types.h
+ * injecting major(), minor(), and makedev() into the compilation environment.
+ * See configure.ac.
+*/
+
+#include <sys/types.h>
+#ifdef MAJOR_IN_MKDEV
+# include <sys/mkdev.h>
+#elif defined(MAJOR_IN_SYSMACROS)
+# include <sys/sysmacros.h>
+#endif
Signature · 1 line
2.32.0
#2 lore raw

Re: [PATCH 1/2] osdep: Introduce major.h and use it

Quoted 9 lines · Petr Vorel
---
 grub-core/osdep/devmapper/getroot.c  |  7 +------
 grub-core/osdep/devmapper/hostdisk.c |  7 +------
 grub-core/osdep/linux/getroot.c      |  7 +------
 grub-core/osdep/unix/getroot.c       |  7 +------
 include/grub/osdep/major.h           | 30 ++++++++++++++++++++++++++++
 5 files changed, 34 insertions(+), 24 deletions(-)
 create mode 100644 include/grub/osdep/major.h

May I ask you to explain in the commit message why this patch is needed?

Daniel

author
#3 lore raw

Re: [PATCH 1/2] osdep: Introduce major.h and use it

Quoted 11 lines · Petr Vorel
Quoted 9 lines
---
 grub-core/osdep/devmapper/getroot.c  |  7 +------
 grub-core/osdep/devmapper/hostdisk.c |  7 +------
 grub-core/osdep/linux/getroot.c      |  7 +------
 grub-core/osdep/unix/getroot.c       |  7 +------
 include/grub/osdep/major.h           | 30 ++++++++++++++++++++++++++++
 5 files changed, 34 insertions(+), 24 deletions(-)
 create mode 100644 include/grub/osdep/major.h
Quoted 1 line

May I ask you to explain in the commit message why this patch is needed?

It's just a small cleanup. If you're not against it, sure, I can mention it in the commit message.

Kind regards,
Petr

Quoted 1 line

Daniel

author
#4 lore raw

Re: [PATCH 1/2] osdep: Introduce major.h and use it

Hi Daniel,

Quoted 11 lines
Quoted 10 lines · Petr Vorel
Quoted 9 lines
---
 grub-core/osdep/devmapper/getroot.c  |  7 +------
 grub-core/osdep/devmapper/hostdisk.c |  7 +------
 grub-core/osdep/linux/getroot.c      |  7 +------
 grub-core/osdep/unix/getroot.c       |  7 +------
 include/grub/osdep/major.h           | 30 ++++++++++++++++++++++++++++
 5 files changed, 34 insertions(+), 24 deletions(-)
 create mode 100644 include/grub/osdep/major.h
Quoted 3 lines
Quoted 1 line

May I ask you to explain in the commit message why this patch is needed?

It's just a small cleanup. If you're not against it, sure, I can mention it in the commit message.

Ah, you probably mean to put the description from major.h also to the commit message.

Kind regards,
Petr

+/*
+ * Fix for glibc 2.25 is deprecating the namespace pollution of sys/types.h
+ * injecting major(), minor(), and makedev() into the compilation environment.
+ * See configure.ac.
+*/

Quoted 4 lines

Kind regards,
Petr

Quoted 1 line

Daniel

#5 lore raw

Re: [PATCH 1/2] osdep: Introduce major.h and use it

Quoted 16 lines · Petr Vorel

Hi Daniel,

Quoted 14 lines
Quoted 11 lines · Petr Vorel
Quoted 9 lines
---
 grub-core/osdep/devmapper/getroot.c  |  7 +------
 grub-core/osdep/devmapper/hostdisk.c |  7 +------
 grub-core/osdep/linux/getroot.c      |  7 +------
 grub-core/osdep/unix/getroot.c       |  7 +------
 include/grub/osdep/major.h           | 30 ++++++++++++++++++++++++++++
 5 files changed, 34 insertions(+), 24 deletions(-)
 create mode 100644 include/grub/osdep/major.h
Quoted 1 line

May I ask you to explain in the commit message why this patch is needed?

It's just a small cleanup. If you're not against it, sure, I can mention it in the commit message.

I am OK with the cleanup.

Quoted 2 lines

Ah, you probably mean to put the description from major.h also to the commit message.

To some extent. I think it should be phrased a bit better in the commit message.

Quoted 7 lines

Kind regards,
Petr

+/*
+ * Fix for glibc 2.25 is deprecating the namespace pollution of sys/types.h
+ * injecting major(), minor(), and makedev() into the compilation environment.
+ * See configure.ac.

It seems to me "See configure.ac." is not relevant in the GRUB source. So, probably it should be replaced with some text from glibc configure.ac.

Daniel

author
#6 lore raw

Re: [PATCH 1/2] osdep: Introduce major.h and use it

Hi Daniel,

Quoted 14 lines · Petr Vorel
Quoted 12 lines

Hi Daniel,

Quoted 10 lines
Quoted 10 lines · Petr Vorel
Quoted 9 lines
---
 grub-core/osdep/devmapper/getroot.c  |  7 +------
 grub-core/osdep/devmapper/hostdisk.c |  7 +------
 grub-core/osdep/linux/getroot.c      |  7 +------
 grub-core/osdep/unix/getroot.c       |  7 +------
 include/grub/osdep/major.h           | 30 ++++++++++++++++++++++++++++
 5 files changed, 34 insertions(+), 24 deletions(-)
 create mode 100644 include/grub/osdep/major.h
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

May I ask you to explain in the commit message why this patch is needed?

It's just a small cleanup. If you're not against it, sure, I can mention it in the commit message.

I am OK with the cleanup.

thx for ack!

Quoted 4 lines
Quoted 2 lines

Ah, you probably mean to put the description from major.h also to the commit message.

To some extent. I think it should be phrased a bit better in the commit message.

sure.

Quoted 10 lines
Quoted 7 lines

Kind regards,
Petr

+/*
+ * Fix for glibc 2.25 is deprecating the namespace pollution of sys/types.h
+ * injecting major(), minor(), and makedev() into the compilation environment.
+ * See configure.ac.

It seems to me "See configure.ac." is not relevant in the GRUB source. So, probably it should be replaced with some text from glibc configure.ac.

I didn't know how to link AC_HEADER_MAJOR with include/grub/osdep/major.h. Because in the future when glibc 2.25 is old enough the header will be removed and AC_HEADER_MAJOR might be left in configure.ac (name is different from MAJOR_IN_{MKDEV,SYSMACROS}). But I can note this in commit message instead.

Kind regards,
Petr

Quoted 1 line

Daniel

author
#7 lore raw

Re: [PATCH 1/2] osdep: Introduce major.h and use it

Hi Daniel,

...

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

+/*
+ * Fix for glibc 2.25 is deprecating the namespace pollution of sys/types.h
+ * injecting major(), minor(), and makedev() into the compilation environment.
+ * See configure.ac.

Quoted 7 lines
Quoted 2 lines

It seems to me "See configure.ac." is not relevant in the GRUB source. So, probably it should be replaced with some text from glibc configure.ac.

I didn't know how to link AC_HEADER_MAJOR with include/grub/osdep/major.h. Because in the future when glibc 2.25 is old enough the header will be removed and AC_HEADER_MAJOR might be left in configure.ac (name is different from MAJOR_IN_{MKDEV,SYSMACROS}). But I can note this in commit message instead.

But thinking about it twice also note in configure.ac instead (+ commit message) would be good. But that's just a minor detail.

Kind regards,
Petr

Quoted 4 lines

Kind regards,
Petr

Quoted 1 line

Daniel

author
#8 lore raw

PATCH 2/2 grub2: use stat instead of udevadm for partition lookup

In reply to Petr Vorel

From: Jeff Mahoney <[email protected]>

sysfs_partition_path calls udevadm to resolve the sysfs path for a block device. That can be accomplished by stating the device node and using the major/minor to follow the symlinks in /sys/dev/block/.

This cuts the execution time of grub2-mkconfig from 10s to 2s on my system.

[ pvorel: include grub/osdep/major.h ]
---
 grub-core/osdep/linux/hostdisk.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/osdep/linux/hostdisk.c +8 -0
index da62f924e..43dc4b0ba 100644
@@ -31,6 +31,7 @@
 #include <grub/misc.h>
 #include <grub/i18n.h>
 #include <grub/list.h>
+#include <grub/osdep/major.h>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -105,6 +106,13 @@ sysfs_partition_path (const char *dev, const char *entry)
   char *buf = NULL;
   size_t len = 0;
   char *path = NULL;
+  struct stat st;
+  int ret;
+
+  ret = stat(dev, &st);
+  if (ret == 0 && S_ISBLK(st.st_mode))
+    return xasprintf ("/sys/dev/block/%u:%u/%s",
+		      major (st.st_rdev), minor (st.st_rdev), entry);
 
   argv[0] = "udevadm";
   argv[1] = "info";
Signature · 1 line
2.32.0
#9 lore raw

Re: [PATCH 2/2] grub2: use stat instead of udevadm for partition lookup

Dear Petr, dear Jeff,

Am 08.07.21 um 17:55 schrieb Petr Vorel:

Quoted 8 lines

From: Jeff Mahoney <[email protected]>

sysfs_partition_path calls udevadm to resolve the sysfs path for a block device. That can be accomplished by stating the device node and using the major/minor to follow the symlinks in /sys/dev/block/.

This cuts the execution time of grub2-mkconfig from 10s to 2s on my system.

Petr, where you able to reproduce this issue? Could the specifications of Jeff’s system be added to the commit message?

Quoted 23 lines
[ pvorel: include grub/osdep/major.h ]
---
  grub-core/osdep/linux/hostdisk.c | 8 ++++++++
  1 file changed, 8 insertions(+)
grub-core/osdep/linux/hostdisk.c +1 -0
index da62f924e..43dc4b0ba 100644
@@ -31,6 +31,7 @@
  #include <grub/misc.h>
  #include <grub/i18n.h>
  #include <grub/list.h>
+#include <grub/osdep/major.h>
 
  #include <stdio.h>
  #include <stdlib.h>
@@ -105,6 +106,13 @@ sysfs_partition_path (const char *dev, const char *entry)
    char *buf = NULL;
    size_t len = 0;
    char *path = NULL;

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 118

Quoted 10 lines
grub-core/osdep/linux/hostdisk.c +7 -0
+  struct stat st;
+  int ret;
+
+  ret = stat(dev, &st);
+  if (ret == 0 && S_ISBLK(st.st_mode))
+    return xasprintf ("/sys/dev/block/%u:%u/%s",
+		      major (st.st_rdev), minor (st.st_rdev), entry);
 
    argv[0] = "udevadm";
    argv[1] = "info";

Now the non-block device case has one stat call more executed each time.

Kind regards,

Paul

author
#10 lore raw

Re: [PATCH 2/2] grub2: use stat instead of udevadm for partition lookup

Hi Paul,

Quoted 6 lines

Dear Petr, dear Jeff,

Am 08.07.21 um 17:55 schrieb Petr Vorel:

Quoted 1 line

From: Jeff Mahoney <[email protected]>

Quoted 8 lines
Quoted 6 lines

sysfs_partition_path calls udevadm to resolve the sysfs path for a block device. That can be accomplished by stating the device node and using the major/minor to follow the symlinks in /sys/dev/block/.

This cuts the execution time of grub2-mkconfig from 10s to 2s on my system.

Petr, where you able to reproduce this issue?

No, I'm sorry, I haven't even tried, because accessing sysfs seems to me as a quickest way anyway. But agree that we drag this patch in opensuse from 2017 (for 2.02~rc1), it might not be relevant for nowadays systems.

Quoted 1 line

Could the specifications of Jeff’s system be added to the commit message?

Jeff, Michael, could you verify if it's still relevant?

Kind regards,
Petr

Quoted 38 lines
Quoted 21 lines
[ pvorel: include grub/osdep/major.h ]
---
  grub-core/osdep/linux/hostdisk.c | 8 ++++++++
  1 file changed, 8 insertions(+)
grub-core/osdep/linux/hostdisk.c +1 -0
index da62f924e..43dc4b0ba 100644
@@ -31,6 +31,7 @@
  #include <grub/misc.h>
  #include <grub/i18n.h>
  #include <grub/list.h>
+#include <grub/osdep/major.h>
  #include <stdio.h>
  #include <stdlib.h>
@@ -105,6 +106,13 @@ sysfs_partition_path (const char *dev, const char *entry)
    char *buf = NULL;
    size_t len = 0;

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 117

Quoted 10 lines
grub-core/osdep/linux/hostdisk.c +7 -0
    char *path = NULL;
+  struct stat st;
+  int ret;
+
+  ret = stat(dev, &st);
+  if (ret == 0 && S_ISBLK(st.st_mode))
+    return xasprintf ("/sys/dev/block/%u:%u/%s",
+		      major (st.st_rdev), minor (st.st_rdev), entry);
    argv[0] = "udevadm";
    argv[1] = "info";

Now the non-block device case has one stat call more executed each time.

Kind regards,

Paul

#11 lore raw

Re: [PATCH 2/2] grub2: use stat instead of udevadm for partition lookup

Quoted 15 lines · Petr Vorel

Hi Paul,

Quoted 12 lines

Dear Petr, dear Jeff,

Am 08.07.21 um 17:55 schrieb Petr Vorel:

Quoted 8 lines

From: Jeff Mahoney <[email protected]>

sysfs_partition_path calls udevadm to resolve the sysfs path for a block device. That can be accomplished by stating the device node and using the major/minor to follow the symlinks in /sys/dev/block/.

This cuts the execution time of grub2-mkconfig from 10s to 2s on my system.

Quoted 7 lines
Quoted 1 line

Petr, where you able to reproduce this issue?

No, I'm sorry, I haven't even tried, because accessing sysfs seems to me as a quickest way anyway. But agree that we drag this patch in opensuse from 2017 (for 2.02~rc1), it might not be relevant for nowadays systems.

Quoted 1 line

Could the specifications of Jeff’s system be added to the commit message?

Jeff, Michael, could you verify if it's still relevant?

It is still relevant per my test, although not as considerable as Jeff's system.

I was using openSUSE Tumbleweed, pretty much all defaults and simple, btrfs as root file system without any abstraction disks (lvm, mdadm and encryption).

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

 sr0   11:0    1  663M  0 rom  
 vda  253:0    0   20G  0 disk 
 vda1 253:1    0  1.4G  0 part [SWAP]
 vda2 253:2    0 18.6G  0 part /

The output of time grub2-mkconfig -o /boot/grub2/grub.cfg with the patch applied.

 real    0m1.292s
 user    0m0.782s
 sys     0m0.427s

and without

 real    0m2.323s
 user    0m1.295s
 sys     0m0.956s

This cuts the execution to somewhere near 55%, which is pretty signifcant.

Please note if testing on lvm, there wouldn't have difference on the execution time as libdevmapper is used instead of udevadm to get the partition start.

Thanks,
Michael

Quoted 43 lines

Kind regards,
Petr

Quoted 38 lines
Quoted 21 lines
[ pvorel: include grub/osdep/major.h ]
---
  grub-core/osdep/linux/hostdisk.c | 8 ++++++++
  1 file changed, 8 insertions(+)
grub-core/osdep/linux/hostdisk.c +1 -0
index da62f924e..43dc4b0ba 100644
@@ -31,6 +31,7 @@
  #include <grub/misc.h>
  #include <grub/i18n.h>
  #include <grub/list.h>
+#include <grub/osdep/major.h>
  #include <stdio.h>
  #include <stdlib.h>
@@ -105,6 +106,13 @@ sysfs_partition_path (const char *dev, const char *entry)
    char *buf = NULL;
    size_t len = 0;

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 117

Quoted 10 lines
grub-core/osdep/linux/hostdisk.c +7 -0
    char *path = NULL;
+  struct stat st;
+  int ret;
+
+  ret = stat(dev, &st);
+  if (ret == 0 && S_ISBLK(st.st_mode))
+    return xasprintf ("/sys/dev/block/%u:%u/%s",
+		      major (st.st_rdev), minor (st.st_rdev), entry);
    argv[0] = "udevadm";
    argv[1] = "info";

Now the non-block device case has one stat call more executed each time.

Kind regards,

Paul

author
#12 lore raw

Re: [PATCH 2/2] grub2: use stat instead of udevadm for partition lookup

Hi Michael,

Quoted 44 lines · Petr Vorel
Quoted 15 lines

Hi Paul,

Quoted 12 lines

Dear Petr, dear Jeff,

Am 08.07.21 um 17:55 schrieb Petr Vorel:

Quoted 8 lines

From: Jeff Mahoney <[email protected]>

sysfs_partition_path calls udevadm to resolve the sysfs path for a block device. That can be accomplished by stating the device node and using the major/minor to follow the symlinks in /sys/dev/block/.

This cuts the execution time of grub2-mkconfig from 10s to 2s on my system.

Quoted 7 lines
Quoted 1 line

Petr, where you able to reproduce this issue?

No, I'm sorry, I haven't even tried, because accessing sysfs seems to me as a quickest way anyway. But agree that we drag this patch in opensuse from 2017 (for 2.02~rc1), it might not be relevant for nowadays systems.

Quoted 1 line

Could the specifications of Jeff’s system be added to the commit message?

Jeff, Michael, could you verify if it's still relevant?

It is still relevant per my test, although not as considerable as Jeff's system.

I was using openSUSE Tumbleweed, pretty much all defaults and simple, btrfs as root file system without any abstraction disks (lvm, mdadm and encryption).

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

 sr0   11:0    1  663M  0 rom  
 vda  253:0    0   20G  0 disk 
 vda1 253:1    0  1.4G  0 part [SWAP]
 vda2 253:2    0 18.6G  0 part /

The output of time grub2-mkconfig -o /boot/grub2/grub.cfg with the patch applied.

 real    0m1.292s
 user    0m0.782s
 sys     0m0.427s
Quoted 7 lines

and without

 real    0m2.323s
 user    0m1.295s
 sys     0m0.956s

This cuts the execution to somewhere near 55%, which is pretty signifcant.

Thanks a lot for testing. I'll send v2 shortly.

Quoted 3 lines

Please note if testing on lvm, there wouldn't have difference on the execution time as libdevmapper is used instead of udevadm to get the partition start.

Good to know (some of my system have lvm).

Kind regards,
Petr

Quoted 2 lines

Thanks,
Michael

#13 lore raw

Re: [PATCH 2/2] grub2: use stat instead of udevadm for partition lookup

In reply to Paul Menzel
Quoted 41 lines · Paul Menzel

Dear Petr, dear Jeff,

Am 08.07.21 um 17:55 schrieb Petr Vorel:

Quoted 8 lines

From: Jeff Mahoney <[email protected]>

sysfs_partition_path calls udevadm to resolve the sysfs path for a block device. That can be accomplished by stating the device node and using the major/minor to follow the symlinks in /sys/dev/block/.

This cuts the execution time of grub2-mkconfig from 10s to 2s on my system.

Petr, where you able to reproduce this issue? Could the specifications of Jeff’s system be added to the commit message?

Quoted 25 lines
[ pvorel: include grub/osdep/major.h ]
---
  grub-core/osdep/linux/hostdisk.c | 8 ++++++++
  1 file changed, 8 insertions(+)
grub-core/osdep/linux/hostdisk.c +4 -0
index da62f924e..43dc4b0ba 100644
@@ -31,6 +31,7 @@
  #include <grub/misc.h>
  #include <grub/i18n.h>
  #include <grub/list.h>
+#include <grub/osdep/major.h>
  #include <stdio.h>
  #include <stdlib.h>
@@ -105,6 +106,13 @@ sysfs_partition_path (const char *dev, const char *entry)
    char *buf = NULL;
    size_t len = 0;
    char *path = NULL;
+  struct stat st;
+  int ret;
+
Quoted 8 lines

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 118

Quoted 6 lines
grub-core/osdep/linux/hostdisk.c +4 -0
+  ret = stat(dev, &st);
+  if (ret == 0 && S_ISBLK(st.st_mode))
+    return xasprintf ("/sys/dev/block/%u:%u/%s",
+		      major (st.st_rdev), minor (st.st_rdev), entry);
    argv[0] = "udevadm";
    argv[1] = "info";

Now the non-block device case has one stat call more executed each time.

Practically this should not happen imho, as the device here are returned by grub_guess_root_devices() to which underlying block devices is used by a given path or mount point.

Thanks,
Michael

Quoted 6 lines

Kind regards,

Paul

#14 lore raw

Re: [PATCH 2/2] grub2: use stat instead of udevadm for partition lookup

In reply to Petr Vorel
Quoted 29 lines · Petr Vorel

From: Jeff Mahoney <[email protected]>

sysfs_partition_path calls udevadm to resolve the sysfs path for a block device. That can be accomplished by stating the device node and using the major/minor to follow the symlinks in /sys/dev/block/.

This cuts the execution time of grub2-mkconfig from 10s to 2s on my system.

[ pvorel: include grub/osdep/major.h ]
---
 grub-core/osdep/linux/hostdisk.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/osdep/linux/hostdisk.c +1 -0
index da62f924e..43dc4b0ba 100644
@@ -31,6 +31,7 @@
 #include <grub/misc.h>
 #include <grub/i18n.h>
 #include <grub/list.h>
+#include <grub/osdep/major.h>
 
 #include <stdio.h>
 #include <stdlib.h>

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 112

Quoted 7 lines
grub-core/osdep/linux/hostdisk.c +4 -0
@@ -105,6 +106,13 @@ sysfs_partition_path (const char *dev, const char *entry)
   char *buf = NULL;
   size_t len = 0;
   char *path = NULL;
+  struct stat st;
+  int ret;
+
+  ret = stat(dev, &st);

Missing space between "stat" and "(".

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 113

Quoted 1 line
grub-core/osdep/linux/hostdisk.c +1 -0
+  if (ret == 0 && S_ISBLK(st.st_mode))

Same for S_ISBLK...

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 118

Quoted 5 lines
grub-core/osdep/linux/hostdisk.c +2 -0
+    return xasprintf ("/sys/dev/block/%u:%u/%s",
+		      major (st.st_rdev), minor (st.st_rdev), entry);
 
   argv[0] = "udevadm";
   argv[1] = "info";

Do we really need udevadm fallback mechanism? If something went wrong here for us I do not expect it will work for udevadm either.

Daniel

#15 lore raw

Re: [PATCH 2/2] grub2: use stat instead of udevadm for partition lookup

Quoted 44 lines · Daniel Kiper
Quoted 29 lines · Petr Vorel

From: Jeff Mahoney <[email protected]>

sysfs_partition_path calls udevadm to resolve the sysfs path for a block device. That can be accomplished by stating the device node and using the major/minor to follow the symlinks in /sys/dev/block/.

This cuts the execution time of grub2-mkconfig from 10s to 2s on my system.

[ pvorel: include grub/osdep/major.h ]
---
 grub-core/osdep/linux/hostdisk.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/osdep/linux/hostdisk.c +1 -0
index da62f924e..43dc4b0ba 100644
@@ -31,6 +31,7 @@
 #include <grub/misc.h>
 #include <grub/i18n.h>
 #include <grub/list.h>
+#include <grub/osdep/major.h>
 
 #include <stdio.h>
 #include <stdlib.h>

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 112

Quoted 7 lines
grub-core/osdep/linux/hostdisk.c +4 -0
@@ -105,6 +106,13 @@ sysfs_partition_path (const char *dev, const char *entry)
   char *buf = NULL;
   size_t len = 0;
   char *path = NULL;
+  struct stat st;
+  int ret;
+
+  ret = stat(dev, &st);

Missing space between "stat" and "(".

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 113

Quoted 1 line
grub-core/osdep/linux/hostdisk.c +1 -0
+  if (ret == 0 && S_ISBLK(st.st_mode))

Same for S_ISBLK...

Quoted 8 lines

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 118

Quoted 5 lines
grub-core/osdep/linux/hostdisk.c +2 -0
+    return xasprintf ("/sys/dev/block/%u:%u/%s",
+		      major (st.st_rdev), minor (st.st_rdev), entry);
 
   argv[0] = "udevadm";
   argv[1] = "info";

Do we really need udevadm fallback mechanism? If something went wrong here for us I do not expect it will work for udevadm either.

I suspect not. 'udevadm info --query path --name <dev>' does essentially the same thing but with many more steps and the cost starting an external program for every block device. I left it in with the assumption that it was probably there for a reason and perhaps udevadm on early udev systems did something more special than just doing stat + sysfs path building. I didn't spend any time on validating that assumption.

-Jeff

Signature · 2 lines
Jeff Mahoney
Director, SUSE Labs Data & Performance
author
#16 lore raw

Re: [PATCH 2/2] grub2: use stat instead of udevadm for partition lookup

Hi all,

Quoted 54 lines · Daniel Kiper
Quoted 44 lines · Petr Vorel
Quoted 29 lines

From: Jeff Mahoney <[email protected]>

sysfs_partition_path calls udevadm to resolve the sysfs path for a block device. That can be accomplished by stating the device node and using the major/minor to follow the symlinks in /sys/dev/block/.

This cuts the execution time of grub2-mkconfig from 10s to 2s on my system.

[ pvorel: include grub/osdep/major.h ]
---
 grub-core/osdep/linux/hostdisk.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/osdep/linux/hostdisk.c +1 -0
index da62f924e..43dc4b0ba 100644
@@ -31,6 +31,7 @@
 #include <grub/misc.h>
 #include <grub/i18n.h>
 #include <grub/list.h>
+#include <grub/osdep/major.h>
 
 #include <stdio.h>
 #include <stdlib.h>

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 112

Quoted 7 lines
grub-core/osdep/linux/hostdisk.c +4 -0
@@ -105,6 +106,13 @@ sysfs_partition_path (const char *dev, const char *entry)
   char *buf = NULL;
   size_t len = 0;
   char *path = NULL;
+  struct stat st;
+  int ret;
+
+  ret = stat(dev, &st);

Missing space between "stat" and "(".

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 113

Quoted 1 line
grub-core/osdep/linux/hostdisk.c +1 -0
+  if (ret == 0 && S_ISBLK(st.st_mode))

Same for S_ISBLK...

Quoted 8 lines

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 118

Quoted 5 lines
grub-core/osdep/linux/hostdisk.c +2 -0
+    return xasprintf ("/sys/dev/block/%u:%u/%s",
+		      major (st.st_rdev), minor (st.st_rdev), entry);
 
   argv[0] = "udevadm";
   argv[1] = "info";

Do we really need udevadm fallback mechanism? If something went wrong here for us I do not expect it will work for udevadm either.

Quoted 7 lines

I suspect not. 'udevadm info --query path --name <dev>' does essentially the same thing but with many more steps and the cost starting an external program for every block device. I left it in with the assumption that it was probably there for a reason and perhaps udevadm on early udev systems did something more special than just doing stat + sysfs path building. I didn't spend any time on validating that assumption.

As it'd be wort to remove udevadm fallback, I'll stop being lazy and setup VM to test.

Kind regards,
Petr

Quoted 1 line

-Jeff

#17 lore raw

Re: [PATCH 2/2] grub2: use stat instead of udevadm for partition lookup

Quoted 64 lines · Petr Vorel

Hi all,

Quoted 54 lines · Daniel Kiper
Quoted 44 lines · Petr Vorel
Quoted 29 lines

From: Jeff Mahoney <[email protected]>

sysfs_partition_path calls udevadm to resolve the sysfs path for a block device. That can be accomplished by stating the device node and using the major/minor to follow the symlinks in /sys/dev/block/.

This cuts the execution time of grub2-mkconfig from 10s to 2s on my system.

[ pvorel: include grub/osdep/major.h ]
---
 grub-core/osdep/linux/hostdisk.c | 8 ++++++++
 1 file changed, 8 insertions(+)
grub-core/osdep/linux/hostdisk.c +1 -0
index da62f924e..43dc4b0ba 100644
@@ -31,6 +31,7 @@
 #include <grub/misc.h>
 #include <grub/i18n.h>
 #include <grub/list.h>
+#include <grub/osdep/major.h>
 
 #include <stdio.h>
 #include <stdlib.h>

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 112

Quoted 7 lines
grub-core/osdep/linux/hostdisk.c +4 -0
@@ -105,6 +106,13 @@ sysfs_partition_path (const char *dev, const char *entry)
   char *buf = NULL;
   size_t len = 0;
   char *path = NULL;
+  struct stat st;
+  int ret;
+
+  ret = stat(dev, &st);

Missing space between "stat" and "(".

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 113

Quoted 1 line
grub-core/osdep/linux/hostdisk.c +1 -0
+  if (ret == 0 && S_ISBLK(st.st_mode))

Same for S_ISBLK...

Quoted 8 lines

grub-core/osdep/linux/hostdisk.c sysfs_partition_path (const char *dev, const char *entry) · line 118

Quoted 5 lines
grub-core/osdep/linux/hostdisk.c +2 -0
+    return xasprintf ("/sys/dev/block/%u:%u/%s",
+		      major (st.st_rdev), minor (st.st_rdev), entry);
 
   argv[0] = "udevadm";
   argv[1] = "info";

Do we really need udevadm fallback mechanism? If something went wrong here for us I do not expect it will work for udevadm either.

Quoted 7 lines

I suspect not. 'udevadm info --query path --name <dev>' does essentially the same thing but with many more steps and the cost starting an external program for every block device. I left it in with the assumption that it was probably there for a reason and perhaps udevadm on early udev systems did something more special than just doing stat + sysfs path building. I didn't spend any time on validating that assumption.

Quoted 2 lines

As it'd be wort to remove udevadm fallback, I'll stop being lazy and setup VM to test.

Please do. I prefer to remove this fallback because it will be mostly dead code after your fix.

Daniel