From patchwork Tue Jun 14 12:28:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2068 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id CF3233F03E for ; Tue, 14 Jun 2022 14:28:31 +0200 (CEST) Received: by mail-ed1-f71.google.com with SMTP id a4-20020a056402168400b0042dc5b94da6sf6063992edv.10 for ; Tue, 14 Jun 2022 05:28:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1655209711; cv=pass; d=google.com; s=arc-20160816; b=pSr3pTbrBGHhQJBCJleJGbcq/6nXV1zgaTQzE1ImMXx+oyXnQ2tFg33B/ldGEBCZIS pBYwo8A449RZ64m3fMC8xiPcoeWI7dNH2racqM+wgjironxZxyfCsM9vOtNYAOKP8kc8 4aqq1VX3uG26hIFqntDMXuUVXGQQCYlzu72xHFwUKMkUwv6d5o2qUXuoR9mSgdoF/JPL QZFsVtZQW84tUamTa9F4Es6hZ3OtSmTPFCt/AwVi33Ssoivs7fZL9RiXgFMFlAtqErft 3F9xv0TFlZDy9DirOXNTeV3RMR0ixarGAj+FdwF2DicddIg9eVKBYn8OjuVz6uLoXaWn zTqQ== 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=B5BRL75Rqq/QsX4g5ZP6nUrszmkaYEUGj4R6g8/lDX0=; b=yH9kSPFKLs1FsxzSioDDIt7i3UfQfsCn3AAuHusPf5nDduvAm9pqkZcOHL8ERvvs2L Bn9LTX3Sreg0LhluNTjtthlVDHlH8fsIKucDeXrz/q3khZXD8tdaLCM38HmYC9TZ82FU TQOfsbLNpX/MbwMCVNP/J0G5qS7iD2OrWqB74vT8qX4pdezpq6riixo6vyTjv48wlZ47 8nIc3sYEaxOlToX5Z7cgcjGOBh4Fs/QEzeWiGWa4SAGd4Ly50qMGVPc3ykzmRb1ZeWPh PZuPPtbS2auHXFwHoj86x7dracGP43brkPML+F0gj3UcQ1C0NQP4Kiz+yauOh6SiAdr2 S5tw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=lA8lImzu; 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=B5BRL75Rqq/QsX4g5ZP6nUrszmkaYEUGj4R6g8/lDX0=; b=XbH+cqODDKafVs1gooWbWPGPUDrB6gl2ptG3hA4xcMc7ziqcJ4mWkhaG2HsAyf/+0J RfpJMzUlsnOB6rFK3aUHxGYyQz82oo5r5lRqKepPS1+N6FV1ZziD3+8mF2kd6uVWnuQS jyEj56B9dXJA9s/E6uWzQm+KyS8c4TGeQHYew= 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=B5BRL75Rqq/QsX4g5ZP6nUrszmkaYEUGj4R6g8/lDX0=; b=BbtwV2NWKp8c328cPq/fToavpknQyFoV6Vl8IRXnYejARPsBh7BJjJKHM/Jbc4HRFa Q6TBBuJ/RC0HXu+FiXF7QJ4Hjbm+3AD7GHApJWz1L9TiDikY+8TxbKMKCg5eVseZ6U1V 65YFBwo7jBmvRi+iSp7S68qBs5Cp2TMhvXimPsdSiZ89cR88sn5TIJwKVLBaBNi72xEB pezRUgWzLLR59z7qlvtFkhUjw84IKdL3IreQue2p7pKQE4UwTJiaWBQ1UbMvCI65YN/f mU64N2WZ6HXTRgqodNnkEbqhCGq6d3t8duwYHsjY4Xv6E3maLj0e5k6tm/hABn/BEjjP Kcag== X-Gm-Message-State: AOAM532FydbRcQSXqUj8spVS9L2bXwDvU6dNfAJfe7GJOB3XPBZs5n5X bf9vsqGSe7XFeGY+r37pU0yj5pVC X-Google-Smtp-Source: AGRyM1uQgeBHOk8hAEHLRuDM4GsCOuwIg0HpKV1x4pBTc9ZqK+J6wjCK0BqFPoYH8oMtz/pXBcuS5g== X-Received: by 2002:a17:907:1c91:b0:711:c834:754c with SMTP id nb17-20020a1709071c9100b00711c834754cmr4106346ejc.85.1655209711505; Tue, 14 Jun 2022 05:28:31 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:907:16a7:b0:718:c04a:5161 with SMTP id hc39-20020a17090716a700b00718c04a5161ls991587ejc.11.gmail; Tue, 14 Jun 2022 05:28:30 -0700 (PDT) X-Received: by 2002:a17:906:c7c1:b0:711:d2e9:99d0 with SMTP id dc1-20020a170906c7c100b00711d2e999d0mr4078171ejb.639.1655209710412; Tue, 14 Jun 2022 05:28:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655209710; cv=none; d=google.com; s=arc-20160816; b=q1zb+SGU72gJINnkkz92nnL0CYdNNApmV3QFZNmeHQGcDL0/LUDarkpgB+zgVYt6Ez eyaF8AruG1pM1ImuW79gk4DTCk1c6F1P+7RiGeSM35zBMccVUTbPpkhhgZ5WwFaMju3R P5sxhOsIEGXd2mnN4fejw9iGs2ZeBNUYu9nKIpcJZ75sjVYIeLzivj1tDVW3nor4i6X0 5WqAi/ManMnWF+tJYc9MsFALmDEGHF8oKL++tUfMawcg8r4GHBR1nXer43hSQcQ6XpH+ Gj9H8NbmJv0k/k83knzq2JfGUOm9Ns4lL/KqHrHZMTwlBO0cJIcTf6RCMh5uHMfyOUEw tTGw== 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=lfqn0geMWlo2m+epnZ6MFo0Gce8D/c5YwbUMrCOB20U=; b=Qyf9tRJQFKlYf1FLycZrZ0UmQMy5jzW+GcwWko0CcH8urs8cr+fXGP3f5DdYdWq9hh yqjVWis2dD8svO3O8WR6tSRLGA4hnCAvCWJotI2xt6CUICxcsycPMo2WZ2SZ2WAqTOTG EBH6nREajqqmWx0Xr58mbzzqirthgZhJCGY7DZlcsKQ/hSzN/7wF4S6j0QNoGfTqZoAs pe/CdgYMKyjSgBzThegw16htDaD9bpa7zGObEi4X0z6fV7q7aNrfa8ryB8o3KHC1ZMsk gGplwtWVtfixVjr5hwQJmsTXHiiTwSoewqMys+EMFe3nzUXmLvkA7r3SwsfW1gway7Hu 9xeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=lA8lImzu; 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 r24-20020aa7cfd8000000b0042bd1b53284sor4211814edy.60.2022.06.14.05.28.30 for (Google Transport Security); Tue, 14 Jun 2022 05:28:30 -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:a05:6402:21a:b0:431:5c52:82bb with SMTP id t26-20020a056402021a00b004315c5282bbmr5877909edv.70.1655209710212; Tue, 14 Jun 2022 05:28:30 -0700 (PDT) Received: from dario-ThinkPad-T14s-Gen-2i.amarulasolutions.com (host-80-116-90-174.pool80116.interbusiness.it. [80.116.90.174]) by smtp.gmail.com with ESMTPSA id z22-20020a17090655d600b006f3ef214e2csm5087043ejp.146.2022.06.14.05.28.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jun 2022 05:28:29 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Amarula patchwork , Oliver Hartkopp , 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 v4 03/12] can: slcan: use the alloc_can_skb() helper Date: Tue, 14 Jun 2022 14:28:12 +0200 Message-Id: <20220614122821.3646071-4-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220614122821.3646071-1-dario.binacchi@amarulasolutions.com> References: <20220614122821.3646071-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=lA8lImzu; 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 --- (no changes since v3) Changes in v3: - Increment the error counter in case of decoding failure. 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 | 70 +++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/drivers/net/can/slcan.c b/drivers/net/can/slcan.c index 6162a9c21672..c39580b142e0 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,80 @@ 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: + sl->dev->stats.rx_errors++; + dev_kfree_skb(skb); } /* parse tty input stream */