Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1212952rdb; Fri, 1 Dec 2023 09:43:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IELrxA4PWu5pmNoHhOWnWH7I0n3VXmtJKDP2rZJo2NEsKcjqEcQidJAEUGudAcyChxQwqQa X-Received: by 2002:a17:90b:3804:b0:285:cca0:df7f with SMTP id mq4-20020a17090b380400b00285cca0df7fmr18446864pjb.36.1701452612082; Fri, 01 Dec 2023 09:43:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701452612; cv=none; d=google.com; s=arc-20160816; b=aUlvbapNGu5/jTWixVAtABTguVkkXJb5UeAGxc0PB1emP66wnp7gznqQX62zkgAk66 eJl1d4suJORMQK3TD+qJvJQLwvJ/lj2tPoEDTL1Si/X8WGulLtG/y2bOqhuCasynMa+I FW3uhtMGIxwa9rLehq2cXndgHM+FblBDhIHpegADgOWcE2PXTi3p8Gqxz/Kf9FY+uoKX ZWG1F9P06WzFCYXeZg7rU0uW/XKXY2iaMjR9EJzSi83M5nJduEpLbmhitkHLicXwQo2C 0SThmmAdrEOBE6spj/cTjgfj7h4kuPp9MM+P1jAdmzEXlW3dlNhQA8+wepnZu1xOYCD6 lcag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=pbX2X80nmyjZX82rv/r7YFYgyku+kUEoXFmQeyn/H7Y=; fh=hEeDbytX3UJ11o0djAS0OrC1LXA/x2uQfBKuOyuo4VE=; b=IeOYS8NpJhRzWmNGdiDv2mdVhDvWGI46tPny6emohl9GEMz5hXdlDZeRNpsChIUQKd AXnDCpoBk3LWN8rke8eS0qBZVhdeio6f3IITCy9LCmDAwaeCBY/tyZbGFUY1wmArd0Zg 4yfMQmtBpzhP6nzL+15fRrVQQPuWvRYLlj8fjLheBwVm6Xvq9TMvLCrUFMpPFQXfQWpA AHW93g2hCoIt4TjLiu7SdptwGJ3g64MA0+bGcQBDnUIDCG00JqvROqGSE+uySYnvRX2Y A1Anti1ZBjMewlrRGwPB0mimuPg3YdLDBB2ohGCcVT8O1xmfeI6ENeNeB9jjUl/JwiVr I+dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google09082023 header.b=X8ZvwL7B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id a21-20020a17090ad81500b0028511adb1acsi5733470pjv.67.2023.12.01.09.43.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 09:43:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google09082023 header.b=X8ZvwL7B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 4CB6D8116E69; Fri, 1 Dec 2023 09:43:27 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230262AbjLARnN (ORCPT + 99 others); Fri, 1 Dec 2023 12:43:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229534AbjLARnL (ORCPT ); Fri, 1 Dec 2023 12:43:11 -0500 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4F09FE for ; Fri, 1 Dec 2023 09:43:16 -0800 (PST) Received: by mail-qv1-xf29.google.com with SMTP id 6a1803df08f44-67a9febb2bfso3073406d6.1 for ; Fri, 01 Dec 2023 09:43:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google09082023; t=1701452596; x=1702057396; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=pbX2X80nmyjZX82rv/r7YFYgyku+kUEoXFmQeyn/H7Y=; b=X8ZvwL7BMXHp9Sbo9kVdeGBTJAMfweMWYRyy9FPtSowLQwQ0dT+mYADrFdkYorLI6e mXX1y8ECbcv3/lEe2G8XH7VvfuuGMQjS4N097L8Oy8Ebpeyoy+Cmcc0fHhb0qWT2G6Yf ilmbJfUB8XDwXADTFFx9SucMHsm/1HnXectlx2LfEK0/ltqr4FBYYpm1Shd7ic6jbwyy 861/Mfcw6V8ylUptlqUsXVsae/ge6oQGn1TrnUwfUdV+w82MaX5R/csvtF0+43vgDa0B QXPZCp83ByN9PU6lG+/qKHoSQ3sv8lmwPSSoluShw5VoLlCxXcOIMcoYccey6LkRuXzw 9pXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701452596; x=1702057396; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pbX2X80nmyjZX82rv/r7YFYgyku+kUEoXFmQeyn/H7Y=; b=idT2zGgc+RA6qCx6tDKyv+ClzENL3Qmy82rtFsRYARZhsu8MdcbzvRujmverNYhs4H cAmpj9Xeg1uQVzPJhS/5X0JuH1h1IBMacX+Kij+fBDfinfOg+1u4CUxpGiAOMPBfPhey EhUy8TMl991adB6ZQVc02AdPMIMAvvw49sdZSOnGujI/tEcD4/P31X2gvfMUGH3sg8Zh m/EMn75ICkN6k3A+dn2A9Ba+s8DbHlZWKMWiCB+rOSQJKowjmenyFu+aJ3mbiaYwCNEB gt88Kn2HRHWuEFCq+lg72VMWfgjUO4DihJeqGochAdaQ7pzyti3lQ47r2tZvjr/ni7WV hk7A== X-Gm-Message-State: AOJu0Yx0QJwNHlY6gOQgMFkA4sc4lM0Cz5qf4zsYaJvTKcK3MNN+iuXg 9hWdhl7veDO0D7TM7AJM4JBAu4OW+qCkH24RY5XDWQ== X-Received: by 2002:a05:6214:518a:b0:67a:9895:3aeb with SMTP id kl10-20020a056214518a00b0067a98953aebmr3093893qvb.53.1701452595831; Fri, 01 Dec 2023 09:43:15 -0800 (PST) Received: from debian.debian ([140.141.197.139]) by smtp.gmail.com with ESMTPSA id k7-20020a0cabc7000000b0067a3e703e3asm1702786qvb.37.2023.12.01.09.43.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 09:43:15 -0800 (PST) Date: Fri, 1 Dec 2023 09:43:13 -0800 From: Yan Zhai To: netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Weongyo Jeong , linux-kernel@vger.kernel.org, kernel-team@cloudflare.com, Jesper Brouer Subject: [PATCH v2 net-next] packet: add a generic drop reason for receive Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,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 agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Fri, 01 Dec 2023 09:43:27 -0800 (PST) Commit da37845fdce2 ("packet: uses kfree_skb() for errors.") switches from consume_skb to kfree_skb to improve error handling. However, this could bring a lot of noises when we monitor real packet drops in kfree_skb[1], because in tpacket_rcv or packet_rcv only packet clones can be freed, not actual packets. Adding a generic drop reason to allow distinguish these "clone drops". [1]: https://lore.kernel.org/netdev/CABWYdi00L+O30Q=Zah28QwZ_5RU-xcxLFUK2Zj08A8MrLk9jzg@mail.gmail.com/ Fixes: da37845fdce2 ("packet: uses kfree_skb() for errors.") Suggested-by: Eric Dumazet Signed-off-by: Yan Zhai --- include/net/dropreason-core.h | 6 ++++++ net/packet/af_packet.c | 22 +++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/include/net/dropreason-core.h b/include/net/dropreason-core.h index 3c70ad53a49c..278e4c7d465c 100644 --- a/include/net/dropreason-core.h +++ b/include/net/dropreason-core.h @@ -86,6 +86,7 @@ FN(IPV6_NDISC_NS_OTHERHOST) \ FN(QUEUE_PURGE) \ FN(TC_ERROR) \ + FN(PACKET_SOCK_ERROR) \ FNe(MAX) /** @@ -378,6 +379,11 @@ enum skb_drop_reason { SKB_DROP_REASON_QUEUE_PURGE, /** @SKB_DROP_REASON_TC_ERROR: generic internal tc error. */ SKB_DROP_REASON_TC_ERROR, + /** + * @SKB_DROP_REASON_PACKET_SOCK_ERROR: generic packet socket errors + * after its filter matches an incoming packet. + */ + SKB_DROP_REASON_PACKET_SOCK_ERROR, /** * @SKB_DROP_REASON_MAX: the maximum of core drop reasons, which * shouldn't be used as a real 'reason' - only for tracing code gen diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index a84e00b5904b..0a7c05d8fe9f 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -2128,6 +2128,7 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev, int skb_len = skb->len; unsigned int snaplen, res; bool is_drop_n_account = false; + enum skb_drop_reason drop_reason = SKB_CONSUMED; if (skb->pkt_type == PACKET_LOOPBACK) goto drop; @@ -2161,6 +2162,10 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev, res = run_filter(skb, sk, snaplen); if (!res) goto drop_n_restore; + + /* skb will only be "consumed" not "dropped" before this */ + drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR; + if (snaplen > res) snaplen = res; @@ -2227,10 +2232,7 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev, skb->len = skb_len; } drop: - if (!is_drop_n_account) - consume_skb(skb); - else - kfree_skb(skb); + kfree_skb_reason(skb, drop_reason); return 0; } @@ -2253,6 +2255,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, bool is_drop_n_account = false; unsigned int slot_id = 0; int vnet_hdr_sz = 0; + enum skb_drop_reason drop_reason = SKB_CONSUMED; /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT. * We may add members to them until current aligned size without forcing @@ -2355,6 +2358,10 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, vnet_hdr_sz = 0; } } + + /* skb will only be "consumed" not "dropped" before this */ + drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR; + spin_lock(&sk->sk_receive_queue.lock); h.raw = packet_current_rx_frame(po, skb, TP_STATUS_KERNEL, (macoff+snaplen)); @@ -2498,10 +2505,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, skb->len = skb_len; } drop: - if (!is_drop_n_account) - consume_skb(skb); - else - kfree_skb(skb); + kfree_skb_reason(skb, drop_reason); return 0; drop_n_account: @@ -2510,7 +2514,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, is_drop_n_account = true; sk->sk_data_ready(sk); - kfree_skb(copy_skb); + kfree_skb_reason(copy_skb, drop_reason); goto drop_n_restore; } -- 2.30.2