From patchwork Wed Jun 8 16:51:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2031 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id A14053F1F7 for ; Wed, 8 Jun 2022 18:51:29 +0200 (CEST) Received: by mail-ej1-f70.google.com with SMTP id fp4-20020a1709069e0400b00711911cecf9sf4638548ejc.3 for ; Wed, 08 Jun 2022 09:51:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1654707089; cv=pass; d=google.com; s=arc-20160816; b=R/e001wjgun/wQXXamD5afqLDQNfcKYHyAnRbH4llXy0+wa3qRNoXd3xmeW6h1ngzv lJX80S0Onh93DGMtux5W1GI0BQWKNZby+r0z2S70ZfophBbh2dh4S+fz5kWVDl5GpEpV ozhNLUF9srPE6il4i1tKv7amcd8f0Tk5AgCuJTt/Nch2e4uNhjNS0oXiJw87JNJniCHc cVrO5o/NyVTGNddM0u3RjmkErc5yW3vVPVSWyLh7xs6Tl4od7TniksnD2rDVQjcLQ8PL bcfsOWdPcUhr/bnVyND6Onw620m1vCv75Kq70HF5P0sxrwtUod/Au/4vYdxq/1FzBDx8 jODA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-archive:list-help:list-post:list-id :mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=/UVqq1ExqN7CuSGkTFqGLAoJq1pVmGTN1oEi1z8Bgsw=; b=jNtv2izXCASydJXCWqw0706FqkhiTExvlE3lbQCYFAZa+rI7zbS5oCDaLaNwMkcDoj nV2yy8fkIZvnZHIi4mP/+1dBavG9z2aCMfnPFRcpSHD1tRAaCZyQcriULUeeXcvirwe6 +bD5OLTL7PlBhWbom/0hu/q7B04G+xJAYVj/n0uo+XnuXzDIQb2COBq/qLUl1t4BMpar gu/6GnESCX3dnMohe9rqvZePtjCvgdIYNfgFrCuU/RQ+LB6ixt3SJieo1y/tZXqGfNek oxdETOuGbuthvho5H/oaYFmCUc2flvxzr0TS7q2qq3lQTeRW+ZgsTjITlWTVHrpYsXHC rkfQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="GL2/5+TA"; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-unsubscribe; bh=/UVqq1ExqN7CuSGkTFqGLAoJq1pVmGTN1oEi1z8Bgsw=; b=Oxmtcg9hVRp76n9QpWSKNYWLD8ij6u3v5YkSqp6I5Rk2GbvLtNUf8LyiiuNUpmW8ge QyEJ5cg3Df35pBjULkJu3WcSiSMoJ2FxZoltossjKH35x3bf7zPY2LNWtwFdkAh3zk6C zRg5ur32j5eblgRU2lkyjllm+aFiHY8PaWIUg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-unsubscribe; bh=/UVqq1ExqN7CuSGkTFqGLAoJq1pVmGTN1oEi1z8Bgsw=; b=SKt20TbVIWVlD20xUlndehALrF7Op2WD7ESfwfbqaCtZDMExZMr1DP0dbD2VrQpRbX X5CysLNoU1o8SK9DAjcqppnNfpIS34uVqQ6SFWoYSowTS/kE8lbAb0bGoGzjpMFbSQs9 lOFiUa16skpB/FmnuxrKRfIKyB8xfHYQ5+PRYIrJG/O+V1zUH/A5yUcTVHfSCqogUJ6d HLcRel53PMEENNYn2IQCKJBGmjbYwNVH9kx7lECgjjq/tpJ//Kr7/59vgzur9Ji2BZ6V cQVJQUbwkoeB2+rAnt/1YdbcNiIZ89BPLTLCpVsR5KjCE/48sfo8CfDUh6Y/vLQ2Ijgz bPwg== X-Gm-Message-State: AOAM530D1Kw6fCoeCGHmUUiKEjjy66bKbp5pp4GOIx1jFO3y5vaKhfjh AjWaT6OeyMoNJvZ5zgWzRbd8/Jrh X-Google-Smtp-Source: ABdhPJzgjvFyarfZntg1063MNZsYPj0J9B/s+2Ej9lOxwmAQdnxRCzxPBxY+j3QreFTXnzu/g1odKQ== X-Received: by 2002:a05:6402:3907:b0:431:6776:64e7 with SMTP id fe7-20020a056402390700b00431677664e7mr17915649edb.0.1654707089348; Wed, 08 Jun 2022 09:51:29 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:906:6a26:b0:70a:1088:57d1 with SMTP id qw38-20020a1709066a2600b0070a108857d1ls1236856ejc.3.gmail; Wed, 08 Jun 2022 09:51:28 -0700 (PDT) X-Received: by 2002:a17:906:d54e:b0:6f6:3ea:8e7b with SMTP id cr14-20020a170906d54e00b006f603ea8e7bmr32847518ejc.292.1654707088182; Wed, 08 Jun 2022 09:51:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654707088; cv=none; d=google.com; s=arc-20160816; b=QwqEWS9yUjrSWsc6w3McJ3AaMIufNKsrcaQZNJwpnATEQb4PbDtyyc4APPdYVdPg4c 72guqTqU+shGuAhOTCu/tsgg64GeXEMerOL2Yf39/oFsg+nApRIL4TMyVfx9Kj7ul9g1 Z1LY6zEev2NEdpuqvbbiBVthp2KSxcfB/c/ipxc1XMczZwuv6/bqyNhtGeMt/QOQjE4V LqdzXhqs27SciLcTRj4tcwo0fE9FJ01FX7AQdr4PneUzwnDpwz6OPNrMf0iJRcOYao3z OAWw8p25MzdOUheOCcuxCHqAP6JVTKKlmQVQrqXbo4RzxtrWmE9lEVbN1Kv1jyUy2Fms Arbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=I5VTRziMnkGl4xU+BjwtCkmgql6hb+Z7QIN+Rj1ilOY=; b=lSqT6PoGgvW+ACDgK5yIYoPBVmBi0Ml2YmFhWG6V36lDsGh+VJUu2k5s8doEeq6EqR uF3yjfd4rsNoQyAhdEueggtQxZ/SeVYitXR/hhsnUp/HaDvtHslAHCMnJeiZabjgolZL VIVrrOTtqrdQDoDiQYQdLSaCk2XxOXgsELgiACsuz9F9mqtrPnPqwnOgDM3nX0LkTW/9 9HSalv4/m6SRj4YpzXU2herLttPPbr+++jTlkGKFZu71tY+9owpEJry3D3ywDJlFySfB NQD3Aya9vLzmdJd7yCdvUrI2KmatG81DkjH/Gd1iNNKTAv7DUZyjY+tdcKXcFdOsPd8J Sv1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="GL2/5+TA"; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id d18-20020a1709061f5200b006feece1e047sor5404261ejk.133.2022.06.08.09.51.28 for (Google Transport Security); Wed, 08 Jun 2022 09:51:28 -0700 (PDT) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; X-Received: by 2002:a17:906:9f1c:b0:711:cdda:a01f with SMTP id fy28-20020a1709069f1c00b00711cddaa01fmr16104703ejc.372.1654707087890; Wed, 08 Jun 2022 09:51:27 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.homenet.telecomitalia.it (host-80-116-90-174.pool80116.interbusiness.it. [80.116.90.174]) by smtp.gmail.com with ESMTPSA id c22-20020a17090654d600b0070587f81bcfsm9569071ejp.19.2022.06.08.09.51.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 09:51:27 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Amarula patchwork , michael@amarulasolutions.com, Dario Binacchi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Marc Kleine-Budde , Paolo Abeni , Wolfgang Grandegger , linux-can@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v2 03/13] can: slcan: use the alloc_can_skb() helper Date: Wed, 8 Jun 2022 18:51:06 +0200 Message-Id: <20220608165116.1575390-4-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220608165116.1575390-1-dario.binacchi@amarulasolutions.com> References: <20220608165116.1575390-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 X-Original-Sender: dario.binacchi@amarulasolutions.com X-Original-Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b="GL2/5+TA"; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Content-Type: text/plain; charset="UTF-8" Precedence: list Mailing-list: list linux-amarula@amarulasolutions.com; contact linux-amarula+owners@amarulasolutions.com List-ID: X-Spam-Checked-In-Group: linux-amarula@amarulasolutions.com X-Google-Group-Id: 476853432473 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , It is used successfully by most (if not all) CAN device drivers. It allows to remove replicated code. Signed-off-by: Dario Binacchi --- Changes in v2: - Put the data into the allocated skb directly instead of first filling the "cf" on the stack and then doing a memcpy(). drivers/net/can/slcan.c | 69 +++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c index 6162a9c21672..5d87e25e2285 100644 --- a/drivers/net/can/slcan.c +++ b/drivers/net/can/slcan.c @@ -54,6 +54,7 @@ #include #include #include +#include #include #include @@ -143,85 +144,79 @@ static struct net_device **slcan_devs; static void slc_bump(struct slcan *sl) { struct sk_buff *skb; - struct can_frame cf; + struct can_frame *cf; int i, tmp; u32 tmpid; char *cmd = sl->rbuff; - memset(&cf, 0, sizeof(cf)); + skb = alloc_can_skb(sl->dev, &cf); + if (unlikely(!skb)) { + sl->dev->stats.rx_dropped++; + return; + } switch (*cmd) { case 'r': - cf.can_id = CAN_RTR_FLAG; + cf->can_id = CAN_RTR_FLAG; fallthrough; case 't': /* store dlc ASCII value and terminate SFF CAN ID string */ - cf.len = sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN]; + cf->len = sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN]; sl->rbuff[SLC_CMD_LEN + SLC_SFF_ID_LEN] = 0; /* point to payload data behind the dlc */ cmd += SLC_CMD_LEN + SLC_SFF_ID_LEN + 1; break; case 'R': - cf.can_id = CAN_RTR_FLAG; + cf->can_id = CAN_RTR_FLAG; fallthrough; case 'T': - cf.can_id |= CAN_EFF_FLAG; + cf->can_id |= CAN_EFF_FLAG; /* store dlc ASCII value and terminate EFF CAN ID string */ - cf.len = sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN]; + cf->len = sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN]; sl->rbuff[SLC_CMD_LEN + SLC_EFF_ID_LEN] = 0; /* point to payload data behind the dlc */ cmd += SLC_CMD_LEN + SLC_EFF_ID_LEN + 1; break; default: - return; + goto decode_failed; } if (kstrtou32(sl->rbuff + SLC_CMD_LEN, 16, &tmpid)) - return; + goto decode_failed; - cf.can_id |= tmpid; + cf->can_id |= tmpid; /* get len from sanitized ASCII value */ - if (cf.len >= '0' && cf.len < '9') - cf.len -= '0'; + if (cf->len >= '0' && cf->len < '9') + cf->len -= '0'; else - return; + goto decode_failed; /* RTR frames may have a dlc > 0 but they never have any data bytes */ - if (!(cf.can_id & CAN_RTR_FLAG)) { - for (i = 0; i < cf.len; i++) { + if (!(cf->can_id & CAN_RTR_FLAG)) { + for (i = 0; i < cf->len; i++) { tmp = hex_to_bin(*cmd++); if (tmp < 0) - return; - cf.data[i] = (tmp << 4); + goto decode_failed; + + cf->data[i] = (tmp << 4); tmp = hex_to_bin(*cmd++); if (tmp < 0) - return; - cf.data[i] |= tmp; + goto decode_failed; + + cf->data[i] |= tmp; } } - skb = dev_alloc_skb(sizeof(struct can_frame) + - sizeof(struct can_skb_priv)); - if (!skb) - return; - - skb->dev = sl->dev; - skb->protocol = htons(ETH_P_CAN); - skb->pkt_type = PACKET_BROADCAST; - skb->ip_summed = CHECKSUM_UNNECESSARY; - - can_skb_reserve(skb); - can_skb_prv(skb)->ifindex = sl->dev->ifindex; - can_skb_prv(skb)->skbcnt = 0; - - skb_put_data(skb, &cf, sizeof(struct can_frame)); - sl->dev->stats.rx_packets++; - if (!(cf.can_id & CAN_RTR_FLAG)) - sl->dev->stats.rx_bytes += cf.len; + if (!(cf->can_id & CAN_RTR_FLAG)) + sl->dev->stats.rx_bytes += cf->len; netif_rx(skb); + return; + +decode_failed: + dev_kfree_skb(skb); } /* parse tty input stream */