Skip to content

Commit d12e1e5

Browse files
manfred-colorfutorvalds
authored andcommitted
ipc/sem.c: replace shared sem_otime with per-semaphore value
sem_otime contains the time of the last semaphore operation that completed successfully. Every operation updates this value, thus access from multiple cpus can cause thrashing. Therefore the patch replaces the variable with a per-semaphore variable. The per-array sem_otime is only calculated when required. No performance improvement on a single-socket i3 - only important for larger systems. Signed-off-by: Manfred Spraul <manfred@colorfullife.com> Cc: Rik van Riel <riel@redhat.com> Cc: Davidlohr Bueso <davidlohr.bueso@hp.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f269f40 commit d12e1e5

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

‎include/linux/sem.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ struct task_struct;
1212
struct sem_array {
1313
struct kern_ipc_perm ____cacheline_aligned_in_smp
1414
sem_perm; /* permissions .. see ipc.h */
15-
time_t sem_otime; /* last semop time */
1615
time_t sem_ctime; /* last change time */
1716
struct sem *sem_base; /* ptr to first semaphore in array */
1817
struct list_head pending_alter; /* pending operations */

‎ipc/sem.c‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct sem {
9999
/* that alter the semaphore */
100100
struct list_head pending_const; /* pending single-sop operations */
101101
/* that do not alter the semaphore*/
102+
time_t sem_otime; /* candidate for sem_otime */
102103
} ____cacheline_aligned_in_smp;
103104

104105
/* One queue for each sleeping process in the system. */
@@ -911,8 +912,14 @@ static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsop
911912
}
912913
}
913914
}
914-
if (otime)
915-
sma->sem_otime = get_seconds();
915+
if (otime) {
916+
if (sops == NULL) {
917+
sma->sem_base[0].sem_otime = get_seconds();
918+
} else {
919+
sma->sem_base[sops[0].sem_num].sem_otime =
920+
get_seconds();
921+
}
922+
}
916923
}
917924

918925

@@ -1058,6 +1065,21 @@ static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in,
10581065
}
10591066
}
10601067

1068+
static time_t get_semotime(struct sem_array *sma)
1069+
{
1070+
int i;
1071+
time_t res;
1072+
1073+
res = sma->sem_base[0].sem_otime;
1074+
for (i = 1; i < sma->sem_nsems; i++) {
1075+
time_t to = sma->sem_base[i].sem_otime;
1076+
1077+
if (to > res)
1078+
res = to;
1079+
}
1080+
return res;
1081+
}
1082+
10611083
static int semctl_nolock(struct ipc_namespace *ns, int semid,
10621084
int cmd, int version, void __user *p)
10631085
{
@@ -1131,9 +1153,9 @@ static int semctl_nolock(struct ipc_namespace *ns, int semid,
11311153
goto out_unlock;
11321154

11331155
kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
1134-
tbuf.sem_otime = sma->sem_otime;
1135-
tbuf.sem_ctime = sma->sem_ctime;
1136-
tbuf.sem_nsems = sma->sem_nsems;
1156+
tbuf.sem_otime = get_semotime(sma);
1157+
tbuf.sem_ctime = sma->sem_ctime;
1158+
tbuf.sem_nsems = sma->sem_nsems;
11371159
rcu_read_unlock();
11381160
if (copy_semid_to_user(p, &tbuf, version))
11391161
return -EFAULT;
@@ -2025,6 +2047,9 @@ static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
20252047
{
20262048
struct user_namespace *user_ns = seq_user_ns(s);
20272049
struct sem_array *sma = it;
2050+
time_t sem_otime;
2051+
2052+
sem_otime = get_semotime(sma);
20282053

20292054
return seq_printf(s,
20302055
"%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
@@ -2036,7 +2061,7 @@ static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
20362061
from_kgid_munged(user_ns, sma->sem_perm.gid),
20372062
from_kuid_munged(user_ns, sma->sem_perm.cuid),
20382063
from_kgid_munged(user_ns, sma->sem_perm.cgid),
2039-
sma->sem_otime,
2064+
sem_otime,
20402065
sma->sem_ctime);
20412066
}
20422067
#endif

0 commit comments

Comments
 (0)