Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp4056533pxk; Tue, 22 Sep 2020 09:14:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy7kdxx+mBWlH0c+7FK6RuPGwujlyELXrrl0+Su6C8l0lFlnGjFl2NzXo2MqbI6x3vHpoXV X-Received: by 2002:a05:6402:10c6:: with SMTP id p6mr4771093edu.76.1600791261835; Tue, 22 Sep 2020 09:14:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600791261; cv=none; d=google.com; s=arc-20160816; b=sowtHz7GrIir20+dEMqetR35j7mXH0KP3wvbKuHgk+t95GW4hzf6g3TXZzqoQC4Md5 /N22vVTYhFA7M7r0wOhRv3D7z5vH02EYiBjs4r8xE/mmRap50cyRuUmGfEJ6p7uQqd+l F2ExnOv7R/Nm8VEQUWR07J6SacyMZSbpnMXoOrRYiTWoP57YNt46bOBxbhaHLrEwUFY2 +4+3FCOvksNLVb10W8aEjrHKjUvielJw8yOIn1mNqTU1kcbLovAZRu5Rbd6EeyDJPYvm RPCsrIUUI4TepotnmiWlcvt568+XCHuPzXcnfdB4qyKyX8aPkTFKZwIcEj/d51WE5jkW +CxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=5Vl0iSMcOXyao6qb+mlx5tyYrlNVljHFBTvCu/sbwcE=; b=E/JnCgNTZWZDdZSoXV+XzU6Zdmn2xqpewOMNLhp66cRJ/Oqs6O2fof97duN5bGpF9N XdKvPrQVTlIjsxLVonaxocUGezBvQKxRDECPttm3P0YbNM2sL7rvCMM3IC5r/boaD4/v idhD0eIbQdNNosqwpSHBFDhrh1/uQrdGC9yF7oReW7BbKrffH3+Zg7dhqcmRCPXrGEgZ dyGhtE3oR75SX6BcQT8u9NA//H92nr2UuXDYh17549GMTFKH+UitadQA4U+4qntQYomM Kx7KfpTzVH1FtIsFQ16sOGAssm+Y9nmg1yYEVoGUC8VDEIgvjD6qnyE9JpfgI8k+7KMs RYOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=DQmup5Ia; 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 x1si10959122edq.177.2020.09.22.09.13.56; Tue, 22 Sep 2020 09:14:21 -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=DQmup5Ia; 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 S1726632AbgIVQKu (ORCPT + 99 others); Tue, 22 Sep 2020 12:10:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726617AbgIVQKt (ORCPT ); Tue, 22 Sep 2020 12:10:49 -0400 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17BE3C061755 for ; Tue, 22 Sep 2020 09:10:49 -0700 (PDT) Received: by mail-qt1-x843.google.com with SMTP id k25so15978023qtu.4 for ; Tue, 22 Sep 2020 09:10:49 -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=5Vl0iSMcOXyao6qb+mlx5tyYrlNVljHFBTvCu/sbwcE=; b=DQmup5IacmK0M1JgFhK8SgL6Orwefv8vRSFfd4Utb6UlC/AtTAq3uWvYcoCsPXoO+0 jSHSc/lDrYcODasxoB6mCfSdgciqS9yar5ov7yS/52yEYdaSNz0VR8xVTcbROjBEFo6G zyOiOJgfn2SPh4dvpeK8JyCG4VW8l3kLXNeO8cYku4OlUbhERNC4yuqrCobVzauG4FYt tIr4cp+pW2aVx44S8cDUBH6hRcvDV/gYZ5xKTJvmZ6anyZAeB4PXvOqdljKM72xS4oWf fKDbNqCODLJq2zuD46CWwlDZt0igf5EDNTP+GE9KbGwpB+N6jMBQTLiO7ihOFLoFR7gM Sk9Q== 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=5Vl0iSMcOXyao6qb+mlx5tyYrlNVljHFBTvCu/sbwcE=; b=ISgntfcYVe+4LnU9IUso9AuGMDJmDxcF0SFZuppWdkCohYmnHe0dIzHTJ8vowA5aOe DVcDw3Yr69AR9NJuLLvqroYL1/R6ImvTa/qAdM3J5QtBdZsUZwboKc8lPnT1vh09lL6s c0JtcDqBOgup3kPSjPvwDZ23jToqYhUSeG/wfbNgAlmzNDjsaQlSccOl6pYdRTCQpex/ IwnWyT9VeYDuvxJoPuALvNtZjVMxA/8d7hGlUzu/zJC3FIxZyeYhliAvdh9A/kavTFjo GS0RNes/AeYGvXoGZ6z4tHXbXicCSe4USgsIm7PWsy8b7iC7BgHhwSMTKGv98cVITqHa pAWg== X-Gm-Message-State: AOAM531ZCKbIID7QgcX/sbT06cOq71j1t+Z9U6om1jiKuERGfpH6UZUo 8bIWxZ4S9GByV3T4zl3P5oGh2A== X-Received: by 2002:ac8:1a48:: with SMTP id q8mr5508936qtk.240.1600791048289; Tue, 22 Sep 2020 09:10:48 -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 202sm11573021qkg.56.2020.09.22.09.10.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Sep 2020 09:10:47 -0700 (PDT) Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1kKksQ-0034i7-Mt; Tue, 22 Sep 2020 13:10:46 -0300 Date: Tue, 22 Sep 2020 13:10:46 -0300 From: Jason Gunthorpe To: Peter Xu Cc: John Hubbard , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Jan Kara , Michal Hocko , Kirill Tkhai , Kirill Shutemov , Hugh Dickins , Christoph Hellwig , Andrea Arcangeli , Oleg Nesterov , Leon Romanovsky , Linus Torvalds , Jann Horn Subject: Re: [PATCH 1/5] mm: Introduce mm_struct.has_pinned Message-ID: <20200922161046.GB731578@ziepe.ca> References: <20200921211744.24758-1-peterx@redhat.com> <20200921211744.24758-2-peterx@redhat.com> <224908c1-5d0f-8e01-baa9-94ec2374971f@nvidia.com> <20200922151736.GD19098@xz-x1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200922151736.GD19098@xz-x1> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Sep 22, 2020 at 11:17:36AM -0400, Peter Xu wrote: > > But it's admittedly a cosmetic point, combined with my perennial fear that > > I'm missing something when I look at a READ_ONCE()/WRITE_ONCE() pair. :) > > Yeah but I hope I'm using it right.. :) I used READ_ONCE/WRITE_ONCE explicitly > because I think they're cheaper than atomic operations, (which will, iiuc, lock > the bus). It is worth thinking a bit about racing fork with pin_user_pages(). The desired outcome is: If fork wins the page is write protected, and pin_user_pages_fast() will COW it. If pin_user_pages_fast() wins then fork must see the READ_ONCE and the pin. As get_user_pages_fast() is lockless it looks like the ordering has to be like this: pin_user_pages_fast() fork() atomic_set(has_pinned, 1); [..] atomic_add(page->_refcount) ordered check write protect() ordered set write protect() atomic_read(page->_refcount) atomic_read(has_pinned) Such that in all the degenerate racy cases the outcome is that both sides COW, never neither. Thus I think it does have to be atomics purely from an ordering perspective, observing an increased _refcount requires that has_pinned != 0 if we are pinning. So, to make this 100% this ordering will need to be touched up too. Jason