From patchwork Wed Mar 27 17:17:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13607116 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 24599C47DD9 for ; Wed, 27 Mar 2024 17:17:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ADAEE6B0099; Wed, 27 Mar 2024 13:17:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A8B346B009B; Wed, 27 Mar 2024 13:17:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 952C16B009C; Wed, 27 Mar 2024 13:17:57 -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 783236B0099 for ; Wed, 27 Mar 2024 13:17:57 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0CD6880DCC for ; Wed, 27 Mar 2024 17:17:57 +0000 (UTC) X-FDA: 81943476594.04.FBE977D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf04.hostedemail.com (Postfix) with ESMTP id 563CC40008 for ; Wed, 27 Mar 2024 17:17:55 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QlHyqx4J; spf=pass (imf04.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=1711559875; 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=gs7mz2T2Nzxhsdo7I/ZcfY9h1l0ujt1Yaj1fAdyaXDw=; b=z12FsU99s1FZ4PLVgIZySgKrfiPPBCVwS9skf99CXS2sU33od/lFqN6T5IfCZ8y/a2sKYo 7E87imPVt5JrKEEeHpBJ6MDasRgL6Ysu6PaWdDSNt/DTPWHG6L8yxPf3VC1kIZLK9IQBym BW9mQTBCARJDs5or85ncY+3v62jrzo8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711559875; a=rsa-sha256; cv=none; b=ZGY5aN5oYaSI9IWjTNwqDmLCLpIQb7q+iLxuKrybBF3KJIUwYCACcGu8ByNYc2njm6r07Q 1IjwqhIcisleBntLZB6QPxsgbsGTkBNiZ4yTEUtwW5SdPwuEZ66FdkKn++xRxsqB5KLrpM IYiRdM3tSkgIOsBGVWztIBVkaWAjkew= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=QlHyqx4J; spf=pass (imf04.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=1711559874; 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=gs7mz2T2Nzxhsdo7I/ZcfY9h1l0ujt1Yaj1fAdyaXDw=; b=QlHyqx4J2K5PbSdGQP7f8KwnpuXSovqXb3qVDwPO0OIrVhVHFuQh1LyAMcJtwfdwvSuIOU 4BD8diFXNreZvSPFOtuP1SHXhSW62yjPMCuf6nRQ6s8dd4WdwUwjYCQjnB+x8gx1WhGVZz o7x2cBiBzhzC0Cl9960u9gJcRCBXkjc= 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-206-ENn_kGGANHuMOOroM-8g4Q-1; Wed, 27 Mar 2024 13:17:49 -0400 X-MC-Unique: ENn_kGGANHuMOOroM-8g4Q-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 9A686101A526; Wed, 27 Mar 2024 17:17:48 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.208]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0EC5A1121312; Wed, 27 Mar 2024 17:17:43 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Andrew Morton , Peter Xu , Alexander Gordeev , Sven Schnelle , Gerald Schaefer , Andrea Arcangeli , kvm@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 0/2] s390/mm: shared zeropage + KVM fixes Date: Wed, 27 Mar 2024 18:17:35 +0100 Message-ID: <20240327171737.919590-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Stat-Signature: t7izow9t19s1jtio6zyqe9awda5qotw3 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 563CC40008 X-Rspam-User: X-HE-Tag: 1711559875-913065 X-HE-Meta: U2FsdGVkX19Nod2yjX07bkQk/n/zKY6GUK7nwpCDFutqVzBShHKoo3ZV9JsJh/4odA940gYSukVaGK0aH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jnledFbzwCzKsy9umj4l0rE5EAMPuhMj5TSmlWHi+OtZGek/cWjwhfROTfdYZgh3PBKUiS5CY2+0kW95JHHOLaJ0pGRb1nOG/HUJhZLgtDe/8Dc5eglexjNz2YTCN5YaolDP5CkKACkQ2mOi5Z 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: This series fixes one issue with uffd + shared zeropages on s390x and fixes that "ordinary" KVM guests can make use of shared zeropages again. userfaultfd could currently end up mapping shared zeropages into processes that forbid shared zeropages. This only apples to s390x, relevant for handling PV guests and guests that use storage kets correctly. Fix it by placing a zeroed folio instead of the shared zeropage during UFFDIO_ZEROPAGE instead. I stumbled over this issue while looking into a customer scenario that is using: (1) Memory ballooning for dynamic resizing. Start a VM with, say, 100 GiB and inflate the balloon during boot to 60 GiB. The VM has ~40 GiB available and additional memory can be "fake hotplugged" to the VM later on demand by deflating the balloon. Actual memory overcommit is not desired, so physical memory would only be moved between VMs. (2) Live migration of VMs between sites to evacuate servers in case of emergency. Without the shared zeropage, during (2), the VM would suddenly consume 100 GiB on the migration source and destination. On the migration source, where we don't excpect memory overcommit, we could easilt end up crashing the VM during migration. Independent of that, memory handed back to the hypervisor using "free page reporting" would end up consuming actual memory after the migration on the destination, not getting freed up until reused+freed again. While there might be ways to optimize parts of this in QEMU, we really should just support the shared zeropage again for ordinary VMs. We only expect legcy guests to make use of storage keys, so let's handle zeropages again when enabling storage keys or when enabling PV. To not break userfaultfd like we did in the past, don't zap the shared zeropages, but instead trigger unsharing faults, just like we do for unsharing KSM pages in break_ksm(). Unsharing faults will simply replace the shared zeropage by a zeroed anonymous folio. We can already trigger the same fault path using GUP, when trying to long-term pin a shared zeropage, but also when unmerging a KSM-placed zeropages, so this is nothing new. Patch #1 tested on 86-64 by forcing mm_forbids_zeropage() to be 1, and running the uffd selftests. Patch #2 tested on s390x: the live migration scenario now works as expected, and kvm-unit-tests that trigger usage of skeys work well, whereby I can see detection and unsharing of shared zeropages. Based on Linus master. Andrew agreed that both patches can go via the s390x tree. v1 -> v2: * "mm/userfaultfd: don't place zeropages when zeropages are disallowed" -> Minor "ret" ahndling tweaks * "s390/mm: re-enable the shared zeropage for !PV and !skeys KVM guests" -> Added Fixes: tag Cc: Christian Borntraeger Cc: Janosch Frank Cc: Claudio Imbrenda Cc: Heiko Carstens Cc: Vasily Gorbik Cc: Andrew Morton Cc: Peter Xu Cc: Alexander Gordeev Cc: Sven Schnelle Cc: Gerald Schaefer Cc: Andrea Arcangeli Cc: kvm@vger.kernel.org Cc: linux-s390@vger.kernel.org David Hildenbrand (2): mm/userfaultfd: don't place zeropages when zeropages are disallowed s390/mm: re-enable the shared zeropage for !PV and !skeys KVM guests arch/s390/include/asm/gmap.h | 2 +- arch/s390/include/asm/mmu.h | 5 + arch/s390/include/asm/mmu_context.h | 1 + arch/s390/include/asm/pgtable.h | 15 ++- arch/s390/kvm/kvm-s390.c | 4 +- arch/s390/mm/gmap.c | 163 +++++++++++++++++++++------- mm/userfaultfd.c | 34 ++++++ 7 files changed, 177 insertions(+), 47 deletions(-)