Skip to contentSkip to conversation

eal/linux: add operation LOCK_NB to flock()

PATCH In review [email protected] 3 participants 4 messages Mar 25, 2021 – Jun 30, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal/linux: add operation LOCK_NB to flock()

From: Tonghao Zhang <[email protected]>

The hugepage of different size, 2MB, 1GB may be mounted on the same directory (e.g /dev/hugepages). Then dpdk primary process will be blocked. To address this issue, add the LOCK_NB flags to flock().

$ cat /proc/mounts
...
none /dev/hugepages hugetlbfs rw,seclabel,relatime,pagesize=1024M 0 0
none /dev/hugepages hugetlbfs rw,seclabel,relatime,pagesize=2M 0 0

Add more details for err logs.

---
 lib/librte_eal/linux/eal_hugepage_info.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
lib/librte_eal/linux/eal_hugepage_info.c +5 -2
index d97792cadeb6..1ff76e539053 100644
@@ -451,9 +451,12 @@ hugepage_info_init(void)
 		hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
 
 		/* if blocking lock failed */
-		if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
+		if (flock(hpi->lock_descriptor, LOCK_EX | LOCK_NB) == -1) {
 			RTE_LOG(CRIT, EAL,
-				"Failed to lock hugepage directory!\n");
+				"Failed to lock hugepage directory! "
+				"The hugepage dir (%s) was locked by "
+				"other processes or self twice.\n",
+				hpi->hugedir);
 			break;
 		}
 		/* clear out the hugepages dir from unused pages */
Signature · 1 line
2.27.0
#1 lore raw
Quoted 2 lines · On Thu, Mar 25, 2021 at 4:25 PM <[email protected]> wrote:

From: Tonghao Zhang <[email protected]>

ping

Quoted 39 lines

The hugepage of different size, 2MB, 1GB may be mounted on the same directory (e.g /dev/hugepages). Then dpdk primary process will be blocked. To address this issue, add the LOCK_NB flags to flock().

$ cat /proc/mounts
...
none /dev/hugepages hugetlbfs rw,seclabel,relatime,pagesize=1024M 0 0
none /dev/hugepages hugetlbfs rw,seclabel,relatime,pagesize=2M 0 0

Add more details for err logs.

---
 lib/librte_eal/linux/eal_hugepage_info.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
lib/librte_eal/linux/eal_hugepage_info.c +5 -3
index d97792cadeb6..1ff76e539053 100644
@@ -451,9 +451,12 @@ hugepage_info_init(void)
                hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
 
                /* if blocking lock failed */
-               if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {
+               if (flock(hpi->lock_descriptor, LOCK_EX | LOCK_NB) == -1) {
                        RTE_LOG(CRIT, EAL,
-                               "Failed to lock hugepage directory!\n");
+                               "Failed to lock hugepage directory! "
+                               "The hugepage dir (%s) was locked by "
+                               "other processes or self twice.\n",
+                               hpi->hugedir);
                        break;
                }
                /* clear out the hugepages dir from unused pages */
--

2.27.0

Signature · 1 line
Best regards, Tonghao
#2 lore raw
In reply to [email protected]
Quoted 29 lines · On 25-Mar-21 8:21 AM, [email protected] wrote:

From: Tonghao Zhang <[email protected]>

The hugepage of different size, 2MB, 1GB may be mounted on the same directory (e.g /dev/hugepages). Then dpdk primary process will be blocked. To address this issue, add the LOCK_NB flags to flock().

$ cat /proc/mounts
...
none /dev/hugepages hugetlbfs rw,seclabel,relatime,pagesize=1024M 0 0
none /dev/hugepages hugetlbfs rw,seclabel,relatime,pagesize=2M 0 0

Add more details for err logs.

---
  lib/librte_eal/linux/eal_hugepage_info.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
lib/librte_eal/linux/eal_hugepage_info.c +0 -1
index d97792cadeb6..1ff76e539053 100644
@@ -451,9 +451,12 @@ hugepage_info_init(void)
  		hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
 
  		/* if blocking lock failed */
-		if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {

lib/librte_eal/linux/eal_hugepage_info.c hugepage_info_init(void) · line 462

Quoted 10 lines
lib/librte_eal/linux/eal_hugepage_info.c +5 -1
+		if (flock(hpi->lock_descriptor, LOCK_EX | LOCK_NB) == -1) {
  			RTE_LOG(CRIT, EAL,
-				"Failed to lock hugepage directory!\n");
+				"Failed to lock hugepage directory! "
+				"The hugepage dir (%s) was locked by "
+				"other processes or self twice.\n",
+				hpi->hugedir);
  			break;
  		}
  		/* clear out the hugepages dir from unused pages */

Use cases such as "having two hugetlbfs page sizes on the same hugetlbfs mountpoint" are user error, but i agree that deadlocking is probably not the way we want to go about it.

An alternative way would be to check if we already have a mountpoint with the same path, and this would produce a better error message (as a user, "hugepage dir is locked by self twice" doesn't tell me anything useful), at a cost of slightly more complicated code.

I'm not sure which way i want to go here. Normally, hugetlbfs shouldn't be staying locked for long, so i'm wary of adding a LOCK_NB here, so i feel slightly uneasy about this patch. Do you have any opinions?

Also, do other OS's EALs need similar fix?

Signature · 2 lines
Thanks,
Anatoly
#3 lore raw
Quoted 50 lines · Anatoly
Quoted 29 lines · On 25-Mar-21 8:21 AM, [email protected] wrote:

From: Tonghao Zhang <[email protected]>

The hugepage of different size, 2MB, 1GB may be mounted on the same directory (e.g /dev/hugepages). Then dpdk primary process will be blocked. To address this issue, add the LOCK_NB flags to flock().

$ cat /proc/mounts
...
none /dev/hugepages hugetlbfs rw,seclabel,relatime,pagesize=1024M 0 0
none /dev/hugepages hugetlbfs rw,seclabel,relatime,pagesize=2M 0 0

Add more details for err logs.

---
  lib/librte_eal/linux/eal_hugepage_info.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
lib/librte_eal/linux/eal_hugepage_info.c +0 -1
index d97792cadeb6..1ff76e539053 100644
@@ -451,9 +451,12 @@ hugepage_info_init(void)
  		hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY);
 
  		/* if blocking lock failed */
-		if (flock(hpi->lock_descriptor, LOCK_EX) == -1) {

lib/librte_eal/linux/eal_hugepage_info.c hugepage_info_init(void) · line 462

Quoted 10 lines
lib/librte_eal/linux/eal_hugepage_info.c +5 -1
+		if (flock(hpi->lock_descriptor, LOCK_EX | LOCK_NB) == -1) {
  			RTE_LOG(CRIT, EAL,
-				"Failed to lock hugepage directory!\n");
+				"Failed to lock hugepage directory! "
+				"The hugepage dir (%s) was locked by "
+				"other processes or self twice.\n",
+				hpi->hugedir);
  			break;
  		}
  		/* clear out the hugepages dir from unused pages */

Use cases such as "having two hugetlbfs page sizes on the same hugetlbfs mountpoint" are user error, but i agree that deadlocking is probably not the way we want to go about it.

An alternative way would be to check if we already have a mountpoint with the same path, and this would produce a better error message (as a user, "hugepage dir is locked by self twice" doesn't tell me anything useful), at a cost of slightly more complicated code.

Quoted 6 lines

I'm not sure which way i want to go here. Normally, hugetlbfs shouldn't be staying locked for long, so i'm wary of adding a LOCK_NB here, so i feel slightly uneasy about this patch. Do you have any opinions?

Also, do other OS's EALs need similar fix?

Dropping this patch. It is one of those:
"It hurts when I do this stupid thing" patches.