From patchwork Sat Oct 14 03:12:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mirsad Todorovac X-Patchwork-Id: 152887 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp2278586vqb; Fri, 13 Oct 2023 20:16:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IETA51qsqzzbhmt7LHxIw/xsfpThbIVaGYB5mM2Mo8di9v0kusFsKpXZwv/pmv+AwcmGKIn X-Received: by 2002:a05:6a20:8f01:b0:15a:2c0b:6c81 with SMTP id b1-20020a056a208f0100b0015a2c0b6c81mr33436991pzk.3.1697253391578; Fri, 13 Oct 2023 20:16:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697253391; cv=none; d=google.com; s=arc-20160816; b=q9470KWPsSdCSUTXnv5U0k2VDqf53FEtZ1bR+lbm4vsp3C0YCK3Yzz+pmte6qkpsgl yps6nQAyk7MFGy3ZTOcbVffiw3ElqVefIhswShx2fyTARnA93CdHktDkaGGsnWGUMgkY JzPysCk147QEusPjDw6TPee6tLCX3IqbVfBcD6qgXMvROF/gx3s8KngNlZEY6EJMRoxt gY2LWD5X8UAi/15CUz+V+JM2th9XlvLHewWOYwikKCJPYU0aw6Qk1TBwg9yvKe2yPwbO RUFBSw856ywF2ub+2wj36/jW5i7AgHkQRBzNGYl3GKgxfwxOWd9Y8wIdlZbj0iB+N21o b3rg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature:dkim-signature; bh=pFdrzLzM/rq+MQx3dlMfOvYjPo9PWBz0y+ifg5VdRSE=; fh=7SsncGRbN74Z5AHCSqGaWZ8qQBPFU2OgoSp70ZMB+sA=; b=hcYCwW9gvNuMHQ/3PtiUW3Py8fQdWZbKwGF2R6dMzkqyg0BuQ9ARRIYfp6ZvOTN04Q mu+GhTYd16CI9OySuFv/xJdVSda8zzjtGZMsPv6xfcQ3YL0JP1GiLhk2PyMJw9/O1RFa SVcC+fzHYAG6ImyJybbKljeZ3tdAH6FqCgBnNd3F71mMB7s/78NHNmOQhoUKkDvwnIwM +CWvGl2r1VODUi+aaAjn+sho/Y87rF17PuJ8lh/9QZ2IxZ7FaqEifBp+ar+dbF38evxP +BjjlgFnSoVBlYPxKCyyuK3/nh8zUR26GhTqa3hmbz7Vr4ohDmkHtgRaXJkjC1U0Q6m2 o9Hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=h3MFNTe7; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=qTCFjXWl; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id i186-20020a6387c3000000b005aa72875784si3047898pge.274.2023.10.13.20.16.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 20:16:31 -0700 (PDT) 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=fail header.i=@alu.unizg.hr header.s=mail header.b=h3MFNTe7; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=qTCFjXWl; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id B8EBE82ACAF1; Fri, 13 Oct 2023 20:16:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231469AbjJNDP5 (ORCPT + 20 others); Fri, 13 Oct 2023 23:15:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229518AbjJNDPy (ORCPT ); Fri, 13 Oct 2023 23:15:54 -0400 Received: from domac.alu.hr (domac.alu.unizg.hr [IPv6:2001:b68:2:2800::3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1445B7; Fri, 13 Oct 2023 20:15:51 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by domac.alu.hr (Postfix) with ESMTP id F20BF60152; Sat, 14 Oct 2023 05:15:49 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697253349; bh=0LkZfwzKwKNgRkuMZX6TvvfAt9PdEsq166wj+rMJcYM=; h=From:To:Cc:Subject:Date:From; b=h3MFNTe76sFuHATOndIfAp6M/7bcwOt1hfxXKwn5qQKushdcJtyFzhELV25p9kN+6 LGBeg3GgBY8NBIjyaUJKv2IIqUzXouRaWWk4VOm8fGSQA7VCiGzFpmVKa+HgzLykV3 Eyespw1tyTj1+IAZHjs+NgH30XsQGHafm+JUptcOrpPKChRXJv2BFMKdrvytRuqees w53DY9PjMPfSc9b8rmO8BsMoJ9hAhj/hvJh8qYoq212UD8KTsph/3BZd01NhEwJ6T2 zAEuTvuvihhsi2TZQ7UVtIdUrH25ZERS96VgRK4pI+a2AexGD3e6eFyMQcbDIT/8Cf BjTP8XsXeq2gA== X-Virus-Scanned: Debian amavisd-new at domac.alu.hr Received: from domac.alu.hr ([127.0.0.1]) by localhost (domac.alu.hr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id i1tZRJxgq527; Sat, 14 Oct 2023 05:15:46 +0200 (CEST) Received: from defiant.home (78-2-88-84.adsl.net.t-com.hr [78.2.88.84]) by domac.alu.hr (Postfix) with ESMTPSA id 1AC166013C; Sat, 14 Oct 2023 05:15:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697253346; bh=0LkZfwzKwKNgRkuMZX6TvvfAt9PdEsq166wj+rMJcYM=; h=From:To:Cc:Subject:Date:From; b=qTCFjXWlEBL4aCEnQqO4cSIsk7/5xLyOfLN12S6rnSAPYLz65IaOGrqjdnZO900a+ rAQ7Nn7kPchBuBLDbduodGysBk7wsgXjy3uDoE6lY/wubLSvgY89uDGlm8Bs8Nqalt f7RTTtK2o0u+rqkmO6rl9zUNs0sMd7HKdBw68WxBd2QNCoDGK9b9jJjydjCvjiths5 fCcs6gDCXGkSnC2UaPn4jAgUCXo/T5iKzR7cdQkQQ4dEUeQ+mcZWXy4t3ZNXaH4vlK jrsHvGRxMu8dgqTX118jQlyrxOsB7S0iQgEUkZ4jziS1agusuCsRAbXs7rgccjL2qM f1rEEMzuEO+KQ== From: Mirsad Goran Todorovac To: Heiner Kallweit , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: nic_swsd@realtek.com, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Mirsad Goran Todorovac , Marco Elver Subject: [PATCH v1 1/3] r8169: fix the KCSAN reported data-race in rtl_tx() while reading tp->cur_tx Date: Sat, 14 Oct 2023 05:12:56 +0200 Message-Id: <20231014031257.178630-1-mirsad.todorovac@alu.unizg.hr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,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 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, 13 Oct 2023 20:16:28 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779699172276628486 X-GMAIL-MSGID: 1779699172276628486 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 --- drivers/net/ethernet/realtek/r8169_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c index 6351a2dc13bc..281aaa851847 100644 --- a/drivers/net/ethernet/realtek/r8169_main.c +++ b/drivers/net/ethernet/realtek/r8169_main.c @@ -4394,7 +4394,7 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp, * If skb is NULL then we come here again once a tx irq is * triggered after the last fragment is marked transmitted. */ - if (tp->cur_tx != dirty_tx && skb) + if (READ_ONCE(tp->cur_tx) != dirty_tx && skb) rtl8169_doorbell(tp); } } From patchwork Sat Oct 14 03:12:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mirsad Todorovac X-Patchwork-Id: 152888 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp2278669vqb; Fri, 13 Oct 2023 20:16:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFu/MS792x+ID+L4A8oe+4grtI6QpafESb2JnfXCqZJCDpjNgHV9r2vEmEGL0G/GzCYlG31 X-Received: by 2002:a05:6e02:60a:b0:34f:7ba2:50e8 with SMTP id t10-20020a056e02060a00b0034f7ba250e8mr28265496ils.2.1697253411977; Fri, 13 Oct 2023 20:16:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697253411; cv=none; d=google.com; s=arc-20160816; b=mkRB+POw2ht9LE/zOx/vJ0pqcUlBrSSgV110ps3dXcLQpIxoPPahHlgLHddm1O53gy dvur/gTt30KVYu7+eT1ibMbqLm+3QzfyUDy/kq2szx9taDnUJfwqfb7SonhQAIh+KVO7 gSqDDkShqolIb43wHBIxkTD975V4B6DyQrMR+Fx/P2oFupzsceDvDyxlpdIkFBB8THZ3 JOYl7/RbElc3pBy7aG0YLgc5F760zyIleWx6LhQcSTuSNKBkxAS61fnLeh1ZL26D7Qw8 HI3U8yt0Iqao6+EBg7ojK9pfkl7VX5jTjBbbTto7LDsB2kk+Ooy4qfnUQwTuhUCznzbe 1ADw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=shtGQZTA8x/ZskajPiRs74nN/R7Nybn+TE0QOW76kKA=; fh=7SsncGRbN74Z5AHCSqGaWZ8qQBPFU2OgoSp70ZMB+sA=; b=vNFjfIyu7NrzVr/fEb09drd2O+8HD+mC11gdl3pka5MiRBVAGvIo7DNzBQKoz+ifTW ARTqcHrv96LyxZFDI3zmqa5P/RDd73R8Ad/ZgFgwsRHtt29HCVKKiZRE8gHXi698Uuso d//zPsmWgIVGdLFrXdsZRrx7G7D2TkOxD1/pI4rJSXpLu0KCBbqzya2R/xJUWnwT+FRO qtDUcBCIkX7IBh5L212uYi4iTmWmeVqv1Sk40gO/8C+OK4dumKLodDOfbSLI0XHhGoe0 O0kcVkc/O6LimTeO8Gt73fzw0hD9Q0SV+MeH6a14cEV1PSx/omrUtq+zJwzARuGULHdi sYow== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=TeXv0wia; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=e2Uo6ftm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id d15-20020a63ed0f000000b00553800fe4dcsi5979769pgi.583.2023.10.13.20.16.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 20:16:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=TeXv0wia; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=e2Uo6ftm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 20002822B728; Fri, 13 Oct 2023 20:16:48 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232686AbjJNDQR (ORCPT + 20 others); Fri, 13 Oct 2023 23:16:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232799AbjJNDQL (ORCPT ); Fri, 13 Oct 2023 23:16:11 -0400 Received: from domac.alu.hr (domac.alu.unizg.hr [161.53.235.3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37B2EDC; Fri, 13 Oct 2023 20:16:09 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by domac.alu.hr (Postfix) with ESMTP id 38F5160152; Sat, 14 Oct 2023 05:16:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697253367; bh=qpOvD4XKNURhpjg+KezJHAqIpJEgRBJVeTOQFFPohOI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TeXv0wiavGusj1Qc5l1MBeaQYu9LQTT+L+2Gnnv3zRNLtWWic0x06WB2m+/L43s2J Q7EUlusEHyRsFfsxARCOfkRev/ZYGC80t+rlnKG8NoUZRntCxclsMkIOZMsEDobTQJ 0CCGNwPHsdfUs8ZomBmNls2/fFz8+JR1kDCmqpgMYSr5Exn6r8VJRGo2CnIFzfCvRw Xvn7Nlj3nkLtqYHbSRmn3qqdFImp+FwyrklL6PVSrJ61gytLgrPTvmTj9bX18zbtBu phN8pXrOAX/WL61HJc8xu8OVEKqh73N1c3nB3F6WhcPV+AX9Bd0ri2gbN1kDsgymk0 yO5F1MaqRbDeA== X-Virus-Scanned: Debian amavisd-new at domac.alu.hr Received: from domac.alu.hr ([127.0.0.1]) by localhost (domac.alu.hr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id O7q1u_sy9m9E; Sat, 14 Oct 2023 05:16:04 +0200 (CEST) Received: from defiant.home (78-2-88-84.adsl.net.t-com.hr [78.2.88.84]) by domac.alu.hr (Postfix) with ESMTPSA id AD4606013C; Sat, 14 Oct 2023 05:16:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697253364; bh=qpOvD4XKNURhpjg+KezJHAqIpJEgRBJVeTOQFFPohOI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e2Uo6ftmNu9RizOSdjuT3QyjrqJLfp/dudX+moNZQmcIonxeEOTbj9MdjiUoxfDFn D1Wr1B6xgbGA3xi4eqqkxngEBVxvADh/FDTAl0J4mEOvgHbPcZAgBKCQW6Izqhfg6G S8gSejnBwLMpfA6EJyjt+y9Ik9Fx5WN/liFtFbEcC4iSxAxFAdJwDFTtrzvj0d8TG4 Hh1v6d+apw2mJ+9ExZjBX/ny798VHpJvEgwOAOFT6340wdhRSobJrxqvN0pLCU4mYJ 3+sJg2MsYrenhXS0Ak5VpU2cuc5WTg8vokelC+UXQbu86kC0kbkTI4OiE/1u3KrNpF Wbg/3RT6znKRw== From: Mirsad Goran Todorovac To: Heiner Kallweit , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: nic_swsd@realtek.com, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Mirsad Goran Todorovac , Marco Elver Subject: [PATCH v1 2/3] r8169: fix the KCSAN reported data-race in rtl_tx while reading TxDescArray[entry].opts1 Date: Sat, 14 Oct 2023 05:12:58 +0200 Message-Id: <20231014031257.178630-2-mirsad.todorovac@alu.unizg.hr> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231014031257.178630-1-mirsad.todorovac@alu.unizg.hr> References: <20231014031257.178630-1-mirsad.todorovac@alu.unizg.hr> MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,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 morse.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 (morse.vger.email [0.0.0.0]); Fri, 13 Oct 2023 20:16:49 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779699193738748786 X-GMAIL-MSGID: 1779699193738748786 KCSAN reported the following data-race: ================================================================== BUG: KCSAN: data-race in rtl8169_poll (drivers/net/ethernet/realtek/r8169_main.c:4368 drivers/net/ethernet/realtek/r8169_main.c:4581) r8169 race at unknown origin, with read to 0xffff888140d37570 of 4 bytes by interrupt on cpu 21: rtl8169_poll (drivers/net/ethernet/realtek/r8169_main.c:4368 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) sysvec_apic_timer_interrupt (arch/x86/kernel/apic/apic.c:1074 (discriminator 14)) asm_sysvec_apic_timer_interrupt (./arch/x86/include/asm/idtentry.h:645) 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: 0xb0000042 -> 0x00000000 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 read side is in drivers/net/ethernet/realtek/r8169_main.c ========================================= 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 (READ_ONCE(tp->cur_tx) != dirty_tx && skb) 4398 rtl8169_doorbell(tp); 4399 } 4400 } tp->TxDescArray[entry].opts1 is reported to have a data-race and READ_ONCE() fixes this KCSAN warning. 4366 → 4367 status = le32_to_cpu(READ_ONCE(tp->TxDescArray[entry].opts1)); 4368 if (status & DescOwn) 4369 break; 4370 Fixes: ^1da177e4c3f4 ("initial git repository build") 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 --- drivers/net/ethernet/realtek/r8169_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c index 81be6085a480..361b90007148 100644 --- a/drivers/net/ethernet/realtek/r8169_main.c +++ b/drivers/net/ethernet/realtek/r8169_main.c @@ -4364,7 +4364,7 @@ static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp, unsigned int entry = dirty_tx % NUM_TX_DESC; u32 status; - status = le32_to_cpu(tp->TxDescArray[entry].opts1); + status = le32_to_cpu(READ_ONCE(tp->TxDescArray[entry].opts1)); if (status & DescOwn) break; From patchwork Sat Oct 14 03:13:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mirsad Todorovac X-Patchwork-Id: 152889 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp2278757vqb; Fri, 13 Oct 2023 20:17:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHBVnvdkuH7vvG0l2W5FilQ5+KLZA7pH6lBXf2eHXbHUzRzcSprkO+k8csWViEbhyAoOlPE X-Received: by 2002:a05:6a20:7d85:b0:163:57ba:2ad4 with SMTP id v5-20020a056a207d8500b0016357ba2ad4mr33674256pzj.2.1697253434294; Fri, 13 Oct 2023 20:17:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697253434; cv=none; d=google.com; s=arc-20160816; b=W55L794sLZ9mZQgUNd5UAKTekPVpq7gra+QuRPhwb0XsvpWAyX9vTCuUjYJPpoMnnB zHn/e0giXABCBPpthCjxxWh4ohXRyUnRMmS3Ej0lhs7YLoF890mSXUEcPxTOwJzA1rVs 3a6bwuKzurjIUlLOWpc3fM0y8aWpGokskD9dh+YE5p37qMIEDK4qQ4BUnZYwrZLKCIDk ILGNOXFJHo3Qlx76bhk9u0QEIy3nn5bwZzU96yYQZF8FM6ui2N6kyAr8/Fw4fkf0HQKO Y4cPEJ4OMsvE+Gk8mnvJobdwbiaSuYJ5HM1EEZpGAymWX0dsTHZ2fPbFz3zS3skpJqhD QePw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=+kahk+1GBXctX075cXAihS0Utq69kOCAGWnWTNHbEQI=; fh=7SsncGRbN74Z5AHCSqGaWZ8qQBPFU2OgoSp70ZMB+sA=; b=ZgIwQC/IRjwHUj3andshanvxDDcd3OG837sSc5SAaeTdGFPeN2deoTrw4eaWramtf4 H+ZpTsGEzEsvJaQWwmDTO9NCJ28Mzymgy/57XKTLPoY53N7vacH4Q06ix12URVHKzF2S 9mVtYd8xvsaIqZtw3RmSflzVtYIazL9C2CWEZ5aF9TXREHUJHOfuj42yV5E8uq8Nk1+i YgDsLirEVntiALTRVu+WljJ+Fiw05NCr2W5vwu7O291P9j2lchW4tLh7mr0282H6WKUA L8ffsRALceMItFtpWkMbO4ykd9Dqy+qmUY09KjaIWG0fDfWv7A8q64NWM4yhPTGhxBTR if7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b="f+p/rckt"; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=n9fyNiZF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id y19-20020a63ce13000000b00566058702e8si5866527pgf.236.2023.10.13.20.17.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 20:17:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b="f+p/rckt"; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=n9fyNiZF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 3DABC80C841D; Fri, 13 Oct 2023 20:17:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232799AbjJNDQf (ORCPT + 20 others); Fri, 13 Oct 2023 23:16:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232834AbjJNDQ2 (ORCPT ); Fri, 13 Oct 2023 23:16:28 -0400 Received: from domac.alu.hr (domac.alu.unizg.hr [IPv6:2001:b68:2:2800::3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F12AEB; Fri, 13 Oct 2023 20:16:26 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by domac.alu.hr (Postfix) with ESMTP id 0601660152; Sat, 14 Oct 2023 05:16:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697253385; bh=SwhrpCal8ipGqLtP/4ZpVWC2b261mDXg4ooT66eGQv4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f+p/rcktS56TWtGa9rXxDvsonlQN1KNvorvNnoGTzdrBT+iB/8GPsitwIrs/W15Uj Jwqn+QIDiY69Lx1P+dakUPLb2ZxkhRPZgVhFRemGg5v2dTnYck8iuFHNplbajC2xMW z6pW4dF9SoB3Z8WjOchVIgkmtiZ1AXK17dDe0h3YmRtr8Uijj4mfaUZhEkE8Z9+LOt AbacSDV7Gz2bd778FudRp/xlXDCsae9c2njapdzqpXVyMFHY13mS8S3jkQ/+FaMP63 IInxsNn2VRVn1pxbcgD1y8hfBGsfKG1wtbMufk68j5sYBBfrNjjG7dEpwjPl6cGgMP oHjt9VWd01ubw== X-Virus-Scanned: Debian amavisd-new at domac.alu.hr Received: from domac.alu.hr ([127.0.0.1]) by localhost (domac.alu.hr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 1d7Q1rcixTo6; Sat, 14 Oct 2023 05:16:22 +0200 (CEST) Received: from defiant.home (78-2-88-84.adsl.net.t-com.hr [78.2.88.84]) by domac.alu.hr (Postfix) with ESMTPSA id 792166013C; Sat, 14 Oct 2023 05:16:22 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697253382; bh=SwhrpCal8ipGqLtP/4ZpVWC2b261mDXg4ooT66eGQv4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n9fyNiZFoXALzeKAyyflbXFpE9QWuKv3lfASlgvkhXMknnqQWRqjQzDF/gElU2qhR w5EpWAZNh+y/X+i7YO8Fsb1JNcf+mrv3kAhwrjz4NFOTULIGNYg/Qc3rItubIveCnd JVjjFNee0wtG1dy7pJbG9E6irJfLb4H3CWZ/68ITNdLMXrSN8F4Yq+v6Cpue7O1D9w A0ToupKTBxVe3wsq62XTcGCAAFwsG9WJQqS+RJaSM6AhCpJdoGBJpZY32ozvGnyPi8 BIN825ukev6ikcjNwkJraDk81PvulnEZNsmT09crguoxJnFbz+C+fbA/vPZjqsm802 T1+VwU4WC7omw== From: Mirsad Goran Todorovac To: Heiner Kallweit , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: nic_swsd@realtek.com, "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Mirsad Goran Todorovac , Marco Elver Subject: [PATCH v1 3/3] r8169: fix the KCSAN reported data race in rtl_rx while reading desc->opts1 Date: Sat, 14 Oct 2023 05:13:00 +0200 Message-Id: <20231014031257.178630-3-mirsad.todorovac@alu.unizg.hr> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231014031257.178630-1-mirsad.todorovac@alu.unizg.hr> References: <20231014031257.178630-1-mirsad.todorovac@alu.unizg.hr> MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,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 fry.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 (fry.vger.email [0.0.0.0]); Fri, 13 Oct 2023 20:17:06 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779699216833792623 X-GMAIL-MSGID: 1779699216833792623 KCSAN reported the following data-race bug: ================================================================== BUG: KCSAN: data-race in rtl8169_poll (drivers/net/ethernet/realtek/r8169_main.c:4430 drivers/net/ethernet/realtek/r8169_main.c:4583) r8169 race at unknown origin, with read to 0xffff888117e43510 of 4 bytes by interrupt on cpu 21: rtl8169_poll (drivers/net/ethernet/realtek/r8169_main.c:4430 drivers/net/ethernet/realtek/r8169_main.c:4583) 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) sysvec_apic_timer_interrupt (arch/x86/kernel/apic/apic.c:1074 (discriminator 14)) asm_sysvec_apic_timer_interrupt (./arch/x86/include/asm/idtentry.h:645) 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: 0x80003fff -> 0x3402805f 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 ================================================================== drivers/net/ethernet/realtek/r8169_main.c: ========================================== 4429 → 4430 status = le32_to_cpu(desc->opts1); 4431 if (status & DescOwn) 4432 break; 4433 4434 /* This barrier is needed to keep us from reading 4435 * any other fields out of the Rx descriptor until 4436 * we know the status of DescOwn 4437 */ 4438 dma_rmb(); 4439 4440 if (unlikely(status & RxRES)) { 4441 if (net_ratelimit()) 4442 netdev_warn(dev, "Rx ERROR. status = %08x\n", Marco Elver explained that dma_rmb() doesn't prevent the compiler to tear up the access to desc->opts1 which can be written to concurrently. READ_ONCE() should prevent that from happening: 4429 → 4430 status = le32_to_cpu(READ_ONCE(desc->opts1)); 4431 if (status & DescOwn) 4432 break; 4433 As the consequence of this fix, this KCSAN warning was eliminated. Fixes: 6202806e7c03a ("r8169: drop member opts1_mask from struct rtl8169_private") Suggested-by: Marco Elver Cc: Heiner Kallweit Cc: nic_swsd@realtek.com Cc: "David S. Miller" Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni 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 Signed-off-by: Mirsad Goran Todorovac Acked-by: Marco Elver --- drivers/net/ethernet/realtek/r8169_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/net/ethernet/realtek/r8169_main.c b/drivers/net/ethernet/realtek/r8169_main.c index 281aaa851847..81be6085a480 100644 --- a/drivers/net/ethernet/realtek/r8169_main.c +++ b/drivers/net/ethernet/realtek/r8169_main.c @@ -4427,7 +4427,7 @@ static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, int budget dma_addr_t addr; u32 status; - status = le32_to_cpu(desc->opts1); + status = le32_to_cpu(READ_ONCE(desc->opts1)); if (status & DescOwn) break;