Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3755756ybv; Mon, 10 Feb 2020 05:56:31 -0800 (PST) X-Google-Smtp-Source: APXvYqwC8osChjxttCWb5Qn1mBPvc8RNuM0pawdkyLNF/pD5YkCgHvyez3bcGjN9XHvboytrnKl8 X-Received: by 2002:a05:6808:50:: with SMTP id v16mr813237oic.133.1581342991501; Mon, 10 Feb 2020 05:56:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581342991; cv=none; d=google.com; s=arc-20160816; b=HIZmDs8FIpade+xbSLbSx+bWR0mu2YgB5CFHfMstTwliVx8x484d+zc5/D+tHmKH2Y gpyjmkGHoL+t7RyWrZOUleLaJ7IPwa7v4/7zdcqt0tuV6gELldIhlcM67i6ENmP1wz11 Jg3Sk5RQNu3rAOl/MOJpdVAkaqclzXseR4PCpXZLpJBj5KSMl1a+QBoOspW9OhPz05T2 TnIwAe5EQMyco2jY7FC7yJvHLRKJFeTfAJ/PbcXtxjT4Pq4IiXgWkxuxBT3z5QEGPiPA bD7u227Ekc0o0oYsCiy32Ip3Nk+X3DPtp0yfIOlHaSeCrbuhsSSqR/mEE99ampwQ/g4O Xh0w== 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:mime-version :references:in-reply-to:date:cc:to:from:subject:message-id :dkim-signature; bh=uvc+HCAjGfX6aDuiEDkX1stZqSNz9kTlsYc/8zRaw1M=; b=Rea2/p1K7UlRFfZNrCOvmjxGXYE3A3r4eMchFZkOVgqhPeRuCIjcWK4LDCt3zj1gkl RQpPYkhoKroAbaOhoKy6AXT3Hg6QD7rbZwaKVR01SCxT+UPzK+1tV/h86dzb1liGZA/s 3WItNXIO+sxzQYSDrygu3Pey5UI4PKHRJtGQlu7txG65fnh0EOF+/90njRmB587nZW6i KHEdUyfYh21WMau99bG3CBp/VFAoe1Uw2S8+PZHmvdbGNsstPbd1gFqVwPUOYn5DCwCy bUV9ThEzoENUEiSP1QYEq0Q/TVRrIRce0TQiTasHOjmUItJyv3FqEzQoZJKn3hOOpwTN IVMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=StiymXhX; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i10si220382otk.195.2020.02.10.05.56.18; Mon, 10 Feb 2020 05:56:31 -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=@lca.pw header.s=google header.b=StiymXhX; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728877AbgBJNz6 (ORCPT + 99 others); Mon, 10 Feb 2020 08:55:58 -0500 Received: from mail-qt1-f193.google.com ([209.85.160.193]:40590 "EHLO mail-qt1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728254AbgBJNz6 (ORCPT ); Mon, 10 Feb 2020 08:55:58 -0500 Received: by mail-qt1-f193.google.com with SMTP id v25so5135155qto.7 for ; Mon, 10 Feb 2020 05:55:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=message-id:subject:from:to:cc:date:in-reply-to:references :mime-version:content-transfer-encoding; bh=uvc+HCAjGfX6aDuiEDkX1stZqSNz9kTlsYc/8zRaw1M=; b=StiymXhXU8O8XtYolLxmQCpaPboJPGmag5L97rZ1HtVEAypI2ojif8OJMfrxHNxGgP f9TeY01de6tYRd/pz9atN9bfseymq13x1E+bI6DyQJ8gGLeDqjDnoSbHF5oUvs8IKmWf uIp8m0FiL6gKKn/bQ62WMwVcur18Vk8JTz4sSl91hVOjU7uKqukzQAp2iRVZ3t6rT0Sl I+b5cPeX2vr0DNr6ik7kYoZsPIbpl2RPSho8osX5lOzKvqSL3KensYUwzX/C24EYOYRh VphGzULJ6PyiOElvDG8i+swpGjQcYJ62V0iY96ZpW34GL6gmc65lxFhaScgdb7nrWt2/ wmsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:mime-version:content-transfer-encoding; bh=uvc+HCAjGfX6aDuiEDkX1stZqSNz9kTlsYc/8zRaw1M=; b=cwWS+4EczXERCGngzemmEKW3VELZw4fMMOhiJ+qJpq4S+qmd7CoiIomPcbGKsZzuRF Y97LsFFdz5p3KFGaAH3088lZzj6uFjWacpnunGlgvC2bpjpw9pZ1gpxFzGddgIlhXJaX 8wSmI7wMAqv/C+9gpVjt6dzXA1Xyne34sH5kRk0kAMA+tLvLJtQ84aFML1SwjT5odZHC nzMcxpgzlFapvKwMuPKL3EWKbRw1Qx2Ln3qwcOHpQ7lfZPuZduFo6EIlU6y6RugQY25N 74dMZ/RYp1TfBNDMlOAqx4lY6JoiQXm64v9trjnY+Fp8K5hSuVMNB877esVYfZ0uLNeG eCTA== X-Gm-Message-State: APjAAAUn6zxzNtdUU1uOPnxu/K3SDsL+xUnsoOgB2iGA7ZfITrVt5sJ5 NmvpNoAOOdICWqt3mgDSZY5zWw== X-Received: by 2002:aed:3eee:: with SMTP id o43mr10143047qtf.33.1581342956768; Mon, 10 Feb 2020 05:55:56 -0800 (PST) Received: from dhcp-41-57.bos.redhat.com (nat-pool-bos-t.redhat.com. [66.187.233.206]) by smtp.gmail.com with ESMTPSA id a24sm157600qkl.82.2020.02.10.05.55.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Feb 2020 05:55:56 -0800 (PST) Message-ID: <1581342954.7365.27.camel@lca.pw> Subject: Re: [PATCH] mm: fix a data race in put_page() From: Qian Cai To: Marco Elver 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 Date: Mon, 10 Feb 2020 08:55:54 -0500 In-Reply-To: References: <26B88005-28E6-4A09-B3A7-DC982DABE679@lca.pw> <1581341769.7365.25.camel@lca.pw> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.22.6 (3.22.6-10.el7) Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 2020-02-10 at 14:38 +0100, Marco Elver wrote: > 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_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 various 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’t know. Having to write the same line twice does not feel 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'll 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. Surely, we could add many of those to catch theoretical issues. I can think of more like ASSERT_HARMLESS_COUNTERS() because the worry about one day someone might change the code to use counters from printing out information to making important MM heuristic decisions. Then, we might end up with those too many macros situation again. The list goes on, ASSERT_COMPARE_ZERO_NOLOOP(), ASSERT_SINGLE_BIT() etc. On the other hand, maybe to take a more pragmatic approach that if there are strong evidences that developers could easily make mistakes in a certain place, then we could add a new macro, so the next time Joe developer wants to a new macro, he/she has to provide the same strong justifications? > > > > > > > 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