Skip to contentSkip to conversation

ipc/sem.c: remove sem_base, embed struct sem

PATCH New [email protected] 1 participant 1 message Jul 12, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 32/96 ipc/sem.c: remove sem_base, embed struct sem

From: Manfred Spraul <[email protected]>
Subject: ipc/sem.c: remove sem_base, embed struct sem

sma->sem_base is initialized with

	sma->sem_base = (struct sem *) &sma[1];

The current code has four problems:
- There is an unnecessary pointer dereference - sem_base is not needed.
- Alignment for struct sem only works by chance.
- The current code causes false positive for static code analysis.
- This is a cast between different non-void types, which the future randstruct GCC plugin warns on.

And, as bonus, the code size gets smaller:

Before:
0 .text 00003770
After:
0 .text 0000374e

---

 include/linux/sem.h |   22 ++++++++++
 ipc/sem.c           |   88 ++++++++++++++++--------------------------
 2 files changed, 55 insertions(+), 55 deletions(-)
include/linux/sem.h renamed from include/linux/sem.h~ipc-semc-remove-sem_base-embed-struct-sem +21 -1
diff -puN include/linux/sem.h~ipc-semc-remove-sem_base-embed-struct-sem include/linux/sem.h
@@ -8,11 +8,29 @@
 
 struct task_struct;
 
+/* One semaphore structure for each semaphore in the system. */
+struct sem {
+	int	semval;		/* current value */
+	/*
+	 * PID of the process that last modified the semaphore. For
+	 * Linux, specifically these are:
+	 *  - semop
+	 *  - semctl, via SETVAL and SETALL.
+	 *  - at task exit when performing undo adjustments (see exit_sem).
+	 */
+	int	sempid;
+	spinlock_t	lock;	/* spinlock for fine-grained semtimedop */
+	struct list_head pending_alter; /* pending single-sop operations */
+					/* that alter the semaphore */
+	struct list_head pending_const; /* pending single-sop operations */
+					/* that do not alter the semaphore*/
+	time_t	sem_otime;	/* candidate for sem_otime */
+} ____cacheline_aligned_in_smp;
+
 /* One sem_array data structure for each set of semaphores in the system. */
 struct sem_array {
 	struct kern_ipc_perm	sem_perm;	/* permissions .. see ipc.h */
 	time_t			sem_ctime;	/* last change time */
-	struct sem		*sem_base;	/* ptr to first semaphore in array */
 	struct list_head	pending_alter;	/* pending operations */
 						/* that alter the array */
 	struct list_head	pending_const;	/* pending complex operations */
@@ -21,6 +39,8 @@ struct sem_array {
 	int			sem_nsems;	/* no. of semaphores in array */
 	int			complex_count;	/* pending complex operations */
 	unsigned int		use_global_lock;/* >0: global lock required */
+
+	struct sem		sems[];
 };
 
 #ifdef CONFIG_SYSVIPC
ipc/sem.c renamed from ipc/sem.c~ipc-semc-remove-sem_base-embed-struct-sem +34 -54

266 lines not on this page · raw message

_