From patchwork Fri Jun 3 20:57:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12869410 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F05AC43334 for ; Fri, 3 Jun 2022 20:57:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 944666B0072; Fri, 3 Jun 2022 16:57:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8EEB38D0001; Fri, 3 Jun 2022 16:57:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B8DB6B0074; Fri, 3 Jun 2022 16:57:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6DD706B0072 for ; Fri, 3 Jun 2022 16:57:53 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3B77D35D61 for ; Fri, 3 Jun 2022 20:57:53 +0000 (UTC) X-FDA: 79538136426.05.B292C30 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf01.hostedemail.com (Postfix) with ESMTP id 6DDD340005 for ; Fri, 3 Jun 2022 20:57:44 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-310061f47faso33718697b3.9 for ; Fri, 03 Jun 2022 13:57:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=rLWu+mkuMiZJwNpdLayTrCXX3MnKK+cmNqmcZ/mBvwY=; b=OBEwrNSluEd0bNvqaMjphL7UKxoam5UzDjKcYtfrt3UGHt3Qi+VteT1dPI86v82gxo 7iD9PmFt/LmOhUWsfiNZAxJHTwT84Xp3h+lC6zdjUIeW2k0oqndllnhpQyv11QEirI/G kE/r0HZXFRlKMsdZyMdnv35UVZisCr5fXdGOkf23c6VBxC8j6aSHLvKkrU1JqEZCUDSW 6txWhhL5DQYMAb4pepUqYRJaz6RlhnkKWBCRWSnNnEspIxSU98Jn73KP6f/4++sNOriK omKdxky4CMNnpADLPvSRq1sqBvreuVQ01WClJEU0YNUIoeJeRjlw3n6DGlbzXrkFMkGR XrWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=rLWu+mkuMiZJwNpdLayTrCXX3MnKK+cmNqmcZ/mBvwY=; b=Ml/ZxM53/AAxMdOtkEOac7l0cBU1NB713nAf/YnZ5Kv/BZERgRIcL0k2K1iq7BZWil djzZjoJUQuKt2c7I8Wm0MxpdoPXjuOESsXiH+Wzukj9kG/6NpZhRz3gOHhhTYUQIFpwi lkO1/oDGwDMMcpuMjFGZ9ubzz8ZSmaqBIk7o8JrRG+T5WXnBI6oYe12YWqIpZIsjFCWL /GxQ/hKRCvTEKNi86f0Gqs9uxFZzAzivSJOd+nPkGxvNR9uTde/bKuKJWkxicx2o+DzD 7UPt02NMJf/qLPBKNhiQncWJgCR8rZWFa9V5GIQKkGLdGTOp2wvJvdxLjAJ+E/YzW9CK hFXQ== X-Gm-Message-State: AOAM531Sgg5SNaDGCnsbQd2ThTmZvLTbibn5LQ0pFIQ2oisLOiVqAvV2 MNEgQijDIfzqBs0hBonnfBo9wkIM2VhPtIV2frDC X-Google-Smtp-Source: ABdhPJwriWQgdUK7gB2ED+zM3smPBkANF/4W6Hz3s3ybmUltLgXLxLrOFBaxoeax8EHhui11zuzLSibrLNZ5aDNy7w5B X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:fbb0:d120:cc2e:23c1]) (user=axelrasmussen job=sendgmr) by 2002:a25:c883:0:b0:64e:a298:304 with SMTP id y125-20020a25c883000000b0064ea2980304mr13114666ybf.38.1654289871981; Fri, 03 Jun 2022 13:57:51 -0700 (PDT) Date: Fri, 3 Jun 2022 13:57:41 -0700 Message-Id: <20220603205741.12888-1-axelrasmussen@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH] mm: userfaultfd: fix UFFDIO_CONTINUE on fallocated shmem pages From: Axel Rasmussen To: Andrew Morton , Peter Xu , Hugh Dickins Cc: Axel Rasmussen , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 6DDD340005 X-Stat-Signature: b8361j8uqk49nhwjzky6xf7o43sgphzb Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=OBEwrNSl; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of 3z3WaYg0KCEAczgntcuowuugpiqqing.eqonkpwz-oomxcem.qti@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3z3WaYg0KCEAczgntcuowuugpiqqing.eqonkpwz-oomxcem.qti@flex--axelrasmussen.bounces.google.com X-HE-Tag: 1654289864-863236 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: When fallocate() is used on a shmem file, the pages we allocate can end up with !PageUptodate. Since UFFDIO_CONTINUE tries to find the existing page the user wants to map with SGP_READ, we would fail to find such a page, since shmem_getpage_gfp returns with a "NULL" pagep for SGP_READ if it discovers !PageUptodate. As a result, UFFDIO_CONTINUE returns -EFAULT, as it would do if the page wasn't found in the page cache at all. This isn't the intended behavior. UFFDIO_CONTINUE is just trying to find if a page exists, and doesn't care whether it still needs to be cleared or not. So, instead of SGP_READ, pass in SGP_NOALLOC. This is the same, except for one critical difference: in the !PageUptodate case, SGP_NOALLOC will clear the page and then return it. With this change, UFFDIO_CONTINUE works properly (succeeds) on a shmem file which has been fallocated, but otherwise not modified. Fixes: 153132571f02 ("userfaultfd/shmem: support UFFDIO_CONTINUE for shmem") Cc: stable@vger.kernel.org Signed-off-by: Axel Rasmussen Acked-by: Hugh Dickins --- mm/userfaultfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 4f4892a5f767..c156f7f5b854 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -246,7 +246,7 @@ static int mcontinue_atomic_pte(struct mm_struct *dst_mm, struct page *page; int ret; - ret = shmem_getpage(inode, pgoff, &page, SGP_READ); + ret = shmem_getpage(inode, pgoff, &page, SGP_NOALLOC); if (ret) goto out; if (!page) {