Skip to content

Commit 58b5c20

Browse files
manfred-colorfuakpm00
authored andcommitted
ipc/util.c: cleanup and improve sysvipc_find_ipc()
sysvipc_find_ipc() can be simplified further: - It uses a for() loop to locate the next entry in the idr. This can be replaced with idr_get_next(). - It receives two parameters (pos - which is actually an idr index and not a position, and new_pos, which is really a position). One parameter is sufficient. Link: https://lore.kernel.org/all/[email protected]/ Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Manfred Spraul <[email protected]> Acked-by: Davidlohr Bueso <[email protected]> Acked-by: Waiman Long <[email protected]> Cc: "Eric W . Biederman" <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 765f2bf commit 58b5c20

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

ipc/util.c

+32-21
Original file line numberDiff line numberDiff line change
@@ -782,28 +782,37 @@ struct pid_namespace *ipc_seq_pid_ns(struct seq_file *s)
782782
return iter->pid_ns;
783783
}
784784

785-
/*
786-
* This routine locks the ipc structure found at least at position pos.
785+
/**
786+
* sysvipc_find_ipc - Find and lock the ipc structure based on seq pos
787+
* @ids: ipc identifier set
788+
* @pos: expected position
789+
*
790+
* The function finds an ipc structure, based on the sequence file
791+
* position @pos. If there is no ipc structure at position @pos, then
792+
* the successor is selected.
793+
* If a structure is found, then it is locked (both rcu_read_lock() and
794+
* ipc_lock_object()) and @pos is set to the position needed to locate
795+
* the found ipc structure.
796+
* If nothing is found (i.e. EOF), @pos is not modified.
797+
*
798+
* The function returns the found ipc structure, or NULL at EOF.
787799
*/
788-
static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
789-
loff_t *new_pos)
800+
static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t *pos)
790801
{
791-
struct kern_ipc_perm *ipc = NULL;
792-
int max_idx = ipc_get_maxidx(ids);
802+
int tmpidx;
803+
struct kern_ipc_perm *ipc;
793804

794-
if (max_idx == -1 || pos > max_idx)
795-
goto out;
805+
/* convert from position to idr index -> "-1" */
806+
tmpidx = *pos - 1;
796807

797-
for (; pos <= max_idx; pos++) {
798-
ipc = idr_find(&ids->ipcs_idr, pos);
799-
if (ipc != NULL) {
800-
rcu_read_lock();
801-
ipc_lock_object(ipc);
802-
break;
803-
}
808+
ipc = idr_get_next(&ids->ipcs_idr, &tmpidx);
809+
if (ipc != NULL) {
810+
rcu_read_lock();
811+
ipc_lock_object(ipc);
812+
813+
/* convert from idr index to position -> "+1" */
814+
*pos = tmpidx + 1;
804815
}
805-
out:
806-
*new_pos = pos + 1;
807816
return ipc;
808817
}
809818

@@ -817,11 +826,13 @@ static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
817826
if (ipc && ipc != SEQ_START_TOKEN)
818827
ipc_unlock(ipc);
819828

820-
return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
829+
/* Next -> search for *pos+1 */
830+
(*pos)++;
831+
return sysvipc_find_ipc(&iter->ns->ids[iface->ids], pos);
821832
}
822833

823834
/*
824-
* File positions: pos 0 -> header, pos n -> ipc id = n - 1.
835+
* File positions: pos 0 -> header, pos n -> ipc idx = n - 1.
825836
* SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
826837
*/
827838
static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
@@ -846,8 +857,8 @@ static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
846857
if (*pos == 0)
847858
return SEQ_START_TOKEN;
848859

849-
/* Find the (pos-1)th ipc */
850-
return sysvipc_find_ipc(ids, *pos - 1, pos);
860+
/* Otherwise return the correct ipc structure */
861+
return sysvipc_find_ipc(ids, pos);
851862
}
852863

853864
static void sysvipc_proc_stop(struct seq_file *s, void *it)

0 commit comments

Comments
 (0)