Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp55239imm; Mon, 21 May 2018 02:05:35 -0700 (PDT) X-Google-Smtp-Source: AB8JxZr8pJhpotFp8mHnDhIzbOR4R7K1Edku3dk9BklI+gswyKOOL2HqxJQ6WGmmTvnzQOoyf161 X-Received: by 2002:a62:d6da:: with SMTP id a87-v6mr19342997pfl.200.1526893535469; Mon, 21 May 2018 02:05:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526893535; cv=none; d=google.com; s=arc-20160816; b=uR1JpFAxauVni0UYt1eHMlb0xX03Iw0oAcbFOAuRuektMK7DWeD6E7qbPvlg9kN7+c +LDhT8FirzU96BhVkQYwRVZyHOwa7RPtanDcn7EUGHrWm9Egsu3pZQTMC/hTQ0PfUWHY My0f4Ac0oedf1LUfpREQzE67EO2gLGr9Rrbq5ZqnDtuAg1N5CluX74UPOQqQsLr/eOxx gkK3vhLJldvn4ihy6Y7l//rluveMS9gpU3TC57Ogo9w8fyZyiFtrzYx6eN6hBpqitP2w IBmWSpNWBM57IRXoAIBnaW/TpTRWCOV3qiM1PgifsHogvmz3ylkG+EQu/vDBLTfyOvuF k2wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=5WcYO1zAvUA8BY5/BJC3w4kC99EAhbRKOgjudOd2EXE=; b=dtgFADF0IZxWx0Je5X1xDsZdcT57hC29S1l0T9NXF77m0ic347MzZLnpVST/O6J9kR ILRpH6s+FvY9k20GSr974FrCCS4VbsUfHnBOzNKe1cMuknCCoFkE7eKMFqTDEjVNJ7Ak 0BRHwYQf+Qx8sibOz1bVbQMfvrt0WCaD3AggxTmBgg0TE55H81j6VMjsdosaZvCebCfQ K814mshRRjGQUzlwJ5hHXE+RIGPR1sd+MAqroY0nPscU9KcWfhWU87VGGg35SoEnlo1n OsHyQ2MVFPiv3Kph6FyBRhC0/TWD5D9k2yDWVcKfl9OIyVWac5YFRDXXNoTzAlP8K8kG Texw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x14-v6si13954488pll.24.2018.05.21.02.05.21; Mon, 21 May 2018 02:05:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752697AbeEUJFS (ORCPT + 99 others); Mon, 21 May 2018 05:05:18 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:43678 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752653AbeEUJFL (ORCPT ); Mon, 21 May 2018 05:05:11 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D05F97C6A9; Mon, 21 May 2018 09:05:10 +0000 (UTC) Received: from jason-ThinkPad-T450s.redhat.com (ovpn-12-30.pek2.redhat.com [10.72.12.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id B533976C0; Mon, 21 May 2018 09:05:06 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH net-next 07/12] tuntap: simplify error handling in tun_build_skb() Date: Mon, 21 May 2018 17:04:28 +0800 Message-Id: <1526893473-20128-8-git-send-email-jasowang@redhat.com> In-Reply-To: <1526893473-20128-1-git-send-email-jasowang@redhat.com> References: <1526893473-20128-1-git-send-email-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Mon, 21 May 2018 09:05:10 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Mon, 21 May 2018 09:05:10 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'jasowang@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Signed-off-by: Jason Wang --- drivers/net/tun.c | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 24ecd82..f6e0f96 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -1612,7 +1612,7 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun, int len, int *skb_xdp) { struct page_frag *alloc_frag = ¤t->task_frag; - struct sk_buff *skb; + struct sk_buff *skb = NULL; struct bpf_prog *xdp_prog; int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); unsigned int delta = 0; @@ -1638,6 +1638,9 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun, if (copied != len) return ERR_PTR(-EFAULT); + get_page(alloc_frag->page); + alloc_frag->offset += buflen; + /* There's a small window that XDP may be set after the check * of xdp_prog above, this should be rare and for simplicity * we do XDP on skb in case the headroom is not enough. @@ -1665,24 +1668,16 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun, switch (act) { case XDP_REDIRECT: - get_page(alloc_frag->page); - alloc_frag->offset += buflen; err = xdp_do_redirect(tun->dev, &xdp, xdp_prog); xdp_do_flush_map(); if (err) - goto err_redirect; - rcu_read_unlock(); - preempt_enable(); - return NULL; + goto err_xdp; + goto out; case XDP_TX: - get_page(alloc_frag->page); - alloc_frag->offset += buflen; if (tun_xdp_tx(tun->dev, &xdp)) - goto err_redirect; + goto err_xdp; tun_xdp_flush(tun->dev); - rcu_read_unlock(); - preempt_enable(); - return NULL; + goto out; case XDP_PASS: delta = orig_data - xdp.data; len = xdp.data_end - xdp.data; @@ -1702,25 +1697,22 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun, skb = build_skb(buf, buflen); if (!skb) { - rcu_read_unlock(); - preempt_enable(); - return ERR_PTR(-ENOMEM); + skb = ERR_PTR(-ENOMEM); + goto out; } skb_reserve(skb, pad - delta); skb_put(skb, len); - get_page(alloc_frag->page); - alloc_frag->offset += buflen; return skb; -err_redirect: - put_page(alloc_frag->page); err_xdp: + alloc_frag->offset -= buflen; + put_page(alloc_frag->page); +out: rcu_read_unlock(); preempt_enable(); - this_cpu_inc(tun->pcpu_stats->rx_dropped); - return NULL; + return skb; } /* Get packet from user space buffer */ -- 2.7.4