Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp686877lqo; Wed, 8 May 2024 11:31:14 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV3WDEtS+H/0e8Dm1hNiAiTv+igcvRRtEAQ/Qh9jTUUKSN5/qWGEs5Tl4LDdD52LonPDc4hvQtFr9MVIjXJnqx2d3C0/GyQp+fdlBVn7g== X-Google-Smtp-Source: AGHT+IHGOBhuJmJjyLxKaUc2kIOrNJFcaVANZwjope1SIU68t0GtVRYYBTSOKF1VmFieJobk0WTc X-Received: by 2002:a17:907:3f19:b0:a59:c23d:85d9 with SMTP id a640c23a62f3a-a59fb9f4c98mr237921566b.72.1715193074364; Wed, 08 May 2024 11:31:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715193073; cv=pass; d=google.com; s=arc-20160816; b=Q99aKCkk6MPq9bdoipjR8/B1babIdomLmIUxfQRCJVbqXj3oYs10iV9j+jqTfKizpf 0HQeyi4aKrrCbbIr5drEH+CXiKcwoPUhPpa898uKjupu5HqcIaMZ50VPruNoQ9bflMuT GqtjpKXWTerMCy7bMhVtilcjHOwfLnooCDugfsYUmh6MsnOclnRWFAiRrlO85CaKeWIY A91T8CLlzmadqcMEzEnpjL/DNAYkk9AgN2G0XFesXBrlXX2HX7pO6vwojcyDZ1cKQVa8 ds1Gk/wWaYNlIFbCNHk4dCHM6lNpwiRXF8I+brnqVhKHxk77A+xxW53iBANoO0DQtif8 6I9A== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Z0zJtbHvVl56vNC8QA+cdVl06TDILXjVZh0VBjwQ4rY=; fh=A+QcyawwK6Yq8daxuWFm+g/417ivXyGCFoP00qnMPro=; b=mAFOZhuD31oXVk8r7F7qHmjE+pblvIqciQbjxHi+o1HFSZZo1L89XQOqOpiXEj4Jsi egXjQklSY64tNumu4zmel/jK47+B562KxaNiSAoOZxLZzDR7YBgsaI6m+I/YVMbe7CuU HvDJwQdw9Yo0AdQIgtNjhb0dBoaaDWHLL58Rz++xQQbEpKGKDNWzZvQWIGLBjX7CRC9M 3ju3CpUnfuyToaMRygfA884C9S6okieHGPH8Dcd6acwFl+J5BPQqylGLeC9po9xf657l e4aDOfmMlhKvYcBVGNKdgOG555uw3alq4Tk3WeW4eD8LpVQemcqMxQ5jVtO8F1o5pTJG G8iw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SrskzZLI; 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-173704-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-173704-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id ds22-20020a170907725600b00a59b7305ce2si4961273ejc.604.2024.05.08.11.31.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 11:31:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-173704-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SrskzZLI; 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-173704-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-173704-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 9CB831F25148 for ; Wed, 8 May 2024 18:31:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4C741130A7E; Wed, 8 May 2024 18:30:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="SrskzZLI" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 E538712E1E5 for ; Wed, 8 May 2024 18:30:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715193019; cv=none; b=TFp2Y1oOJHYBg9G3FLMIzVHMlLZZWcnXU7gnRgujsP03y86JA2KVLOtdfEClZXys40H81Eay2YDfFE1/7Y+Uio8N8j65dZa+t+Ldk9hQQNw89W9z+YW3cCEX4wuPIg2euLbLV2dvtoNTZ5pwMiFzOyfuVBBngfFt/I3nCGCRAm4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715193019; c=relaxed/simple; bh=4Ant1hPx1kFJaIwGTzV/S4xrLjPsAStc05zlJcLsxfI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CCss7c6wcr1AUedTzzj7oY301TQwvQnNfMrTJt/XItpbR+y4QUp8ueTai/Mgt6PPJdyqUKTZM3Yc7/t6BlfFJEuo+n+x2uiqKVv9g83cJdrRhNQ/PruhUbIu+wRkxwfeoDaxWIknSBoSd77tL/J3kBwkCrc9VKdDEKBFdjSlBpM= 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=SrskzZLI; arc=none smtp.client-ip=170.10.129.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=1715193017; 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=Z0zJtbHvVl56vNC8QA+cdVl06TDILXjVZh0VBjwQ4rY=; b=SrskzZLIgNEd5CwAW37GKmx9aQyqYtSJwLsL3QwclWSIte9zCDvTDwiZxxs4BRYhojpZ1H BvBHxTkaVr7AgOGka84Kl5LBn+FK69qiE0aizyi1xduN0Feq9dVHQS6ztWgSC3G6ATMNw0 UXKu9TdeP/5vL5MBq3sZOVSdI6kcIA4= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-663-Z4KsAZmoMXSB97FxwWbamg-1; Wed, 08 May 2024 14:30:12 -0400 X-MC-Unique: Z4KsAZmoMXSB97FxwWbamg-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 0F9193C0C492; Wed, 8 May 2024 18:30:09 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.63]) by smtp.corp.redhat.com (Postfix) with ESMTP id 121C010009E6; Wed, 8 May 2024 18:30:05 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, linux-s390@vger.kernel.org, David Hildenbrand , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Janosch Frank , Claudio Imbrenda , Gerald Schaefer , Matthew Wilcox , Thomas Huth Subject: [PATCH v3 03/10] s390/uv: split large folios in gmap_make_secure() Date: Wed, 8 May 2024 20:29:48 +0200 Message-ID: <20240508182955.358628-4-david@redhat.com> In-Reply-To: <20240508182955.358628-1-david@redhat.com> References: <20240508182955.358628-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.3 While s390x makes sure to never have PMD-mapped THP in processes that use KVM -- by remapping them using PTEs in thp_split_walk_pmd_entry()->split_huge_pmd() -- there is still the possibility of having PTE-mapped THPs (large folios) mapped into guest memory. This would happen if user space allocates memory before calling KVM_CREATE_VM (which would call s390_enable_sie()). With upstream QEMU, this currently doesn't happen, because guest memory is setup and conditionally preallocated after KVM_CREATE_VM. Could it happen with shmem/file-backed memory when another process allocated memory in the pagecache? Likely, although currently not a common setup. Trying to split any PTE-mapped large folios sounds like the right and future-proof thing to do here. So let's call split_folio() and handle the return values accordingly. Signed-off-by: David Hildenbrand --- arch/s390/kernel/uv.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/arch/s390/kernel/uv.c b/arch/s390/kernel/uv.c index 25fe28d189df..3c6d86e3e828 100644 --- a/arch/s390/kernel/uv.c +++ b/arch/s390/kernel/uv.c @@ -338,11 +338,10 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb) goto out; if (pte_present(*ptep) && !(pte_val(*ptep) & _PAGE_INVALID) && pte_write(*ptep)) { folio = page_folio(pte_page(*ptep)); - rc = -EINVAL; - if (folio_test_large(folio)) - goto unlock; rc = -EAGAIN; - if (folio_trylock(folio)) { + if (folio_test_large(folio)) { + rc = -E2BIG; + } else if (folio_trylock(folio)) { if (should_export_before_import(uvcb, gmap->mm)) uv_convert_from_secure(PFN_PHYS(folio_pfn(folio))); rc = make_folio_secure(folio, uvcb); @@ -353,15 +352,35 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb) * Once we drop the PTL, the folio may get unmapped and * freed immediately. We need a temporary reference. */ - if (rc == -EAGAIN) + if (rc == -EAGAIN || rc == -E2BIG) folio_get(folio); } -unlock: pte_unmap_unlock(ptep, ptelock); out: mmap_read_unlock(gmap->mm); switch (rc) { + case -E2BIG: + folio_lock(folio); + rc = split_folio(folio); + folio_unlock(folio); + folio_put(folio); + + switch (rc) { + case 0: + /* Splitting succeeded, try again immediately. */ + goto again; + case -EAGAIN: + /* Additional folio references. */ + if (drain_lru(&drain_lru_called)) + goto again; + return -EAGAIN; + case -EBUSY: + /* Unexpected race. */ + return -EAGAIN; + } + WARN_ON_ONCE(1); + return -ENXIO; case -EAGAIN: /* * If we are here because the UVC returned busy or partial -- 2.45.0