diff mbox series

lib/tst_device.c: Add exception handling of function tst_find_free_loopdev

Message ID 20210310081048.27993-1-zhaogongyi@huawei.com
State Rejected
Headers show
Series lib/tst_device.c: Add exception handling of function tst_find_free_loopdev | expand

Commit Message

Zhao Gongyi March 10, 2021, 8:10 a.m. UTC
Because of race condition or abnormal env, set_dev_path may be
return 1. And when set_dev_path return 1 in tst_find_free_loopdev,
we should return Immediately.

Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
 lib/tst_device.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

--
2.17.1

Comments

Petr Vorel March 10, 2021, 11:13 a.m. UTC | #1
Hi Gongyi,

> Because of race condition or abnormal env, set_dev_path may be
> return 1. And when set_dev_path return 1 in tst_find_free_loopdev,
You mean return 0, right?

> we should return Immediately.

Not really sure about this patch, mainly question for others.
Could you please more describe your problem?

Kind regards,
Petr
Li Wang March 10, 2021, 12:20 p.m. UTC | #2
Hi Gongyi,

On Wed, Mar 10, 2021 at 4:11 PM Zhao Gongyi <zhaogongyi@huawei.com> wrote:

> Because of race condition or abnormal env, set_dev_path may be
> return 1. And when set_dev_path return 1 in tst_find_free_loopdev,
> we should return Immediately.
>

If there exists a race condition, I firstly think of the TST_RETRY_FUNC
macro
to try more times for the set_dev_path. That might help to get the function
success
in the follow-up tries.



>
> Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
> ---
>  lib/tst_device.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/lib/tst_device.c b/lib/tst_device.c
> index c096b418b..51cf1ba7e 100644
> --- a/lib/tst_device.c
> +++ b/lib/tst_device.c
> @@ -83,8 +83,13 @@ int tst_find_free_loopdev(char *path, size_t path_len)
>                 rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
>                 close(ctl_fd);
>                 if (rc >= 0) {
> -                       if (path)
> -                               set_dev_path(rc, path, path_len);
> +                       if (path)
> +                               if (!set_dev_path(rc, path, path_len)) {
> +                                       tst_resm(TINFO,
> +                                               "loop device not exist");
> +                                       return -1;
> +                               }
> +
>                         tst_resm(TINFO, "Found free device %d '%s'",
>                                 rc, path ?: "");
>                         return rc;
> --
> 2.17.1
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
>
>
Petr Vorel March 10, 2021, 2:03 p.m. UTC | #3
Hi,

> > Because of race condition or abnormal env, set_dev_path may be
> > return 1. And when set_dev_path return 1 in tst_find_free_loopdev,
> > we should return Immediately.


> If there exists a race condition, I firstly think of the TST_RETRY_FUNC
> macro
> to try more times for the set_dev_path. That might help to get the function
> success
> in the follow-up tries.

+1

Kind regards,
Petr
diff mbox series

Patch

diff --git a/lib/tst_device.c b/lib/tst_device.c
index c096b418b..51cf1ba7e 100644
--- a/lib/tst_device.c
+++ b/lib/tst_device.c
@@ -83,8 +83,13 @@  int tst_find_free_loopdev(char *path, size_t path_len)
 		rc = ioctl(ctl_fd, LOOP_CTL_GET_FREE);
 		close(ctl_fd);
 		if (rc >= 0) {
-			if (path)
-				set_dev_path(rc, path, path_len);
+			if (path)
+				if (!set_dev_path(rc, path, path_len)) {
+					tst_resm(TINFO,
+						"loop device not exist");
+					return -1;
+				}
+
 			tst_resm(TINFO, "Found free device %d '%s'",
 				rc, path ?: "");
 			return rc;