From patchwork Sun Jun 12 21:39:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 2050 Return-Path: X-Original-To: linux-amarula@patchwork.amarulasolutions.com Delivered-To: linux-amarula@patchwork.amarulasolutions.com Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by ganimede.amarulasolutions.com (Postfix) with ESMTPS id 1FE433F1DD for ; Sun, 12 Jun 2022 23:39:51 +0200 (CEST) Received: by mail-ed1-f69.google.com with SMTP id y13-20020a056402358d00b0042dfb820070sf2958430edc.6 for ; Sun, 12 Jun 2022 14:39:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1655069991; cv=pass; d=google.com; s=arc-20160816; b=g4HCikg8bK3uYxG690qWoTt8FScIpOv3aqaKVmO7Y8r4zErbCnNGaWnZ5OO5RppYmN bEmdL+0YFzGf8i/7pKy7Qztj+EmSfP2tkQ4LsfeEcmeX2c90me5mgeE4EGWX4irnteGB iJcbKueYkeCPkDt7QPircszvFGzC5DRZK5R+b9eZE8Iup0nn2NIhagsQ/HYghLfR9nL5 QlZDmdsvmYdnKdRCIDoyj/6JIOh6nBugvvkL12ihq6mcFO/iw21zOeWb0l0e3HjupSSi ocYsri97EJQSZaFlomq7+kwNR1iWEsAtvklAR0JUE5KV62kyCdaH3SYqd5wW5ijnDB42 wXvg== 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=Q4JaH70kvnHa+iEhSPHtUN64xuZCzRoDcU6PXVQeckQ=; b=yE/w/hXfT8z0Ksrc3savKD4CYVhsp2kiYFZ+S/QVDDhwZXgRrSqEOvHYPgE2gC1LXU e1UnSAG8uKE6fqUHdRwQhgexgcKmDCtEYL6PHoYaQ8SLQs1ynZRs9UdCJNUGje5r4ZB+ Z5n3qk9GJdXZhKrkGbPya/i9KTlRmVeBiVWGYOLt3u7HVKrS93WXXbmWk6ATRHeoRTlx fX+MHscjWeGQpVf93g0zVmAl/qpOumjKXZI+RSNHlvbYKhVLBvNNReNjCd61vbjgtMjj Ekdon3eZJee/kcdPBN6rYMc/tvfTO/gLxm9yXX3DEXD9+VEw94+DTX0VI0Mv3Gm4I9IK efqw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=I9pBc0a6; 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=Q4JaH70kvnHa+iEhSPHtUN64xuZCzRoDcU6PXVQeckQ=; b=OznYUaMEDq8ax9aEGc2RyAFxyKbb367E1IY8pn+soeZEmlVLBg1hmQYCTCkdRH8N6h 6u+LW27RI/J7gw+eLDdK0J8FFF7/qU+WUCEFcJN4tHefcWk6XbUhK2YjD2M6pP3pOj0I vdZD+nY2bGjV13zJ5g6l2Zc27x7iqpCsgRufw= 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=Q4JaH70kvnHa+iEhSPHtUN64xuZCzRoDcU6PXVQeckQ=; b=W4TaPrdhoyO/fR1ypRmed4niIcwwb8+6D9XYgq3hBReJwWKAuZdrQ8G2iG+a8KXwKA zO5Txm2WVA541azhtbGgko/Z546PcFRKzLz36RCdlrq3fSHMFsp8wYGlNn1kdtCCHink PkiNF+kkx9dMBzXa1Hg96EAvW0zyvBirzbcKS5NPSHBOOxxhqU15Ak//5dMsL7MTFufM lEJSNIasQcIklYvhHtSkN5GIS4MtlIoKZoVJRDyPQ2cqTcwi0oyndWW4VpLU2XyebfNB LvaM1V0vFAhBLhAgRs3ok3xKMI22SnviTu/t/it6jZ1V22vwXs+LYH3neF29OupWQoD0 hySg== X-Gm-Message-State: AOAM533HtGHI2RERYsNN679SnFOVF0HvlEP/C2yJ932DXOq76jGuzi27 Xcf1Lld0rs2NkXiqsssb/Riav0I9 X-Google-Smtp-Source: ABdhPJwqaUotkBxlMXWyiZngtmM+3qvRfuw6AUYo2Oc+nTceym4pPnu/bay9RxztSPo0Lrl+B0O8iQ== X-Received: by 2002:a17:906:6d91:b0:715:7d4e:84a2 with SMTP id h17-20020a1709066d9100b007157d4e84a2mr4643460ejt.504.1655069990973; Sun, 12 Jun 2022 14:39:50 -0700 (PDT) X-BeenThere: linux-amarula@amarulasolutions.com Received: by 2002:a17:907:97c4:b0:711:d2c2:9fa4 with SMTP id js4-20020a17090797c400b00711d2c29fa4ls1010946ejc.9.gmail; Sun, 12 Jun 2022 14:39:50 -0700 (PDT) X-Received: by 2002:a17:906:6086:b0:716:a658:d8b4 with SMTP id t6-20020a170906608600b00716a658d8b4mr3221566ejj.672.1655069989853; Sun, 12 Jun 2022 14:39:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655069989; cv=none; d=google.com; s=arc-20160816; b=aushjBRWDo26WkdpHhtlZDYq07VuObb9TteXMpNK62bEB09U/uV0ZNpEJ8IxWLnJhb DZ96a/p8AuKg1h96m4cObqZn041dBE+r4HMCAXUpNWX23g0eWUoF5JlL0GaNsbEzQV3G s/cZUOmwcOPcF4a+pj50SR5SnwcKvzQMO7f+HIDx84yDcLUgwsNxQW2eBbn2HwQJCZMN juxYIYNsyaRji+/7hvTHLbXBSzeKxIpEqB5vvpo6IQ6OuLozpxdHPImhcln5yAWEQpHz QupbnSpcp0LsI+9BwhRTyfZ0s7R84UJB3/NB1TNeARf/q84IO1fXqHTDqBsZnOnQhCZx et8w== 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=K0omjWVAQknKh80VLWsQETRXyxuJ8wx+RydBE1BQOZE=; b=pi5DHiCClUqQ1v/D9tXLuXqNJ7sVjvyfvS+DrR11jO3ol4w1uUGAZb6ybCMXPo2sKr I6uUe4ScQN/seWu0wYQjXZZvtc5Dby/iCPRjS1B5g21EW+z3atCN64Kq6Eed80OSJzHS 2vPmhYLVDsW7IEEntKlckKsuYZ+jp/WI6uHFRZTIFZ5nEWezaZd67yMZ5G+p0thRhNfU 7lf/+G+3kKzTBykxZGq8qiTBr/3auk8WlUaEa7d+33B3U//cIEsIT82ZP0kuMlH09ajr yscYg9BfjnapDaXF2ZKFt838JrP21+Hvf3jrqOlzuFu7NKWjRxc1UjTFXNx+fOn08e53 Scpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=I9pBc0a6; 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 kk25-20020a170907767900b00708ae482facsor1269498ejc.77.2022.06.12.14.39.49 for (Google Transport Security); Sun, 12 Jun 2022 14:39:49 -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:907:1b07:b0:6fe:2cbc:15c5 with SMTP id mp7-20020a1709071b0700b006fe2cbc15c5mr49898642ejc.677.1655069989671; Sun, 12 Jun 2022 14:39:49 -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 u10-20020a1709061daa00b00711d546f8a8sm2909398ejh.139.2022.06.12.14.39.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jun 2022 14:39:49 -0700 (PDT) From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: michael@amarulasolutions.com, Amarula patchwork , Oliver Hartkopp , 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 v3 03/13] can: slcan: use the alloc_can_skb() helper Date: Sun, 12 Jun 2022 23:39:17 +0200 Message-Id: <20220612213927.3004444-4-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220612213927.3004444-1-dario.binacchi@amarulasolutions.com> References: <20220612213927.3004444-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=I9pBc0a6; 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 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 */