Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp490105ybl; Wed, 29 Jan 2020 04:23:12 -0800 (PST) X-Google-Smtp-Source: APXvYqzyifAJt55qbylK/sJ7Lj2Sa2uozAFGMjzwsI2hWHiw864NwY0t0/qoo8vRxrW3fnvQMpqD X-Received: by 2002:a05:6830:1e37:: with SMTP id t23mr20705667otr.16.1580300592845; Wed, 29 Jan 2020 04:23:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580300592; cv=none; d=google.com; s=arc-20160816; b=tFyMho+YZ9ckaSvGqUOPp5WDoMqwcnygB/poufCeGhbqXNswO5VDt3N24h7BUyUAos ZN9jo9qYTH2aXnNv3XxIlENX8oijH4XwJlrtBs5LR7o3xJjDEyQSYkLEFg9lp6meIm8R hTKez6SkOh6q8NTri8QxLh1/IyNSjGTI8Zdcc5j5igkHDRpvILsSWOOmz+9TTWlMPxvo PRx20EMsrh92asH+XhzcypTEVLcqht6mL1NaW0nESp2ZPFgvcScdHzaVafTwMbzGdSA6 YGh0B8x188iP+QSKyX0etcHyWZFZv5F+mDbiq/JWL4QW+QqCKTpOaeY3mIsMFPi+CWjb 433g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=Kgn4GQBjvZyBAIu6NGCbaVyen3lY97LcGgNiimjGj/Q=; b=oefkeIVe4n/gjqRx+PElzFAgAWl7k4WOVhba+qJEh9uOyI+gmMmpaymyEqsxXiqjCR 00AJwFyGGY5Yf00R6iXiStCg3uF1d2GF08/ce7pORCblwkO0L1j7qJPvxaIvhAisdrit cCSMs6kivCmPzP8b0B3HE5A6G/16SOjMlAXn6zGydPQiEjJZhYtJKe7Pw0AwNFEUAbGG FIBFfv/clZfoDYbj6Zm9k8YVx3Ng0NIdiR2eFVMknp10ekv8NC4Pod276XwSen0OdDx8 l6pwbxHDJ75KKTpgZccK9NyXcZLPGZZt0ZAgHUtb5Ssg8vMzBmjyyt2bJamgsdjpdQye ATGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=phRBcjau; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n14si937504otk.179.2020.01.29.04.22.59; Wed, 29 Jan 2020 04:23:12 -0800 (PST) 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=@google.com header.s=20161025 header.b=phRBcjau; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726466AbgA2MWD (ORCPT + 99 others); Wed, 29 Jan 2020 07:22:03 -0500 Received: from mail-ot1-f65.google.com ([209.85.210.65]:43407 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726145AbgA2MWC (ORCPT ); Wed, 29 Jan 2020 07:22:02 -0500 Received: by mail-ot1-f65.google.com with SMTP id p8so15292507oth.10 for ; Wed, 29 Jan 2020 04:22:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Kgn4GQBjvZyBAIu6NGCbaVyen3lY97LcGgNiimjGj/Q=; b=phRBcjaur7ITBOkqk5XTPrxyTXZOs2jwKLwhceVHmZrIGkzjAA+YGugG9jRKZhA6Cd kwEAM87ZSvXBKr6baOvtgQJhb7e1hO3Ppm4SJKTJtE5DN8nbSv4k5WpiHW8QaeTl651J l93XHr9fMHLJRqRfTqvcZwfSIHqGyxXnDdQi98uWUA2p9doiLia3Zl+YWF6GXmhSOFop 3I1JBPp59ih27dLmEn88Kby+o8RQgUgyL9AA1er0WLmmm2s0M4BA/1cRWtTkwSNGTaxL dnREovtzSOuKw8t/KR5ZmGQECMciE2XkcIwEEXh4R6TZ+JngFiNBjw5bAJwlK31KUSLE we3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Kgn4GQBjvZyBAIu6NGCbaVyen3lY97LcGgNiimjGj/Q=; b=AVx3oZRpJPz0FvrtME0u87PbSJdkgpDncR+2S/zOO1MVRhRDDhyjurzYiPtNI8IR0M gCe8ZjhUTd3lplffa5YVKXKLaLj4DkoLWwZt3N/p44ckf8DCIfnHoqMbOvtJ6j8fI9oc zRbQ9qZA89SqvPBlMLZcQUA0tU4sHfVY832Phm6ctjqFH/XCaD7Q56OvFhSV/pwtZ01p 9lOmwfjq4Fukmws7teWZ9likgLtgw37YyyNxxrFlV/Vk+CFDBP+tb5uBLXl7fv/eVpAz vu5mCHYD0YzVd93++zRNj44WCJJo0afbkJZNFB8pmn6+XHOuKvFKjU9NNyS7Vl9fQ6gd lXGg== X-Gm-Message-State: APjAAAXw2tJbZCbN3Ithk0mAEqlZnq59vpNtFjjYKJvu6DHAm9x9PHZX CzBA8k1sR6zpQ1hhpRQ+XNZE8FNC7vbxaclJprvyFw== X-Received: by 2002:a05:6830:1d7b:: with SMTP id l27mr18932727oti.251.1580300521393; Wed, 29 Jan 2020 04:22:01 -0800 (PST) MIME-Version: 1.0 References: <20200129105224.4016-1-cai@lca.pw> <20200129120302.GJ24244@dhcp22.suse.cz> <59f892d0-5fc4-ae32-ce65-5a688d9180c8@I-love.SAKURA.ne.jp> In-Reply-To: <59f892d0-5fc4-ae32-ce65-5a688d9180c8@I-love.SAKURA.ne.jp> From: Marco Elver Date: Wed, 29 Jan 2020 13:21:49 +0100 Message-ID: Subject: Re: [PATCH] mm/page_counter: fix various data races To: Tetsuo Handa Cc: Qian Cai , Dmitry Vyukov , Michal Hocko , Andrew Morton , Johannes Weiner , Linux Memory Management List , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 29 Jan 2020 at 13:13, Tetsuo Handa wrote: > > On 2020/01/29 21:03, Michal Hocko wrote: > >> Fixes: 3e32cb2e0a12 ("mm: memcontrol: lockless page counters") > >> Signed-off-by: Qian Cai > > > > Acked-by: Michal Hocko > > Please include > > Reported-by: syzbot+f36cfe60b1006a94f9dc@syzkaller.appspotmail.com > > for https://syzkaller.appspot.com/bug?id=744097b8b91cecd8b035a6f746bb12e4efc7669f . > > By the way, can READ_ONCE()/WRITE_ONCE() really solve this warning? > The link above says read/write on the same location ( mm/page_counter.c:129 ). > I don't know how READ_ONCE()/WRITE_ONCE() can solve the race. It avoids the *data* race, with *_ONCE telling the compiler to not optimize the accesses in concurrency-unfriendly ways. Since *_ONCE is used, it conveys clear intent that the code here is meant to be concurrent, and KCSAN stops complaining (and assumes that the *logic* is correct). The race itself is still there, but as per comment in the file, apparently fine and not a logic bug. > > > >> --- > >> mm/page_counter.c | 8 ++++---- > >> 1 file changed, 4 insertions(+), 4 deletions(-) > >> > >> diff --git a/mm/page_counter.c b/mm/page_counter.c > >> index de31470655f6..a17841150906 100644 > >> --- a/mm/page_counter.c > >> +++ b/mm/page_counter.c > >> @@ -82,8 +82,8 @@ void page_counter_charge(struct page_counter *counter, unsigned long nr_pages) > >> * This is indeed racy, but we can live with some > >> * inaccuracy in the watermark. > >> */ > >> - if (new > c->watermark) > >> - c->watermark = new; > >> + if (new > READ_ONCE(c->watermark)) > >> + WRITE_ONCE(c->watermark, new); > >> } > >> } > >> > >> @@ -135,8 +135,8 @@ bool page_counter_try_charge(struct page_counter *counter, > >> * Just like with failcnt, we can live with some > >> * inaccuracy in the watermark. > >> */ > >> - if (new > c->watermark) > >> - c->watermark = new; > >> + if (new > READ_ONCE(c->watermark)) > >> + WRITE_ONCE(c->watermark, new); > >> } > >> return true; > >> > >> -- > >> 2.21.0 (Apple Git-122.2) > > >