]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
selftests: ublk: make ublk_stop_io_daemon() more reliable
authorMing Lei <ming.lei@redhat.com>
Mon, 3 Mar 2025 12:43:11 +0000 (20:43 +0800)
committerJens Axboe <axboe@kernel.dk>
Mon, 10 Mar 2025 15:17:13 +0000 (09:17 -0600)
Improve ublk_stop_io_daemon() in the following ways:

- don't wait if ->ublksrv_pid becomes -1, which means that the disk
has been stopped

- don't wait if ublk char device doesn't exist any more, so we can
avoid to rely on inoitfy for wait until the char device is closed

And this way may reduce time of delete command a lot.

Signed-off-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Link: https://lore.kernel.org/r/20250303124324.3563605-2-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
tools/testing/selftests/ublk/kublk.c

index b65bdaf7e281535f7c426427d9b75393997d0bce..2072d880fdc4ec8f1307fe9cc4ab07e4b33b7894 100644 (file)
@@ -691,13 +691,14 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
        return ret;
 }
 
-static int wait_ublk_dev(char *dev_name, int evt_mask, unsigned timeout)
+static int wait_ublk_dev(const char *path, int evt_mask, unsigned timeout)
 {
 #define EV_SIZE (sizeof(struct inotify_event))
 #define EV_BUF_LEN (128 * (EV_SIZE + 16))
        struct pollfd pfd;
        int fd, wd;
        int ret = -EINVAL;
+       const char *dev_name = basename(path);
 
        fd = inotify_init();
        if (fd < 0) {
@@ -761,18 +762,23 @@ static int ublk_stop_io_daemon(const struct ublk_dev *dev)
        char ublkc[64];
        int ret = 0;
 
+       if (daemon_pid < 0)
+               return 0;
+
        /* daemon may be dead already */
        if (kill(daemon_pid, 0) < 0)
                goto wait;
 
-       /*
-        * Wait until ublk char device is closed, when our daemon is shutdown
-        */
-       snprintf(ublkc, sizeof(ublkc), "%s%d", "ublkc", dev_id);
-       ret = wait_ublk_dev(ublkc, IN_CLOSE_WRITE, 10);
-       /* double check and inotify may not be 100% reliable */
+       snprintf(ublkc, sizeof(ublkc), "/dev/%s%d", "ublkc", dev_id);
+
+       /* ublk char device may be gone already */
+       if (access(ublkc, F_OK) != 0)
+               goto wait;
+
+       /* Wait until ublk char device is closed, when the daemon is shutdown */
+       ret = wait_ublk_dev(ublkc, IN_CLOSE, 10);
+       /* double check and since it may be closed before starting inotify */
        if (ret == -ETIMEDOUT)
-               /* the daemon doesn't exist now if kill(0) fails */
                ret = kill(daemon_pid, 0) < 0;
 wait:
        waitpid(daemon_pid, NULL, 0);
@@ -910,8 +916,6 @@ static int __cmd_dev_del(struct dev_ctx *ctx)
                                __func__, dev->dev_info.ublksrv_pid, number, ret);
        ublk_ctrl_del_dev(dev);
 fail:
-       if (ret >= 0)
-               ret = ublk_ctrl_get_info(dev);
        ublk_ctrl_deinit(dev);
 
        return (ret >= 0) ? 0 : ret;