Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp2888629imb; Mon, 4 Mar 2019 17:21:48 -0800 (PST) X-Google-Smtp-Source: APXvYqxOuF/uyA+bA/TgDqLK7T/aDhO+fkgxdnrTO/3zpuCYNnxC9jwTVXEb2ArI1xiLq84GgvlW X-Received: by 2002:a17:902:968a:: with SMTP id n10mr974216plp.283.1551748908394; Mon, 04 Mar 2019 17:21:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551748908; cv=none; d=google.com; s=arc-20160816; b=ay5VBfG3aw/7FBjEHKUeyvefZnudMdcDlnLiQJ80q+VPBXbhJnxc6dInlLBB/9PSA/ RoijdfymClxlWtzDoWLXSp1fhSaT24rskyXyQYsusURyhyQO3codF5IUUmPfdyCiuSxf bqHGWC5i00CwaTmEA4hMj0AKfJwqN7AtE+YUKxxr7SQRdbSKdmvotnqyfKWdzBhzxUeZ g/UPwJwseosfSIgX8qehGHU9MBPA5Q8LYgSB+Xenn0M1lRbudi72Xc1DIBVIlx/0+hnc B2mllAVyXIcvzCwe1jSG50pYagakOMelff0qEs7Y6jeX5BG0sdsNxEPy3rBVkbvfo3rz 4M7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=aFG445oOy8n4O5uioOiOLEAEskjWzleMv6+MHtYWxus=; b=G/y51hU4PBioKViHRsmFTT3FHStH7Wp6bJWLhoyPGiiZjU/pRMPoxX8t2hKDmUFN81 t2xq2X1628Gmn75LmEsVYPQgBTBa75Ad6pYAd7PSzexBraSPyM0blyeg3sztGPsc3m7g wpeKRbgH/xYRVbv3r1VjlBZhdqym2gyx/YZpNSAYS6AB0hcAzS3WqFsIUxrK72Omo2jZ s+QAt+5aq4M8XPvdShBhLtzLbgJ9T6KFxM+IA1sWPI89vjyQ3o1koKGPEvu6xLJXmURY o/Gf9sh6gzGFw958OB23ig5isnt0+UpP6Jv2OVntZVA9K1TYGM5fe47lPEvOslv6eqMl znPQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d22si6406666pfo.142.2019.03.04.17.21.33; Mon, 04 Mar 2019 17:21:48 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726917AbfCEBTx (ORCPT + 99 others); Mon, 4 Mar 2019 20:19:53 -0500 Received: from mail.cn.fujitsu.com ([183.91.158.132]:28403 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726066AbfCEBTx (ORCPT ); Mon, 4 Mar 2019 20:19:53 -0500 X-IronPort-AV: E=Sophos;i="5.58,441,1544457600"; d="scan'208";a="55912118" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 05 Mar 2019 09:19:50 +0800 Received: from G08CNEXCHPEKD03.g08.fujitsu.local (unknown [10.167.33.85]) by cn.fujitsu.com (Postfix) with ESMTP id 8CC954C7F2B1; Tue, 5 Mar 2019 09:19:34 +0800 (CST) Received: from ubuntu.g08.fujitsu.local (10.167.226.33) by G08CNEXCHPEKD03.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.408.0; Tue, 5 Mar 2019 09:19:53 +0800 From: Su Yanjun To: , , CC: , , Subject: [PATCH v2] net: xfrm: Add '_rcu' tag for rcu protected pointer in netns_xfrm Date: Mon, 4 Mar 2019 20:19:14 -0500 Message-ID: <1551748754-16895-1-git-send-email-suyj.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.167.226.33] X-yoursite-MailScanner-ID: 8CC954C7F2B1.A0A48 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: suyj.fnst@cn.fujitsu.com X-Spam-Status: No Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For rcu protected pointers, we'd better add '__rcu' for them. Once added '__rcu' tag for rcu protected pointer, the sparse tool reports warnings. net/xfrm/xfrm_user.c:1198:39: sparse: expected struct sock *sk net/xfrm/xfrm_user.c:1198:39: sparse: got struct sock [noderef] *nlsk [...] So introduce a new wrapper function of nlmsg_unicast to handle type conversions. No functional change. Signed-off-by: Su Yanjun --- Changes from v1: - fix spelling mistakes and the sparse tool' warnings include/net/netns/xfrm.h | 2 +- net/xfrm/xfrm_user.c | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/include/net/netns/xfrm.h b/include/net/netns/xfrm.h index 59f45b1..d2a36fb 100644 --- a/include/net/netns/xfrm.h +++ b/include/net/netns/xfrm.h @@ -57,7 +57,7 @@ struct netns_xfrm { struct list_head inexact_bins; - struct sock *nlsk; + struct sock __rcu *nlsk; struct sock *nlsk_stash; u32 sysctl_aevent_etime; diff --git a/net/xfrm/xfrm_user.c b/net/xfrm/xfrm_user.c index d832783..e8f23e4 100644 --- a/net/xfrm/xfrm_user.c +++ b/net/xfrm/xfrm_user.c @@ -1071,6 +1071,22 @@ static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb, return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC); } +/* A similar wrapper like xfrm_nlmsg_multicast checking that nlsk is still + * available. + */ +static inline int xfrm_nlmsg_unicast(struct net *net, struct sk_buff *skb, + u32 pid) +{ + struct sock *nlsk = rcu_dereference(net->xfrm.nlsk); + + if (!nlsk) { + kfree_skb(skb); + return -EPIPE; + } + + return nlmsg_unicast(nlsk, skb, pid); +} + static inline unsigned int xfrm_spdinfo_msgsize(void) { return NLMSG_ALIGN(4) @@ -1195,7 +1211,7 @@ static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, err = build_spdinfo(r_skb, net, sportid, seq, *flags); BUG_ON(err < 0); - return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); + return xfrm_nlmsg_unicast(net, r_skb, sportid); } static inline unsigned int xfrm_sadinfo_msgsize(void) @@ -1254,7 +1270,7 @@ static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, err = build_sadinfo(r_skb, net, sportid, seq, *flags); BUG_ON(err < 0); - return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid); + return xfrm_nlmsg_unicast(net, r_skb, sportid); } static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, @@ -1274,7 +1290,7 @@ static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, if (IS_ERR(resp_skb)) { err = PTR_ERR(resp_skb); } else { - err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); + err = xfrm_nlmsg_unicast(net, resp_skb, NETLINK_CB(skb).portid); } xfrm_state_put(x); out_noput: @@ -1337,7 +1353,7 @@ static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, goto out; } - err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid); + err = xfrm_nlmsg_unicast(net, resp_skb, NETLINK_CB(skb).portid); out: xfrm_state_put(x); @@ -1903,8 +1919,8 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, if (IS_ERR(resp_skb)) { err = PTR_ERR(resp_skb); } else { - err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, - NETLINK_CB(skb).portid); + err = xfrm_nlmsg_unicast(net, resp_skb, + NETLINK_CB(skb).portid); } } else { xfrm_audit_policy_delete(xp, err ? 0 : 1, true); @@ -2062,7 +2078,7 @@ static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, err = build_aevent(r_skb, x, &c); BUG_ON(err < 0); - err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid); + err = xfrm_nlmsg_unicast(net, r_skb, NETLINK_CB(skb).portid); spin_unlock_bh(&x->lock); xfrm_state_put(x); return err; -- 2.7.4