• Kuniyuki Iwashima's avatar
    af_unix: Allocate struct unix_vertex for each inflight AF_UNIX fd. · 1fbfdfaa
    Kuniyuki Iwashima authored
    We will replace the garbage collection algorithm for AF_UNIX, where
    we will consider each inflight AF_UNIX socket as a vertex and its file
    descriptor as an edge in a directed graph.
    
    This patch introduces a new struct unix_vertex representing a vertex
    in the graph and adds its pointer to struct unix_sock.
    
    When we send a fd using the SCM_RIGHTS message, we allocate struct
    scm_fp_list to struct scm_cookie in scm_fp_copy().  Then, we bump
    each refcount of the inflight fds' struct file and save them in
    scm_fp_list.fp.
    
    After that, unix_attach_fds() inexplicably clones scm_fp_list of
    scm_cookie and sets it to skb.  (We will remove this part after
    replacing GC.)
    
    Here, we add a new function call in unix_attach_fds() to preallocate
    struct unix_vertex per inflight AF_UNIX fd and link each vertex to
    skb's scm_fp_list.vertices.
    
    When sendmsg() succeeds later, if the socket of the inflight fd is
    still not inflight yet, we will set the preallocated vertex to struct
    unix_sock.vertex and link it to a global list unix_unvisited_vertices
    under spin_lock(&unix_gc_lock).
    
    If the socket is already inflight, we free the preallocated vertex.
    This is to avoid taking the lock unnecessarily when sendmsg() could
    fail later.
    
    In the following patch, we will similarly allocate another struct
    per edge, which will finally be linked to the inflight socket's
    unix_vertex.edges.
    
    And then, we will count the number of edges as unix_vertex.out_degree.
    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-2-kuniyu@amazon.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
    1fbfdfaa
scm.h 5.27 KB