Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1404825iog; Tue, 14 Jun 2022 05:33:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwkXNWGt6wqXdC9dIpaEnzNoww6Av1R7wMQdhuvGyCS85rztGY7GhLyFPGvNW5Ouei1ViF/ X-Received: by 2002:a17:907:7f9e:b0:711:d84d:408 with SMTP id qk30-20020a1709077f9e00b00711d84d0408mr4070405ejc.17.1655209990158; Tue, 14 Jun 2022 05:33:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655209990; cv=none; d=google.com; s=arc-20160816; b=0f/hI3YOwpl1G5/qG8+vurHNCLzl8jTBZm7wsiyFqYikATjors5pE8oM39Bk1yuz/t 9Pr1vTw4byNOLg9sYxihRLJXcpF2d3gDKlWn79nBaeYXc50OWJbIFeeSSW3HgW3Jsr3z FFKEyO2j76uqJ+QEiYFBHLWMmwMZip/O4XHjHLE5lMein8vp9G5BKTajqrsaQEsG73vJ 3lxDrf9RWHu4ZUs24FV3a8Odv2yu4piyfV2kKpi3SzLkq02ivlE0fABok3h9ohEnaq63 bUKgxLDr4Zo6EziAcEmPpDXFxcWu2TipbuL1XFoyfl33Uh776awjnPOcNNg4D6JT5rJr YbxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lfqn0geMWlo2m+epnZ6MFo0Gce8D/c5YwbUMrCOB20U=; b=PFuRhmiV/qFXfXwPVrKJJOEo5y+Z6XDD9QR+cWXLlRG7TW1MVo0crtltX7s2iT2mx4 SwUSU04TfrwbT4jfhUwirZLL6X8fIyewByn6vJzBko4kIvGHez40LfSR3pABx297VQLd axEg/cmnkTtz4fdHCPV+jPwBttownijBQXhWfYjY8FFlxxZH98SoGedOoKxfzojPGWOG y1IdtDO/rFl7u31Qk9ql34WHUMf+oaSw0xWgIyjzoHMghYFx5ANS4ti3bYeV8oVQnjUw e+4lY7N1nnGRK7koyhzsbJDThgPpmkx3aQ+A61tvBa1gw2jdTmBkENHkaKsNpzLZXkKm Mbzg== 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 linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cb11-20020a0564020b6b00b0042b816bcc59si10388646edb.127.2022.06.14.05.32.40; Tue, 14 Jun 2022 05:33:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=lA8lImzu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242172AbiFNMaD (ORCPT + 99 others); Tue, 14 Jun 2022 08:30:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243448AbiFNM2i (ORCPT ); Tue, 14 Jun 2022 08:28:38 -0400 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 038B2237DB for ; Tue, 14 Jun 2022 05:28:32 -0700 (PDT) Received: by mail-ed1-x52f.google.com with SMTP id x5so11418108edi.2 for ; Tue, 14 Jun 2022 05:28:31 -0700 (PDT) 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:content-transfer-encoding; bh=lfqn0geMWlo2m+epnZ6MFo0Gce8D/c5YwbUMrCOB20U=; b=lA8lImzum331TJJHsTJ65jBz5jhNkv8tw3ezsIi7YzTZ4BvI2AM5rhz+aOgWERPVL8 wf6IQ/SDWFEwFigWABBsxv8ZrP5t/FPjJTo8+VozEf4XwRH8ChY7HINrKepv9pKwMjZZ 2tLBe1IhgdW/M3DVZLQDHV1wWR1udzWI6jLZQ= 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:content-transfer-encoding; bh=lfqn0geMWlo2m+epnZ6MFo0Gce8D/c5YwbUMrCOB20U=; b=f0rKEROsHAJ2KTFAVITduNnwowGGfSsSxZQdvZW0uML4Amd3CVeWS/UxNYWJiEBlwo 8+Q9VMgkjeTzqTwpVF/m2q4t+ki530lPn2TuVRk8E4GkbcCrutfF5PsClb7kpXTy78zr y+6BWWeZ4w0sv9ZqrbqaCPBfuFQ8C0vVGtxsGtACDZvVoDFPq5SG5NmFQbO6VYkbm5CI b0pM+hLdkxwQCBF2Dt+enHUHLIEZXsF66N7TB9IyOJoAMNu8UImBGkZq6HHpBP/i7Zyt oYejHAxjyL2h3AmG1jkz5Fc4NnNCLHLTKUP1WCN+Lb1R5oEbBID4SA4e4758cMZ5JB0A +vCw== X-Gm-Message-State: AOAM532ARf0Pg5rFlvjwcFHj3BLtnr8zuvQLDApqegGTP1Lx8Gdnu8Fc islJ18TbG6kdAVSWC80NKzWlk/mbqC4lsA== 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 */ -- 2.32.0