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=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=unavailable 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 2DB04C433ED for ; Wed, 28 Apr 2021 16:00:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EAF90613F0 for ; Wed, 28 Apr 2021 16:00:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240699AbhD1QBV (ORCPT ); Wed, 28 Apr 2021 12:01:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240634AbhD1QBS (ORCPT ); Wed, 28 Apr 2021 12:01:18 -0400 Received: from mail-qt1-x830.google.com (mail-qt1-x830.google.com [IPv6:2607:f8b0:4864:20::830]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5360C06138A for ; Wed, 28 Apr 2021 09:00:32 -0700 (PDT) Received: by mail-qt1-x830.google.com with SMTP id o21so18908504qtp.7 for ; Wed, 28 Apr 2021 09:00:32 -0700 (PDT) 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=2SuF9mSmF53oX7jF/kwRpkLK22JzuM8F0Omk1lKrm3U=; b=c91OhnqmUMdrp5NwBmhDw0TWIKfd7cy5vfjKAMC5+iO+8WsXa/1iWeYeOn3iiAdnee K687+nQwR9YSd0rPz34NBcYSpywzjMxeDXC53/Pb4AEGt7HZEY5Icnr0ahoHBsonGrbg HXyuj//ZVjv22KpBoGWMZ2LFIIXIny1zBaq8Ckc7R2iE49O+5QqGx11lTuePdNiVNZQe kuJ5UJThxkM79QHzgt2Li5You3M+/fDQ8yIHwND0dIhYaufGJNCSGtpVdCHwu6nPhiZQ W8SjrEQQJNQEUo944EZmAwLRIc9DO2ZQpaS+Vg8i61WI8H2F5Vmk/5l5GjuPT/zghXGr DYyg== 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=2SuF9mSmF53oX7jF/kwRpkLK22JzuM8F0Omk1lKrm3U=; b=MXIaVmP53QHIiNlHP/28Z9xzM3s5MNWI39ajGUTG2qNQkwTifLmuXn5Jdp/wdipb+K oW9oNeD0C2FxfHbrS25lXZJ+EWDfiIjztxnnmT7XIgVxRio+M+X/XL5SlRrqy0J4I+Xl 67+0WOC+FnAdeoNtxnujtHvnGNsaJamwtRjbCsn5bCqFqjjlPSqFLkEw9j6rtZ+2oAXR c91CxUrLINmSgjoNiVEzx9DJtHZsZOg3ddf0R1NVTT8l7GBl5K624+xCZn79peQhbNLM VYi+U2iWtif+wQved13ybbHejowxlEni/LdngG9aRHye75Vcu45zVypxWlwtDpueL+g4 LYrw== X-Gm-Message-State: AOAM533fxJ1rp08yCVfea7EngusIyZx8DR5REQNID28HvMJnBy7qsf9W ldU4DsIy8gIuijsblaFikbZ6e7dQ48yoMJ/JPB6SEg== X-Google-Smtp-Source: ABdhPJxw/1TATKg60w/SjCaCBhfHoT8ss0Bn6ygLRSzh2SsIWTbTBD0bqD3Zs2Gx7AOSnVSgCP23TbUXpGj11qrDJVs= X-Received: by 2002:ac8:51d3:: with SMTP id d19mr26641416qtn.358.1619625629931; Wed, 28 Apr 2021 09:00:29 -0700 (PDT) MIME-Version: 1.0 References: <20210427225244.4326-1-axelrasmussen@google.com> <20210427225244.4326-7-axelrasmussen@google.com> <20210428155638.GD6584@xz-x1> In-Reply-To: <20210428155638.GD6584@xz-x1> From: Axel Rasmussen Date: Wed, 28 Apr 2021 08:59:53 -0700 Message-ID: Subject: Re: [PATCH v5 06/10] userfaultfd/shmem: modify shmem_mcopy_atomic_pte to use install_pte() To: Peter Xu Cc: Hugh Dickins , Alexander Viro , Andrea Arcangeli , Andrew Morton , Jerome Glisse , Joe Perches , Lokesh Gidra , Mike Kravetz , Mike Rapoport , Shaohua Li , Shuah Khan , Stephen Rothwell , Wang Qing , linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, LKML , linux-kselftest@vger.kernel.org, Linux MM , Brian Geffon , "Dr . David Alan Gilbert" , Mina Almasry , Oliver Upton Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 28, 2021 at 8:56 AM Peter Xu wrote: > > On Tue, Apr 27, 2021 at 05:58:16PM -0700, Hugh Dickins wrote: > > On Tue, 27 Apr 2021, Axel Rasmussen wrote: > > > > > In a previous commit, we added the mcopy_atomic_install_pte() helper. > > > This helper does the job of setting up PTEs for an existing page, to map > > > it into a given VMA. It deals with both the anon and shmem cases, as > > > well as the shared and private cases. > > > > > > In other words, shmem_mcopy_atomic_pte() duplicates a case it already > > > handles. So, expose it, and let shmem_mcopy_atomic_pte() use it > > > directly, to reduce code duplication. > > > > > > This requires that we refactor shmem_mcopy_atomic_pte() a bit: > > > > > > Instead of doing accounting (shmem_recalc_inode() et al) part-way > > > through the PTE setup, do it afterward. This frees up > > > mcopy_atomic_install_pte() from having to care about this accounting, > > > and means we don't need to e.g. shmem_uncharge() in the error path. > > > > > > A side effect is this switches shmem_mcopy_atomic_pte() to use > > > lru_cache_add_inactive_or_unevictable() instead of just lru_cache_add(). > > > This wrapper does some extra accounting in an exceptional case, if > > > appropriate, so it's actually the more correct thing to use. > > > > > > Signed-off-by: Axel Rasmussen > > > > Not quite. Two things. > > > > One, in this version, delete_from_page_cache(page) has vanished > > from the particular error path which needs it. > > Agreed. I also spotted that the set_page_dirty() seems to have been overlooked > when reusing mcopy_atomic_install_pte(), which afaiu should be move into the > helper. I think this is covered: we explicitly call SetPageDirty() just before returning in shmem_mcopy_atomic_pte(). If I remember correctly from a couple of revisions ago, we consciously put it here instead of in the helper because it resulted in simpler code (error handling in particular, I think?), and not all callers of the new helper need it. > > > > > Two, and I think this predates your changes (so needs a separate > > fix patch first, for backport to stable? a user with bad intentions > > might be able to trigger the BUG), in pondering the new error paths > > and that /* don't free the page */ one in particular, isn't it the > > case that the shmem_inode_acct_block() on entry might succeed the > > first time, but atomic copy fail so -ENOENT, then something else > > fill up the tmpfs before the retry comes in, so that retry then > > fail with -ENOMEM, and hit the BUG_ON(page) in __mcopy_atomic()? > > > > (As I understand it, the shmem_inode_unacct_blocks() has to be > > done before returning, because the caller may be unable to retry.) > > > > What the right fix is rather depends on other uses of __mcopy_atomic(): > > if they obviously cannot hit that BUG_ON(page), you may prefer to leave > > it in, and fix it here where shmem_inode_acct_block() fails. Or you may > > prefer instead to delete that "else BUG_ON(page);" - looks as if that > > would end up doing the right thing. Peter may have a preference. > > To me, the BUG_ON(page) wanted to guarantee mfill_atomic_pte() should have > consumed the page properly when possible. Removing the BUG_ON() looks good > already, it will just stop covering the case when e.g. ret==0. > > So maybe slightly better to release the page when shmem_inode_acct_block() > fails (so as to still keep some guard on the page)? This second issue, I will take some more time to investigate. :) > > Thanks, > > -- > Peter Xu > 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=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=ham 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 CE92BC433B4 for ; Wed, 28 Apr 2021 16:00:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 52BF1613CA for ; Wed, 28 Apr 2021 16:00:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 52BF1613CA 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 C045A6B0096; Wed, 28 Apr 2021 12:00:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BDB5A6B0099; Wed, 28 Apr 2021 12:00:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AA2F48D0005; Wed, 28 Apr 2021 12:00:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0181.hostedemail.com [216.40.44.181]) by kanga.kvack.org (Postfix) with ESMTP id 8CFE66B0096 for ; Wed, 28 Apr 2021 12:00:31 -0400 (EDT) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 4CFBB824999B for ; Wed, 28 Apr 2021 16:00:31 +0000 (UTC) X-FDA: 78082238262.07.AC17A35 Received: from mail-qt1-f180.google.com (mail-qt1-f180.google.com [209.85.160.180]) by imf21.hostedemail.com (Postfix) with ESMTP id 6BC51E01616C for ; Wed, 28 Apr 2021 16:00:27 +0000 (UTC) Received: by mail-qt1-f180.google.com with SMTP id q4so18677370qtn.5 for ; Wed, 28 Apr 2021 09:00:30 -0700 (PDT) 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=2SuF9mSmF53oX7jF/kwRpkLK22JzuM8F0Omk1lKrm3U=; b=c91OhnqmUMdrp5NwBmhDw0TWIKfd7cy5vfjKAMC5+iO+8WsXa/1iWeYeOn3iiAdnee K687+nQwR9YSd0rPz34NBcYSpywzjMxeDXC53/Pb4AEGt7HZEY5Icnr0ahoHBsonGrbg HXyuj//ZVjv22KpBoGWMZ2LFIIXIny1zBaq8Ckc7R2iE49O+5QqGx11lTuePdNiVNZQe kuJ5UJThxkM79QHzgt2Li5You3M+/fDQ8yIHwND0dIhYaufGJNCSGtpVdCHwu6nPhiZQ W8SjrEQQJNQEUo944EZmAwLRIc9DO2ZQpaS+Vg8i61WI8H2F5Vmk/5l5GjuPT/zghXGr DYyg== 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=2SuF9mSmF53oX7jF/kwRpkLK22JzuM8F0Omk1lKrm3U=; b=oa7Hx9BtoTEf6EiMgGB/5C4wKxAXQCP8bz3XlM3AILEGCMVaN8/QTAlyg19PEOxXGe QRa026eD31pwhGW8/8qYn2/WGexWMmWabsKd8dLAzLX/Ho2YeOaVzTJzNT1UyoD3WM7F MgpQ7F1ksenuLFflS4UR7Bp9Hbj2F6kA+VA+QdToucNFFlO8sVutIjTh9eqsZw/CCDbl 2C+6c1TRTh2gf034jotHEmlwuxeYXvmYKVF6ERLgfiTGlbwtqYAPGnNf/qHdrAOlGbgB KJcYeACQnIPX596LEfxpfZyT43gcaTV82LTRItdTETHSeEEXb37fBmSsoLFTERmWSfd1 YS6A== X-Gm-Message-State: AOAM533/dj2XQHMKSgpplhFFpg55C90/nbhgcFJcmXlMEmy816HjG6qW Xgk0BUtwQX+1b8BeRxyqejwQOXeLwdYVoqUE7Fz/eQ== X-Google-Smtp-Source: ABdhPJxw/1TATKg60w/SjCaCBhfHoT8ss0Bn6ygLRSzh2SsIWTbTBD0bqD3Zs2Gx7AOSnVSgCP23TbUXpGj11qrDJVs= X-Received: by 2002:ac8:51d3:: with SMTP id d19mr26641416qtn.358.1619625629931; Wed, 28 Apr 2021 09:00:29 -0700 (PDT) MIME-Version: 1.0 References: <20210427225244.4326-1-axelrasmussen@google.com> <20210427225244.4326-7-axelrasmussen@google.com> <20210428155638.GD6584@xz-x1> In-Reply-To: <20210428155638.GD6584@xz-x1> From: Axel Rasmussen Date: Wed, 28 Apr 2021 08:59:53 -0700 Message-ID: Subject: Re: [PATCH v5 06/10] userfaultfd/shmem: modify shmem_mcopy_atomic_pte to use install_pte() To: Peter Xu Cc: Hugh Dickins , Alexander Viro , Andrea Arcangeli , Andrew Morton , Jerome Glisse , Joe Perches , Lokesh Gidra , Mike Kravetz , Mike Rapoport , Shaohua Li , Shuah Khan , Stephen Rothwell , Wang Qing , linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, LKML , linux-kselftest@vger.kernel.org, Linux MM , Brian Geffon , "Dr . David Alan Gilbert" , Mina Almasry , Oliver Upton Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 6BC51E01616C X-Stat-Signature: 31uy1dfu7oje3ujafd9zw5419eee9hm4 Received-SPF: none (google.com>: No applicable sender policy available) receiver=imf21; identity=mailfrom; envelope-from=""; helo=mail-qt1-f180.google.com; client-ip=209.85.160.180 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1619625627-954785 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 Wed, Apr 28, 2021 at 8:56 AM Peter Xu wrote: > > On Tue, Apr 27, 2021 at 05:58:16PM -0700, Hugh Dickins wrote: > > On Tue, 27 Apr 2021, Axel Rasmussen wrote: > > > > > In a previous commit, we added the mcopy_atomic_install_pte() helper. > > > This helper does the job of setting up PTEs for an existing page, to map > > > it into a given VMA. It deals with both the anon and shmem cases, as > > > well as the shared and private cases. > > > > > > In other words, shmem_mcopy_atomic_pte() duplicates a case it already > > > handles. So, expose it, and let shmem_mcopy_atomic_pte() use it > > > directly, to reduce code duplication. > > > > > > This requires that we refactor shmem_mcopy_atomic_pte() a bit: > > > > > > Instead of doing accounting (shmem_recalc_inode() et al) part-way > > > through the PTE setup, do it afterward. This frees up > > > mcopy_atomic_install_pte() from having to care about this accounting, > > > and means we don't need to e.g. shmem_uncharge() in the error path. > > > > > > A side effect is this switches shmem_mcopy_atomic_pte() to use > > > lru_cache_add_inactive_or_unevictable() instead of just lru_cache_add(). > > > This wrapper does some extra accounting in an exceptional case, if > > > appropriate, so it's actually the more correct thing to use. > > > > > > Signed-off-by: Axel Rasmussen > > > > Not quite. Two things. > > > > One, in this version, delete_from_page_cache(page) has vanished > > from the particular error path which needs it. > > Agreed. I also spotted that the set_page_dirty() seems to have been overlooked > when reusing mcopy_atomic_install_pte(), which afaiu should be move into the > helper. I think this is covered: we explicitly call SetPageDirty() just before returning in shmem_mcopy_atomic_pte(). If I remember correctly from a couple of revisions ago, we consciously put it here instead of in the helper because it resulted in simpler code (error handling in particular, I think?), and not all callers of the new helper need it. > > > > > Two, and I think this predates your changes (so needs a separate > > fix patch first, for backport to stable? a user with bad intentions > > might be able to trigger the BUG), in pondering the new error paths > > and that /* don't free the page */ one in particular, isn't it the > > case that the shmem_inode_acct_block() on entry might succeed the > > first time, but atomic copy fail so -ENOENT, then something else > > fill up the tmpfs before the retry comes in, so that retry then > > fail with -ENOMEM, and hit the BUG_ON(page) in __mcopy_atomic()? > > > > (As I understand it, the shmem_inode_unacct_blocks() has to be > > done before returning, because the caller may be unable to retry.) > > > > What the right fix is rather depends on other uses of __mcopy_atomic(): > > if they obviously cannot hit that BUG_ON(page), you may prefer to leave > > it in, and fix it here where shmem_inode_acct_block() fails. Or you may > > prefer instead to delete that "else BUG_ON(page);" - looks as if that > > would end up doing the right thing. Peter may have a preference. > > To me, the BUG_ON(page) wanted to guarantee mfill_atomic_pte() should have > consumed the page properly when possible. Removing the BUG_ON() looks good > already, it will just stop covering the case when e.g. ret==0. > > So maybe slightly better to release the page when shmem_inode_acct_block() > fails (so as to still keep some guard on the page)? This second issue, I will take some more time to investigate. :) > > Thanks, > > -- > Peter Xu >