FS/XFS testing framework
 help / color / mirror / Atom feed
From: Stas Sergeev <stsp2@yandex.ru>
To: fstests@vger.kernel.org
Cc: Stas Sergeev <stsp2@yandex.ru>, Murphy Zhou <xzhou@redhat.com>,
	Jeff Layton <jlayton@kernel.org>, Zorro Lang <zlang@redhat.com>
Subject: [PATCH 1/2] t_ofd_locks: fix stalled semaphore handling
Date: Mon, 31 Jul 2023 16:28:06 +0500	[thread overview]
Message-ID: <20230731112807.1463846-2-stsp2@yandex.ru> (raw)
In-Reply-To: <20230731112807.1463846-1-stsp2@yandex.ru>

Currently IPC_RMID was attempted on a semid returned after failed
semget() with flags=IPC_CREAT|IPC_EXCL. So nothing was actually removed.

This patch introduces the much more reliable scheme where the sem key
is provided from the wrapper script, after making sure there are no
stalled semaphores with that key.

This patch uses $BASHPID for sem key, and adds the -K option to
t_ofd_locks to pass the sem key.

CC: fstests@vger.kernel.org
CC: Murphy Zhou <xzhou@redhat.com>
CC: Jeff Layton <jlayton@kernel.org>
CC: Zorro Lang <zlang@redhat.com>

Signed-off-by: Stas Sergeev <stsp2@yandex.ru>
---
 src/t_ofd_locks.c | 48 ++++++++++++-----------------------------------
 tests/generic/478 | 14 ++++++++++----
 2 files changed, 22 insertions(+), 40 deletions(-)

diff --git a/src/t_ofd_locks.c b/src/t_ofd_locks.c
index e77f2659..95739dc7 100644
--- a/src/t_ofd_locks.c
+++ b/src/t_ofd_locks.c
@@ -180,16 +180,16 @@ int main(int argc, char **argv)
 	int setlk_macro = F_OFD_SETLKW;
 	int getlk_macro = F_OFD_GETLK;
 	struct timespec ts;
-	key_t semkey;
+	key_t semkey = -1;
 	unsigned short vals[2];
 	union semun semu;
 	struct semid_ds sem_ds;
 	struct sembuf sop;
-	int opt, ret, retry;
+	int opt, ret;
 
 	//avoid libcap errno bug
 	errno = 0;
-	while((opt = getopt(argc, argv, "sgrwo:l:PRWtFd")) != -1) {
+	while((opt = getopt(argc, argv, "sgrwo:l:PRWtFdK:")) != -1) {
 		switch(opt) {
 		case 's':
 			lock_cmd = 1;
@@ -227,6 +227,9 @@ int main(int argc, char **argv)
 		case 'd':
 			use_dup = 1;
 			break;
+		case 'K':
+			semkey = atoi(optarg);
+			break;
 		default:
 			usage(argv[0]);
 			return -1;
@@ -276,37 +279,15 @@ int main(int argc, char **argv)
 		err_exit("test_ofd_getlk", errno);
 	}
 
-	if((semkey = ftok(argv[optind], 255)) == -1)
+	if (semkey == -1)
+		semkey = ftok(argv[optind], 255);
+	if (semkey == -1)
 		err_exit("ftok", errno);
 
 	/* setlk, and always init the semaphore at setlk time */
 	if (lock_cmd == 1) {
-		/*
-		 * Init the semaphore, with a key related to the testfile.
-		 * getlk routine will wait untill this sem has been created and
-		 * iniialized.
-		 *
-		 * We must make sure the semaphore set is newly created, rather
-		 * then the one left from last run. In which case getlk will
-		 * exit immediately and left setlk routine waiting forever.
-		 * Also because newly created semaphore has zero sem_otime,
-		 * which is used here to sync with getlk routine.
-		 */
-		retry = 0;
-		do {
-			semid = semget(semkey, 2, IPC_CREAT|IPC_EXCL);
-			if (semid < 0 && errno == EEXIST) {
-				/* remove sem set after one round of test */
-				if (semctl(semid, 2, IPC_RMID, semu) == -1)
-					err_exit("rmid 0", errno);
-				retry++;
-			} else if (semid < 0)
-				err_exit("semget", errno);
-			else
-				retry = 10;
-		} while (retry < 5);
-		/* We can't create a new semaphore set in 5 tries */
-		if (retry == 5)
+		semid = semget(semkey, 2, IPC_CREAT|IPC_EXCL);
+		if (semid < 0)
 			err_exit("semget", errno);
 
 		/* Init both new sem to 1 */
@@ -393,7 +374,7 @@ int main(int argc, char **argv)
 	/* getlck */
 	if (lock_cmd == 0) {
 		/* wait sem created and initialized */
-		retry = 5;
+		int retry = 5;
 		do {
 			semid = semget(semkey, 2, 0);
 			if (semid != -1)
@@ -406,11 +387,6 @@ int main(int argc, char **argv)
 				err_exit("getlk_semget", errno);
 			}
 		} while (1);
-		do {
-			memset(&sem_ds, 0, sizeof(sem_ds));
-			semu.buf = &sem_ds;
-			ret = semctl(semid, 0, IPC_STAT, semu);
-		} while (!(ret == 0 && sem_ds.sem_otime != 0));
 
 		/* wait sem0 == 0 (setlk and close fd done) */
 		sop.sem_num = 0;
diff --git a/tests/generic/478 b/tests/generic/478
index 480762d2..2478256b 100755
--- a/tests/generic/478
+++ b/tests/generic/478
@@ -94,6 +94,9 @@ _supported_fs generic
 _require_test
 _require_ofd_locks
 
+# rm possibly stalled sem
+ipcrm -S $BASHPID 2>/dev/null
+
 # real QA test starts here
 # prepare a 4k testfile in TEST_DIR
 $XFS_IO_PROG -f -c "pwrite -S 0xFF 0 4096" \
@@ -101,8 +104,11 @@ $XFS_IO_PROG -f -c "pwrite -S 0xFF 0 4096" \
 
 do_test()
 {
-	local soptions="$1"
-	local goptions="$2"
+	local _so="$1 -K $BASHPID"
+	local _go="$2 -K $BASHPID"
+	shift 2
+	local soptions="$_so"
+	local goptions="$_go"
 	# print options and getlk output for debug
 	echo $* >> $seqres.full 2>&1
 	# -s : do setlk
@@ -113,7 +119,7 @@ do_test()
 	wait $!
 
 	# add -F to clone with CLONE_FILES
-	soptions="$1 -F"
+	soptions="$_so -F"
 	# with -F, new locks are always file to place
 	$here/src/t_ofd_locks $soptions $TEST_DIR/testfile &
 	$here/src/t_ofd_locks $goptions $TEST_DIR/testfile | \
@@ -121,7 +127,7 @@ do_test()
 	wait $!
 
 	# add -d to dup and close
-	soptions="$1 -d"
+	soptions="$_so -d"
 	$here/src/t_ofd_locks $soptions $TEST_DIR/testfile &
 	$here/src/t_ofd_locks $goptions $TEST_DIR/testfile | \
 		tee -a $seqres.full
-- 
2.39.2


  reply	other threads:[~2023-07-31 11:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-31 11:28 [PATCH v2 0/2] t_ofd_locks: ipc semaphore fixes Stas Sergeev
2023-07-31 11:28 ` Stas Sergeev [this message]
2023-07-31 11:28 ` [PATCH 2/2] t_ofd_locks: fix sem initialization sequence Stas Sergeev
  -- strict thread matches above, loose matches on Subject: below --
2023-08-01 17:52 [PATCH v3 0/2] t_ofd_locks: ipc semaphore fixes Stas Sergeev
2023-08-01 17:52 ` [PATCH 1/2] t_ofd_locks: fix stalled semaphore handling Stas Sergeev
2023-08-11 11:39   ` Jeff Layton
2023-08-11 11:53     ` stsp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230731112807.1463846-2-stsp2@yandex.ru \
    --to=stsp2@yandex.ru \
    --cc=fstests@vger.kernel.org \
    --cc=jlayton@kernel.org \
    --cc=xzhou@redhat.com \
    --cc=zlang@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox