Received: by 2002:ab2:7041:0:b0:1f4:bcc8:f211 with SMTP id x1csp181278lql; Fri, 12 Apr 2024 07:23:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVbR0H7fq8R6id7b0u0LZbdiNzaCdwLC0ozH83wgwRfWZS4Rw6WMqORakA3JSb7KlnN0fuYC8EDKvaxPHxtHmjm4233y+pkFOPlEYde9Q== X-Google-Smtp-Source: AGHT+IHX/E5XMZo+nIJKRnMny3Rso1cp5A9lgrqUNXEI3OFVu8kL58b64ROxnKRliAjFBaPmbYdq X-Received: by 2002:a19:5e48:0:b0:517:8636:9461 with SMTP id z8-20020a195e48000000b0051786369461mr1570265lfi.15.1712931782405; Fri, 12 Apr 2024 07:23:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712931782; cv=pass; d=google.com; s=arc-20160816; b=MXcNq3/0SjMxXiLe4ZNmzmPE4XgRwYj8/ViwHW6kRZtD4WxN09UM2amT1Oq+xM7U6N tC5qcAeOInG3+OkhmFd3VyQD11IHpY93k6c2lhU6YK5wetoAhLobPsZwKRjDGkFnQPLL 28Ezh+IfKZXC1Y43j9W92TIxL2cS+3zEMlM3L7egAATWH2ELrdDT0oMiWmdgODl4iNcf 5zqTPSEN3u9U/fQv1yQYBaAzd00d70LyAtv1JPL1rSvItpyyfDYa/f95ryhGFii0nUlA 0XigMDo+ANNBwG5MuxRRy9fkoxxq6o7xV0j+BtmG8ZjsVAhX6Bz6HzvxDp9ptbt5p7ev kb9g== 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=UqesERZ4v5DDEN2i8HY7U82j76k04tcHkpQ5V/iITyc=; fh=A+QcyawwK6Yq8daxuWFm+g/417ivXyGCFoP00qnMPro=; b=0CIUkEGha61luF1zclg4KOFSnUX9uHx7mRFvvWh60sXWEcPVJ51lgSNMZzVsk4oZQP WrLjGT2+Y9Y64NEJRpye5VUOuzoXNXxyb1xXST4/cDqbIJAS5+pKPa4DHY/gqqsuJSno EzDmXjNA11uiutTBG3YuAoubEYY6sKJWm0qJAckoybjhEUdPFz4yhEnMl10EaaqL9uVK SSzO3rIuWFU18qN0b83RXjmhTZVOOVE2tql9+6Vj65Jt1XIvhlklP8XaJXlR+hgs9CFX GOnB06I/F6TRYwJYo0nznNfopcCZieIsVCmcyx0Qke0d0ST74Lbb0dQksAvhniDEvhht 6BzA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=N8LMaGQh; 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-142816-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142816-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. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id qf41-20020a1709077f2900b00a463eb97691si1871855ejc.350.2024.04.12.07.23.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 07:23:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-142816-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=N8LMaGQh; 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-142816-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142816-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 EC1761F23764 for ; Fri, 12 Apr 2024 14:23:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9332512BF31; Fri, 12 Apr 2024 14:22:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="N8LMaGQh" 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 F413212B154 for ; Fri, 12 Apr 2024 14:22:01 +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=1712931723; cv=none; b=IMfgLJFouyuKgVzDNyv+7AdXuIhBVErmsQcWbL8pOWSZkjPMasybxy+VI/OxiVsot9Ivgmxq3hJj3jYlNLLJbnZhRYLv/sNPwVp3O66ec24k83dTewcvkBxb62SSdZxWOIwm8FCY7gdzhoDaybpL/X94r36799ZkCe8+wN/ahng= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712931723; c=relaxed/simple; bh=6fBfLn1F0T/o+E+4HUx46WIsuzu8qJCxEFjOoOqxYuM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OpMLn/nBtH9PIIrCsyaOFyf4h1DQclsuBefuR0e8i/TdlqDVOw2aoOEhOid911sh7Pv0+UV/WdCqt0pORejWMBFWQfqLL6DxiJqteUA5W13HN9HR+3AdfcDKpM6VfcNI1CDmtL7K3Ug+dLVjb0YlLixYF1mS9VKfEANjMFLy53w= 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=N8LMaGQh; 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=1712931721; 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=UqesERZ4v5DDEN2i8HY7U82j76k04tcHkpQ5V/iITyc=; b=N8LMaGQhQ12hxn64Pq87Uoeeh7kppEyhF4dVRUjlMME1B2rjXoNJwKaACumAmZVxEezJ1X dpdU8VGVeKXN/4Qozgvn9Tyg5NyALoMv/Z7sNJ3vkFCIrd850B4tmDCwtUIjdvk9w2t0w0 4JbZbdQzffX8ZRe0iQRKowl61MHiXD4= 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-66-3RdVJAd9MG2A_gljV7dFoA-1; Fri, 12 Apr 2024 10:21:57 -0400 X-MC-Unique: 3RdVJAd9MG2A_gljV7dFoA-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 267DF1044572; Fri, 12 Apr 2024 14:21:56 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.193.165]) by smtp.corp.redhat.com (Postfix) with ESMTP id A38B940C6CBF; Fri, 12 Apr 2024 14:21:52 +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 v2 03/10] s390/uv: split large folios in gmap_make_secure() Date: Fri, 12 Apr 2024 16:21:13 +0200 Message-ID: <20240412142120.220087-4-david@redhat.com> In-Reply-To: <20240412142120.220087-1-david@redhat.com> References: <20240412142120.220087-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.2 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 condiitonally 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.44.0