From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.4 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D837C352A4 for ; Mon, 10 Feb 2020 20:45:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id ADEC7214DB for ; Mon, 10 Feb 2020 20:45:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="R/fyo5zV" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org ADEC7214DB Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 391D76B000E; Mon, 10 Feb 2020 15:45:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 31B9E6B0032; Mon, 10 Feb 2020 15:45:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E1FA6B0037; Mon, 10 Feb 2020 15:45:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0211.hostedemail.com [216.40.44.211]) by kanga.kvack.org (Postfix) with ESMTP id 0366E6B000E for ; Mon, 10 Feb 2020 15:45:10 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 97720181AEF07 for ; Mon, 10 Feb 2020 20:45:10 +0000 (UTC) X-FDA: 76475397180.21.shape38_4a3e3d558c617 X-HE-Tag: shape38_4a3e3d558c617 X-Filterd-Recvd-Size: 4975 Received: from mail-ot1-f67.google.com (mail-ot1-f67.google.com [209.85.210.67]) by imf05.hostedemail.com (Postfix) with ESMTP for ; Mon, 10 Feb 2020 20:45:10 +0000 (UTC) Received: by mail-ot1-f67.google.com with SMTP id h9so7779617otj.11 for ; Mon, 10 Feb 2020 12:45:10 -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=LfSzqarGyzjK5g9zOwnGoHsxJ7bhaam0WuaTghWmBsA=; b=R/fyo5zVfADZJo7Tpjb53UIIzvJfGoUB7gqXf4ge2dEpoAr00vAxg/cyCQto0McPQg BB8PlP8GEEWafyvg6+WW/zlF61EgAnWKiNAvF5LnMMEpiwTfOoDQlljP836tExcPaeVC VD+Hr8l2gbCc66iuQmK2FhlJ4KUSeKFBfseMg7rgO3d7ViGoJrshrue4PD0YleIpV651 bzkn+ZvFBPYWctmQqOFrqi5tu7uzY29cXz5jrrGXlS1I0rT4dKaB9nuLt7Zcm2zQ/UGJ Vs+GbjT7i8M7BSTubXeGEQ9Ze/zr57PexYSVxz0kic/riC1dqfDi1tfLnzvwx0tRg6JQ zTpw== 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=LfSzqarGyzjK5g9zOwnGoHsxJ7bhaam0WuaTghWmBsA=; b=e5J7hXIYK7LN6bGEPOhN6lcgpOVuUQXE6C0fO6bQ153GteMAbQVaSPMn1hgee2Rz00 pjCNNsF6BpxtwhRlW8Dpr7MBq3RhzQUBJ0BUO9Id/cHh8cVr/FIpyp7dJhWdSbPe5GEb 8X9+bl8cjoMFuUKRttXHDiVttbl21hK9CThUpfvgHjCQy56+QcljER97agkPk7I1hYim fQmo0C7pq6yRVJen+FNYc2F4PZXZR2/bKgM3aO/vuWzAGcF5SNUnwXSHxuFI1rryZ7yo KBBh45vYjr0ofhLs9m4W+7uCUZnwFiNwu9YDxD/Pd3uzlHp3eviTv7mRSFXiGjz3rgJA ti7A== X-Gm-Message-State: APjAAAUsrr+lZ9KOgZ6XCHiwq61fllCC0lpz5LmDhMgpUGidhnwz5zu9 lwLjkLLVuVpCZWrl/t9my8l7/0LtEOM7gl2XLOcMkw== X-Google-Smtp-Source: APXvYqwBjO/aq81r8LsHvtE2wRaMmsPC5G4CPsZNmX+aOkG4bL6R8zoX0XKD50lPzkBLgCL+Sve6rCWVwdOQuCyA8vw= X-Received: by 2002:a9d:7f12:: with SMTP id j18mr2628214otq.17.1581367509151; Mon, 10 Feb 2020 12:45:09 -0800 (PST) MIME-Version: 1.0 References: <1581354029-20154-1-git-send-email-cai@lca.pw> <20200210172511.GL8731@bombadil.infradead.org> <1581362448.7365.38.camel@lca.pw> <20200210192155.GM8731@bombadil.infradead.org> <1581366529.7365.49.camel@lca.pw> In-Reply-To: <1581366529.7365.49.camel@lca.pw> From: Marco Elver Date: Mon, 10 Feb 2020 21:44:58 +0100 Message-ID: Subject: Re: [PATCH -next] mm/filemap: fix a data race in filemap_fault() To: Qian Cai Cc: Matthew Wilcox , Andrew Morton , Linux Memory Management List , LKML Content-Type: text/plain; charset="UTF-8" X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, 10 Feb 2020 at 21:28, Qian Cai wrote: > > On Mon, 2020-02-10 at 11:21 -0800, Matthew Wilcox wrote: > > On Mon, Feb 10, 2020 at 02:20:48PM -0500, Qian Cai wrote: > > > On Mon, 2020-02-10 at 09:25 -0800, Matthew Wilcox wrote: > > > > On Mon, Feb 10, 2020 at 12:00:29PM -0500, Qian Cai wrote: > > > > > @@ -2622,7 +2622,7 @@ void filemap_map_pages(struct vm_fault *vmf, > > > > > if (page->index >= max_idx) > > > > > goto unlock; > > > > > > > > > > - if (file->f_ra.mmap_miss > 0) > > > > > + if (data_race(file->f_ra.mmap_miss > 0)) > > > > > file->f_ra.mmap_miss--; > > > > > > > > How is this safe? Two threads can each see 1, and then both decrement the > > > > in-memory copy, causing it to end up at -1. > > > > > > Well, I meant to say it is safe from *data* races rather than all other races, > > > but it is a good catch for the underflow cases and makes some sense to fix them > > > together (so we don't need to touch the same lines over and over again). > > > > My point is that this is a legitimate warning from the sanitiser. > > The point of your patches should not be to remove all the warnings! > > The KCSAN will assume the write is "atomic" if it is aligned and within word- > size which is the case for "ra->mmap_miss", so I somehow skip auditing the > locking around the concurrent writers, but I got your point. Next time, I'll > spend a bit more time looking. Note: the fact that we assume writes aligned up to word-size are atomic is based on current preferences we were told about. Just because the tool won't complain right now (although a simple config switch will make it complain again), we don't want to forget the writes entirely. If it is a simple write, do the WRITE_ONCE if it makes sense. I, for one, still can't prove if all compilers won't screw up a write due to an omitted WRITE_ONCE somehow. [Yes, for more complex ops like 'var++', turning them into 'WRITE_ONCE(var, var + 1)' isn't as readable, so these are a bit tricky until we get primitives to properly deal with them.]