From patchwork Mon Nov 7 08:31:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuang Wang X-Patchwork-Id: 16295 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1916623wru; Mon, 7 Nov 2022 00:36:02 -0800 (PST) X-Google-Smtp-Source: AMsMyM6omdiaJcoLwwRU8NLliz9FgANwWmhv5B6rwtStWBz/RZBUGLRopcxInzJ+WIpOSPEd0iLq X-Received: by 2002:a63:595:0:b0:470:8b7:255a with SMTP id 143-20020a630595000000b0047008b7255amr23377047pgf.329.1667810162245; Mon, 07 Nov 2022 00:36:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667810162; cv=none; d=google.com; s=arc-20160816; b=IQueZ4HiDKOFQu5BA8JtWCL71vWZ2dgRr258nBVDv1zIxdI797FLRCzUw9FOEsjXfq WiLpGmBdWV4JnszEPO+0EwMZ2Z8mzNnRQCsO24fA/6A0S4KT+NaY5+AfHRxWci65NsYK LT514eMczEUCzpbbVKDTwD993EXOiPxBrzoWmisTE1c2G49qopgqw5KBbhI7MiL8xjJ6 v+jKbrINCwXOyDscbpLCRn0JRIhCFBijy7ubqcC5KtuyTZpFT0z13JmnSh0tvDtGhWH1 DkOGwin+syQsj4WZJdg2e8rv4wouwV0dk+nQuM0G7RSuc4fYivkvfsitQGEpU5GFcfhf IGZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:content-transfer-encoding:mime-version :message-id:date:subject:cc:from:dkim-signature; bh=BQ9Z4bc9iFOixXTMmuVqqXWRg72dHOOtqIyMVDHCuI0=; b=s9W49BHmz2bLkyhDyCiC+sWrGoOSTCI1y8Dpv1Fkkwb9Fx1g+it9svM7FxAEADs2k6 XGoETAyPMeimR1HFe3d4+HUR05r6uHoEpR8BE8toL4ce9cSr8nUF0IgHZcr7Ibc9SOub 9SkU7D33kmZ2zQ3alpuM0xS/xmLxp0OAznd5M5Vow8jvJcJvh2NPiWOzn3ENlkyIWVAa wWSdpTbuojh3pvkvvBfTpiuP+X8xWDIwj+iu06gOz2DnhNN34Kb3+naT2RLZ9C+OX/ZQ TiZ0yDQGNxVNWsda3sB7cReUhgYTPVeSPA63pWC+hDHWnogzZ0TkuEJdDMVaGaIjNNv4 ylYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20210112 header.b=ciEc6wWN; 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=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h15-20020a170902ac8f00b0016d2e8c2233si8462943plr.333.2022.11.07.00.35.48; Mon, 07 Nov 2022 00:36:02 -0800 (PST) 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=fail header.i=@gmail.com header.s=20210112 header.b=ciEc6wWN; 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=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231359AbiKGIcI (ORCPT + 99 others); Mon, 7 Nov 2022 03:32:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231313AbiKGIcE (ORCPT ); Mon, 7 Nov 2022 03:32:04 -0500 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64A3514002; Mon, 7 Nov 2022 00:32:03 -0800 (PST) Received: by mail-pl1-x62f.google.com with SMTP id c2so10395370plz.11; Mon, 07 Nov 2022 00:32:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=BQ9Z4bc9iFOixXTMmuVqqXWRg72dHOOtqIyMVDHCuI0=; b=ciEc6wWN290+pHsj0unsWuRhg2OSaL7h+mJiRzgvuBUK9PYthe7IpI7JWLpI42pPBH lMGM3xA8cfXilotWph7PUs1DGe6NlIGg0BQW5ZfDlR4ocMJWWBWeJ1m8GrA1EoEtcC2y 8xxXS1eIP3LVK/spN1Lah/euoh1qaRXTNG9AkIQC6jNOIlDEb5DJx10j+w0oPs6rnP5C LvsIXeoQwPm7mvS25W7bxfGh3H540jPma7cgF9+tvjilWnzKcrcA8+mPAxn/WgIgGuaB wxnTZZbnIjLuCTGsIkhlwyKQaMJnQmZO7JK1mNVMfsvqvdtSuJj5ASdquK6oEKO2Y/tY wN7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=BQ9Z4bc9iFOixXTMmuVqqXWRg72dHOOtqIyMVDHCuI0=; b=Ay0YUG0D/Nh9eJqgljqHHTRtiV/t3G0nLIC95JovN3KQ3t9WzUOuyTpKFmLBKyrkht dwYxY/9BoqGQNOgbqaGzvxiAeVNuJOb3EJaAdEdGAQTmHQvTYqL6sU2hYuljOZInAPK4 VpYkpxI0DnXINmCBSOPc9kdDCdaR/ZQFW4uBfnVOJdFaXCk4rE1DLkNvkcgeJIrS6cL1 T/1Sh1qdL1ZdzAYZpnqPwq6zcVkRlrs2SQ0+Mralsb/NUAd1x1Cs3sVQct4SZGKaLHG7 jaMw+bi/N7SHGInHnMrMJmG6DTsgy1uepalJsz5vnvCs/WlQK+7y5WlhqqHQ8mQ/+Keh 7PiA== X-Gm-Message-State: ANoB5pnUSY5ExvLcKu+tu/faR0/bR/wO8bQF6AFk3Jy3Irfczl9tisoj 4ZZvEXEi8qgVCWgu6NyNbf6vpl+sphY= X-Received: by 2002:a17:902:f687:b0:188:6cce:671b with SMTP id l7-20020a170902f68700b001886cce671bmr14394231plg.71.1667809922707; Mon, 07 Nov 2022 00:32:02 -0800 (PST) Received: from localhost.localdomain ([47.242.114.172]) by smtp.gmail.com with ESMTPSA id p1-20020a170902a40100b00177f4ef7970sm4424631plq.11.2022.11.07.00.32.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Nov 2022 00:32:02 -0800 (PST) From: Chuang Wang Cc: Chuang Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] net: tun: rebuild error handling in tun_get_user Date: Mon, 7 Nov 2022 16:31:44 +0800 Message-Id: <20221107083145.685824-1-nashuiliang@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748825709097220627?= X-GMAIL-MSGID: =?utf-8?q?1748825709097220627?= The error handling in tun_get_user is very scattered. This patch unifies error handling, reduces duplication of code, and makes the logic clearer. Signed-off-by: Chuang Wang --- drivers/net/tun.c | 68 ++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 4bf2b268df4a..6a214c2251b9 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -1742,11 +1742,11 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, int good_linear; int copylen; bool zerocopy = false; - int err; + int err = 0; u32 rxhash = 0; int skb_xdp = 1; bool frags = tun_napi_frags_enabled(tfile); - enum skb_drop_reason drop_reason; + enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED; if (!(tun->flags & IFF_NO_PI)) { if (len < sizeof(pi)) @@ -1808,11 +1808,11 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, */ skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); if (IS_ERR(skb)) { - dev_core_stats_rx_dropped_inc(tun->dev); - return PTR_ERR(skb); + err = PTR_ERR(skb); + goto drop; } if (!skb) - return total_len; + goto out; } else { if (!zerocopy) { copylen = len; @@ -1836,11 +1836,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, } if (IS_ERR(skb)) { - if (PTR_ERR(skb) != -EAGAIN) - dev_core_stats_rx_dropped_inc(tun->dev); - if (frags) - mutex_unlock(&tfile->napi_mutex); - return PTR_ERR(skb); + err = PTR_ERR(skb); + goto drop; } if (zerocopy) @@ -1851,27 +1848,14 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, if (err) { err = -EFAULT; drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT; -drop: - dev_core_stats_rx_dropped_inc(tun->dev); - kfree_skb_reason(skb, drop_reason); - if (frags) { - tfile->napi.skb = NULL; - mutex_unlock(&tfile->napi_mutex); - } - - return err; + goto drop; } } if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { atomic_long_inc(&tun->rx_frame_errors); - kfree_skb(skb); - if (frags) { - tfile->napi.skb = NULL; - mutex_unlock(&tfile->napi_mutex); - } - - return -EINVAL; + err = -EINVAL; + goto drop; } switch (tun->flags & TUN_TYPE_MASK) { @@ -1887,9 +1871,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, pi.proto = htons(ETH_P_IPV6); break; default: - dev_core_stats_rx_dropped_inc(tun->dev); - kfree_skb(skb); - return -EINVAL; + err = -EINVAL; + drop_reason = SKB_DROP_REASON_INVALID_PROTO; + goto drop; } } @@ -1931,11 +1915,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, if (ret != XDP_PASS) { rcu_read_unlock(); local_bh_enable(); - if (frags) { - tfile->napi.skb = NULL; - mutex_unlock(&tfile->napi_mutex); - } - return total_len; + goto unlock_unlock; } } rcu_read_unlock(); @@ -1952,8 +1932,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, rcu_read_lock(); if (unlikely(!(tun->dev->flags & IFF_UP))) { - err = -EIO; rcu_read_unlock(); + err = -EIO; drop_reason = SKB_DROP_REASON_DEV_READY; goto drop; } @@ -2007,7 +1987,23 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, if (rxhash) tun_flow_update(tun, rxhash, tfile); - return total_len; + goto out; + +drop: + if (err != -EAGAIN) + dev_core_stats_rx_dropped_inc(tun->dev); + + if (!IS_ERR_OR_NULL(skb)) + kfree_skb_reason(skb, drop_reason); + +unlock_unlock: + if (frags) { + tfile->napi.skb = NULL; + mutex_unlock(&tfile->napi_mutex); + } + +out: + return err ?: total_len; } static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)