/* One semaphore structure for each semaphore in the system. */
 struct sem {
        int     semval;         /* current value */
-       int     sempid;         /* pid of last operation */
+       /*
+        * 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 */
                        goto out_unlock;
                }
 
-               for (i = 0; i < nsems; i++)
+               for (i = 0; i < nsems; i++) {
                        sma->sem_base[i].semval = sem_io[i];
+                       sma->sem_base[i].sempid = task_tgid_vnr(current);
+               }
 
                ipc_assert_locked_object(&sma->sem_perm);
                list_for_each_entry(un, &sma->list_id, list_id) {