Received: by 10.192.165.148 with SMTP id m20csp3750339imm; Mon, 7 May 2018 18:57:11 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrKz7DiTOSwqgxh5+pIGiBJQk6/Y//anGd6moIB9K4l18CYBS0AuOj5LDcOoUgv3HadLd81 X-Received: by 2002:a63:7253:: with SMTP id c19-v6mr31323675pgn.425.1525744631401; Mon, 07 May 2018 18:57:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525744631; cv=none; d=google.com; s=arc-20160816; b=vygChYCHNd1PDjLhE6SPzSE/6ZFqPsm9v7vT/xmmtH57vduSGFkA0ahHqMyaaxxY8t EOudeAVgnbZo/qtsxU8dps4auM/snYMX/T3EJesskpJu1ESA8zz3ycOB8eLbMJSqLhOn B2KpJnprVvPNIliRJwAVIYLZ7gBgUC5GP8dhQBtwswzGPTOW9rR6ccyMjOwY4I0nfaFf N4eCf/QCDGnXv+dpcc7N3VOAIKqi5UUXjwYjZxHSpgS7P4uuMvfHAJriJ9du2szUe9by m58Myf0pUaIG8viGGDpPTtuREBqJyZLvKlZpot7ap9foBryQAyzrYrf4pMysDMv4h6VW jRRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=xFC+pS3bZElqc628pp6f+5MP8BOt0wZ8JTHTPRZR2hE=; b=VgAEo0HOJe4Xo/xhX5miNxEz9ZeljMzNdUqZ/IwIeff683cGmta1V9Yco36sgvS9aB KJzgI+l1PIsD3j8jmZYYdGy9n6iXHVbi6J8ID+HGRxP6Lu0Fbf4AAYgxDWOQFyBdNxI3 GCMrxhLqPW9dhx1fYtNLBP8GEYbIcLgWRzzaq+DtZ4m4wc7KaLYbhaLuKM6kLLw63kfq QhscnTn7BxsxRg2ejWTnSPsqAk6/9DGvoOWUHfNKIfYQ0B7XKg7o+MbMJAxx8pXoKBMG 73DFNJHoJ+A7WSM7uZ8URP1MfGRBtLw/uYZG1vJ1l7+5XPdnKAryYsURgPZ85yOucXae dUbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=I1ekTD33; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l8-v6si4116112pgr.343.2018.05.07.18.56.56; Mon, 07 May 2018 18:57:11 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=I1ekTD33; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753887AbeEHB4g (ORCPT + 99 others); Mon, 7 May 2018 21:56:36 -0400 Received: from mail-pl0-f68.google.com ([209.85.160.68]:36081 "EHLO mail-pl0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753737AbeEHB4c (ORCPT ); Mon, 7 May 2018 21:56:32 -0400 Received: by mail-pl0-f68.google.com with SMTP id v24-v6so1231725plo.3; Mon, 07 May 2018 18:56:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=xFC+pS3bZElqc628pp6f+5MP8BOt0wZ8JTHTPRZR2hE=; b=I1ekTD33VRFuX4jD8iRUVS1noNR52NHcfUEddMXOnshIOCU5dB+6JSi+P9oIDXhsZ0 e8zQG66vEqQ2tcYUARcoJF9jR2kiZDySShkIEi4Z/42GMzXhP4LTkvbOh2hSTNFkSWir OLXN0MJJV9azdJE4l2bTS4HYlSLU/JV1VybkDuQQCH1HN82ejw9UAVgS6eeBXqpgSBZ4 PNNlfG3sLuGOWv7+9fBNE/3fvv2c+PVRDy2HoUQa2s5uyfOHKqtRDNJboGM88vcFBHFV Y54BHwPNJK3mSBL8DTDBXJ2ceAbrQ815Mp323N0/VJStlT2nvjFBWOrx6xJh2G0hpHIt nQQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=xFC+pS3bZElqc628pp6f+5MP8BOt0wZ8JTHTPRZR2hE=; b=noIvEOd7kvruUyjNUEK5Tvcb7JYigJG8aSy/xCzp5EteeiYSh9O4+VE3maQcK1VNnZ ANK661wMfrANnxe0+iwiUlcgDsJP7QSZsONxxx2x+OINB+K4KW2yeP6rpEkfgS9zcPcl 1s1zHWil3Ntg/OXUdWK9BwtLLhxlZAWMfn9FK6+TpbqDIZL7OYYmwJO0Iq/nLAq7r0ll JHsZ2MKX8TH/njCpq4FOUPeJHXKv5CRLUUz/3x59QZ3SprWmZWiTvy54vGMZCChc8t6E vcwZtHcoODwV64hafeKlqVXouovEB7oF4fgT8jvRWfaPYowM5pMFyJSOQeUtR/Kko9NS chMQ== X-Gm-Message-State: ALQs6tDP8QReXdcddz6bQvYGdWtf58ow6LVaqKhc0igC4GyLg3nZaZ3h dPbl7x44T7xXhKtTyhKosuWAB80z X-Received: by 2002:a17:902:9a9:: with SMTP id 38-v6mr40075930pln.114.1525744592025; Mon, 07 May 2018 18:56:32 -0700 (PDT) Received: from [192.168.86.235] (c-67-180-167-114.hsd1.ca.comcast.net. [67.180.167.114]) by smtp.gmail.com with ESMTPSA id 65-v6sm1097804pgh.87.2018.05.07.18.56.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 May 2018 18:56:30 -0700 (PDT) Subject: Re: [PATCH] net: 8390: Fix possible data races in __ei_get_stats To: Jia-Ju Bai , Eric Dumazet , davem@davemloft.net, fthain@telegraphics.com.au, joe@perches.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org References: <20180507140809.28847-1-baijiaju1990@gmail.com> From: Eric Dumazet Message-ID: Date: Mon, 7 May 2018 18:56:29 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05/07/2018 05:51 PM, Jia-Ju Bai wrote: > > > On 2018/5/7 22:15, Eric Dumazet wrote: >> >> On 05/07/2018 07:08 AM, Jia-Ju Bai wrote: >>> The write operations to "dev->stats" are protected by >>> the spinlock on line 862-864, but the read operations to >>> this data on line 858 and 867 are not protected by the spinlock. >>> Thus, there may exist data races for "dev->stats". >>> >>> To fix the data races, the read operations to "dev->stats" are >>> protected by the spinlock, and a local variable is used for return. >>> >>> Signed-off-by: Jia-Ju Bai >>> --- >>>   drivers/net/ethernet/8390/lib8390.c | 14 ++++++++++---- >>>   1 file changed, 10 insertions(+), 4 deletions(-) >>> >>> diff --git a/drivers/net/ethernet/8390/lib8390.c b/drivers/net/ethernet/8390/lib8390.c >>> index c9c55c9eab9f..198952247d30 100644 >>> --- a/drivers/net/ethernet/8390/lib8390.c >>> +++ b/drivers/net/ethernet/8390/lib8390.c >>> @@ -852,19 +852,25 @@ static struct net_device_stats *__ei_get_stats(struct net_device *dev) >>>       unsigned long ioaddr = dev->base_addr; >>>       struct ei_device *ei_local = netdev_priv(dev); >>>       unsigned long flags; >>> +    struct net_device_stats *stats; >>> + >>> +    spin_lock_irqsave(&ei_local->page_lock, flags); >>>         /* If the card is stopped, just return the present stats. */ >>> -    if (!netif_running(dev)) >>> -        return &dev->stats; >>> +    if (!netif_running(dev)) { >>> +        stats = &dev->stats; >>> +        spin_unlock_irqrestore(&ei_local->page_lock, flags); >>> +        return stats; >>> +    } >>>   -    spin_lock_irqsave(&ei_local->page_lock, flags); >>>       /* Read the counter registers, assuming we are in page 0. */ >>>       dev->stats.rx_frame_errors  += ei_inb_p(ioaddr + EN0_COUNTER0); >>>       dev->stats.rx_crc_errors    += ei_inb_p(ioaddr + EN0_COUNTER1); >>>       dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2); >>> +    stats = &dev->stats; >>>       spin_unlock_irqrestore(&ei_local->page_lock, flags); >>>   -    return &dev->stats; >>> +    return stats; >>>   } >>>     /* >>> >> dev->stats is not a pointer, it is an array embedded in the >> struct net_device >> >> So this patch is not needed, since dev->stats can not change. > > Thanks for your reply :) > > I do not understand that why "dev->stats can not change". > Its data is indeed changed by the code: >      dev->stats.rx_frame_errors  += ei_inb_p(ioaddr + EN0_COUNTER0); >      dev->stats.rx_crc_errors    += ei_inb_p(ioaddr + EN0_COUNTER1); >      dev->stats.rx_missed_errors += ei_inb_p(ioaddr + EN0_COUNTER2); So ? > > So I think a data race may occur when returning "dev->stats" without lock protection. &dev->stats is a stable value. It wont change over the lifetime of net_device object. Adding a barrier before or after getting &dev->stats is useless, confusing and really not needed. > > By the way, I find this possible data race is similar to the previous commit 7b31b4deda76 for the tg3 driver. Very different things really. This does a copy of the whole stats, not the pointer : *stats = tp->net_stats_prev; I guess you are confusing simple C semantics about returning the address of a structure, instead of returning a whole structure. If __ei_get_stats(struct net_device *dev) prototype was : struct net_device_stats __ei_get_stats(struct net_device *dev) instead of : struct net_device_stats *__ei_get_stats(struct net_device *dev) Then sure, your patch might been needed.