From patchwork Wed May 22 12:57:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13670845 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 351D6C25B7A for ; Wed, 22 May 2024 12:57:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6302E6B0085; Wed, 22 May 2024 08:57:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5DFCA6B0088; Wed, 22 May 2024 08:57:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4A7EB6B0089; Wed, 22 May 2024 08:57:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2ED476B0085 for ; Wed, 22 May 2024 08:57:24 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 97060A1459 for ; Wed, 22 May 2024 12:57:23 +0000 (UTC) X-FDA: 82146032766.28.635B030 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id ECB181C0013 for ; Wed, 22 May 2024 12:57:21 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KvGGMnT5; spf=pass (imf18.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716382642; a=rsa-sha256; cv=none; b=Lbk5Lsx3fLn73ObawVDaXPpE86tNfA7ms5GLGKq7Uvgiic4KjiyS7m23UE+mOB4Xq9CHJN V1/hl6u5DCVQVTZdbmUQ9SY+eiRbkS3d1JPbxmgLPvb7dnmLJP7p1LCJRZUNmGJLwedQLm f0Wr3bvtxRRdWKFkddRoGQvgWs6jb4g= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KvGGMnT5; spf=pass (imf18.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=1716382642; 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:references:dkim-signature; bh=o/utX1MEHc2TwOEpLKg+wlbdf8OjMA6nlb8SpmUn5jQ=; b=a5yZMg+kGKSqoNzwpTrWcjJ1lXFBEQegL2ySyro06OJyU0vgr/cMlmFxyebYpE2+eKUnbu a+oJDkQpzRmPMBPD328XICZTZbwNpG+TAZne5hw3C4mvp7r69QpAWmqff2Y/S0VDdh+NO1 wNJgjx/Q54BpqVC0Br/kZovtqp70QxQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716382641; 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; bh=o/utX1MEHc2TwOEpLKg+wlbdf8OjMA6nlb8SpmUn5jQ=; b=KvGGMnT5wEpQwxXqHp9vPIWNl5I0ISo/AdJcJG8nzcyBTuoeDlMsz41hO37uoZ2Cusitas zKFPQdyirkU5jdhO84s3aH0MF/Rw9FHu5aTNQphyFj3mv0iL58Hq6+3AKu20YkyvHzWMd/ Jsxu9r9aqtVyBAVdDCQzO2GVW4oHRNM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-590-AwzxaaifN8WmtjwIMvUPeQ-1; Wed, 22 May 2024 08:57:15 -0400 X-MC-Unique: AwzxaaifN8WmtjwIMvUPeQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5C2EE185A780; Wed, 22 May 2024 12:57:15 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.195.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 13B5040C6EB7; Wed, 22 May 2024 12:57:13 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Vincent Donnefort , Dan Williams Subject: [PATCH v2 0/3] mm/memory: cleanly support zeropage in vm_insert_page*(), vm_map_pages*() and vmf_insert_mixed() Date: Wed, 22 May 2024 14:57:10 +0200 Message-ID: <20240522125713.775114-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Rspamd-Queue-Id: ECB181C0013 X-Rspam-User: X-Rspamd-Server: rspam12 X-Stat-Signature: xqwnhdio5158mz4b99ttopjud7jux6bo X-HE-Tag: 1716382641-692067 X-HE-Meta: U2FsdGVkX1/HLnJFnolTTVCs2wcJyhMiAaHRMQHvzbaVbnkyP88hnhon156wPdDddtfZDW3Xfer+2NQosFcQd/csGTyWsI1qAdEdoM1953peRXEefjp7pQ5+bBuwjOWRZQrn6drtgDcicpCiXJHeiKd3OR/c2dLCjCF/DNMh8BlcT92dF2WH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18GWDD5eau033v/HEXmMawn8Kp20VE2AAIMFjFvIQOBy6p5y47lSjZKdJbnEMehSXcJjgaQYyze4P2ehBk6jb0kMAox1olrIYtunpN7bg2zpXsmtqNn 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: List-Subscribe: List-Unsubscribe: There is interest in mapping zeropages via vm_insert_pages() [1] into MAP_SHARED mappings. For now, we only get zeropages in MAP_SHARED mappings via vmf_insert_mixed() from FSDAX code, and I think it's a bit shaky in some cases because we refcount the zeropage when mapping it but not necessarily always when unmapping it ... and we should actually never refcount it. It's all a bit tricky, especially how zeropages in MAP_SHARED mappings interact with GUP (FOLL_LONGTERM), mprotect(), write-faults and s390x forbidding the shared zeropage (rewrite [2] s now upstream). This series tries to take the careful approach of only allowing the zeropage where it is likely safe to use (which should cover the existing FSDAX use case and [1]), preventing that it could accidentally get mapped writable during a write fault, mprotect() etc, and preventing issues with FOLL_LONGTERM in the future with other users. Tested with a patch from Vincent that uses the zeropage in context of [1]. Vincent will post that patch based on this series soon. (not tested with FSDAX, but I don't expect surprises). [1] https://lkml.kernel.org/r/20240430111354.637356-1-vdonnefort@google.com [2] https://lkml.kernel.org/r/20240411161441.910170-1-david@redhat.com v1 -> v2: * "mm/memory: move page_count() check into validate_page_before_insert()" -> Added * "mm/memory: cleanly support zeropage in vm_insert_page*(), ..." -> Fixed "return true;" for never-writable VMAs Cc: Andrew Morton Cc: Vincent Donnefort Cc: Dan Williams David Hildenbrand (3): mm/memory: move page_count() check into validate_page_before_insert() mm/memory: cleanly support zeropage in vm_insert_page*(), vm_map_pages*() and vmf_insert_mixed() mm/rmap: sanity check that zeropages are not passed to RMAP include/linux/rmap.h | 3 ++ mm/memory.c | 97 ++++++++++++++++++++++++++++++++------------ mm/mprotect.c | 2 + 3 files changed, 77 insertions(+), 25 deletions(-) base-commit: 29c73fc794c83505066ee6db893b2a83ac5fac63