Skip to content

Commit

Permalink
ipc/sem.c: update sem_otime for all operations
Browse files Browse the repository at this point in the history
In commit 0a2b9d4 ("ipc/sem.c: move wake_up_process out of the
spinlock section"), the update of semaphore's sem_otime(last semop time)
was moved to one central position (do_smart_update).

But since do_smart_update() is only called for operations that modify
the array, this means that wait-for-zero semops do not update sem_otime
anymore.

The fix is simple:
Non-alter operations must update sem_otime.

[[email protected]: coding-style fixes]
Signed-off-by: Manfred Spraul <[email protected]>
Reported-by: Jia He <[email protected]>
Tested-by: Jia He <[email protected]>
Cc: Davidlohr Bueso <[email protected]>
Cc: Mike Galbraith <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
manfred-colorfu authored and torvalds committed Sep 30, 2013
1 parent fb31ba3 commit 0e8c665
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions ipc/sem.c
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,24 @@ static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
return semop_completed;
}

/**
* set_semotime(sma, sops) - set sem_otime
* @sma: semaphore array
* @sops: operations that modified the array, may be NULL
*
* sem_otime is replicated to avoid cache line trashing.
* This function sets one instance to the current time.
*/
static void set_semotime(struct sem_array *sma, struct sembuf *sops)
{
if (sops == NULL) {
sma->sem_base[0].sem_otime = get_seconds();
} else {
sma->sem_base[sops[0].sem_num].sem_otime =
get_seconds();
}
}

/**
* do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
* @sma: semaphore array
Expand Down Expand Up @@ -967,17 +985,10 @@ static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsop
}
}
}
if (otime) {
if (sops == NULL) {
sma->sem_base[0].sem_otime = get_seconds();
} else {
sma->sem_base[sops[0].sem_num].sem_otime =
get_seconds();
}
}
if (otime)
set_semotime(sma, sops);
}


/* The following counts are associated to each semaphore:
* semncnt number of tasks waiting on semval being nonzero
* semzcnt number of tasks waiting on semval being zero
Expand Down Expand Up @@ -1839,12 +1850,17 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,

error = perform_atomic_semop(sma, sops, nsops, un,
task_tgid_vnr(current));
if (error <= 0) {
if (alter && error == 0)
if (error == 0) {
/* If the operation was successful, then do
* the required updates.
*/
if (alter)
do_smart_update(sma, sops, nsops, 1, &tasks);

goto out_unlock_free;
else
set_semotime(sma, sops);
}
if (error <= 0)
goto out_unlock_free;

/* We need to sleep on this operation, so we put the current
* task into the pending queue and go to sleep.
Expand Down

0 comments on commit 0e8c665

Please sign in to comment.