Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp999634rdb; Fri, 20 Oct 2023 06:00:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHfhK3cGgFZhrjpSjNc4EjoVZxmlO9SiD+WYNaE+Fd8ITpWOC28xtoMUC+CJfPuUglyJNsy X-Received: by 2002:a92:d989:0:b0:351:a18:51be with SMTP id r9-20020a92d989000000b003510a1851bemr1977966iln.15.1697806800697; Fri, 20 Oct 2023 06:00:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697806800; cv=none; d=google.com; s=arc-20160816; b=UrRXJ0+SoYm19Dm+uwz2UZMSodP+zDDDxb797RhcDzkcFYc9xX5BwHmmCB/GrwTYLx WthQFFilCnZPhlvGRy7PIGWI/3iXo0c/ZNhZPvI0Bo3pdwgdLPKrSYau6DGhRfSoM0oT rkd7QmACJ3xJWYfn05GKavsUaTcKS8mdjoxFVJSsYXqNlHXCrwvreh0PZsjPKjyI165m RWJ3aG3fcI4ge1rPobdr768UXr7r7ti1Gpf8JDKUJyrutGqyzJI1c64u67tAw3NLqwdE fLZZyTt7R25tndMF1XFZKzTDVm9z+LRhAGYICLYwF3TWmoSFm7R119YXInuM4dnKhf60 pHuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=a/5NRTtxkK9vQLLQjWfWBLavMvrH4UOyrYUeLMiOwhQ=; fh=WGuxsrrIFLvPEKF5r+WXA/XenY0YZ41arZ3Pqkm0jyU=; b=jdR3JWj+4nPYDTjfD5Ex2psbbX27Tk/4WtbFb2cxrPB6u0ub5P4P4C72g69GgoR0My djpTNVoxgPqCXp2Yi9siwLcc98KobVOMzglQ5PtyZF2xqGrh24Cu1Svyp2W0hJX79LHG p964wfSNSlTjSC2e4f/qFofx9aeZr9XXGru3Yxizt0fzLLUr2spDqvx5nxAbwo3OzRET ahlRDQ9l1ktBm1ggqaF1BoJmV/i150le73RtX+PQ3VrAuWtOAbTgDpruBqPaQBrAxH9G G2UV7YLcb8IJmqGaJUA2YciJGtg6Smxi1oMLjyGQGaIF8flZaVr2KaK4jhAJZomdx4i4 e7iA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JsxpVkdI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id l70-20020a639149000000b0059c0f9ef964si1719961pge.635.2023.10.20.06.00.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 06:00:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JsxpVkdI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id E7A0082CC459; Fri, 20 Oct 2023 05:59:57 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377194AbjJTM7v (ORCPT + 99 others); Fri, 20 Oct 2023 08:59:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376956AbjJTM7u (ORCPT ); Fri, 20 Oct 2023 08:59:50 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E32999F for ; Fri, 20 Oct 2023 05:59:48 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86F25C433C7; Fri, 20 Oct 2023 12:59:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697806788; bh=g6ZMLx5zIM4EGJxsqn2G4LlIvk4EwJD9EV5J8EOxWS8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=JsxpVkdINyGiMwPikUEVCe3lsQhXsJK8egUXaKCikogZ36aLPxmKZoviHA7cMa73J lWnWQg8OpUpVaYwg7a6dmTwIAaaI1ykffAC3J07dm6PoHGLuAaNl1/iECE6SPt+gl+ QZglpgjK2/YgTz3XuUkV3gqa+rHhD0FftOdp0nxb0GiDLPaodkGGsd91zx55KrfwWp nw5bAPbkJ96UFHXSA++IimS8j7XPgJRNaltbuYSbGYFJQwLmaE5NYAzIaOmbLjUlAE RXFnh3F3/3UYAHCvex779blHi2zrEligrNOeL3OKrdbkqOB9v6oi7TvVK6bBeIix7d uzYv1X/ATfXDA== Date: Fri, 20 Oct 2023 14:59:44 +0200 From: Simon Horman To: Mirsad Goran Todorovac Cc: Heiner Kallweit , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, nic_swsd@realtek.com, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Marco Elver Subject: Re: [PATCH v4 1/3] r8169: fix the KCSAN reported data-race in rtl_tx() while reading tp->cur_tx Message-ID: <20231020125944.GD2208164@kernel.org> References: <20231018193434.344176-1-mirsad.todorovac@alu.unizg.hr> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20231018193434.344176-1-mirsad.todorovac@alu.unizg.hr> X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Fri, 20 Oct 2023 05:59:58 -0700 (PDT) On Wed, Oct 18, 2023 at 09:34:34PM +0200, Mirsad Goran Todorovac wrote: > KCSAN reported the following data-race: > > ================================================================== > BUG: KCSAN: data-race in rtl8169_poll [r8169] / rtl8169_start_xmit [r8169] > > write (marked) to 0xffff888102474b74 of 4 bytes by task 5358 on cpu 29: > rtl8169_start_xmit (drivers/net/ethernet/realtek/r8169_main.c:4254) r8169 > dev_hard_start_xmit (./include/linux/netdevice.h:4889 ./include/linux/netdevice.h:4903 net/core/dev.c:3544 net/core/dev.c:3560) > sch_direct_xmit (net/sched/sch_generic.c:342) > __dev_queue_xmit (net/core/dev.c:3817 net/core/dev.c:4306) > ip_finish_output2 (./include/linux/netdevice.h:3082 ./include/net/neighbour.h:526 ./include/net/neighbour.h:540 net/ipv4/ip_output.c:233) > __ip_finish_output (net/ipv4/ip_output.c:311 net/ipv4/ip_output.c:293) > ip_finish_output (net/ipv4/ip_output.c:328) > ip_output (net/ipv4/ip_output.c:435) > ip_send_skb (./include/net/dst.h:458 net/ipv4/ip_output.c:127 net/ipv4/ip_output.c:1486) > udp_send_skb (net/ipv4/udp.c:963) > udp_sendmsg (net/ipv4/udp.c:1246) > inet_sendmsg (net/ipv4/af_inet.c:840 (discriminator 4)) > sock_sendmsg (net/socket.c:730 net/socket.c:753) > __sys_sendto (net/socket.c:2177) > __x64_sys_sendto (net/socket.c:2185) > do_syscall_64 (arch/x86/entry/common.c:50 arch/x86/entry/common.c:80) > entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:120) > > read to 0xffff888102474b74 of 4 bytes by interrupt on cpu 21: > rtl8169_poll (drivers/net/ethernet/realtek/r8169_main.c:4397 drivers/net/ethernet/realtek/r8169_main.c:4581) r8169 > __napi_poll (net/core/dev.c:6527) > net_rx_action (net/core/dev.c:6596 net/core/dev.c:6727) > __do_softirq (kernel/softirq.c:553) > __irq_exit_rcu (kernel/softirq.c:427 kernel/softirq.c:632) > irq_exit_rcu (kernel/softirq.c:647) > common_interrupt (arch/x86/kernel/irq.c:247 (discriminator 14)) > asm_common_interrupt (./arch/x86/include/asm/idtentry.h:636) > cpuidle_enter_state (drivers/cpuidle/cpuidle.c:291) > cpuidle_enter (drivers/cpuidle/cpuidle.c:390) > call_cpuidle (kernel/sched/idle.c:135) > do_idle (kernel/sched/idle.c:219 kernel/sched/idle.c:282) > cpu_startup_entry (kernel/sched/idle.c:378 (discriminator 1)) > start_secondary (arch/x86/kernel/smpboot.c:210 arch/x86/kernel/smpboot.c:294) > secondary_startup_64_no_verify (arch/x86/kernel/head_64.S:433) > > value changed: 0x002f4815 -> 0x002f4816 > > Reported by Kernel Concurrency Sanitizer on: > CPU: 21 PID: 0 Comm: swapper/21 Tainted: G L 6.6.0-rc2-kcsan-00143-gb5cbe7c00aa0 #41 > Hardware name: ASRock X670E PG Lightning/X670E PG Lightning, BIOS 1.21 04/26/2023 > ================================================================== > > The write side of drivers/net/ethernet/realtek/r8169_main.c is: > ================== > 4251 /* rtl_tx needs to see descriptor changes before updated tp->cur_tx */ > 4252 smp_wmb(); > 4253 > → 4254 WRITE_ONCE(tp->cur_tx, tp->cur_tx + frags + 1); > 4255 > 4256 stop_queue = !netif_subqueue_maybe_stop(dev, 0, rtl_tx_slots_avail(tp), > 4257 R8169_TX_STOP_THRS, > 4258 R8169_TX_START_THRS); > > The read side is the function rtl_tx(): > > 4355 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp, > 4356 int budget) > 4357 { > 4358 unsigned int dirty_tx, bytes_compl = 0, pkts_compl = 0; > 4359 struct sk_buff *skb; > 4360 > 4361 dirty_tx = tp->dirty_tx; > 4362 > 4363 while (READ_ONCE(tp->cur_tx) != dirty_tx) { > 4364 unsigned int entry = dirty_tx % NUM_TX_DESC; > 4365 u32 status; > 4366 > 4367 status = le32_to_cpu(tp->TxDescArray[entry].opts1); > 4368 if (status & DescOwn) > 4369 break; > 4370 > 4371 skb = tp->tx_skb[entry].skb; > 4372 rtl8169_unmap_tx_skb(tp, entry); > 4373 > 4374 if (skb) { > 4375 pkts_compl++; > 4376 bytes_compl += skb->len; > 4377 napi_consume_skb(skb, budget); > 4378 } > 4379 dirty_tx++; > 4380 } > 4381 > 4382 if (tp->dirty_tx != dirty_tx) { > 4383 dev_sw_netstats_tx_add(dev, pkts_compl, bytes_compl); > 4384 WRITE_ONCE(tp->dirty_tx, dirty_tx); > 4385 > 4386 netif_subqueue_completed_wake(dev, 0, pkts_compl, bytes_compl, > 4387 rtl_tx_slots_avail(tp), > 4388 R8169_TX_START_THRS); > 4389 /* > 4390 * 8168 hack: TxPoll requests are lost when the Tx packets are > 4391 * too close. Let's kick an extra TxPoll request when a burst > 4392 * of start_xmit activity is detected (if it is not detected, > 4393 * it is slow enough). -- FR > 4394 * If skb is NULL then we come here again once a tx irq is > 4395 * triggered after the last fragment is marked transmitted. > 4396 */ > → 4397 if (tp->cur_tx != dirty_tx && skb) > 4398 rtl8169_doorbell(tp); > 4399 } > 4400 } > > Obviously from the code, an earlier detected data-race for tp->cur_tx was fixed in the > line 4363: > > 4363 while (READ_ONCE(tp->cur_tx) != dirty_tx) { > > but the same solution is required for protecting the other access to tp->cur_tx: > > → 4397 if (READ_ONCE(tp->cur_tx) != dirty_tx && skb) > 4398 rtl8169_doorbell(tp); > > The write in the line 4254 is protected with WRITE_ONCE(), but the read in the line 4397 > might have suffered read tearing under some compiler optimisations. > > The fix eliminated the KCSAN data-race report for this bug. > > It is yet to be evaluated what happens if tp->cur_tx changes between the test in line 4363 > and line 4397. This test should certainly not be cached by the compiler in some register > for such a long time, while asynchronous writes to tp->cur_tx might have occurred in line > 4254 in the meantime. > > Fixes: 94d8a98e6235c ("r8169: reduce number of workaround doorbell rings") > Cc: Heiner Kallweit > Cc: nic_swsd@realtek.com > Cc: "David S. Miller" > Cc: Eric Dumazet > Cc: Jakub Kicinski > Cc: Paolo Abeni > Cc: Marco Elver > Cc: netdev@vger.kernel.org > Link: https://lore.kernel.org/lkml/dc7fc8fa-4ea4-e9a9-30a6-7c83e6b53188@alu.unizg.hr/ > Signed-off-by: Mirsad Goran Todorovac > Acked-by: Marco Elver Reviewed-by: Simon Horman