Skip to contentSkip to conversation

osdep: Retry SETLK upon EINTR

PATCH Applied Fam Zheng 3 participants 4 messages Dec 26, 2017 – Jan 8, 2018

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH 1/1 osdep: Retry SETLK upon EINTR

We could hit lock failure if there is a signal that makes fcntl return -1 and errno set to EINTR. In this case we should retry.

---
 util/osdep.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
util/osdep.c +3 -1
index 1231f9f876..a73de0e1ba 100644
@@ -244,7 +244,9 @@ static int qemu_lock_fcntl(int fd, int64_t start, int64_t len, int fl_type)
         .l_type   = fl_type,
     };
     qemu_probe_lock_ops();
-    ret = fcntl(fd, fcntl_op_setlk, &fl);
+    do {
+        ret = fcntl(fd, fcntl_op_setlk, &fl);
+    } while (ret == -1 && errno == EINTR);
     return ret == -1 ? -errno : 0;
 }
 
Signature · 1 line
2.14.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Fam Zheng

We could hit lock failure if there is a signal that makes fcntl return -1 and errno set to EINTR. In this case we should retry.

Did you hit this in practice? In 'man fcntl' on my Fedora 27 box, the DESCRIPTION section only mentions EINTR as possible for F_[OFD_]SETLKW, but we don't appear to be using that one (just SETLK and GETLK). On the other hand, the ERRORS section of the same document mentions:

EINTR cmd is F_SETLKW or F_OFD_SETLKW and the operation was
inter‐
rupted by a signal; see signal(7).

EINTR cmd is F_GETLK, F_SETLK, F_OFD_GETLK, or F_OFD_SETLK,
and the
operation was interrupted by a signal before the
lock was
checked or acquired. Most likely when locking a remote
file
(e.g., locking over NFS), but can sometimes happen locally.

(I hate it when information differs between two places in the same document, especially if I only read the first place)

Quoted 12 lines
---
 util/osdep.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
util/osdep.c +0 -0
index 1231f9f876..a73de0e1ba 100644

util/osdep.c static int qemu_lock_fcntl(int fd, int64_t start, int64_t len, int fl_type) · line 249

Quoted 7 lines
util/osdep.c +3 -1
@@ -244,7 +244,9 @@ static int qemu_lock_fcntl(int fd, int64_t start, int64_t len, int fl_type)
         .l_type   = fl_type,
     };
     qemu_probe_lock_ops();
-    ret = fcntl(fd, fcntl_op_setlk, &fl);
+    do {
+        ret = fcntl(fd, fcntl_op_setlk, &fl);
+    } while (ret == -1 && errno == EINTR);

The change makes sense from a maintenance point of view, whether or not you hit it in practice.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#2 lore raw
Quoted 22 lines · Eric Blake
Quoted 2 lines · Fam Zheng

We could hit lock failure if there is a signal that makes fcntl return -1 and errno set to EINTR. In this case we should retry.

Did you hit this in practice? In 'man fcntl' on my Fedora 27 box, the DESCRIPTION section only mentions EINTR as possible for F_[OFD_]SETLKW, but we don't appear to be using that one (just SETLK and GETLK). On the other hand, the ERRORS section of the same document mentions:

EINTR cmd is F_SETLKW or F_OFD_SETLKW and the operation was
inter‐
rupted by a signal; see signal(7).

EINTR cmd is F_GETLK, F_SETLK, F_OFD_GETLK, or F_OFD_SETLK,
and the
operation was interrupted by a signal before the
lock was
checked or acquired. Most likely when locking a remote
file
(e.g., locking over NFS), but can sometimes happen locally.

Quoted 2 lines

(I hate it when information differs between two places in the same document, especially if I only read the first place)

Yes, our QE found it when hammering qemu-img convert with SIGUSR1. So both SETLK and SETLKW can get EINTR.

Quoted 8 lines
Quoted 8 lines
---
 util/osdep.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines

util/osdep.c static int qemu_lock_fcntl(int fd, int64_t start, int64_t len, int fl_type) · line 249

Quoted 12 lines
util/osdep.c +3 -1
index 1231f9f876..a73de0e1ba 100644
@@ -244,7 +244,9 @@ static int qemu_lock_fcntl(int fd, int64_t start, int64_t len, int fl_type)
         .l_type   = fl_type,
     };
     qemu_probe_lock_ops();
-    ret = fcntl(fd, fcntl_op_setlk, &fl);
+    do {
+        ret = fcntl(fd, fcntl_op_setlk, &fl);
+    } while (ret == -1 && errno == EINTR);

The change makes sense from a maintenance point of view, whether or not you hit it in practice.

Thank you for reviewing!

Fam