Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp760052lqp; Thu, 21 Mar 2024 15:00:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXyMEtMLQrVYT40P9afKNposWD7ZMd5TQzCtxYKvNatHxTDOyqUs1lMcTZfHagI6KYunpsBtnb0r5Xh26HFNZohhQTXkDvDlQsoLJN4SQ== X-Google-Smtp-Source: AGHT+IEDBzcMiHxbdmvL+xcmsihEHS7IO4ooUpRhau050Z1+CUhsWPm3Yo0UKf3pceQG6ctVNgo7 X-Received: by 2002:a05:620a:568c:b0:78a:312b:be0e with SMTP id wg12-20020a05620a568c00b0078a312bbe0emr567425qkn.11.1711058439105; Thu, 21 Mar 2024 15:00:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711058439; cv=pass; d=google.com; s=arc-20160816; b=p1N4LuPsXKZPGFX2gjw+9UQR2FwS55gh+R9L9NYSAT+8mdvJbX/GGpi7+1foJYN0T1 6V0HtOqwrpm2JaX0QzmCRRybCNHiSVKTO3O4gRnwCUk2/XRd0FfpS67MScrGF5We7Ji8 rhs5DVGFmy4sbrPKx8liof4u+2B7ESPJw6Paj6F48JlrzCvHMoI9fiSt+PtNPyg+npx4 pB0p+10o5HKYX6qa24WP2zQxLMdZ4ccsJUspozmpSOrmw5MeSf8i+wYkagGqIBS63THS 8YDLmoD2akj0RhoyEXGZIvELPwNULOOP0+MONZm/1sdZz2Apde3sUp7iMSoFfKscV04T cHRA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=nBGES3csgpwp5vpSi4JEYoorjMw2XPXljjFYfhTZHaY=; fh=lTN169O21rOOr7J+7lxx3V02qSPUFVwSLBMTyvj5SSQ=; b=NeXZNKdkV/3qZaoPROru80pwEDSexEfIBdKe90HDFZltcVYJeym8gVkf605/+lFXwF fj1d4MbbQ8d18dgt2qtUpNxOlzZcb9WjD3uetJv0xxYPkOXhfx3Bw3+xRWATHE4qnI/V Lv/X6cfVY5jNaqKp+mLnz4DRNcWn74xwTP5TL6M68hisabrVXJnn+mQ7djCqBwZAUTUs rEwnKFH4zYhMKILPQxLQ6j8ZiX6q9N5mImBSS4n/YNZScqmpFSKniWi1wGdAN2R3+1Tl XJeLjqCsTC1OdpN/H2CqrxpjbdwZ17/fGUlgyYaq4RJYgI+9QINWa9xj2AJ+pLbroPlU c7Ig==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bmDjklXs; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-110715-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110715-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id du16-20020a05620a47d000b00789c69aab5fsi695686qkb.15.2024.03.21.15.00.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:00:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110715-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bmDjklXs; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-110715-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110715-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id CD97D1C21FA2 for ; Thu, 21 Mar 2024 22:00:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3C0CC1369B2; Thu, 21 Mar 2024 22:00:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="bmDjklXs" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90AD1133998 for ; Thu, 21 Mar 2024 22:00:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058414; cv=none; b=nI68XW6jngRS8uR+nanRHoI/1k6ttuvWN9M2mTF6svi20eU2ME4ioIojVbtqgLM/LbkjJoNeg2oSvkEV8TLxlwlPpvbxB38/WcgF6fmGPuE5WJBhPFjANXN41kI8Dq3fBPN4mff4qrjxKN66KsiyicwSfB7oBzWfJADmZ53/EJY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058414; c=relaxed/simple; bh=MUnq4/9M9vqTEBi0VBbrZcfbKui9X18koTYZs2S57UU=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=TZIiZiyh8L7gALq0NaWHR1lj4rnGQQJXsWRJB1TIcH9DNlycdryeGL0gP7K5nbvRxcD8hE6N0z17lgMMn+amD/WFvRfQEKT/lBO61o0fKxPFmyuiJVjZh+BMP48Zwweid0EVVoLkneubgCJpT7Cj/r90N2Lgv6lrRaIWBZkJoN4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=bmDjklXs; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711058411; 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=nBGES3csgpwp5vpSi4JEYoorjMw2XPXljjFYfhTZHaY=; b=bmDjklXsmlJRFG9K6OJKt53nHePLQRIeh3/Du0GX3tvoiS6uwedpJ6g3CVCEuihdIpLoE6 /lr45PHnU7w+U9S5ZYKDCXoEJIkqsPmC9eRw/D9UcnQwDYkpTZeAza4V9cnthvDWYdRecJ NS4ORCdUNX7vwhjPeqa/ES7kUZ6zxBA= 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-654-QPBc37aMNa2ekgj5rzr0ow-1; Thu, 21 Mar 2024 18:00:06 -0400 X-MC-Unique: QPBc37aMNa2ekgj5rzr0ow-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 28A5E185A781; Thu, 21 Mar 2024 22:00:06 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.192.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 91CEC1C060A6; Thu, 21 Mar 2024 21:59:59 +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 v1 0/2] s390/mm: shared zeropage + KVM fix and optimization Date: Thu, 21 Mar 2024 22:59:52 +0100 Message-ID: <20240321215954.177730-1-david@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 This series fixes one issue with uffd + shared zeropages on s390x and optimizes "ordinary" KVM guests to 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 current mm-unstable. Maybe at least the second patch should go via the s390x tree, I think patch #1 could go that route as well. 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 | 35 ++++++ 7 files changed, 178 insertions(+), 47 deletions(-) -- 2.43.2