Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1275648pxk; Thu, 10 Sep 2020 11:15:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwvWe1gh7h6W5AP3rQppeDwBCwm2IemhkrQxtUqkVBXMu3pVBGgorzBKsxFSppgJcn8qA87 X-Received: by 2002:a50:9355:: with SMTP id n21mr10372736eda.237.1599761731049; Thu, 10 Sep 2020 11:15:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599761731; cv=none; d=google.com; s=arc-20160816; b=s6303kw8ZE/iDb5buUQ96L9RdIrL39T/WtPulZ9aI//0NyVWKfsSw0quhdYB7DVc8v VgDJwmky4cv0Hjm1HBSfceZQUpCs5UCJI+uU9UkW25Q+4K1RktAoiMcNpSGe8T9Icp8w Ucr9cPXlsGXDEPC4BkI9BC/SvQ+Qfsmi813lGngPccEzBM8E5KNz9Xa10tj6IAcX3/4S uNT6auzNttVx+ANuSwilc9OpEFSrKLytUUROTpUhOa3I/T48iKDrPnfyUMciBzEENQdg U09ifpSrtg1FM0qBxsrBOKjNLTrru03QOMK3eHPpEyiVfDpDbf1XdL2r1mbZdmQ6JQ2S 9l+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=vsgOBuvd+PmHZlIl6zNL7J0E+9iuQlAQQf0cZFL5gMI=; b=tKmnzZ/je2GQOtQW/lKn5tO20NkjYDngQqhLUIOXcc4vKpvqhyakYd/koBOs9kEybr /vkHZswFDR1F5c1yjQJJvr3aSz4wAW3ViYjdA91C90d2KH6QwPg2QYL461tOD4lpPXRV LcjnebcA26Wv4MUiGv3dek6pP549QHswgm9MEeRDI6ZYyloeidRnExNLPGWA0AeOdCtJ 9Ah56spdcNt/hBiZGWsLkgPWkXFOo/rc+FNped6olW6viC02zxfltpcWMZcV40fRFcM2 I8sS7u3vdTRnVJNcNQmIJ/vxYh2cLZ0exSgoOBigG6uzDXmpyhh/tYn9m6Qx2JXmBa25 pIcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b="QjySHJ/1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id da1si4411751edb.102.2020.09.10.11.15.08; Thu, 10 Sep 2020 11:15:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b="QjySHJ/1"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725913AbgIJSNu (ORCPT + 99 others); Thu, 10 Sep 2020 14:13:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726109AbgIJSNe (ORCPT ); Thu, 10 Sep 2020 14:13:34 -0400 Received: from mail-qt1-x841.google.com (mail-qt1-x841.google.com [IPv6:2607:f8b0:4864:20::841]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80F9AC061756 for ; Thu, 10 Sep 2020 11:13:21 -0700 (PDT) Received: by mail-qt1-x841.google.com with SMTP id y11so5587825qtn.9 for ; Thu, 10 Sep 2020 11:13:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=vsgOBuvd+PmHZlIl6zNL7J0E+9iuQlAQQf0cZFL5gMI=; b=QjySHJ/1iTMrz5prM8y+VncJt3fFZa9ZyFpYw/Th/jKpZaffgf1LyWVdNJm/j/vjyA nLjh8OzRb1c9mhAkanY5btD+efOqZZ+Udi6ZslYCr7YtJIfes2snd5UrIRKpUyv8hkdr 36M2e6eOhe91VaA9oyoKzmgg9xjBknxyQhh5BZGAXX5wie19agL2HJs44WBmda9L90te rB4sfQyqX1cSC/80uyvJQCn48IBDTjUyr7fuRqyi1tok7cmTDWGXQTd1b0rwTzF0s+WH TbjL9aVyKRcQ18FE4ZG1EMR1arXpN/YNqyXD5nLN+6IKf2BR7oX+4/YatX70bdzY6jl7 k0sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=vsgOBuvd+PmHZlIl6zNL7J0E+9iuQlAQQf0cZFL5gMI=; b=qi9WmDXxZZWuUFS7W5qWsfv6XLk/dlIh1KZeJeqVqV/9VJjqfqJWStCQh1jxu32t7v JDmxLnMibdarQqWf9vaJI81Iahn5R9vr1mnfwXeXMKyGSHTIQTwl8KwzBsFNk82MDc7j 7BcdE4aTxL5tz7jYbmc6qkAH91qCChB6+4r5HAOrPprC7UCSOcHfk7/AiIC7UUEPaY7y CggLIIx//PS9Bxr67YGZSdlRbUmjfwVNmmcT+uI2JUXfHjD9TOKkTx/HBMg9mraiCe/X kIoguRt074+QFX4vXAIU51fhkVMzn7F+vg7iQ0VJg+c5pzX215We3rm8rX1Up2pCz2py yWJw== X-Gm-Message-State: AOAM5320dQDjwoMIQXVHl+HaF90QivIYvsX/tki2bZJnVEZEQL/RCCKY yTpKSW02Jl3l5RiXN/egHbOgWQ== X-Received: by 2002:ac8:7108:: with SMTP id z8mr9521321qto.55.1599761600525; Thu, 10 Sep 2020 11:13:20 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-156-34-48-30.dhcp-dynamic.fibreop.ns.bellaliant.net. [156.34.48.30]) by smtp.gmail.com with ESMTPSA id c43sm8103713qtk.24.2020.09.10.11.13.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 11:13:19 -0700 (PDT) Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1kGR4R-004OkZ-38; Thu, 10 Sep 2020 15:13:19 -0300 Date: Thu, 10 Sep 2020 15:13:19 -0300 From: Jason Gunthorpe To: Linus Torvalds Cc: Alexander Gordeev , Gerald Schaefer , Dave Hansen , John Hubbard , LKML , linux-mm , linux-arch , Andrew Morton , Russell King , Mike Rapoport , Catalin Marinas , Will Deacon , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Jeff Dike , Richard Weinberger , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Arnd Bergmann , Andrey Ryabinin , linux-x86 , linux-arm , linux-power , linux-sparc , linux-um , linux-s390 , Vasily Gorbik , Heiko Carstens , Christian Borntraeger , Claudio Imbrenda Subject: Re: [RFC PATCH v2 1/3] mm/gup: fix gup_fast with dynamic page table folding Message-ID: <20200910181319.GO87483@ziepe.ca> References: <20200907180058.64880-1-gerald.schaefer@linux.ibm.com> <20200907180058.64880-2-gerald.schaefer@linux.ibm.com> <0dbc6ec8-45ea-0853-4856-2bc1e661a5a5@intel.com> <20200909142904.00b72921@thinkpad> <20200909192534.442f8984@thinkpad> <20200909180324.GI87483@ziepe.ca> <20200910093925.GB29166@oc3871087118.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 10, 2020 at 10:35:38AM -0700, Linus Torvalds wrote: > On Thu, Sep 10, 2020 at 2:40 AM Alexander Gordeev > wrote: > > > > It is only gup_fast case that exposes the issue. It hits because > > pointers to stack copies are passed to gup_pXd_range iterators, not > > pointers to real page tables itself. > > Can we possibly change fast-gup to not do the stack copies? > > I'd actually rather do something like that, than the "addr_end" thing. > As you say, none of the other page table walking code does what the > GUP code does, and I don't think it's required. As I understand it, the requirement is because fast-gup walks without the page table spinlock, or mmap_sem held so it must READ_ONCE the *pXX. It then checks that it is a valid page table pointer, then calls pXX_offset(). The arch implementation of pXX_offset() derefs again the passed pXX pointer. So it defeats the READ_ONCE and the 2nd load could observe something that is no longer a page table pointer and crash. Passing it the address of the stack value is a way to force pXX_offset() to use the READ_ONCE result which has already been tested to be a page table pointer. Other page walking code that holds the mmap_sem tends to use pmd_trans_unstable() which solves this problem by injecting a barrier. The load hidden in pte_offset() after a pmd_trans_unstable() can't be re-ordered and will only see a page table entry under the mmap_sem. However, I think that logic would have been much clearer following the GUP model of READ_ONCE vs extra reads and a hidden barrier. At least it took me a long time to work it out :( I also think there are real bugs here where places are reading *pXX multiple times without locking the page table. One was found recently in the wild in the huge tlb code IIRC. The mm/pagewalk.c has these missing READ_ONCE bugs too. So.. To change away from the stack option I think we'd have to pass the READ_ONCE value to pXX_offset() as an extra argument instead of it derefing the pointer internally. Jason