From patchwork Wed Oct 18 19:28:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mirsad Todorovac X-Patchwork-Id: 155096 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp5020461vqb; Wed, 18 Oct 2023 12:30:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF/PMS5vtrxvrAp9AFuWJTmpE1IkzPywP254XLgkQpH0LJonFU48Hr64Yg3de4SDVTwbGLG X-Received: by 2002:a05:6a21:3286:b0:16b:cc6c:d728 with SMTP id yt6-20020a056a21328600b0016bcc6cd728mr100475pzb.44.1697657443438; Wed, 18 Oct 2023 12:30:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697657443; cv=none; d=google.com; s=arc-20160816; b=jRSG2blcN+zvU903sV0pop9ro96KkVTzeQXsSXXlhDCOtpGWDALc+512MLDr8/rDsc b8Cnau4XdQsxicD4kOm3tzd1hqJYPF+dNm7tFe2qzaGC8hdXS5HUmdislarMhR6j8nx1 aV7OZ6iljsBWhl0iAjcwJFeOwMq1YLrxFEtVbceHNKLUnN+cHctMhU+jRV4CzV+Kgjej jwDTirglaM2vxjZ5POEuMmlEdA3oasjVVfylkLk/vQkVsTXoBiOgd5XDKxuK0/ZAYVP7 gQou4KSlEt3rWgzVjV5LqaNGNBOlNXP0Cun+jObpNEMz2SjQmujTgrFsMfi5vKl792/P zuAg== 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=hyOgw9rQEtA5jSJUU1fhvESIpZir252UHY16h2CDjxE=; fh=7SsncGRbN74Z5AHCSqGaWZ8qQBPFU2OgoSp70ZMB+sA=; b=TvwCK9ypgMQKBiDlsOeVL/PEyDgHXiCHCXfcwDUz6lklu3aJQg5kOkZ98whhwvmLQr tw4NsPwvQy+Tk+FB+IwjjZScVzu6cPOeDeELB3y6JAROnlFE2AX5CvS69Cr33j/Gnumv T7677rH5bh5nYntlJ8HlhHoDgFJpjcOzfmv2OpgQxUtJoTxF+e1ereIMztCp42GMr4ZT /eCOwpZ6CHMczz5KPwO1rTQbVDXNRG04JSX4BGDwrVg6UsFVTLHaT2H1ay0bnCW+f3Yy p16D207aTJThV+Ljab7YiClJnVyc4S6aYHW3c0qs4mSYCiCVqqEHaqgGAM+Zi1lcTpxq E48g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=JHL3uEbQ; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b="ug//8ap9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id b13-20020a63d30d000000b0056a1ed17deasi2756288pgg.675.2023.10.18.12.30.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 12:30:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=JHL3uEbQ; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b="ug//8ap9"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 68C3081BDF24; Wed, 18 Oct 2023 12:30:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230147AbjJRTaf (ORCPT + 24 others); Wed, 18 Oct 2023 15:30:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229552AbjJRTad (ORCPT ); Wed, 18 Oct 2023 15:30:33 -0400 Received: from domac.alu.hr (domac.alu.unizg.hr [161.53.235.3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B76E2AB; Wed, 18 Oct 2023 12:30:30 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by domac.alu.hr (Postfix) with ESMTP id 215206017F; Wed, 18 Oct 2023 21:30:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697657428; bh=XkKql79KvtYio+gIsoamLBoAA43GneTQXqGS71oA1/o=; h=From:To:Cc:Subject:Date:From; b=JHL3uEbQKJnuX1Fj1RxtZP0awkogRce8jPBOILqwzv8Sw+08rWLIshIyCQ1vEPSmP ajrUCrPdhTLmz8gNA1aPCEsxDnq6nggk/FEne5XR4uMGcafrtKwx4oWas+zih99nze 5j0LGMpevAwVaHY8i+cTvLmTd73J9Cx27h+beEmHTwxoNNG1zDb/MWSTuIRtbOuZTd RcZhnv2MaKCCGAcyFVhMa6kLuD5BpSdbiBdqdMuqK7bSYr4sTvXG/7tZkYxrHiOytm ++6pvow4NyBenrUGcEMQk3KOc7pIPGRCg7boU5pPZvo52o8nPzsQqppfSOtOeL+jDo 7GZN+RceDqsLQ== 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 PHCQU5ZtOC5t; Wed, 18 Oct 2023 21:30:25 +0200 (CEST) Received: from defiant.home (78-2-200-105.adsl.net.t-com.hr [78.2.200.105]) by domac.alu.hr (Postfix) with ESMTPSA id 7093D6017E; Wed, 18 Oct 2023 21:30:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697657425; bh=XkKql79KvtYio+gIsoamLBoAA43GneTQXqGS71oA1/o=; h=From:To:Cc:Subject:Date:From; b=ug//8ap9RpG3lYPrg3JT4BK3xvdWEP9jcZW0vRaIp7xyaL/z5drdfWw0I0Uh/GLOb /nnibIVUMAvJ320TbsWX2nHHBpPOssYXrtz7FYcOoJj8JpRhEsD9MpFyQKRR8/2C6F VlZ6rNuP7yAileVKx1MOUFlJpKCsDsWf0P32pDKcSJQVNilLYtWmNa+ZoJj72kbx3q vFpA+9nt53Q3H9bqwdwq/NCVebb928b5h4c/dTILcAlvJ0+UVhby6YVTP5Y3AcMFzV AWsfOZAVrDDnGvGF8q3ze+SNXXf9ubFt5ovbMR3QOLQ1miZT9ALPQL4YUcdBokRWbI 0RFx6cu+16hpA== 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 v3 1/3] r8169: fix the KCSAN reported data-race in rtl_tx() while reading tp->cur_tx Date: Wed, 18 Oct 2023 21:28:27 +0200 Message-Id: <20231018192828.343562-1-mirsad.todorovac@alu.unizg.hr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Wed, 18 Oct 2023 12:30:40 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780122851528443675 X-GMAIL-MSGID: 1780122851528443675 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 --- v3: fixed the Fixes: tag for 3/3. v2: fixed double Signed-off-by: tag 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 Wed Oct 18 19:28:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mirsad Todorovac X-Patchwork-Id: 155097 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp5020970vqb; Wed, 18 Oct 2023 12:31:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGDtqT6IBBBL7Itqv5UWZGO6LYd3LAGpLpz6WKqwc6zE38CsNNIV0mO6LIIYApbDErvq+Ls X-Received: by 2002:a17:902:e841:b0:1c9:b785:bc50 with SMTP id t1-20020a170902e84100b001c9b785bc50mr299479plg.47.1697657492294; Wed, 18 Oct 2023 12:31:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697657492; cv=none; d=google.com; s=arc-20160816; b=LVuoOlBhzpJtQlW9unWnFnZlh07VY5zYTpiKWIBIK3SzdxKHbSyY/qLZR0sKUWS2Sp F87j7hATkTYuKFzeljcI6RtLDyKl9o9SWitd/KdFYdypwUDN6oFnm+DQX0le1GVPE2K+ alpIqtilBCvVtMIxYJetrQ0BZ+55AA5kCj1BtYCXXNuMRw3lHd1cF1C2gnvR+ryHxLj1 +luq/PJ5IFMpyxE3ZYpRtUn4n5Ah23S+tE+lyB+0BDMnAU+HZpreklALP1a7Kx6x0ezk FAoljG9+jFs8CoZ5f7fYdGy/l/2XR9y/SRGN/W+nl6dwlVCfKf8ianqmpWE3ZaSNbOI2 oasA== 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=XUvfZiKqoIO0LHrJ8MSzmyOlxcFuBwmJhDJZOpaZizs=; fh=7SsncGRbN74Z5AHCSqGaWZ8qQBPFU2OgoSp70ZMB+sA=; b=HPsdfo2ag3eMYxkk3HKr5L0opUk1DvRpmXlDsFFWo6ZU8UiFP8JjTBadAE7XsSpFsO SfcTbPcbBjpKDYEa64Iu3dwwokoU9YczsUvDEODhrWXkgNEZ1Wzt7WceHTSLtD+HzwX5 MkpeSlH4u607Wo/nUZNGZ56XAyrqBLdILEjx14/Y/ljsShIJaCiJsaiVa/w8UHwvpFlZ ym1mAwd2yT7ZbxQi6bCtlpCCjWofjM87Pl5bdTwIs3DEYnYXyVR8QQDBm9V+QTFSypr1 I8gjoJgpR6YRILby6Kn50N+PlI/Ub4fCMMdnOk4kgwX5APXQt32P0/C86V/yXWVY+HCm Gcjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=KHM3q59b; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=UUZZK7g5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id q3-20020a17090311c300b001c9d96cbd97si572127plh.66.2023.10.18.12.31.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 12:31:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=KHM3q59b; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=UUZZK7g5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (Postfix) with ESMTP id 459BF80D6AF1; Wed, 18 Oct 2023 12:31:29 -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 S229923AbjJRTa7 (ORCPT + 24 others); Wed, 18 Oct 2023 15:30:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231513AbjJRTaz (ORCPT ); Wed, 18 Oct 2023 15:30:55 -0400 Received: from domac.alu.hr (domac.alu.unizg.hr [IPv6:2001:b68:2:2800::3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42D29138; Wed, 18 Oct 2023 12:30:52 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by domac.alu.hr (Postfix) with ESMTP id 5165E6017F; Wed, 18 Oct 2023 21:30:49 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697657449; bh=gVLlUryR/gipZWvKZqxamud/RSVtVmRXx/zDZWcW8J8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KHM3q59b3fBMsxuL4XvOgaV0YYxaI/ByW4tlxu5RfkjeO02mmri2caKWCdLC83Q0r XOeuKvbImB2wick8pojWOmqLKke6SKn7f5R6PN8prByaz9VTgGVdi4kkcWCSBGX5qE R670ghfcEeXyjyauscMSf7hJxnMrz4gN5Ib3SuuOvno4rV+Q/KKQvF1wqqjpuwH1j/ TrPX935L/DVjsl2t7NfduejkINkFOOxp3k2fabBHA/mXWIz+64TuRdFxDNZE9aWoxi 47qNZ9OySdK552lf27DwE+cxDfRDYMGiemJElzwm8bdqOleEjease3Lo60VVoHk+dI tKtGNlcE6/Gbg== 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 fSOPNPGDigns; Wed, 18 Oct 2023 21:30:46 +0200 (CEST) Received: from defiant.home (78-2-200-105.adsl.net.t-com.hr [78.2.200.105]) by domac.alu.hr (Postfix) with ESMTPSA id 8A31E6017E; Wed, 18 Oct 2023 21:30:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697657446; bh=gVLlUryR/gipZWvKZqxamud/RSVtVmRXx/zDZWcW8J8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UUZZK7g5hVaTqZp9aM00jXAF7lp92Hp/fZTMqkyOdJmZOnMZ0mdqVCvHyE3G45BTb vhF6KkX7fJ+6I+8cQ2kCqj+Hq8+DWlziAH3uzSRDlzpnH1uJj65GaHgkgTh4R6NxVU DXmrb7/VACThgSuVjcvUQvdWTzWn33Xaab3kOHWvfqXJH4YrPxvBgN7K/9mjhNxuMx o5Ke80WCG/IvPBsoXpWC5pk9hSA4t1UAyAaWmkWchV2tGjk/0CwS0GcEsHD6+8nY3q HcfsyfBa/Dm+ObexgcB8nbm+ly3ZLLrMUk6gCMWGHfFQNbJe7sPMNMYR6nXgurKezO 4htcAjy8F3kvg== 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 v3 2/3] r8169: fix the KCSAN reported data-race in rtl_tx while reading TxDescArray[entry].opts1 Date: Wed, 18 Oct 2023 21:28:29 +0200 Message-Id: <20231018192828.343562-2-mirsad.todorovac@alu.unizg.hr> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231018192828.343562-1-mirsad.todorovac@alu.unizg.hr> References: <20231018192828.343562-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 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]); Wed, 18 Oct 2023 12:31:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780122902507303187 X-GMAIL-MSGID: 1780122902507303187 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 Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") --- v3: fixed the Fixes: tag for 3/3. v2: fixed double Signed-off-by: tag 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 Wed Oct 18 19:28:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mirsad Todorovac X-Patchwork-Id: 155098 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp5022434vqb; Wed, 18 Oct 2023 12:34:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEWuTIeNZkZV2gG0jpuLolmNCclqNzUFl1aBtHAScbQWfL21PTgP+7lpc6lLnjHvwv238zf X-Received: by 2002:a05:6a00:124a:b0:693:42d2:cde0 with SMTP id u10-20020a056a00124a00b0069342d2cde0mr37844pfi.22.1697657670337; Wed, 18 Oct 2023 12:34:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697657670; cv=none; d=google.com; s=arc-20160816; b=TerR1/ofpDy11hmqGO9RloGS2e5L9jQg3Wkex9/XzS7jlT5bWBos4LnU6XtVyxkWZ9 uhQgQNFRzfsFelulGkWtbBHEt8K5K3q+eI62lQErxA8LayNNG85wvfp8uLSJi2Fk2N8r wSYdRhuMca9kUmZHoyPWfNsHQQWiEuxFbxs4/Weqc3+taTs8F1q/8En/qFDer1lkQyzV wNRvrWsy48uMbskbSbYp8WY6VtOI0Ughl7EulbCGjkq0cXkSh1H3MRAO2doZ06Jha2mz UrTyLXsow48gHBLLz1ZLES7w4sdAE46AET9jb/pkd5+Pq5r8/sqFInY3tno9837IZvQq EuWg== 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=znSL0HFM5Klv/dylMjO2yIwenk6Ar4TUZT2CgFfVC9g=; fh=7SsncGRbN74Z5AHCSqGaWZ8qQBPFU2OgoSp70ZMB+sA=; b=pWl0dQdjkXPHGpv+MGuIYYByYkA+Y3GFjTcBzrLxaIEIwSCRy5Attvdb285NTiYYlA BDqbxdy2ssLue8z4mPXmAu6Wwquaga+irq8ZpFzAFDngxZ0J9CbjpsL6munXh6AqIBFD aKa9LfFwsWA7Q+tO/usLv1EDCVi1BUAokxNRFK8yjCuAVarxTgSbDvjxlgzuHGuT8YCn D1dpPHzd4JeU+dEOqqWs40kMQj+Mf6SDNJ+1faFKxunI3B3f8cG13AVmxJ61+uM8L7yV +P3p2yaIhb7C11qN1XSY+fLKtsJ5LpMy1G5diayAs2Li833GAhNUiSEQDFXI8YRTticb roPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=ugaIAH3z; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=EhmDyHZk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id fa4-20020a056a002d0400b006901504b6a3si4923104pfb.153.2023.10.18.12.34.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 12:34:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=ugaIAH3z; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=EhmDyHZk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 8FFC981BDF32; Wed, 18 Oct 2023 12:34:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231341AbjJRTe0 (ORCPT + 24 others); Wed, 18 Oct 2023 15:34:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231235AbjJRTeX (ORCPT ); Wed, 18 Oct 2023 15:34:23 -0400 Received: from domac.alu.hr (domac.alu.unizg.hr [IPv6:2001:b68:2:2800::3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5C3811F; Wed, 18 Oct 2023 12:34:20 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by domac.alu.hr (Postfix) with ESMTP id 40C4C6017F; Wed, 18 Oct 2023 21:34:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697657659; bh=/W3mk8TahFX3fe6Zwq2eTM/o+YuIuTXh3wIDyuxcYGk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ugaIAH3z/KUiC9ZBmjFP9shpP559G4snGyQ94CtEH1nndvxEaTajwQdUxBsXHouZd phZllqTU1Bcd7ItNdoPsQf6IDqsUGypTCjuU/lyL5f29UqhkxRvVZFPWxkyxTPfNC/ fzNunZchniaIKUjcAJDZnP9pF5dKhdiqj2Ximiu1hf6YUKZ8LkbAAhLHgUeJxTjr3U BLfKDRorxGIjYLRYxjb1lhJdUuYK5e1lxboLu72w9Xeb8CQknHAYo8QV0kRzXOL2mr L8RxV5H7WzxtuQrfxRfymhgr9lhijLqFxtDT3fRz1vqKq0V8jK/JFbuLLO/F9IjxHg eExZ2I2q0RsPw== 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 pH1VaXI5Ez19; Wed, 18 Oct 2023 21:34:16 +0200 (CEST) Received: from defiant.home (78-2-200-105.adsl.net.t-com.hr [78.2.200.105]) by domac.alu.hr (Postfix) with ESMTPSA id 7C0996017E; Wed, 18 Oct 2023 21:34:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1697657656; bh=/W3mk8TahFX3fe6Zwq2eTM/o+YuIuTXh3wIDyuxcYGk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EhmDyHZk/+90zh97GTcCb56QBpvh+97Fd+5aS5Kg+D/kBjRmxWLvl2vftvvf7GrLX IAE46Mv/szXpw6g5O44A6e17WHGNlQGIqbtv6bPjx7mLHJWZgGZk/JlJftYW9Hmgr5 1wGj3Esp6XERxxA1nbCjxlMWXklsn5cksYCkUXHv5Hvzirr8XfVmiz6REMC2inFGCs DJR9wtC/fj++oXZAvyqDP3vSxAkSo382T7FR4yjEhNgJZhU4NurtuITG3gAV6NI13Y 21FOkUh8R9ShD7v/KLzR8oOd7gL3navjYn35NTtjOn0oT2izy/FmNxHIx+4W0I73En lGif90N9463/A== 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 v3 3/3] r8169: fix the KCSAN reported data race in rtl_rx while reading desc->opts1 Date: Wed, 18 Oct 2023 21:28:31 +0200 Message-Id: <20231018192828.343562-3-mirsad.todorovac@alu.unizg.hr> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231018192828.343562-1-mirsad.todorovac@alu.unizg.hr> References: <20231018192828.343562-1-mirsad.todorovac@alu.unizg.hr> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Wed, 18 Oct 2023 12:34:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780123089355802733 X-GMAIL-MSGID: 1780123089355802733 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 Acked-by: Marco Elver --- v3: fixed the Fixes: tag for 3/3. v2: fixed double Signed-off-by: tag 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;