Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp2937540rdb; Mon, 4 Dec 2023 11:33:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IH9ANq67vLmKhYZ9zu5aqdqYCMxD6KEwR/xs3JOBoToT2SQu1zZdpN6xl3kKAKdyVG0cVmO X-Received: by 2002:a05:6a20:4298:b0:188:444e:2b6c with SMTP id o24-20020a056a20429800b00188444e2b6cmr5851893pzj.49.1701718426101; Mon, 04 Dec 2023 11:33:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701718426; cv=none; d=google.com; s=arc-20160816; b=zefW707z3cC8P8raMl7TYLPTj8pJrDuc/m8ioz6TmezFdLU/GMzKIsDINcLKS+nPof u4WkLvmzacpGugxuO5QCY4OP8BBodlKSkz6t2CufdZbGR4q0/8+ZVF5w6HjSMsD/3q8M ZwwMpNE8Xk31EN/iaOPNV39lSG5u7tR0w8hg1UEBkd45jnUyJERKk+cddPhor+gm/lzT u6vPMl35z6FR9BqIHDVLKW/0iatRX/4UVDy7bJiR9/ViV17wje6GbGBWvqyPDOPw3uMt TDzwBrajFzsCNdT2B4f/nErR/lGwJxHmmX9VjFFR6UuVH/GIgC5SdHtSnrGG5BQiGIGl rk8A== 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=Ypze9oh3HMRwpIkYiVL5giOKcJP1gJiTxNjbvg3EfEk=; fh=9O3qouUhmsKLFvM1TkeoYqxaWGhUSGAHYmvrhWFT7TQ=; b=NvtzgMISbhS2LAwFQxt11+YY703L2A8mpOb9JGUFgHtq6ZQ1H+fVzR/EJM8bGEiZlh GJj1Y0UX3VHfzk74Nf/zgjyWs7V3QoBhFjAdPTCnVVpNgj8r+eLMTPo7p83FgY8u97cw pHLhxiLuYh2GVC+Y5DdvBDEc1oo4k7VsX8brG4ctKwkC5fozEeWq9T0zLJecrWojVEst ROAGifCTd4Trft/DqK3PBudL6FLHomT5Az7vB7ZvEq8HVXNhgrDwSheY7JWTJQfTOyDe OqIyXFEsH617rXm7MsRB8VUjo9LC98N/nnkzXbDrirUKbN/UXT3BR8/b1qdrz3yunk3M 4kpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google09082023 header.b=Fat7gM8P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id l6-20020a656806000000b005c68218e0ffsi2742684pgt.680.2023.12.04.11.33.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 11:33:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google09082023 header.b=Fat7gM8P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 EB5838057B13; Mon, 4 Dec 2023 11:33:42 -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 S230317AbjLDTd1 (ORCPT + 99 others); Mon, 4 Dec 2023 14:33:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229509AbjLDTdZ (ORCPT ); Mon, 4 Dec 2023 14:33:25 -0500 Received: from mail-qv1-xf33.google.com (mail-qv1-xf33.google.com [IPv6:2607:f8b0:4864:20::f33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28E33C1 for ; Mon, 4 Dec 2023 11:33:32 -0800 (PST) Received: by mail-qv1-xf33.google.com with SMTP id 6a1803df08f44-67ad891ff36so2710406d6.1 for ; Mon, 04 Dec 2023 11:33:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google09082023; t=1701718411; x=1702323211; 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=Ypze9oh3HMRwpIkYiVL5giOKcJP1gJiTxNjbvg3EfEk=; b=Fat7gM8PqNI4PGUx3XM+fYGZOtkOAzT6OSIv1Q0SuAC+nDy/AMx8EK+q+hfT8XJd0H yPl3LHg6kIenoXF5gGiLxBcThMlaEjQwjC1Edg2tsvNIBeHnqveCd8VLd2bp4CumF3P9 dxc50ACsld3BTNaGWMbeRb9zt5wIIuJTQHplD2KbZVbf6uRUJiwbs59bqi4WED3y8bkY j/+qGtbLHoOGoCa3GK8YQBhR2lmrPqk9uf/kYJ83H16I7Qq4aRgr33Y3A7EdcsGhFPnx F5apbFB2/u0vF/TMyTbag2IO3NhQ6/3ozV85loD4IByOgHVmVM4F+4vKf6pHN+oh2gum wS5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701718411; x=1702323211; 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=Ypze9oh3HMRwpIkYiVL5giOKcJP1gJiTxNjbvg3EfEk=; b=JAVJp9XD4AFuJf9yExNaN6XHszsoorPkbtljgtbKn3zAdVK2dCi0ymnawo+T7LErJV i2CU5V6LXf9g1ABbRRQG1MONte9Yj06+N6E1jAu/U5Fosb6fuDrVUwdXSsI54IVBI6Kb G8r77guJRWg1MTttY62pxVXqGSMafULWMcMT0nmLvroEJKIZELhQ3DFdLpIMtICtMfKC YrcYXJRdtk7FhS0V+XPs2ysWHrRAbhoyGeiA+6MMsM6oNulsLmtmUN2oUvYdbxlkEFlT UAMYTUm6V07W4Uk2SCCoq3MXP/XumG8QR/eZJLNZ6uV+hE7egeF+s4E7RRKspsMCEb8s FunA== X-Gm-Message-State: AOJu0YxBGYLdnk5r9lNHXuwee0aQaTlyBEoX605Os2wcMDaoxKQrpKQA t9UON07m1rB+Z3/kMi0zd+Ihzw== X-Received: by 2002:a0c:ea24:0:b0:67a:a721:d78c with SMTP id t4-20020a0cea24000000b0067aa721d78cmr76089qvp.114.1701718411197; Mon, 04 Dec 2023 11:33:31 -0800 (PST) Received: from debian.debian ([140.141.197.139]) by smtp.gmail.com with ESMTPSA id mh10-20020a056214564a00b0067a9235d5f0sm3884654qvb.105.2023.12.04.11.33.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 11:33:30 -0800 (PST) Date: Mon, 4 Dec 2023 11:33:28 -0800 From: Yan Zhai To: netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , linux-kernel@vger.kernel.org, kernel-team@cloudflare.com, Jesper Brouer Subject: [PATCH v4 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]); Mon, 04 Dec 2023 11:33:43 -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 Suggested-by: Willem de Bruijn Signed-off-by: Yan Zhai --- v3->v4: code style amend suggested by Willem de Bruijn v2->v3: removed an unused variable v1->v2: fixups suggested by Eric Dumazet v3: https://lore.kernel.org/lkml/ZWomqO8m4vVcW+ro@debian.debian/ v2: https://lore.kernel.org/netdev/ZWobMUp22oTpP3FW@debian.debian/ v1: https://lore.kernel.org/netdev/ZU3EZKQ3dyLE6T8z@debian.debian/ --- include/net/dropreason-core.h | 6 ++++++ net/packet/af_packet.c | 20 +++++++------------- 2 files changed, 13 insertions(+), 13 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..f92edba4c40f 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -2121,13 +2121,13 @@ static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb, static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) { + enum skb_drop_reason drop_reason = SKB_CONSUMED; struct sock *sk; struct sockaddr_ll *sll; struct packet_sock *po; u8 *skb_head = skb->data; int skb_len = skb->len; unsigned int snaplen, res; - bool is_drop_n_account = false; if (skb->pkt_type == PACKET_LOOPBACK) goto drop; @@ -2217,9 +2217,9 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev, return 0; drop_n_acct: - is_drop_n_account = true; atomic_inc(&po->tp_drops); atomic_inc(&sk->sk_drops); + drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR; drop_n_restore: if (skb_head != skb->data && skb_shared(skb)) { @@ -2227,16 +2227,14 @@ 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; } static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) { + enum skb_drop_reason drop_reason = SKB_CONSUMED; struct sock *sk; struct packet_sock *po; struct sockaddr_ll *sll; @@ -2250,7 +2248,6 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct sk_buff *copy_skb = NULL; struct timespec64 ts; __u32 ts_status; - bool is_drop_n_account = false; unsigned int slot_id = 0; int vnet_hdr_sz = 0; @@ -2498,19 +2495,16 @@ 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: spin_unlock(&sk->sk_receive_queue.lock); atomic_inc(&po->tp_drops); - is_drop_n_account = true; + drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR; sk->sk_data_ready(sk); - kfree_skb(copy_skb); + kfree_skb_reason(copy_skb, drop_reason); goto drop_n_restore; } -- 2.30.2