Commit 29b64e35 authored by Kuniyuki Iwashima's avatar Kuniyuki Iwashima Committed by Jakub Kicinski

af_unix: Allocate struct unix_edge for each inflight AF_UNIX fd.

As with the previous patch, we preallocate to skb's scm_fp_list an
array of struct unix_edge in the number of inflight AF_UNIX fds.

There we just preallocate memory and do not use immediately because
sendmsg() could fail after this point.  The actual use will be in
the next patch.

When we queue skb with inflight edges, we will set the inflight
socket's unix_sock as unix_edge->predecessor and the receiver's
unix_sock as successor, and then we will link the edge to the
inflight socket's unix_vertex.edges.

Note that we set NULL to cloned scm_fp_list.edges in scm_fp_dup()
so that MSG_PEEK does not change the shape of the directed graph.
Signed-off-by: default avatarKuniyuki Iwashima <kuniyu@amazon.com>
Acked-by: default avatarPaolo Abeni <pabeni@redhat.com>
Link: https://lore.kernel.org/r/20240325202425.60930-3-kuniyu@amazon.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 1fbfdfaa
...@@ -33,6 +33,12 @@ struct unix_vertex { ...@@ -33,6 +33,12 @@ struct unix_vertex {
unsigned long out_degree; unsigned long out_degree;
}; };
struct unix_edge {
struct unix_sock *predecessor;
struct unix_sock *successor;
struct list_head vertex_entry;
};
struct sock *unix_peer_get(struct sock *sk); struct sock *unix_peer_get(struct sock *sk);
#define UNIX_HASH_MOD (256 - 1) #define UNIX_HASH_MOD (256 - 1)
......
...@@ -23,12 +23,17 @@ struct scm_creds { ...@@ -23,12 +23,17 @@ struct scm_creds {
kgid_t gid; kgid_t gid;
}; };
#ifdef CONFIG_UNIX
struct unix_edge;
#endif
struct scm_fp_list { struct scm_fp_list {
short count; short count;
short count_unix; short count_unix;
short max; short max;
#ifdef CONFIG_UNIX #ifdef CONFIG_UNIX
struct list_head vertices; struct list_head vertices;
struct unix_edge *edges;
#endif #endif
struct user_struct *user; struct user_struct *user;
struct file *fp[SCM_MAX_FD]; struct file *fp[SCM_MAX_FD];
......
...@@ -90,6 +90,7 @@ static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp) ...@@ -90,6 +90,7 @@ static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
fpl->max = SCM_MAX_FD; fpl->max = SCM_MAX_FD;
fpl->user = NULL; fpl->user = NULL;
#if IS_ENABLED(CONFIG_UNIX) #if IS_ENABLED(CONFIG_UNIX)
fpl->edges = NULL;
INIT_LIST_HEAD(&fpl->vertices); INIT_LIST_HEAD(&fpl->vertices);
#endif #endif
} }
...@@ -383,6 +384,7 @@ struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl) ...@@ -383,6 +384,7 @@ struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
new_fpl->max = new_fpl->count; new_fpl->max = new_fpl->count;
new_fpl->user = get_uid(fpl->user); new_fpl->user = get_uid(fpl->user);
#if IS_ENABLED(CONFIG_UNIX) #if IS_ENABLED(CONFIG_UNIX)
new_fpl->edges = NULL;
INIT_LIST_HEAD(&new_fpl->vertices); INIT_LIST_HEAD(&new_fpl->vertices);
#endif #endif
} }
......
...@@ -127,6 +127,11 @@ int unix_prepare_fpl(struct scm_fp_list *fpl) ...@@ -127,6 +127,11 @@ int unix_prepare_fpl(struct scm_fp_list *fpl)
list_add(&vertex->entry, &fpl->vertices); list_add(&vertex->entry, &fpl->vertices);
} }
fpl->edges = kvmalloc_array(fpl->count_unix, sizeof(*fpl->edges),
GFP_KERNEL_ACCOUNT);
if (!fpl->edges)
goto err;
return 0; return 0;
err: err:
...@@ -136,6 +141,7 @@ int unix_prepare_fpl(struct scm_fp_list *fpl) ...@@ -136,6 +141,7 @@ int unix_prepare_fpl(struct scm_fp_list *fpl)
void unix_destroy_fpl(struct scm_fp_list *fpl) void unix_destroy_fpl(struct scm_fp_list *fpl)
{ {
kvfree(fpl->edges);
unix_free_vertices(fpl); unix_free_vertices(fpl);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment