Received: by 10.213.65.68 with SMTP id h4csp107058imn; Thu, 5 Apr 2018 18:58:58 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+53kYM8koemtwv/P9Mple5lTTxIqfHu1clp2C4Jv274IZLb2b1fXnZZ70rOLVW+pVz/SM2 X-Received: by 2002:a17:902:8282:: with SMTP id y2-v6mr24900931pln.7.1522979938007; Thu, 05 Apr 2018 18:58:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522979937; cv=none; d=google.com; s=arc-20160816; b=FcLdWlh+zcRpDggjKpFtrZIKXPbpxcogk3k9pcQ6zqPElcxt1yalNcfkqUL8QfVgP+ hlMChrsKbCfLesrKCEEMqdDDIJoHF1DGWvHJl3Ypr8Y9elmsCbzHTCUD6ExMuEntcn2v jxyuaZjX8LqnSwalfCdrlqB3LaRUTdiV+mlMMOIlvB2BS6KdugVhBhSk+FQS5kuyBL3p dWroIGjyso7dDBIb/IYESVFUgIdSrdnqhomRcYlw+oVArqJjpxOZvOWgly/ix3NA5F5m bWl+J2zNyzF96wbkMxWjD7b3SLNvvAwwlFKYHs5aM3UmPvHcW+k476n3KlVV+zD2lru7 geDw== 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 :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=T0z8Ty+6wq2a550bpGrf0/SRbivVNu4SvMDQrulEGf8=; b=05qkR0B0ytmxULoeVfKjH3dO/BffJhE8YhOmyPBolzwx04q70R28o4gxWRIlTIw01F Aqk6M0NtwwL3QxcOSgNuHtB6i5WWhM7RLqjz8pBdO7hDMMNYHKJse/MjEmPBskB3oeH3 p4HgZJcbPWo6dK5ahcJ6e4E7ss6Kx/NJVBaSJpI/ckNO5wb630yWGhOs/+wGFla2pU5P 5RYBo2v63qEV/rEKcN6JeLan/pi6jOIZxCZMGugdhqxYhz7/8xZNOu+34+hvJLHFdqXB M/p7C9Dx2+aZOA2KIASEPxwkg5goWf2oMEMtfEOVuggMmJoDFbP5aGwovt4VhXRb+AsE VtzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=iUabWOiH; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 14-v6si8509779ple.450.2018.04.05.18.58.43; Thu, 05 Apr 2018 18:58:57 -0700 (PDT) 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=@gmail.com header.s=20161025 header.b=iUabWOiH; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751362AbeDFB5j (ORCPT + 99 others); Thu, 5 Apr 2018 21:57:39 -0400 Received: from mail-ua0-f173.google.com ([209.85.217.173]:44106 "EHLO mail-ua0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751275AbeDFB5i (ORCPT ); Thu, 5 Apr 2018 21:57:38 -0400 Received: by mail-ua0-f173.google.com with SMTP id r16so4121287uak.11 for ; Thu, 05 Apr 2018 18:57:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=T0z8Ty+6wq2a550bpGrf0/SRbivVNu4SvMDQrulEGf8=; b=iUabWOiHxd2SrD0q0ZLliFXf8spzuJps1GY15UEK9U6Lp+IQnn4Qe6jmfqkohNWWnU +XgprWx5FeYgMEx2B2w5/k7DPMt9cWbtxGOEdTrZIW3ydZFoGgKXgJwAw85ih4684DhE 8/w8INHv2zRlsUFyS1O+kQppnkGVz2XNuJDlqhJgcaDfS1XT3ErBY8giKaiMRn/LFouz UuJPECc1jE8HnDgvGU/un5CZOxIWHPeH+N5MninrJAHM/Nzl6xU/XOl5Xru7r5HZUySR KWh6T2VA9FZ4kLcD+lc1Wimu50ItpTgnFCSnHqRpxt040+CXqMgrcVW+QF/MCM+uLngy RRXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=T0z8Ty+6wq2a550bpGrf0/SRbivVNu4SvMDQrulEGf8=; b=A5/Rwz2N//B1K+wOfkaui1friV8zaLtd9puUAvNAT0OnpkrlxGAxxaFW60Em8CA+3V uKjOrP1jXy6B0Iq6l/vMZ0S4EmD8NAo1S+A7jIAI5YrhRn/qy0kYDQ+PLJoDPoELBFV7 OGqDQiVLc4glgyhY5fCCTd9ZmLG3CD12gmTpTpBvurMLdNlHRQ0cwfXHkLxWZ2cbGW+A NzS1JltWmvg+I+6H30cwr+WdWWWlMxMnvdhjITCLcR9fMWrp3JS1/TTmb46QYNcDCOtv cw6owLbs4u5ploqVLCRCnhffy58yQOcteACB1LLnNO5IBcfNMzgC6gK/n5M1tmNw2hLw Kkaw== X-Gm-Message-State: ALQs6tACXsU8MyzosDB1cO93caE5uBzpSNeEWaQd2sO9374LSo+4b6U5 Yr1kbDDTvYKuqTbVBm/GDy9dZBbswq7dplueMY/TiSX8 X-Received: by 10.176.96.19 with SMTP id j19mr4059561ual.179.1522979857682; Thu, 05 Apr 2018 18:57:37 -0700 (PDT) MIME-Version: 1.0 Received: by 10.159.61.98 with HTTP; Thu, 5 Apr 2018 18:57:37 -0700 (PDT) In-Reply-To: <65E6BD75-FBA6-43AC-AC5A-B952DE409BC8@cs.rutgers.edu> References: <20180404032257.11422-1-ying.huang@intel.com> <65E6BD75-FBA6-43AC-AC5A-B952DE409BC8@cs.rutgers.edu> From: huang ying Date: Fri, 6 Apr 2018 09:57:37 +0800 Message-ID: Subject: Re: [PATCH -mm] mm, gup: prevent pmd checking race in follow_pmd_mask() To: Zi Yan Cc: "Huang, Ying" , Andrew Morton , linux-mm@kvack.org, LKML , Al Viro , "Aneesh Kumar K.V" , Dan Williams , "Kirill A. Shutemov" 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, Apr 4, 2018 at 11:02 PM, Zi Yan wrote: > On 3 Apr 2018, at 23:22, Huang, Ying wrote: > >> From: Huang Ying >> >> mmap_sem will be read locked when calling follow_pmd_mask(). But this >> cannot prevent PMD from being changed for all cases when PTL is >> unlocked, for example, from pmd_trans_huge() to pmd_none() via >> MADV_DONTNEED. So it is possible for the pmd_present() check in >> follow_pmd_mask() encounter a none PMD. This may cause incorrect >> VM_BUG_ON() or infinite loop. Fixed this via reading PMD entry again >> but only once and checking the local variable and pmd_none() in the >> retry loop. >> >> As Kirill pointed out, with PTL unlocked, the *pmd may be changed >> under us, so read it directly again and again may incur weird bugs. >> So although using *pmd directly other than pmd_present() checking may >> be safe, it is still better to replace them to read *pmd once and >> check the local variable for multiple times. > > I see you point there. The patch wants to provide a consistent value > for all race checks. Specifically, this patch is trying to avoid the inconsistent > reads of *pmd for if-statements, which causes problem when both if-condition reads *pmd and > the statements inside "if" reads *pmd again and two reads can give different values. > Am I right about this? Yes. > If yes, the problem can be solved by something like: > > if (!pmd_present(tmpval = *pmd)) { > check tmpval instead of *pmd; > } > > Right? I think this isn't enough yet. we need tmpval = READ_ONCE(*pmd); To prevent compiler to generate code to read *pmd again and again. Please check the comments of pmd_none_or_trans_huge_or_clear_bad() about barrier. Best Regards, Huang, Ying > I just wonder if we need some general code for all race checks. > > Thanks. > > -- > Best Regards > Yan Zi