From patchwork Fri Jun 10 17:38:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12877865 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 014C7C433EF for ; Fri, 10 Jun 2022 17:38:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 91E808D00D0; Fri, 10 Jun 2022 13:38:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8CE848D00CB; Fri, 10 Jun 2022 13:38:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7969A8D00D0; Fri, 10 Jun 2022 13:38:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 69F748D00CB for ; Fri, 10 Jun 2022 13:38:32 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 405BC1719 for ; Fri, 10 Jun 2022 17:38:32 +0000 (UTC) X-FDA: 79563035664.12.94354AD Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf11.hostedemail.com (Postfix) with ESMTP id D4D1D4007C for ; Fri, 10 Jun 2022 17:38:31 +0000 (UTC) Received: by mail-yb1-f202.google.com with SMTP id m64-20020a255843000000b00663d6d41f5aso10378212ybb.12 for ; Fri, 10 Jun 2022 10:38:31 -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=5fmQszK1Ftmw30V0gwN1AZaU6pQNh3b494ZogsrG54w=; b=QGT7zos6PMA4nXZmokylnJRPrMcP3oyvrZKGohf5aZSXTn/UheG2FCg5yKy29JsjSN eZSdktXiLVEzvErtwWSo7D3GPQedGFu1CySEfelpPuvGc5+kPlMy1F11sDFL7nveEsQt HjdR6s3jW9l1P7m5y2fAIT8HR/vNIzTz4DnSiZ44GnKtm7drGyA2z/iJjfsvWlNCK1cM Yz5pUQ1uhf0s95Yor3DODnYdVMmWKM1Ad1xVRkLz9bXhY84nxexLIitGVw7Gs2HyoVjZ JyHN0C8XoIBPk0GlyHy+HKIEOTkjHLYujG5EH/8kZpHAw7vSTBubzvRVShRyUJAbYSiU b/og== 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=5fmQszK1Ftmw30V0gwN1AZaU6pQNh3b494ZogsrG54w=; b=AMAPUfyfyikHq2wicxw4Vtl42C+aYD4SXBheTN4gGGooxFYgs69M7QiEvTI2mrHM72 0JE4ov2qI/l79yZ+QJjWDauOF/OC+jIbhJSeZ5Rxk+kLECk64NFCxrUmmNntG4wB2clA xtR9qS5tiuNJ85Q4njKtywPdJJE++9GKqm/C635DJ2PCK+cLMVLhVQJSExrOch7K2tNo K/jlGT1gBNoyG7UBG1F5DprJ++b+xTbMYXKYk7GZuAUGEnSjGw3Y9AbgLeh7EbIV8tr4 1GVgrO91i3Bt/vFyyR/Mzkuipk8fD8tBIvRs+DuA1B7JTGJKwf+0MHLvpX/nrH3/xPh1 HwpA== X-Gm-Message-State: AOAM530tPN4MvomMam7nx2jcdVihsCQxvEuP29iu+zlEjeXUDsK6wdD6 TXhvI4+6JXSUFQR7d73tnW9acckSzEwvuA3L4s8A X-Google-Smtp-Source: ABdhPJzSqTsvnb1ZC/aD7CdAHop/d77Nuh2d8nZpHySjd4kmLswDFlqFdL1Bx2WtIJ/Tzlole4EE2ZwSXQwJGB8+JpE5 X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:9a0c:4d31:bfe1:5abd]) (user=axelrasmussen job=sendgmr) by 2002:a25:58f:0:b0:664:628a:374b with SMTP id 137-20020a25058f000000b00664628a374bmr4604612ybf.329.1654882711003; Fri, 10 Jun 2022 10:38:31 -0700 (PDT) Date: Fri, 10 Jun 2022 10:38:12 -0700 Message-Id: <20220610173812.1768919-1-axelrasmussen@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog Subject: [PATCH v2] 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1654882712; a=rsa-sha256; cv=none; b=yOa1ckSb1gsrjmQp1exqkNo/XsDyaEHITGhG0heDQuXNJVTcJjzelZQsJmnmezI3v/zKUi YvcmYBaautP2pIf/tBB+aAiRxT7Fd49Qrxkkav3ZJidDcoEF2SKIjoerHmuZzWKZr9qrrR cjB3EEZ0GkGHV1NQ+a9z7ShUbNFtShU= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=QGT7zos6; spf=pass (imf11.hostedemail.com: domain of 3l4GjYg0KCEQg3krxgys0yyktmuumrk.iusrot03-ssq1giq.uxm@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3l4GjYg0KCEQg3krxgys0yyktmuumrk.iusrot03-ssq1giq.uxm@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1654882712; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=5fmQszK1Ftmw30V0gwN1AZaU6pQNh3b494ZogsrG54w=; b=5vJigrgIo4yKU9XLgLNsjyOvbEennoyzamdewHvqOHfubUa5Su339/Nn2S3WDFisZQMcEV iPPejP58yeaqS1ZuOnP4DH+SCVoUN1jmL5Sf5hNdm9eR8NHz5sRuu9sqVYPRluGbxmu5tp yOSTAjOTS15ffGzSzhyL5VGoryZgYB0= X-Rspamd-Queue-Id: D4D1D4007C Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=QGT7zos6; spf=pass (imf11.hostedemail.com: domain of 3l4GjYg0KCEQg3krxgys0yyktmuumrk.iusrot03-ssq1giq.uxm@flex--axelrasmussen.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3l4GjYg0KCEQg3krxgys0yyktmuumrk.iusrot03-ssq1giq.uxm@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam06 X-Stat-Signature: osphqxtzwj6d77ky4gi54pa9y93s4pj8 X-HE-Tag: 1654882711-130158 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: Peter Xu --- mm/userfaultfd.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 4f4892a5f767..07d3befc80e4 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -246,7 +246,10 @@ 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); + /* Our caller expects us to return -EFAULT if we failed to find page. */ + if (ret == -ENOENT) + ret = -EFAULT; if (ret) goto out; if (!page) {