Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3742590ybv; Mon, 10 Feb 2020 05:40:11 -0800 (PST) X-Google-Smtp-Source: APXvYqypeOggZEU4G3mRaKJIjGQWr5QYVKQNDhEpXr5FvQDReYgPhnFeefSzdfmuM4QWPn4oDlY3 X-Received: by 2002:aca:d484:: with SMTP id l126mr813378oig.114.1581342011121; Mon, 10 Feb 2020 05:40:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581342011; cv=none; d=google.com; s=arc-20160816; b=M0A1KjuHA5ou1PAR9qdt5BAVQZwPi0ryxGd91DNqivaWfq3cBDQ4AOnomdllQloB4K R8WbEcFoDzZOyLcUKMv16zm6jdWcytXb5zsCCyQ8qUqwNNE6oSe2wOuKNtl/WDUl0zQD gJhRUKfQ5rF7qiIcPUV9LN3Yh/g7ITd3nbclTbZRHwu1rwk7qiL9cfX+Z1kezCVZPtta GO15jXx4Pt7BIO6uXCd3vRc2gYalefeZWnMMJRWGhlqZc3B3aZvZl7xq7GFL09pBykmK mL8LIMHizl9KIboQlB7tFUdcD6TURf/88qbFDayTm2frcqJiDIJDHT7ufmtpXFGzIk8c x6xg== 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:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=n4VkksM4yOFdYfQnXs70fdXcOaBAA6a7KVukuRv0B/0=; b=WN/1hNeqg5ctDFlBg3OYkcO7d0QvNTKP+6O3Eylo/OmbUL8JM5ehe4POowi/dKb6uG EwzGqNJcr89tcqrNYOth2JFkTr03Hv3JWlYin3MyNnsIFHFD8AY/px7bRp6SH2jyAZEj a3G4IA9POk3aj4wk/X5r5YB0V0JVO0ZFVJ/Le6BwTLyXGTg0Hb9xOrInzOFHvOgWWUAG WsXvvv1awpVDng/dQzDzwOCtFu7Dg+ImJFwyzwTOu6I5qWN88BRy8+1vQqJgEAPBXLCl XcaAfEI93fHeLbCwMIB/TqCkIDchmmWgRwJUdfoD7ggtGRB3D0YFqjTn20xoOt6iSoSf QZ+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=k+lzJ4Ld; 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 f17si178488otq.96.2020.02.10.05.39.59; Mon, 10 Feb 2020 05:40:11 -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=k+lzJ4Ld; 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 S1727754AbgBJNik (ORCPT + 99 others); Mon, 10 Feb 2020 08:38:40 -0500 Received: from mail-oi1-f194.google.com ([209.85.167.194]:43579 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727347AbgBJNik (ORCPT ); Mon, 10 Feb 2020 08:38:40 -0500 Received: by mail-oi1-f194.google.com with SMTP id p125so9164462oif.10 for ; Mon, 10 Feb 2020 05:38:39 -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:content-transfer-encoding; bh=n4VkksM4yOFdYfQnXs70fdXcOaBAA6a7KVukuRv0B/0=; b=k+lzJ4LdVh/1CbBpkXb4FWY8y+vVjD7ltGBGbPuFD0uyLFRLCgr/86TNCv3ZuIO5lc iZAhHWmfya+7eTMixVFHqktD33bGbumFIeGqGriRe8MJdzMkrzO3wrfYvo0c6novP3w1 MDf//CvojUaFeIuzJCuWfCOD+OBT9yUirBAEc35ypZ7soOcXMip1sh0I3w1TuTeaVqYw tkqXQSdt6ZAvkY1IfvcwxsqLi0bGerDdyF+WjzpcxhLeGSj1oH6P6MYtVV/4kWMWXfj6 sxccmVhTQ0Zwi9obne74Az6QxE9/MC53fssklkMXyCJhYmKzLFbvxsROdwd2VjNYWjkj MGOw== 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:content-transfer-encoding; bh=n4VkksM4yOFdYfQnXs70fdXcOaBAA6a7KVukuRv0B/0=; b=Vblq41TojGwYCsQFWkrkaLPoFezKS2ywL4dZTvQFtC2RMR9AbtsygPy88uxFxmBOaC /G/vjEakSIWpz/dIv5k0mu6JV9NMWppClW9tDvuzt+wjLhbXD/ZQVa/LX6qsK0mW0SGU U/svI/SXEpVEyueyAgWcvXsW8B+6RejrFAfpe9B50C6XSWu/xoE4Av7zvP8A4KzzvSxN +0FHI4zx34cV+kvRjBLEZ/jPFXX9ZTi/7NxBOWq2RWewd6sJr1qxXLKIhUJQkijE+xjc nd7B7u+RuIl8Os/j1n/cwApniJGOHO2t3AaSNiKDfTdsnz5evoBaeoHLXzknYScN4gXh gxZA== X-Gm-Message-State: APjAAAUXgfKJNK561PNGgv0vARZrWcdgQEs/tlYjFEDkyQeW/tG+47LO WaboJd48G7ncIjW6DeerZiLBBmDK+IDBn0mCZrnEvQ== X-Received: by 2002:aca:2112:: with SMTP id 18mr788817oiz.155.1581341918727; Mon, 10 Feb 2020 05:38:38 -0800 (PST) MIME-Version: 1.0 References: <26B88005-28E6-4A09-B3A7-DC982DABE679@lca.pw> <1581341769.7365.25.camel@lca.pw> In-Reply-To: <1581341769.7365.25.camel@lca.pw> From: Marco Elver Date: Mon, 10 Feb 2020 14:38:27 +0100 Message-ID: Subject: Re: [PATCH] mm: fix a data race in put_page() To: Qian Cai Cc: John Hubbard , Jan Kara , David Hildenbrand , Andrew Morton , ira.weiny@intel.com, Dan Williams , Linux Memory Management List , Linux Kernel Mailing List , "Paul E. McKenney" , kasan-dev Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 10 Feb 2020 at 14:36, Qian Cai wrote: > > On Mon, 2020-02-10 at 13:58 +0100, Marco Elver wrote: > > On Mon, 10 Feb 2020 at 13:16, Qian Cai wrote: > > > > > > > > > > > > > On Feb 10, 2020, at 2:48 AM, Marco Elver wrote: > > > > > > > > Here is an alternative: > > > > > > > > Let's say KCSAN gives you this: > > > > /* ... Assert that the bits set in mask are not written > > > > concurrently; they may still be read concurrently. > > > > The access that immediately follows is assumed to access those > > > > bits and safe w.r.t. data races. > > > > > > > > For example, this may be used when certain bits of @flags may > > > > only be modified when holding the appropriate lock, > > > > but other bits may still be modified locklessly. > > > > ... > > > > */ > > > > #define ASSERT_EXCLUSIVE_BITS(flags, mask) .... > > > > > > > > Then we can write page_zonenum as follows: > > > > > > > > static inline enum zone_type page_zonenum(const struct page *page) > > > > { > > > > + ASSERT_EXCLUSIVE_BITS(page->flags, ZONES_MASK << ZONES_PGSH= IFT); > > > > return (page->flags >> ZONES_PGSHIFT) & ZONES_MASK; > > > > } > > > > > > > > This will accomplish the following: > > > > 1. The current code is not touched, and we do not have to verify th= at > > > > the change is correct without KCSAN. > > > > 2. We're not introducing a bunch of special macros to read bits in = various ways. > > > > 3. KCSAN will assume that the access is safe, and no data race repo= rt > > > > is generated. > > > > 4. If somebody modifies ZONES bits concurrently, KCSAN will tell yo= u > > > > about the race. > > > > 5. We're documenting the code. > > > > > > > > Anything I missed? > > > > > > I don=E2=80=99t know. Having to write the same line twice does not fe= el me any better than data_race() with commenting occasionally. > > > > Point 4 above: While data_race() will ignore cause KCSAN to not report > > the data race, now you might be missing a real bug: if somebody > > concurrently modifies the bits accessed, you want to know about it! > > Either way, it's up to you to add the ASSERT_EXCLUSIVE_BITS, but just > > remember that if you decide to silence it with data_race(), you need > > to be sure there are no concurrent writers to those bits. > > Right, in this case, there is no concurrent writers to those bits, so I'l= l add a > comment should be sufficient. However, I'll keep ASSERT_EXCLUSIVE_BITS() = in mind > for other places. Right now there are no concurrent writers to those bits. But somebody might introduce a bug that will write them, even though they shouldn't have. With ASSERT_EXCLUSIVE_BITS() you can catch that. Once I have the patches for this out, I would consider adding it here for this reason. > > > > There is no way to automatically infer all over the kernel which bits > > we care about, and the most reliable is to be explicit about it. I > > don't see a problem with it per se. > > > > Thanks, > > -- Marco