linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jia He <jiakernel@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Davidlohr Bueso <davidlohr.bueso@hp.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Rik van Riel <riel@redhat.com>,
	Manfred Spraul <manfred@colorfullife.com>,
	Al Viro <viro@zeniv.linux.org.uk>, Jia He <jiakernel@gmail.com>
Subject: [PATCH] ipc/sem.c: fix update sem_otime when calling sem_op in semaphore initialization
Date: Sun, 22 Sep 2013 10:11:24 +0800	[thread overview]
Message-ID: <1379815884-11035-1-git-send-email-jiakernel@gmail.com> (raw)

In commit 0a2b9d4c,the update of semaphore's sem_otime(last semop time)
was removed because he wanted to move setting sem->sem_otime to one
place. But after that, the initial semop() will not set the otime
because its sem_op value is 0(in semtimedop,will not change
otime if alter == 1).

the error case:
process_a(server)       process_b(client)
semget()
semctl(SETVAL)
semop()
                        semget()
                        setctl(IP_STAT)
                        for(;;) {               <--not successful here
                          check until sem_otime > 0
                        }

provide test codes here:
$cat server.c

int semid;
int main()
{
	int key;
	struct semid64_ds sem_info;
	union semun arg;
	struct sembuf sop;

	key = ftok(SEM_PATH,'a');
	semid = semget(key,1,IPC_CREAT|IPC_EXCL|00666);
	if(semid < 0)
		perror("server:semget");

	arg.val = 0;
	if(semctl(semid,0,SETVAL,arg) == -1)
	        perror("semctl setval error");

	sop.sem_num = 0;
	sop.sem_op = 0;
	sop.sem_flg = 0;
	if (semop(semid, &sop, 1) == -1)
		perror("semop error");

	sleep(30);

	if(semctl(semid, 0, IPC_RMID) == -1)
		perror("semctl IPC_RMID");
	else printf("remove sem ok\n");
}

$cat client.c
int semid;
int main()
{
	int i;
	int key;
	union semun arg;
	struct semid64_ds sem_info;

	key = ftok(SEM_PATH,'a');
	semid = semget(key,1,IPC_CREAT|00666);
	if(semid < 0)
		perror("client:semget");
	for (i = 0; i < MAX_TRIES; i++)
	{
		arg.buf = &sem_info;
		semctl(semid, 0, IPC_STAT, arg);
		if (sem_info.sem_otime != 0) break;
		sleep(1);
	}

	if(MAX_TRIES == i)
		printf("error in opening a existed sem\n");
	else
		printf("open exsited sem sucessfully\n");
	return 0;
}

the steps to test:
touch /tmp/my_sem
./server &
sleep 1
./client &

With the patch
1.test output:
error in opening a existed sem
2.cat /proc/sysvipc/sem
the field sem_otime is always zero

Without this patch
1.test output:
open exsited sem sucessfully
2.cat /proc/sysvipc/sem
the field sem_otime is not zero

Signed-off-by: Jia He <jiakernel@gmail.com>
---
 ipc/sem.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/ipc/sem.c b/ipc/sem.c
index 69b6a21..8e01e76 100644
--- a/ipc/sem.c
+++ b/ipc/sem.c
@@ -590,6 +590,7 @@ static int perform_atomic_semop(struct sem_array *sma, struct sembuf *sops,
 		sop--;
 	}
 	
+	sma->sem_base[sops[0].sem_num].sem_otime = get_seconds();
 	return 0;
 
 out_of_range:
-- 
1.8.1.2


             reply	other threads:[~2013-09-22  2:12 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-22  2:11 Jia He [this message]
2013-09-22  8:17 ` [PATCH] ipc/sem.c: fix update sem_otime when calling sem_op in semaphore initialization Mike Galbraith
2013-09-22  8:26   ` Mike Galbraith
2013-09-22  9:34     ` Jia He
2013-09-22 10:00       ` Mike Galbraith
2013-09-22 12:44         ` Jia He
2013-09-22 10:42     ` Manfred Spraul
2013-09-22 12:53       ` Jia He
2013-09-22 15:14       ` Jia He
2013-09-24 21:09         ` Manfred Spraul
2013-09-25  3:05           ` Jia He
2013-09-25  6:55             ` Manfred Spraul
2013-09-25  7:49               ` Jia He
2013-09-23  1:08       ` Mike Galbraith
2013-09-23  2:24         ` Jia He

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=1379815884-11035-1-git-send-email-jiakernel@gmail.com \
    --to=jiakernel@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=davidlohr.bueso@hp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=manfred@colorfullife.com \
    --cc=riel@redhat.com \
    --cc=viro@zeniv.linux.org.uk \
    /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;
as well as URLs for NNTP newsgroup(s).