From patchwork Thu Jul 27 21:28:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13330787 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 3BF71C00528 for ; Thu, 27 Jul 2023 21:29:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5538D6B007D; Thu, 27 Jul 2023 17:29:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4DAE66B007E; Thu, 27 Jul 2023 17:29:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E37E6B0081; Thu, 27 Jul 2023 17:29:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 183196B007E for ; Thu, 27 Jul 2023 17:29:03 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D93211A01DE for ; Thu, 27 Jul 2023 21:29:02 +0000 (UTC) X-FDA: 81058682124.16.A42E529 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf07.hostedemail.com (Postfix) with ESMTP id 200CE4000D for ; Thu, 27 Jul 2023 21:29:00 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eh0T10WH; spf=pass (imf07.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690493341; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NHdYsiNqqxFFYOyFZT8N/uUoLYdRyRl4LhGdK5bhGpM=; b=JCuHY6qJVm+MUc5pE4Jag4M4fD4iU7BD4/Ij8QNf4+SlTe9xvtOZ7doz8TT3WicstcMMjy s+liIFfaB5NHSKdXSWjM7uInaG+CGkjdrH8Hwhwm5V4dnIQ6PgxXHVTwsrtCws2317y7o7 FETqcynE9rWj+3lio/9zXRaTtkj5y/8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690493341; a=rsa-sha256; cv=none; b=dDA74UGseiiUAajfjfJXEEdzA3im9NK3iXsrGh+LV4qPZim+Js9TH39B3F4Aa3TxcHHhKO l01aCsJ3Hb+9cZmaEBOH1ZtwpAjH2uHSfxKGjoQcIdMwEGOVBVAo5Yt1vL3YCcde2tdOnZ pp+cz7XCSk71h9Q+szgxbatr/vFQvgA= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eh0T10WH; spf=pass (imf07.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1690493340; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NHdYsiNqqxFFYOyFZT8N/uUoLYdRyRl4LhGdK5bhGpM=; b=eh0T10WHWVEgPCSHqoYkZMtBdSJ7MBfJ3maGZRLjWv5087hLkSo4x5lEZNH86EhCQpfKKl YUl4uHa+U08b3ERWCZb/VhbsbHLyFvktZCqGYSUDTFaiqvchDp3W8ZyrADY04VVobZkGyv fRSB4ReMm/ghDO9KXjujtVjDxCB3Qag= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-554-F0H8AQw3M0yhKgh2HgjDcA-1; Thu, 27 Jul 2023 17:28:56 -0400 X-MC-Unique: F0H8AQw3M0yhKgh2HgjDcA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 50B76104458B; Thu, 27 Jul 2023 21:28:55 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.192.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 67C2B40C2063; Thu, 27 Jul 2023 21:28:53 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, David Hildenbrand , Andrew Morton , Linus Torvalds , liubo , Peter Xu , Matthew Wilcox , Hugh Dickins , Jason Gunthorpe , John Hubbard , stable@vger.kernel.org Subject: [PATCH v1 2/4] mm/gup: Make follow_page() succeed again on PROT_NONE PTEs/PMDs Date: Thu, 27 Jul 2023 23:28:43 +0200 Message-ID: <20230727212845.135673-3-david@redhat.com> In-Reply-To: <20230727212845.135673-1-david@redhat.com> References: <20230727212845.135673-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Stat-Signature: wdzwgi6yu9eirdpinntd1xn9rmmqisza X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 200CE4000D X-Rspam-User: X-HE-Tag: 1690493340-89599 X-HE-Meta: U2FsdGVkX18XsL8vZRi/O98cxNPcEYC/Mb0BRtVLaNDKQi+9nSuH/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G2HmkHYskWAhtOjIj1PhJrULaKJYW8Aql4i5HNRs1p3MKZju/iD5iukRZ5ijPXQ682cIP6hixAQnGXS/p7ugBvDr7N6JvwedWRMGZIVbCc+sJhttP8OwuilGW63h67dfH5h+LXjwzfpJEyRhHOb79OudA== 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: We accidentally enforced PROT_NONE PTE/PMD permission checks for follow_page() like we do for get_user_pages() and friends. That was undesired, because follow_page() is usually only used to lookup a currently mapped page, not to actually access it. Further, follow_page() does not actually trigger fault handling, but instead simply fails. Let's restore that behavior by conditionally setting FOLL_FORCE if FOLL_WRITE is not set. This way, for example KSM and migration code will no longer fail on PROT_NONE mapped PTEs/PMDS. Handling this internally doesn't require us to add any new FOLL_FORCE usage outside of GUP code. While at it, refuse to accept FOLL_FORCE: we don't even perform VMA permission checks like in check_vma_flags(), so especially FOLL_FORCE|FOLL_WRITE would be dodgy. This issue was identified by code inspection. We'll add some documentation regarding FOLL_FORCE next. Reported-by: Peter Xu Fixes: 474098edac26 ("mm/gup: replace FOLL_NUMA by gup_can_follow_protnone()") Cc: Signed-off-by: David Hildenbrand --- mm/gup.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mm/gup.c b/mm/gup.c index 2493ffa10f4b..da9a5cc096ac 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -841,9 +841,17 @@ struct page *follow_page(struct vm_area_struct *vma, unsigned long address, if (vma_is_secretmem(vma)) return NULL; - if (WARN_ON_ONCE(foll_flags & FOLL_PIN)) + if (WARN_ON_ONCE(foll_flags & (FOLL_PIN | FOLL_FORCE))) return NULL; + /* + * Traditionally, follow_page() succeeded on PROT_NONE-mapped pages + * but failed follow_page(FOLL_WRITE) on R/O-mapped pages. Let's + * keep these semantics by setting FOLL_FORCE if FOLL_WRITE is not set. + */ + if (!(foll_flags & FOLL_WRITE)) + foll_flags |= FOLL_FORCE; + page = follow_page_mask(vma, address, foll_flags, &ctx); if (ctx.pgmap) put_dev_pagemap(ctx.pgmap);