Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4989648pxy; Tue, 27 Apr 2021 18:00:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJpfaTa/8jDO3z1JW2tl8GcYaGYA6y8fOz3lfq/tULWwoXQXVOPWNdaPOm0/mslnn3KyKb X-Received: by 2002:a17:902:a582:b029:ec:d002:623b with SMTP id az2-20020a170902a582b02900ecd002623bmr27450514plb.36.1619571638733; Tue, 27 Apr 2021 18:00:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619571638; cv=none; d=google.com; s=arc-20160816; b=VY6zlYA6art84GpUdfLTE+vppXBr4EbGI7jHt88upP5+NTp7i0ar1aY61lRJQdJvZ3 oRjb8KNj9xcR4+jcmQMTXNcRuXSfyUI8scjFEZKoRPVrWwulvDfkAxPyxCxYMyvOMWGq 03GImmuUmSgfQT72vRlgJZ6on1yTAtazZghe72/x1Y3Bftet1wroNFWzuH3BFegh4EGu 4zoVaWlluq/62iuuLjqCOCBJ/wk9D9O9hx+f8oPZLO/3hkfhkddvisSHiIQQMkYwpggG lImwjELEGQVLRsTtCYY7j4KrEnom3plTJJDMU8KQtbkL0KlKm1H29yaRiHQHVO6K6vWb EzOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date:dkim-signature; bh=nnLhfKmay2hfrNFLJmZvY1slfDRvS1kILjvcSIZf/mg=; b=gL8VXKNA7kA9GArNSYkrdZJHZHW9iVtzd+HY78cYiR8IXP/3Dsjt4OI6/vSWzmZEJC s2tx6Qn4AFziuVlmcEt4055hwcR5yi3hI3F6c1ogA8PxrxNC5VjnJQ/WQRllabUrACNn DJx1XYZKXir2ClrLb4ARpNroHM0w3recDzt9Ext+qgdp9/SriaSFzsMtcA9jT37Tx8jn d7RRwlU6HBSvQuKXp2QWLgopvKdcYffByKOCe+WVQuUdBbmxPY07ZStpa6JHd/ojsdV+ TIDnjWPLLn5+GgK6NbPTIOeosXrKpqDPffYCCcq2WuF2G4arGvIwOXAQ4sYXGGvs+pZU wMWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Hn5od4nT; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k18si1875787plk.418.2021.04.27.18.00.24; Tue, 27 Apr 2021 18:00:38 -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=@google.com header.s=20161025 header.b=Hn5od4nT; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230460AbhD1A7F (ORCPT + 99 others); Tue, 27 Apr 2021 20:59:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235691AbhD1A7E (ORCPT ); Tue, 27 Apr 2021 20:59:04 -0400 Received: from mail-qt1-x829.google.com (mail-qt1-x829.google.com [IPv6:2607:f8b0:4864:20::829]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9DFAC061760 for ; Tue, 27 Apr 2021 17:58:20 -0700 (PDT) Received: by mail-qt1-x829.google.com with SMTP id n22so6056530qtk.9 for ; Tue, 27 Apr 2021 17:58:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=nnLhfKmay2hfrNFLJmZvY1slfDRvS1kILjvcSIZf/mg=; b=Hn5od4nTNCw0oO2WqwOMMft1NWsukPMlQRMrmwYFStQpXM9w0sZauP31fIdtDH/T7P h6jPwZVotyfrbiIrXeMGZ62TkyK6QIwX67ES+gfMl8OiTSmYJuZ52V6nA3KhnyVv4rVw CLhPSfyg0imxrZGzoaYi+sS4g6tNMPJ3cwXA+NwRZiLQFrWWez+EUg+fChxjLjtbljLC SuLpDsBsgIf4kUuDkMscPgXmS7pVdl1eULZREb5iRO8DH7rXP1kkBNdf923GNq8j4Vhg X/HUjp7hcqGBtAi6hqMpCMgABRm2cYcM1xijPrP0MlS4UfszukBRzBa7mfHZOl0IkXsI KAow== 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:in-reply-to:message-id :references:user-agent:mime-version; bh=nnLhfKmay2hfrNFLJmZvY1slfDRvS1kILjvcSIZf/mg=; b=nSUcbOuo7RO0DeMUTOKLQmeAq393MMLbxhiUsCu13VpiY1vxxy8o/SaMwrq9S9XsLp 81QvUZi02bfdlsz/YliXCaMymKBsKnjKzMb1Pdh/4nxs/wHEv0zKEQZzbdpJHBMomtdG 2ao8MNpCuiSPuTWo4RnPz3zczoLAW+mjJ+i6+SkkkSZ7XiRsPM7i2GxiH7aByaLnpFK3 Ie6lBRNvUTV16HXaE7lWL6AZcVf59G/g1L2pXIzD3+FD4cQmZhJSd/Abo0O3TqP9i81q prlwpDaBlM6o7zPo2xE/9UzbXbNNrev1Wpm3HK9MWiyobBTSfFREJ86V7eBOaMl1BTVX Qqiw== X-Gm-Message-State: AOAM5333bXiVQtUbj/wjfUivZRiDWArjj1akiBrLh9Bqpca2uFBtulJG 06efwdIxIjhHRt5ytW3Zy14Uvg== X-Received: by 2002:ac8:6e89:: with SMTP id c9mr24352036qtv.268.1619571499536; Tue, 27 Apr 2021 17:58:19 -0700 (PDT) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id 3sm3869819qko.120.2021.04.27.17.58.17 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Tue, 27 Apr 2021 17:58:19 -0700 (PDT) Date: Tue, 27 Apr 2021 17:58:16 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Axel Rasmussen cc: Alexander Viro , Andrea Arcangeli , Andrew Morton , Hugh Dickins , Jerome Glisse , Joe Perches , Lokesh Gidra , Mike Kravetz , Mike Rapoport , Peter Xu , Shaohua Li , Shuah Khan , Stephen Rothwell , Wang Qing , linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, Brian Geffon , "Dr . David Alan Gilbert" , Mina Almasry , Oliver Upton Subject: Re: [PATCH v5 06/10] userfaultfd/shmem: modify shmem_mcopy_atomic_pte to use install_pte() In-Reply-To: <20210427225244.4326-7-axelrasmussen@google.com> Message-ID: References: <20210427225244.4326-1-axelrasmussen@google.com> <20210427225244.4326-7-axelrasmussen@google.com> User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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. (Or, we could consider doing the shmem_inode_acct_block() only after the page has been copied in: its current placing reflects how shmem.c does it elsewhere, and there's reason for that, but it doesn't always work out right. Don't be surprised if I change the ordering in future, but it's probably best not to mess with that ordering now.) Sorry, if this is a pre-existing issue, then we are taking advantage of you, in asking you to fix it: but I hope that while you're in there, it will make sense to do so. Thanks, Hugh > --- > include/linux/userfaultfd_k.h | 5 ++++ > mm/shmem.c | 48 +++++------------------------------ > mm/userfaultfd.c | 17 +++++-------- > 3 files changed, 18 insertions(+), 52 deletions(-)