Skip to contentSkip to conversation

ipc/sem.c: add a printk_once for semctl(GETNCNT/GETZCNT)

PATCH Manfred Spraul 3 participants 4 messages May 25, 2014 – May 26, 2014

lore
Share & tools
lore
#0 lore raw

The actual Linux implementation for semctl(GETNCNT) and semctl(GETZCNT) always (since 0.99.10) reported a thread as sleeping on all semaphores that are listed in the semop() call.
The documented behavior (both in the Linux man page and in the Single Unix Specification) is that a task should be reported on exactly one semaphore:
The semaphore that caused the thread to got to sleep.

This patch adds a printk_once() that is triggered if a thread hits the relevant case.

Cc Davidlohr Bueso <[email protected]>
Cc Michael Kerrisk <[email protected]>
---
 ipc/sem.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
ipc/sem.c +12 -0
index 9106321..3cc2f7b6 100644
@@ -1000,6 +1000,18 @@ static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
 {
 	struct sembuf *sop = q->blocking;
 
+	/*
+	 * Linux always (since 0.99.10) reported a task as sleeping on all
+	 * semaphores. This violates SUS, therefore it was changed to the
+	 * standard compliant behavior.
+	 * Give the administrators a chance to notice that an application
+	 * might misbehave because it relies on the Linux behavior.
+	 */
+	printk_once(KERN_INFO "semctl(GETNCNT/GETZCNT) is since 3.16 Single " \
+				"Unix Specification compliant.\n" \
+				"The task %d triggered the difference, " \
+				"watch for misbehavior.", current->pid);
+
 	if (sop->sem_num != semnum)
 		return 0;
 
Signature · 1 line
1.9.0
#1 lore raw
Quoted 9 lines · Manfred Spraul

The actual Linux implementation for semctl(GETNCNT) and semctl(GETZCNT) always (since 0.99.10) reported a thread as sleeping on all semaphores that are listed in the semop() call.
The documented behavior (both in the Linux man page and in the Single Unix Specification) is that a task should be reported on exactly one semaphore:
The semaphore that caused the thread to got to sleep.

This patch adds a printk_once() that is triggered if a thread hits the relevant case.

[]

ipc/sem.c

Quoted 1 line
ipc/sem.c +0 -0

[]

Quoted 5 lines
ipc/sem.c +1 -0
@@ -1000,6 +1000,18 @@ static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
 {
 	struct sembuf *sop = q->blocking;
 
+	/*

ipc/sem.c static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q, · line 1013

Quoted 10 lines
ipc/sem.c +10 -0
+	 * Linux always (since 0.99.10) reported a task as sleeping on all
+	 * semaphores. This violates SUS, therefore it was changed to the
+	 * standard compliant behavior.
+	 * Give the administrators a chance to notice that an application
+	 * might misbehave because it relies on the Linux behavior.
+	 */
+	printk_once(KERN_INFO "semctl(GETNCNT/GETZCNT) is since 3.16 Single " \
+				"Unix Specification compliant.\n" \
+				"The task %d triggered the difference, " \
+				"watch for misbehavior.", current->pid);

Unnecessary line continuations.
Missing terminating newline after "misbehavior"
Ideally coalesced or broken at linebreaks like:

pr_info_once("semctl(GETNCNT/GETZCNT) is Single Unix Specification compliant since kernel v3.16\n"
"Task %d triggered the difference, watch for misbehavior\n", current->pid);

ipc/sem.c static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q, · line 1017

Quoted 3 lines
ipc/sem.c +0 -0
 	if (sop->sem_num != semnum)
 		return 0;
 

Should the printk_once (which could be pr_info_once or _ratelimited or maybe even emitted at KERN_DEBUG) be done only when the return is 1?

#2 lore raw

Hi Joe,

Quoted 10 lines · Joe Perches

On Sun, 2014-05-25 at 20:21 +0200, Manfred Spraul wrote:

ipc/sem.c +5 -0
+	 */
+	printk_once(KERN_INFO "semctl(GETNCNT/GETZCNT) is since 3.16 Single " \
+				"Unix Specification compliant.\n" \
+				"The task %d triggered the difference, " \
+				"watch for misbehavior.", current->pid);

Unnecessary line continuations.
Missing terminating newline after "misbehavior"
Ideally coalesced or broken at linebreaks like:

Quoted 3 lines

pr_info_once("semctl(GETNCNT/GETZCNT) is Single Unix Specification compliant since kernel v3.16\n"
"Task %d triggered the difference, watch for misbehavior\n", current->pid);

Thanks. I'll try to remember to really run checkpatch instead of assuming what it might report.

Quoted 7 lines
Quoted 3 lines

if (sop->sem_num != semnum)
return 0;

Should the printk_once (which could be pr_info_once or _ratelimited or maybe even emitted at KERN_DEBUG) be done only when the return is 1?

To fully check if there is a difference would mean that the old code and the new code run in parallel.

The code might trigger slightly too often, but since there are zero known users of GETZCNT / GETNCNT simplicity wins.

--
Manfred

#3 lore raw
In reply to Manfred Spraul
Quoted 7 lines · Manfred Spraul

The actual Linux implementation for semctl(GETNCNT) and semctl(GETZCNT) always (since 0.99.10) reported a thread as sleeping on all semaphores that are listed in the semop() call.
The documented behavior (both in the Linux man page and in the Single Unix Specification) is that a task should be reported on exactly one semaphore:
The semaphore that caused the thread to got to sleep.

Quoted 5 lines

This patch adds a printk_once() that is triggered if a thread hits the relevant case.

With some minor suggestions below.

Quoted 13 lines
Cc Michael Kerrisk <[email protected]>
---
 ipc/sem.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
ipc/sem.c +0 -0
index 9106321..3cc2f7b6 100644
@@ -1000,6 +1000,18 @@ static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
 {
 	struct sembuf *sop = q->blocking;
 

ipc/sem.c static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q, · line 1012

Quoted 10 lines
ipc/sem.c +10 -0
+	/*
+	 * Linux always (since 0.99.10) reported a task as sleeping on all
+	 * semaphores. This violates SUS, therefore it was changed to the
+	 * standard compliant behavior.
+	 * Give the administrators a chance to notice that an application
+	 * might misbehave because it relies on the Linux behavior.
+	 */
+	printk_once(KERN_INFO "semctl(GETNCNT/GETZCNT) is since 3.16 Single " \
+				"Unix Specification compliant.\n" \
+				"The task %d triggered the difference, " \
                                           ^^ %u (->pid is unsigned)

It would also be useful to print ->comm. I hate having to lookup the process name with just the pid.