diff mbox series

[v2,1/2] shmget03: don't depend on existed shared resources

Message ID 20210712075223.10682-1-aleksei.kodanev@bell-sw.com
State Superseded
Headers show
Series [v2,1/2] shmget03: don't depend on existed shared resources | expand

Commit Message

Alexey Kodanev July 12, 2021, 7:52 a.m. UTC
It's unlikely, but still possible that some of them could be
created/released during the test as well, so the patch only
checks errno.

Signed-off-by: Alexey Kodanev <aleksei.kodanev@bell-sw.com>
---
v2: * Move the loop to the test run function and try to get
      ENOSPC errno there.
    * Rename queues* to segments*

 .../kernel/syscalls/ipc/shmget/shmget03.c     | 42 ++++++++++---------
 1 file changed, 22 insertions(+), 20 deletions(-)

Comments

Li Wang July 12, 2021, 8:28 a.m. UTC | #1
On Mon, Jul 12, 2021 at 3:54 PM Alexey Kodanev <aleksei.kodanev@bell-sw.com>
wrote:

> It's unlikely, but still possible that some of them could be
> created/released during the test as well, so the patch only
> checks errno.
>
> Signed-off-by: Alexey Kodanev <aleksei.kodanev@bell-sw.com>
> ---
> v2: * Move the loop to the test run function and try to get
>       ENOSPC errno there.
>

I'm fine to go with this but move the loop to test run without any
limit will bring new fail if running with parameter '-i 2'.

We have to handle that situation (maybe add a judgment to skip
test for run more times) in case someone uses it like:

# ./shmget03 -i 2
tst_test.c:1344: TINFO: Timeout per run is 0h 05m 00s
shmget03.c:44: TPASS: Maximum number of segments reached (4096), used by
test 4096
shmget03.c:41: TFAIL: Failed to trigger ENOSPC error: EEXIST (17)
Alexey Kodanev July 12, 2021, 8:37 a.m. UTC | #2
On 12.07.2021 11:28, Li Wang wrote:
> 
> 
> On Mon, Jul 12, 2021 at 3:54 PM Alexey Kodanev <aleksei.kodanev@bell-sw.com <mailto:aleksei.kodanev@bell-sw.com>> wrote:
> 
>     It's unlikely, but still possible that some of them could be
>     created/released during the test as well, so the patch only
>     checks errno.
> 
>     Signed-off-by: Alexey Kodanev <aleksei.kodanev@bell-sw.com <mailto:aleksei.kodanev@bell-sw.com>>
>     ---
>     v2: * Move the loop to the test run function and try to get
>           ENOSPC errno there.
> 
> 
> I'm fine to go with this but move the loop to test run without any
> limit will bring new fail if running with parameter '-i 2'.
> 
> We have to handle that situation (maybe add a judgment to skip
> test for run more times) in case someone uses it like:

Or just release them asap after tpass?

> 
> # ./shmget03 -i 2
> tst_test.c:1344: TINFO: Timeout per run is 0h 05m 00s
> shmget03.c:44: TPASS: Maximum number of segments reached (4096), used by test 4096
> shmget03.c:41: TFAIL: Failed to trigger ENOSPC error: EEXIST (17)
> 
> -- 
> Regards,
> Li Wang
Li Wang July 12, 2021, 8:42 a.m. UTC | #3
On Mon, Jul 12, 2021 at 4:37 PM Alexey Kodanev <aleksei.kodanev@bell-sw.com>
wrote:

> On 12.07.2021 11:28, Li Wang wrote:
> >
> >
> > On Mon, Jul 12, 2021 at 3:54 PM Alexey Kodanev <
> aleksei.kodanev@bell-sw.com <mailto:aleksei.kodanev@bell-sw.com>> wrote:
> >
> >     It's unlikely, but still possible that some of them could be
> >     created/released during the test as well, so the patch only
> >     checks errno.
> >
> >     Signed-off-by: Alexey Kodanev <aleksei.kodanev@bell-sw.com <mailto:
> aleksei.kodanev@bell-sw.com>>
> >     ---
> >     v2: * Move the loop to the test run function and try to get
> >           ENOSPC errno there.
> >
> >
> > I'm fine to go with this but move the loop to test run without any
> > limit will bring new fail if running with parameter '-i 2'.
> >
> > We have to handle that situation (maybe add a judgment to skip
> > test for run more times) in case someone uses it like:
>
> Or just release them asap after tpass?


Sure, but looks a bit redundant.

Or we can just adding a global varible for saving num:

--- a/testcases/kernel/syscalls/ipc/shmget/shmget03.c
+++ b/testcases/kernel/syscalls/ipc/shmget/shmget03.c
@@ -22,7 +22,7 @@
 #include "libnewipc.h"

 static int *segments;
-static int maxshms, segments_cnt;
+static int number = 0, maxshms, segments_cnt;
 static key_t shmkey;

 static void verify_shmget(void)
@@ -30,7 +30,7 @@ static void verify_shmget(void)
        int res = 0, num;

        errno = 0;
-       for (num = 0; num <= maxshms; ++num) {
+       for (num = number; num <= maxshms; ++num) {
                res = shmget(shmkey + num, SHM_SIZE, IPC_CREAT | IPC_EXCL |
SHM_RW);
                if (res == -1)
                        break;
@@ -42,6 +42,8 @@ static void verify_shmget(void)

        tst_res(TPASS, "Maximum number of segments reached (%d), used by
test %d",
                maxshms, segments_cnt);
+
+       number = num;
 }

 static void setup(void)
Li Wang July 12, 2021, 8:55 a.m. UTC | #4
On Mon, Jul 12, 2021 at 4:42 PM Li Wang <liwang@redhat.com> wrote:

>
>
> On Mon, Jul 12, 2021 at 4:37 PM Alexey Kodanev <
> aleksei.kodanev@bell-sw.com> wrote:
>
>> On 12.07.2021 11:28, Li Wang wrote:
>> >
>> >
>> > On Mon, Jul 12, 2021 at 3:54 PM Alexey Kodanev <
>> aleksei.kodanev@bell-sw.com <mailto:aleksei.kodanev@bell-sw.com>> wrote:
>> >
>> >     It's unlikely, but still possible that some of them could be
>> >     created/released during the test as well, so the patch only
>> >     checks errno.
>> >
>> >     Signed-off-by: Alexey Kodanev <aleksei.kodanev@bell-sw.com <mailto:
>> aleksei.kodanev@bell-sw.com>>
>> >     ---
>> >     v2: * Move the loop to the test run function and try to get
>> >           ENOSPC errno there.
>> >
>> >
>> > I'm fine to go with this but move the loop to test run without any
>> > limit will bring new fail if running with parameter '-i 2'.
>> >
>> > We have to handle that situation (maybe add a judgment to skip
>> > test for run more times) in case someone uses it like:
>>
>> Or just release them asap after tpass?
>
>
> Sure, but looks a bit redundant.
>
> Or we can just adding a global varible for saving num:
>
> --- a/testcases/kernel/syscalls/ipc/shmget/shmget03.c
> +++ b/testcases/kernel/syscalls/ipc/shmget/shmget03.c
> @@ -22,7 +22,7 @@
>  #include "libnewipc.h"
>
>  static int *segments;
> -static int maxshms, segments_cnt;
> +static int number = 0, maxshms, segments_cnt;
>  static key_t shmkey;
>
>  static void verify_shmget(void)
> @@ -30,7 +30,7 @@ static void verify_shmget(void)
>         int res = 0, num;
>
>         errno = 0;
> -       for (num = 0; num <= maxshms; ++num) {
> +       for (num = number; num <= maxshms; ++num) {
>

Oh, this method is thoughtless, because if the test gets ENOSPC at
the last looping time, which means num == maxshms, then the global
number will be larger than maxshms, so the test won't fall into
for() loop next time and report FAIL again.

So, let's go with your way: release them after TPASS. This is safer.



>                 res = shmget(shmkey + num, SHM_SIZE, IPC_CREAT | IPC_EXCL
> | SHM_RW);
>                 if (res == -1)
>                         break;
> @@ -42,6 +42,8 @@ static void verify_shmget(void)
>
>         tst_res(TPASS, "Maximum number of segments reached (%d), used by
> test %d",
>                 maxshms, segments_cnt);
> +
> +       number = num;
>  }
>
>  static void setup(void)
>
>
> --
> Regards,
> Li Wang
>
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/ipc/shmget/shmget03.c b/testcases/kernel/syscalls/ipc/shmget/shmget03.c
index efbc465e1..5dc5d55fd 100644
--- a/testcases/kernel/syscalls/ipc/shmget/shmget03.c
+++ b/testcases/kernel/syscalls/ipc/shmget/shmget03.c
@@ -21,47 +21,49 @@ 
 #include "tst_safe_sysv_ipc.h"
 #include "libnewipc.h"
 
-static int *queues;
-static int maxshms, queue_cnt;
+static int *segments;
+static int maxshms, segments_cnt;
 static key_t shmkey;
 
 static void verify_shmget(void)
 {
-	TST_EXP_FAIL2(shmget(shmkey + maxshms, SHM_SIZE, IPC_CREAT | IPC_EXCL | SHM_RW), ENOSPC,
-		"shmget(%i, %i, %i)", shmkey + maxshms, SHM_SIZE, IPC_CREAT | IPC_EXCL | SHM_RW);
+	int res = 0, num;
+
+	errno = 0;
+	for (num = 0; num <= maxshms; ++num) {
+		res = shmget(shmkey + num, SHM_SIZE, IPC_CREAT | IPC_EXCL | SHM_RW);
+		if (res == -1)
+			break;
+		segments[segments_cnt++] = res;
+	}
+
+	if (res != -1 || errno != ENOSPC)
+		tst_brk(TFAIL | TERRNO, "Failed to trigger ENOSPC error");
+
+	tst_res(TPASS, "Maximum number of segments reached (%d), used by test %d",
+		maxshms, segments_cnt);
 }
 
 static void setup(void)
 {
-	int res, num;
-
 	shmkey = GETIPCKEY();
 
 	SAFE_FILE_SCANF("/proc/sys/kernel/shmmni", "%i", &maxshms);
 
-	queues = SAFE_MALLOC(maxshms * sizeof(int));
-	for (num = 0; num < maxshms; num++) {
-		res = shmget(shmkey + num, SHM_SIZE, IPC_CREAT | IPC_EXCL | SHM_RW);
-		if (res == -1)
-			tst_brk(TBROK | TERRNO, "shmget failed unexpectedly");
-
-		queues[queue_cnt++] = res;
-	}
-	tst_res(TINFO, "The maximum number of memory segments (%d) has been reached",
-		maxshms);
+	segments = SAFE_MALLOC((maxshms + 1) * sizeof(int));
 }
 
 static void cleanup(void)
 {
 	int num;
 
-	if (!queues)
+	if (!segments)
 		return;
 
-	for (num = 0; num < queue_cnt; num++)
-		SAFE_SHMCTL(queues[num], IPC_RMID, NULL);
+	for (num = 0; num < segments_cnt; num++)
+		SAFE_SHMCTL(segments[num], IPC_RMID, NULL);
 
-	free(queues);
+	free(segments);
 }
 
 static struct tst_test test = {