Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp794606rdb; Fri, 6 Oct 2023 22:31:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEuUE7lfsV9PoIKyyi2plmAHeO3GYMGqvQlivgHA50iaLoG5kwo0UOp7sRH4ERbPOsnUqew X-Received: by 2002:a17:90a:c285:b0:263:41d2:4e2 with SMTP id f5-20020a17090ac28500b0026341d204e2mr10416186pjt.32.1696656679103; Fri, 06 Oct 2023 22:31:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696656679; cv=none; d=google.com; s=arc-20160816; b=vSecWcYxoPpNM0btGc40xAsm6qbdxd2mbRgUNly6k8D8/DkGcUMSF2uY9qZysozewJ 80v9hcHSPmBpJVtDMsOsWCTf8MmqBDagcXcrL7+u9mGF08mWf9E38LV0o0Yl9TlpZJIo gq//WtXsDkz/V5qJPmuqxvMk+f9JP3/LfyrooD7LKmvGaB3kugK8ldSm2S+VQdKfzA8V ggoUzBYDohqpfMG9AN1MMEZ1aA+ykIrch9yBMphe28p8amSGLDUHcT3mpEtiHZZL9qRK agV8/OCJLHU6cdeoF5DeUEFb58qlY+UN2hfJMVES5B006XN4xv51X4+XSm2kU1SxlP0n thAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ehXIAL9COr2p+SJpJYVkLUs7bLNsHpyoys6ti/OmwOI=; fh=tX1pVPzUB4VqSEEJuD493A3oLLMsXX2GAylXkqZLhQo=; b=yQukSPTYDQ6tgDH4zeL06UMVd9fLnykhSzF6yNoPIA5FIklAJYtMxjiyRZHuWUO3SB LoAqwx91rqTno1BEFmt9M8C4J2/BA60DRC7cb8X4djDZzL2VxTfg0sIRy3ioLUjfyV66 bR2REqPWp6uGOThfZyZ0Su4hNFwVVAUAIwygn6sEm3+F76DIx7Cs9Ru8omsXczx9PVqk fAhTO7aY6UHTAAfQH7K6qzSqn/z+Njk/vEK/5M0LAtPe9DAJYheuQEZj8bu3mZv8K6l/ Tv/RVwCqigr0BgFblimNyWwMnzYLD9u/Pse26HkNVkwV/Wh+MiTZIoE2weOnbF7jX35M uy3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=OiAMtXJ6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id lk15-20020a17090b33cf00b002774daac5e7si8140440pjb.0.2023.10.06.22.31.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 22:31:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=OiAMtXJ6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id BB3F083FC9CF; Fri, 6 Oct 2023 22:31:16 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343510AbjJGF3j (ORCPT + 99 others); Sat, 7 Oct 2023 01:29:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233675AbjJGF3i (ORCPT ); Sat, 7 Oct 2023 01:29:38 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A3CABB for ; Fri, 6 Oct 2023 22:29:37 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-536ef8a7dcdso5930a12.0 for ; Fri, 06 Oct 2023 22:29:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696656576; x=1697261376; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ehXIAL9COr2p+SJpJYVkLUs7bLNsHpyoys6ti/OmwOI=; b=OiAMtXJ6tynmtPbNf0oVtLtakB4w672tZqGGFGc20uXHn8lXontrHzK4qP1tuSYiUH tLyVfpdQWpzYGzwjZLup748DMmHPLUY0ksXcxOjj6dbos+c/jnmf5EkjJbwz3Gu3KWHH AVIyMbQpxnTa7TxQTI48+P0ceptkAh4J9DuF5UzwMMQYVEa5StYB1tzsJPk89rOkYKit zMgtXb8LXQpzPCbwS5oZc+e3lFxRdXQIypFnvypgIbmnBl2YN8AGc6Rrt3m13xzJswBu 6fCc/bjDt1sg5eUX7Eg1qQodtZkXA/rdY85AK+AP++rpClTfNPyuKDek3KS1PlhruFt+ +NaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696656576; x=1697261376; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ehXIAL9COr2p+SJpJYVkLUs7bLNsHpyoys6ti/OmwOI=; b=wI4Rip4wTe8HQ5vOeAjzfHfNxOeEf0HEPdbNXn2zY2sD3/8nRnI0WI7MObSmcjeBBX u9obmj3PDwVIJ3bzH++MDU4ilFwWrsJ/0AbyObkZNx6bh4eBVEfWri6SRbcnqbief3ap kFi8MlPuTayvtB7mJLnoAqJRQpzkpIJuwnDx/LH7blIW9NpfviD06OL0nWP+CTnK8rYe 5iVAjJq1XeFN9cAkHcViy8BaG1gTdWuFNW4Z3Xm4J5+S95s0w1TQCx8kbuG4C6QT3RA+ HvtDewuLrrc6pq8/4wfAfGnL9RM9OwShbf4ykHZBm2fSmtYugudGuQkZEgabfLJ6fFBI oyjw== X-Gm-Message-State: AOJu0YzpSwht3aUHOyyXRDhsPnOJSJYXuZl7tjOF0ve++JXW8lEYyz6/ bPqncjGrS5m6hpyqoBZZH+czI/RmVbDnFUkV21HNmg== X-Received: by 2002:a50:bb67:0:b0:519:7d2:e256 with SMTP id y94-20020a50bb67000000b0051907d2e256mr268054ede.0.1696656575664; Fri, 06 Oct 2023 22:29:35 -0700 (PDT) MIME-Version: 1.0 References: <20231007050621.1706331-1-yajun.deng@linux.dev> In-Reply-To: <20231007050621.1706331-1-yajun.deng@linux.dev> From: Eric Dumazet Date: Sat, 7 Oct 2023 07:29:24 +0200 Message-ID: Subject: Re: [PATCH net-next v7] net/core: Introduce netdev_core_stats_inc() To: Yajun Deng Cc: davem@davemloft.net, kuba@kernel.org, pabeni@redhat.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Alexander Lobakin Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-4.8 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Fri, 06 Oct 2023 22:31:16 -0700 (PDT) On Sat, Oct 7, 2023 at 7:06=E2=80=AFAM Yajun Deng wr= ote: > > Although there is a kfree_skb_reason() helper function that can be used t= o > find the reason why this skb is dropped, but most callers didn't increase > one of rx_dropped, tx_dropped, rx_nohandler and rx_otherhost_dropped. > ... > + > +void netdev_core_stats_inc(struct net_device *dev, u32 offset) > +{ > + /* This READ_ONCE() pairs with the write in netdev_core_stats_all= oc() */ > + struct net_device_core_stats __percpu *p =3D READ_ONCE(dev->core_= stats); > + unsigned long *field; > + > + if (unlikely(!p)) > + p =3D netdev_core_stats_alloc(dev); > + > + if (p) { > + field =3D (unsigned long *)((void *)this_cpu_ptr(p) + off= set); > + WRITE_ONCE(*field, READ_ONCE(*field) + 1); This is broken... As I explained earlier, dev_core_stats_xxxx(dev) can be called from many different contexts: 1) process contexts, where preemption and migration are allowed. 2) interrupt contexts. Adding WRITE_ONCE()/READ_ONCE() is not solving potential races. I _think_ I already gave you how to deal with this ? Please try instead: +void netdev_core_stats_inc(struct net_device *dev, u32 offset) +{ + /* This READ_ONCE() pairs with the write in netdev_core_stats_alloc= () */ + struct net_device_core_stats __percpu *p =3D READ_ONCE(dev->core_st= ats); + unsigned long __percpu *field; + + if (unlikely(!p)) { + p =3D netdev_core_stats_alloc(dev); + if (!p) + return; + } + field =3D (__force unsigned long __percpu *)((__force void *)p + of= fset); + this_cpu_inc(*field); +}