Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3700394ybv; Mon, 10 Feb 2020 04:58:39 -0800 (PST) X-Google-Smtp-Source: APXvYqwjmDv2gXjxxzgFkBXyMmc2Z4Wxq+q/M5tMbfMZw1uie966nysaNZedtKA7459Z3+tdjTP0 X-Received: by 2002:a54:448b:: with SMTP id v11mr710142oiv.74.1581339518947; Mon, 10 Feb 2020 04:58:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581339518; cv=none; d=google.com; s=arc-20160816; b=KoCs/W5maaD7gIsWNl81REBIMdnjwdleHTtm4kPKELdzP+hrUQO/+e0ha7V1PxNR9Z DL2Y3VzhAtZKsDjuzH3T2iXhemWtYkiw6ujAS4K2+vkoGGc82rBdu9Duk4kx4qlEgQEh SXr3+ENPsGu6N3tzxtKdv95NOhSd/1jNL6WObOGuBbMZKGoxhG+dqG6wuM83uqbbGd4y KzAA9cwgPknppeeh8wFuKLKACdUcVRUmKvE7brfvWylcm0f96zBLMxOqRYlgXGdUhcuQ b6xjjxiG7c02ARIfVs1q6nyq3xka76ROnKH4p07dcu30btegbVJ2m2yNYavddZppJz7k RRhg== 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=zh7WaU7ewGekRG7I1HMZ6lekH2DZziRDqSPnvPUVHsM=; b=WThoU7p9+Jp9Dw2A4gqBTBE3fRi6bOmYs37bJNc8q/qrujvUr3VfxDeBtSKsTsJyVW wJ1k5sFjORTxrwuGBVcWIt/SuO82l+tIVafqHmv2I6jM0tYyz9JD9Gcv1kNIpaq2vj4M Osgc26skIedGLWUx/VmYvR/jP0C5AHywerI14qHTkQFuwcASh8CtN7PbhPIkK+veAwYU 5/VOZrRTgUllfe1V/Qu8j5Jzpyv378UJSUXZRWBajAloW8QAQBxJ9pSuPeTyTdnQf2BP oNo8hB2GzTzGZ9dR52n5fZRKAVylbI66z9Mb2K/NUtll0376EpvPft6J/06pmcnZGSkK wDFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=aB2mqO0I; 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 c196si133742oib.36.2020.02.10.04.58.27; Mon, 10 Feb 2020 04:58:38 -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=aB2mqO0I; 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 S1730013AbgBJM6S (ORCPT + 99 others); Mon, 10 Feb 2020 07:58:18 -0500 Received: from mail-oi1-f194.google.com ([209.85.167.194]:43424 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729969AbgBJM6P (ORCPT ); Mon, 10 Feb 2020 07:58:15 -0500 Received: by mail-oi1-f194.google.com with SMTP id p125so9050251oif.10 for ; Mon, 10 Feb 2020 04:58:14 -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=zh7WaU7ewGekRG7I1HMZ6lekH2DZziRDqSPnvPUVHsM=; b=aB2mqO0I1BE2YVL9HktuWHitoKBpyzRZp78dTOhF8GymM4iLL/UTUr12XfLXer7+2Q tJBqlfHLPj4M8LChNh8/vNK2luTKqHWGkEHCBkyQg8aQD5o6SADs1g9OBN/dowF3Illb eB9WOCXOto4ZifIPH9uojL6ipxlAdUogy56n9NMOb6GwM7gCdasNUNswGwkxiMh7i8LV 7JmEyLwoy7olS9Ue2hSHhkVwWSEmkaH/OTBwbBr0bBIQpwr5IpPz4tEFcGL9bfSIgGwN Al9t+58bAsnLAuigp+kJtMYgS46qixPPoI3ejMsWNjuEYCiLgRp50wwHWxJLU/qRqE/i 7ZSQ== 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=zh7WaU7ewGekRG7I1HMZ6lekH2DZziRDqSPnvPUVHsM=; b=tJ6z+jMuj17N72V2zPO/dXztw5Cu1d/Z3TUxXoLVzxuKwvRPz6ixASfuW5fjZtfynJ 0aKWnThMshLPa04He8f/sV6s+zjtUNZSmiZ5MAfGhyasNXo2BIVlK0+b+PQpceZ5xDbv czqU9wJzm47ZaPQdRmIWidNLAQrQrhwKZd9c92JnCfbhLTGSjVLRScHTHRmyacla4L+J e/bmgye/5ltHvvLAYIu/H0WMu+JhWLEIBQlf6SWhsQkUP5OB+uq4Jncr5hTJI2kJWMhc xLImWIVxgcggKyoCtDn7tEm01l9uc72OIDxg0us/cREKZDjSvFwwRTbVAe4ckVwxofc1 BjfQ== X-Gm-Message-State: APjAAAW4PzE87R6DFuW4vpo96xLatSBpYwiDdsU0ap6lQ5tuYp1XvAAc 1Wo8y0mYlGJVeqv3qgrXii2FPWHf77FyjjzXg9nYlg== X-Received: by 2002:aca:2112:: with SMTP id 18mr684589oiz.155.1581339494032; Mon, 10 Feb 2020 04:58:14 -0800 (PST) MIME-Version: 1.0 References: <26B88005-28E6-4A09-B3A7-DC982DABE679@lca.pw> In-Reply-To: <26B88005-28E6-4A09-B3A7-DC982DABE679@lca.pw> From: Marco Elver Date: Mon, 10 Feb 2020 13:58:02 +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 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_PGSHIFT)= ; > > 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 that > > the change is correct without KCSAN. > > 2. We're not introducing a bunch of special macros to read bits in vari= ous ways. > > 3. KCSAN will assume that the access is safe, and no data race report > > is generated. > > 4. If somebody modifies ZONES bits concurrently, KCSAN will tell you > > 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 feel m= e 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. 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