Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2224965pxb; Tue, 23 Feb 2021 01:29:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJz8K+86uNIWVMnA58U7AizWwLFY1XluLzFBfs1KMi7qM2pTMqOf7AS8ednvVhPjcNGSFtpI X-Received: by 2002:a17:906:145b:: with SMTP id q27mr25515954ejc.432.1614072545464; Tue, 23 Feb 2021 01:29:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614072545; cv=none; d=google.com; s=arc-20160816; b=iseAJ2Cd9+3apTay+BEstf8f8KkNm2rudrff7BsPsk0aTxn/JQMdNneVx5ihcvsRFj BUg0nw0uioQj5gTJYG2g3E77u/6cbT5cRaon6s5l5PDQJCcdGzYfbXl8AClS3XgBbC6M rWEw5DtFYmGbBu71W1tWhsMr5Wjt5PvzhaPcpkuyzIGkCg80R++1znCOTBDXDpZuOdYx KL/npFkWR+pxXJ05W7PrZEHBh5MsNQCZbq/cpZs5XSXpq1O1Q0MAnh6DMJA6t1c6kyTO 07SjoV9M1YVLj7GAIAwDejziwt9FVUEkO6L977MVa57rNPtg7oqnz7/F7gCxzer7oVh6 cEMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=lB/a4iMwzZXCVzQKA/wFs+7TeUdvCzpHO2+5MvW6yfU=; b=UtmTxYqTcz/Sdxsq/nIbPgdYC5QY09AKZUze+rNcMEGuniJ5DBXUYONBeYt1rGHjZD hsOp+tZH9YrRRgnajTro4Vcvqzzpnz+nS/pPdEeMDbDOeuJePV7UEL4AQcQ/zvhvIr1M XPPsM1c6s5LTdvQrXOXJv+8SYE6/i49XD/iX8cpyu68FEsSAXKi3+Ry4/AMjGA8GPred rLIpUSg/4sY8IZsKyp5367mIDlccugyy8v0bLlPubzfC6PHHsrX63Il7NAe6VyX2Bno2 8KUr52/TNcJKuAiFdl8iltK2nPJoQ9HgSJPA/hjpyQhrOBywwb++jSqEOaEd8cTJUp4m BlYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ANVQQS+U; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id eb8si16548107edb.6.2021.02.23.01.28.38; Tue, 23 Feb 2021 01:29:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ANVQQS+U; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232210AbhBWI4w (ORCPT + 99 others); Tue, 23 Feb 2021 03:56:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231845AbhBWI4h (ORCPT ); Tue, 23 Feb 2021 03:56:37 -0500 Received: from mail-yb1-xb2a.google.com (mail-yb1-xb2a.google.com [IPv6:2607:f8b0:4864:20::b2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFA4FC06174A for ; Tue, 23 Feb 2021 00:55:56 -0800 (PST) Received: by mail-yb1-xb2a.google.com with SMTP id p193so15744871yba.4 for ; Tue, 23 Feb 2021 00:55:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=lB/a4iMwzZXCVzQKA/wFs+7TeUdvCzpHO2+5MvW6yfU=; b=ANVQQS+Usc9OSyEkwQ0rKjoeqmy7t/RkfPSQKW/yfFbEW/2mjzUT2ghPUfiGN9I2Jw 9298v+5cbHBiGasuLAljXkJs/uhAJ/PUbY1XwN/N34AeVliE087Dn3tXfXk2a66KMlbc vFv9gzp0rrpMDG1SHaPr+d78Y/64WEVObZjszfGmUK2pODzXOFa+ISUpkk4yBsBVSltI G3o47TvPg53VtBu4HPd397hYtKz1ejg4+SsRIYpHFLusNfauZg52IjGzxHwCaXrSzQCQ V1WCXwXMnr0MHh+yLbHGoC7RMKoz3VX9F4QCQYZWgqhzFp/LjEPwH5hyxZYO824Zr+GE jFbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=lB/a4iMwzZXCVzQKA/wFs+7TeUdvCzpHO2+5MvW6yfU=; b=SRReDYpwapyvEdixXsvVmNml2l2IqNBb8rMm14GdcShOpiiY2xRT9Ylsqi0miD0vG8 ctrdb22MynW4F/iX7VTte6a92roZIRWyqKKVHygZuQ4dxCxuraB0KAPSpieYqhSOpzqv ekRSW/PpkX/rDIKwSvqTemSTitcfjYrypdoeL15AW9ASad6in3h4G50Wdt+VYTB0UG8A sAVqoj4057spC6GkxCxF9JMVbh7Gjc/0Dm8Ui+EP8zHgpd8PF76kkk4vgmjnpe4R0rs3 4srMEKMuTIPpA6c5s2fpPyKNd6Als6TEgIVz7+Dz7fe7gp9s8ETbQqmqB+eqijnzEoPl b2LA== X-Gm-Message-State: AOAM5317YD+D0eIlaL7fwf6VPdzd16EZ6pLtBX5iwxd3QVjNUtMiN8eh zORSySn4ktg544Qjs1oYMwV2lAaRmojD9rUpxZH1fw== X-Received: by 2002:a25:7306:: with SMTP id o6mr39702181ybc.132.1614070555696; Tue, 23 Feb 2021 00:55:55 -0800 (PST) MIME-Version: 1.0 References: <20210223055321.3891-1-o.rempel@pengutronix.de> <20210223055321.3891-3-o.rempel@pengutronix.de> In-Reply-To: <20210223055321.3891-3-o.rempel@pengutronix.de> From: Eric Dumazet Date: Tue, 23 Feb 2021 09:55:43 +0100 Message-ID: Subject: Re: [PATCH net v2 2/2] can: fix ref count warning if socket was closed before skb was cloned To: Oleksij Rempel Cc: Marc Kleine-Budde , "David S. Miller" , Jakub Kicinski , Oliver Hartkopp , Robin van der Gracht , Johannes Berg , Andre Naujoks , kernel@pengutronix.de, linux-can@vger.kernel.org, netdev , LKML , linux-wireless@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org On Tue, Feb 23, 2021 at 6:53 AM Oleksij Rempel wrote: > > There are two ref count variables controlling the free()ing of a socket: > - struct sock::sk_refcnt - which is changed by sock_hold()/sock_put() > - struct sock::sk_wmem_alloc - which accounts the memory allocated by > the skbs in the send path. > > If the socket is closed the struct sock::sk_refcnt will finally reach 0 > and sk_free() is called. Which then calls > refcount_dec_and_test(&sk->sk_wmem_alloc). If sk_wmem_alloc reaches 0 > the socket is actually free()ed. > > In case there are still TX skbs on the fly and the socket() is closed, > the struct sock::sk_refcnt reaches 0. In the TX-path the CAN stack > clones an "echo" skb, calls sock_hold() on the original socket and > references it. This produces the following back trace: Why not simply fix can_skb_set_owner() instead of adding yet another helper ? diff --git a/include/linux/can/skb.h b/include/linux/can/skb.h index 685f34cfba20741d372d340fe7df1084767b2850..655f33aa99e330b8ffc804b0f3a1d61aa9b00b0b 100644 --- a/include/linux/can/skb.h +++ b/include/linux/can/skb.h @@ -65,8 +65,7 @@ static inline void can_skb_reserve(struct sk_buff *skb) static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk) { - if (sk) { - sock_hold(sk); + if (sk && refcount_inc_not_zero(&sk->sk_refcnt)) { skb->destructor = sock_efree; skb->sk = sk; } IMO, CAN seems to use sock_hold() even for tx packets. But tx packets usually have a reference on sockets based on sk->sk_wmem_alloc , look at skb_set_owner_w() for reference. This might be the reason why you catch a zero sk_refcnt while packets are still in flight ? > | WARNING: CPU: 0 PID: 280 at lib/refcount.c:25 refcount_warn_saturate+0x114/0x134 > | refcount_t: addition on 0; use-after-free. > | Modules linked in: coda_vpu(E) v4l2_jpeg(E) videobuf2_vmalloc(E) imx_vdoa(E) > | CPU: 0 PID: 280 Comm: test_can.sh Tainted: G E 5.11.0-04577-gf8ff6603c617 #203 > | Hardware name: Freescale i.MX6 Quad/DualLite (Device Tree) > | Backtrace: > | [<80bafea4>] (dump_backtrace) from [<80bb0280>] (show_stack+0x20/0x24) r7:00000000 r6:600f0113 r5:00000000 r4:81441220 > | [<80bb0260>] (show_stack) from [<80bb593c>] (dump_stack+0xa0/0xc8) > | [<80bb589c>] (dump_stack) from [<8012b268>] (__warn+0xd4/0x114) r9:00000019 r8:80f4a8c2 r7:83e4150c r6:00000000 r5:00000009 r4:80528f90 > | [<8012b194>] (__warn) from [<80bb09c4>] (warn_slowpath_fmt+0x88/0xc8) r9:83f26400 r8:80f4a8d1 r7:00000009 r6:80528f90 r5:00000019 r4:80f4a8c2 > | [<80bb0940>] (warn_slowpath_fmt) from [<80528f90>] (refcount_warn_saturate+0x114/0x134) r8:00000000 r7:00000000 r6:82b44000 r5:834e5600 r4:83f4d540 > | [<80528e7c>] (refcount_warn_saturate) from [<8079a4c8>] (__refcount_add.constprop.0+0x4c/0x50) > | [<8079a47c>] (__refcount_add.constprop.0) from [<8079a57c>] (can_put_echo_skb+0xb0/0x13c) > | [<8079a4cc>] (can_put_echo_skb) from [<8079ba98>] (flexcan_start_xmit+0x1c4/0x230) r9:00000010 r8:83f48610 r7:0fdc0000 r6:0c080000 r5:82b44000 r4:834e5600 > | [<8079b8d4>] (flexcan_start_xmit) from [<80969078>] (netdev_start_xmit+0x44/0x70) r9:814c0ba0 r8:80c8790c r7:00000000 r6:834e5600 r5:82b44000 r4:82ab1f00 > | [<80969034>] (netdev_start_xmit) from [<809725a4>] (dev_hard_start_xmit+0x19c/0x318) r9:814c0ba0 r8:00000000 r7:82ab1f00 r6:82b44000 r5:00000000 r4:834e5600 > | [<80972408>] (dev_hard_start_xmit) from [<809c6584>] (sch_direct_xmit+0xcc/0x264) r10:834e5600 r9:00000000 r8:00000000 r7:82b44000 r6:82ab1f00 r5:834e5600 r4:83f27400 > | [<809c64b8>] (sch_direct_xmit) from [<809c6c0c>] (__qdisc_run+0x4f0/0x534) > > To fix this problem, we have to take into account, that the socket > technically still there but should not used (by any new skbs) any more. > The function skb_clone_sk_optional() (introduced in the previous patch) > takes care of this. It will only clone the skb, if the sk is set and the > refcount has not reached 0. > > Cc: Oliver Hartkopp > Cc: Andre Naujoks > Cc: Eric Dumazet > Fixes: 0ae89beb283a ("can: add destructor for self generated skbs") > Signed-off-by: Oleksij Rempel > --- > include/linux/can/skb.h | 3 +-- > net/can/af_can.c | 6 +++--- > net/can/j1939/main.c | 3 +-- > net/can/j1939/socket.c | 3 +-- > net/can/j1939/transport.c | 4 +--- > 5 files changed, 7 insertions(+), 12 deletions(-) > > diff --git a/include/linux/can/skb.h b/include/linux/can/skb.h > index 685f34cfba20..bc1af38697a2 100644 > --- a/include/linux/can/skb.h > +++ b/include/linux/can/skb.h > @@ -79,13 +79,12 @@ static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb) > { > struct sk_buff *nskb; > > - nskb = skb_clone(skb, GFP_ATOMIC); > + nskb = skb_clone_sk_optional(skb); > if (unlikely(!nskb)) { > kfree_skb(skb); > return NULL; > } > > - can_skb_set_owner(nskb, skb->sk); > consume_skb(skb); > return nskb; > } > diff --git a/net/can/af_can.c b/net/can/af_can.c > index cce2af10eb3e..9e1bd60e7e1b 100644 > --- a/net/can/af_can.c > +++ b/net/can/af_can.c > @@ -251,20 +251,20 @@ int can_send(struct sk_buff *skb, int loop) > * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS > * Therefore we have to ensure that skb->sk remains the > * reference to the originating sock by restoring skb->sk > - * after each skb_clone() or skb_orphan() usage. > + * after each skb_clone() or skb_orphan() usage - > + * skb_clone_sk_optional() takes care of that. > */ > > if (!(skb->dev->flags & IFF_ECHO)) { > /* If the interface is not capable to do loopback > * itself, we do it here. > */ > - newskb = skb_clone(skb, GFP_ATOMIC); > + newskb = skb_clone_sk_optional(skb); > if (!newskb) { > kfree_skb(skb); > return -ENOMEM; > } > > - can_skb_set_owner(newskb, skb->sk); > newskb->ip_summed = CHECKSUM_UNNECESSARY; > newskb->pkt_type = PACKET_BROADCAST; > } > diff --git a/net/can/j1939/main.c b/net/can/j1939/main.c > index da3a7a7bcff2..4f6852d48077 100644 > --- a/net/can/j1939/main.c > +++ b/net/can/j1939/main.c > @@ -47,12 +47,11 @@ static void j1939_can_recv(struct sk_buff *iskb, void *data) > * the header goes into sockaddr. > * j1939 may not touch the incoming skb in such way > */ > - skb = skb_clone(iskb, GFP_ATOMIC); > + skb = skb_clone_sk_optional(iskb); > if (!skb) > return; > > j1939_priv_get(priv); > - can_skb_set_owner(skb, iskb->sk); > > /* get a pointer to the header of the skb > * the skb payload (pointer) is moved, so that the next skb_data > diff --git a/net/can/j1939/socket.c b/net/can/j1939/socket.c > index 4e4a510d82f9..c1be6c26ff76 100644 > --- a/net/can/j1939/socket.c > +++ b/net/can/j1939/socket.c > @@ -305,12 +305,11 @@ static void j1939_sk_recv_one(struct j1939_sock *jsk, struct sk_buff *oskb) > if (!j1939_sk_recv_match_one(jsk, oskcb)) > return; > > - skb = skb_clone(oskb, GFP_ATOMIC); > + skb = skb_clone_sk_optional(oskb); > if (!skb) { > pr_warn("skb clone failed\n"); > return; > } > - can_skb_set_owner(skb, oskb->sk); > > skcb = j1939_skb_to_cb(skb); > skcb->msg_flags &= ~(MSG_DONTROUTE); > diff --git a/net/can/j1939/transport.c b/net/can/j1939/transport.c > index e09d087ba240..e902557bbe17 100644 > --- a/net/can/j1939/transport.c > +++ b/net/can/j1939/transport.c > @@ -1014,12 +1014,10 @@ static int j1939_simple_txnext(struct j1939_session *session) > if (!se_skb) > return 0; > > - skb = skb_clone(se_skb, GFP_ATOMIC); > + skb = skb_clone_sk_optional(se_skb); > if (!skb) > return -ENOMEM; > > - can_skb_set_owner(skb, se_skb->sk); > - > j1939_tp_set_rxtimeout(session, J1939_SIMPLE_ECHO_TIMEOUT_MS); > > ret = j1939_send_one(priv, skb); > -- > 2.29.2 >