Skip to content

Commit 4ce33ec

Browse files
Davidlohr Buesotorvalds
authored andcommitted
ipc/sem: optimize perform_atomic_semop()
This is the main workhorse that deals with semop user calls such that the waitforzero or semval update operations, on the set, can complete on not as the sma currently stands. Currently, the set is iterated twice (setting semval, then backwards for the sempid value). Slowpaths, and particularly SEM_UNDO calls, must undo any altered sem when it is detected that the caller must block or has errored-out. With larger sets, there can occur situations where this involves a lot of cycles and can obviously be a suboptimal use of cached resources in shared memory. Ie, discarding CPU caches that are also calling semop and have the sembuf cached (and can complete), while the current lock holder doing the semop will block, error, or does a waitforzero operation. This patch proposes still iterating the set twice, but the first scan is read-only, and we perform the actual updates afterward, once we know that the call will succeed. In order to not suffer from the overhead of dealing with sops that act on the same sem_num, such (rare) cases use perform_atomic_semop_slow(), which is exactly what we have now. Duplicates are detected before grabbing sem_lock, and uses simple a 32/64-bit hash array variable to based on the sem_num we are working on. In addition add some comments to when we expect to the caller to block. [akpm@linux-foundation.org: coding-style fixes] [colin.king@canonical.com: ensure we left shift a ULL rather than a 32 bit integer] Link: http://lkml.kernel.org/r/20161028181129.7311-1-colin.king@canonical.com Link: http://lkml.kernel.org/r/20160921194603.GB21438@linux-80c1.suse Signed-off-by: Davidlohr Bueso <dbueso@suse.de> Cc: Manfred Spraul <manfred@colorfullife.com> Signed-off-by: Colin Ian King <colin.king@canonical.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9ae949f commit 4ce33ec

1 file changed

Lines changed: 102 additions & 10 deletions

File tree

‎ipc/sem.c‎

Lines changed: 102 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ struct sem_queue {
115115
struct sembuf *sops; /* array of pending operations */
116116
struct sembuf *blocking; /* the operation that blocked */
117117
int nsops; /* number of operations */
118-
int alter; /* does *sops alter the array? */
118+
bool alter; /* does *sops alter the array? */
119+
bool dupsop; /* sops on more than one sem_num */
119120
};
120121

121122
/* Each task has a list of undo requests. They are executed automatically
@@ -587,15 +588,23 @@ SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
587588
}
588589

589590
/**
590-
* perform_atomic_semop - Perform (if possible) a semaphore operation
591+
* perform_atomic_semop[_slow] - Attempt to perform semaphore
592+
* operations on a given array.
591593
* @sma: semaphore array
592594
* @q: struct sem_queue that describes the operation
593595
*
596+
* Caller blocking are as follows, based the value
597+
* indicated by the semaphore operation (sem_op):
598+
*
599+
* (1) >0 never blocks.
600+
* (2) 0 (wait-for-zero operation): semval is non-zero.
601+
* (3) <0 attempting to decrement semval to a value smaller than zero.
602+
*
594603
* Returns 0 if the operation was possible.
595604
* Returns 1 if the operation is impossible, the caller must sleep.
596-
* Negative values are error codes.
605+
* Returns <0 for error codes.
597606
*/
598-
static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
607+
static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
599608
{
600609
int result, sem_op, nsops, pid;
601610
struct sembuf *sop;
@@ -666,6 +675,72 @@ static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
666675
return result;
667676
}
668677

678+
static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
679+
{
680+
int result, sem_op, nsops;
681+
struct sembuf *sop;
682+
struct sem *curr;
683+
struct sembuf *sops;
684+
struct sem_undo *un;
685+
686+
sops = q->sops;
687+
nsops = q->nsops;
688+
un = q->undo;
689+
690+
if (unlikely(q->dupsop))
691+
return perform_atomic_semop_slow(sma, q);
692+
693+
/*
694+
* We scan the semaphore set twice, first to ensure that the entire
695+
* operation can succeed, therefore avoiding any pointless writes
696+
* to shared memory and having to undo such changes in order to block
697+
* until the operations can go through.
698+
*/
699+
for (sop = sops; sop < sops + nsops; sop++) {
700+
curr = sma->sem_base + sop->sem_num;
701+
sem_op = sop->sem_op;
702+
result = curr->semval;
703+
704+
if (!sem_op && result)
705+
goto would_block; /* wait-for-zero */
706+
707+
result += sem_op;
708+
if (result < 0)
709+
goto would_block;
710+
711+
if (result > SEMVMX)
712+
return -ERANGE;
713+
714+
if (sop->sem_flg & SEM_UNDO) {
715+
int undo = un->semadj[sop->sem_num] - sem_op;
716+
717+
/* Exceeding the undo range is an error. */
718+
if (undo < (-SEMAEM - 1) || undo > SEMAEM)
719+
return -ERANGE;
720+
}
721+
}
722+
723+
for (sop = sops; sop < sops + nsops; sop++) {
724+
curr = sma->sem_base + sop->sem_num;
725+
sem_op = sop->sem_op;
726+
result = curr->semval;
727+
728+
if (sop->sem_flg & SEM_UNDO) {
729+
int undo = un->semadj[sop->sem_num] - sem_op;
730+
731+
un->semadj[sop->sem_num] = undo;
732+
}
733+
curr->semval += sem_op;
734+
curr->sempid = q->pid;
735+
}
736+
737+
return 0;
738+
739+
would_block:
740+
q->blocking = sop;
741+
return sop->sem_flg & IPC_NOWAIT ? -EAGAIN : 1;
742+
}
743+
669744
static inline void wake_up_sem_queue_prepare(struct sem_queue *q, int error,
670745
struct wake_q_head *wake_q)
671746
{
@@ -1720,9 +1795,10 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
17201795
struct sembuf fast_sops[SEMOPM_FAST];
17211796
struct sembuf *sops = fast_sops, *sop;
17221797
struct sem_undo *un;
1723-
int undos = 0, alter = 0, max, locknum;
1798+
int max, locknum;
1799+
bool undos = false, alter = false, dupsop = false;
17241800
struct sem_queue queue;
1725-
unsigned long jiffies_left = 0;
1801+
unsigned long dup = 0, jiffies_left = 0;
17261802
struct ipc_namespace *ns;
17271803

17281804
ns = current->nsproxy->ipc_ns;
@@ -1736,10 +1812,12 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
17361812
if (sops == NULL)
17371813
return -ENOMEM;
17381814
}
1815+
17391816
if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
17401817
error = -EFAULT;
17411818
goto out_free;
17421819
}
1820+
17431821
if (timeout) {
17441822
struct timespec _timeout;
17451823
if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
@@ -1753,17 +1831,30 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
17531831
}
17541832
jiffies_left = timespec_to_jiffies(&_timeout);
17551833
}
1834+
17561835
max = 0;
17571836
for (sop = sops; sop < sops + nsops; sop++) {
1837+
unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
1838+
17581839
if (sop->sem_num >= max)
17591840
max = sop->sem_num;
17601841
if (sop->sem_flg & SEM_UNDO)
1761-
undos = 1;
1762-
if (sop->sem_op != 0)
1763-
alter = 1;
1842+
undos = true;
1843+
if (dup & mask) {
1844+
/*
1845+
* There was a previous alter access that appears
1846+
* to have accessed the same semaphore, thus use
1847+
* the dupsop logic. "appears", because the detection
1848+
* can only check % BITS_PER_LONG.
1849+
*/
1850+
dupsop = true;
1851+
}
1852+
if (sop->sem_op != 0) {
1853+
alter = true;
1854+
dup |= mask;
1855+
}
17641856
}
17651857

1766-
17671858
if (undos) {
17681859
/* On success, find_alloc_undo takes the rcu_read_lock */
17691860
un = find_alloc_undo(ns, semid);
@@ -1828,6 +1919,7 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
18281919
queue.undo = un;
18291920
queue.pid = task_tgid_vnr(current);
18301921
queue.alter = alter;
1922+
queue.dupsop = dupsop;
18311923

18321924
error = perform_atomic_semop(sma, &queue);
18331925
if (error == 0) { /* non-blocking succesfull path */

0 commit comments

Comments
 (0)