Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp770064imw; Wed, 13 Jul 2022 07:43:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uMx9A75y5Ur6e0sfG9ZBZVXWAvT8wrFL43MevBs+odcXGljw5RBwj5WMlqp4qug4NK95x1 X-Received: by 2002:a05:6402:540c:b0:434:d965:f8a with SMTP id ev12-20020a056402540c00b00434d9650f8amr5557830edb.30.1657723392035; Wed, 13 Jul 2022 07:43:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657723392; cv=none; d=google.com; s=arc-20160816; b=IdmK8Hye24uXUlcghnmmBFqkCfWU42RdSuHx7FaLbm2UrR0p1n1S+kHoiSw+0BLP23 K+XuFh9mCTKkFRH9mg08+2WeiQatDlI9XEj7r9CJGez9rgZaMmvN5JH2EHICrHvuI1NA R/DiYBizKNHg9j1Uqkzzf8y9gZ7ispLRE4yRhckDA6rBefO13MJF5Vsc5LVupOj4aq+D NTXen474omUbyF2/Lptmle0oLp79TElui9espizaRoiSznVVhTuKcsM7FAD0MoMawixd q1kgMlGH4hl/pzWEr8+d3WL7JHRa9xR+jNzNSnjh3emTawxChq6GW+z8eaAsHqlrDven VNbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:message-id:date:cc:to:from:subject; bh=b80viV9+PB1EAzqXO3jDlfuiQiY84KHEAPc10/kUVAQ=; b=uup7U7TbkIDerMxqZCDhJbE8Xf7We22v+77XNCbBOC0QsQqQx2ODfxGYCiCchLRdfW 5wbwyVdAlp+6o8ybWJ3upyrCTltSEkoQHvsiHBrEfaVWpTdOesSrtbSOMq5qT54v62AZ vseaQIHozljdKb+CyruqFDyWsRJ1gzciCNYN7iR6CRPBHi7lR1ZSq/+Fxmyji6C3Jws4 9orJegTrPAzwtqaIGVP4h314qTN3qhFpWIkYheq4ZDoaZ6R8oJmuCRx0E3BzP8borFZa jja4sHgWe1u8euUoIIusXm0zH6Iajn9Hy5wxORtA4EtYCbm8ojmYmFdYZrfU4/LN4UwX SI6g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mh2-20020a170906eb8200b0070ebb93abe4si3332655ejb.117.2022.07.13.07.42.46; Wed, 13 Jul 2022 07:43:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236408AbiGMO02 (ORCPT + 99 others); Wed, 13 Jul 2022 10:26:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236195AbiGMO00 (ORCPT ); Wed, 13 Jul 2022 10:26:26 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 692FA2F390; Wed, 13 Jul 2022 07:26:25 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0682261DBD; Wed, 13 Jul 2022 14:26:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 255FCC34114; Wed, 13 Jul 2022 14:26:23 +0000 (UTC) Subject: [PATCH v1] net: Add distinct sk_psock field From: Chuck Lever To: john.fastabend@gmail.com, daniel@iogearbox.net, jakub@cloudflare.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, ast@kernel.org, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, kpsingh@kernel.org Cc: chuck.lever@oracle.com, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Date: Wed, 13 Jul 2022 10:26:21 -0400 Message-ID: <165772238175.1757.4978340330606055982.stgit@oracle-102.nfsv4.dev> User-Agent: StGit/1.5 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The sk_psock facility populates the sk_user_data field with the address of an extra bit of metadata. User space sockets never populate the sk_user_data field, so this has worked out fine. However, kernel socket consumers such as the RPC client and server do populate the sk_user_data field. The sk_psock() function cannot tell that the content of sk_user_data does not point to psock metadata, so it will happily return a pointer to something else, cast to a struct sk_psock. Thus kernel socket consumers and psock currently cannot co-exist. We could educate sk_psock() to return NULL if sk_user_data does not point to a struct sk_psock. However, a more general solution that enables full co-existence psock and other uses of sk_user_data might be more interesting. Move the struct sk_psock address to its own pointer field so that the contents of the sk_user_data field is preserved. Reviewed-by: Hannes Reinecke Signed-off-by: Chuck Lever --- include/linux/skmsg.h | 2 +- include/net/sock.h | 4 +++- net/core/skmsg.c | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h index c5a2d6f50f25..5ef3a07c5b6c 100644 --- a/include/linux/skmsg.h +++ b/include/linux/skmsg.h @@ -277,7 +277,7 @@ static inline void sk_msg_sg_copy_clear(struct sk_msg *msg, u32 start) static inline struct sk_psock *sk_psock(const struct sock *sk) { - return rcu_dereference_sk_user_data(sk); + return rcu_dereference(sk->sk_psock); } static inline void sk_psock_set_state(struct sk_psock *psock, diff --git a/include/net/sock.h b/include/net/sock.h index c4b91fc19b9c..d2a513169527 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -327,7 +327,8 @@ struct sk_filter; * @sk_tskey: counter to disambiguate concurrent tstamp requests * @sk_zckey: counter to order MSG_ZEROCOPY notifications * @sk_socket: Identd and reporting IO signals - * @sk_user_data: RPC layer private data + * @sk_user_data: Upper layer private data + * @sk_psock: socket policy data (bpf) * @sk_frag: cached page frag * @sk_peek_off: current peek_offset value * @sk_send_head: front of stuff to transmit @@ -519,6 +520,7 @@ struct sock { struct socket *sk_socket; void *sk_user_data; + struct sk_psock __rcu *sk_psock; #ifdef CONFIG_SECURITY void *sk_security; #endif diff --git a/net/core/skmsg.c b/net/core/skmsg.c index cc381165ea08..2b3d01d92790 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -695,7 +695,7 @@ struct sk_psock *sk_psock_init(struct sock *sk, int node) write_lock_bh(&sk->sk_callback_lock); - if (sk->sk_user_data) { + if (sk->sk_psock) { psock = ERR_PTR(-EBUSY); goto out; } @@ -726,7 +726,7 @@ struct sk_psock *sk_psock_init(struct sock *sk, int node) sk_psock_set_state(psock, SK_PSOCK_TX_ENABLED); refcount_set(&psock->refcnt, 1); - rcu_assign_sk_user_data_nocopy(sk, psock); + rcu_assign_pointer(sk->sk_psock, psock); sock_hold(sk); out: @@ -825,7 +825,7 @@ void sk_psock_drop(struct sock *sk, struct sk_psock *psock) { write_lock_bh(&sk->sk_callback_lock); sk_psock_restore_proto(sk, psock); - rcu_assign_sk_user_data(sk, NULL); + rcu_assign_pointer(sk->sk_psock, NULL); if (psock->progs.stream_parser) sk_psock_stop_strp(sk, psock); else if (psock->progs.stream_verdict || psock->progs.skb_verdict)