Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp760227lqp; Thu, 21 Mar 2024 15:00:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW1Ye789Tz9NAQL/bf/udiwCf3oXALtwlNfDzICoWm6iKPfyEE61ziVJMAPOoEEH8ak9MWCoIdmXUsiY2If8S8dJoFL5bRfqTV2lLYYWg== X-Google-Smtp-Source: AGHT+IEsmE43GGnCR7KrrAgUl04nE882LVfslEc838XEL6+wRYiYFMjM8EcxefHGonmV5J9dedZi X-Received: by 2002:a17:902:d50b:b0:1dd:8daf:99b2 with SMTP id b11-20020a170902d50b00b001dd8daf99b2mr776734plg.13.1711058456302; Thu, 21 Mar 2024 15:00:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711058456; cv=pass; d=google.com; s=arc-20160816; b=s4VO9zdWU4Fz57TZCNkNzgXSD0GqjpLPzCmblhV9q1PB4A9ONreYeAjMFgU09HUhD3 e8vUyrsZpQWrllm3KJJooXtGgVOApO55bO7iwn1gRBV9R2mqZ0SNuo+IWtQ9qzeAJ/iZ YbKlQ6JO5DjAQ3KthgNmTWdePp+mztSnGQYU7NCvzrz1cNnjZq485m2wPjfPjyrB1xoy ucYTSdkxa3pmmj+93ekq99iVXLhv21U7gjHrsMOmVKVOTVzxU1ML7gd8Gj0yEYS55lPz lSCKJaUm/NURKAZZthIWryobpxc7GtdbzYhM7T0KFuTHnKwmvqYUiukOvVKw62q+yyHa S/Dw== 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=I/TUj8l6v7W+3WhwmC9vyikgxzfujRZI8v2Khs5cepE=; fh=lTN169O21rOOr7J+7lxx3V02qSPUFVwSLBMTyvj5SSQ=; b=UlDG8ZuEXVyox6tJZaYVRU/FKQZuOs7LiQnjW5c03e3zaeS6XOB629tZ4+dhkxQBux KdkieX2fa27U0L4kn6JP6GeEldNjIPnIbiX8asHClrmWx/M4u3mnPiH/rJbm3zqvszas CVZFl0hGiGhCYXXY62WWuwVxZ4kYKxBnApTrQhOeF5IwKsE1PVs8onRdHgjNkTHo1GjE m8szBAgNbIyC2biJGhujKGWbtnKNjtWg//peMBadbI+Vl3WwrF/KH5kqTZ9zcKAbU3YT nmNs5fxyIgsl3PVi/iHlIiIX/C3ItOQjlCXtIK7u2L50cdcKGRZP3etU1DIJxLOYL5VD H+8Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UAu5OPjZ; 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-110716-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110716-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h13-20020a170902f7cd00b001dfff0cf80csi482182plw.638.2024.03.21.15.00.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 15:00:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110716-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UAu5OPjZ; 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-110716-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110716-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id F2F36284437 for ; Thu, 21 Mar 2024 22:00:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7570913666D; Thu, 21 Mar 2024 22:00:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UAu5OPjZ" 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 C8D04136661 for ; Thu, 21 Mar 2024 22:00:22 +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=1711058424; cv=none; b=FGmxl+lk3tpQI43uMIuPdViFyGbU1bktFnc7Km8ZsqiymuqdB79NzvMgBzkuL0Mu8lBnNW91iWPhTID01cCWFFRyF2sQICeeFDL1O/pUZfLef48S6uE3WIkLNTNLls/gRmVXeA6Oy6mIyFiMQA+d1hC2EOs82nkUdYfb55snpzU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711058424; c=relaxed/simple; bh=eLxkom/FL+0mjm5qC1owyEPEq3OFeYOTi1SX2LAANCs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ToSSa7D0emjmVKQza1GX3ECZHOT78mA0q2jmK4actINLUxtXuPNjX3pvEMM2GrcMGVUt2hZCtHT5r8guTGMWwW2eyH/XXtbAupzgnkvEJ1HbLuNDJS/U8zt2ZLgVIgoLgd5U6DzmWsvbRDyS4nK0a+uvDBgtUm+4DA7+rTEPUY8= 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=UAu5OPjZ; 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=1711058421; 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=I/TUj8l6v7W+3WhwmC9vyikgxzfujRZI8v2Khs5cepE=; b=UAu5OPjZnM1Xc9wuKwWsixoXSbDIeCuvLaAHPjzNSEKeb2f7M0v/5F4f5J+CLSWrXDz5Zg Riem6sPb7K0itbNoQpj7VtI4buB8NWNZ42MqfBW6jE4kPkB8krZmKZoBW7oV1eUZ5xOCay ujJpjgdFk6ll98GJzBrvOVtZLYyWU7U= 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-524-jcbu2gX6Mr6wqRm3UnAAQA-1; Thu, 21 Mar 2024 18:00:15 -0400 X-MC-Unique: jcbu2gX6Mr6wqRm3UnAAQA-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 32BB3803F6A; Thu, 21 Mar 2024 22:00:14 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.192.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id 887341C060CE; Thu, 21 Mar 2024 22:00:06 +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 1/2] mm/userfaultfd: don't place zeropages when zeropages are disallowed Date: Thu, 21 Mar 2024 22:59:53 +0100 Message-ID: <20240321215954.177730-2-david@redhat.com> In-Reply-To: <20240321215954.177730-1-david@redhat.com> References: <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 s390x must disable shared zeropages for processes running VMs, because the VMs could end up making use of "storage keys" or protected virtualization, which are incompatible with shared zeropages. Yet, with userfaultfd it is possible to insert shared zeropages into such processes. Let's fallback to simply allocating a fresh zeroed anonymous folio and insert that instead. mm_forbids_zeropage() was introduced in commit 593befa6ab74 ("mm: introduce mm_forbids_zeropage function"), briefly before userfaultfd went upstream. Note that we don't want to fail the UFFDIO_ZEROPAGE request like we do for hugetlb, it would be rather unexpected. Further, we also cannot really indicated "not supported" to user space ahead of time: it could be that the MM disallows zeropages after userfaultfd was already registered. Fixes: c1a4de99fada ("userfaultfd: mcopy_atomic|mfill_zeropage: UFFDIO_COPY|UFFDIO_ZEROPAGE preparation") Signed-off-by: David Hildenbrand --- mm/userfaultfd.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 712160cd41eca..1d1061ccd1dea 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -316,6 +316,38 @@ static int mfill_atomic_pte_copy(pmd_t *dst_pmd, goto out; } +static int mfill_atomic_pte_zeroed_folio(pmd_t *dst_pmd, + struct vm_area_struct *dst_vma, unsigned long dst_addr) +{ + struct folio *folio; + int ret; + + folio = vma_alloc_zeroed_movable_folio(dst_vma, dst_addr); + if (!folio) + return -ENOMEM; + + ret = -ENOMEM; + if (mem_cgroup_charge(folio, dst_vma->vm_mm, GFP_KERNEL)) + goto out_put; + + /* + * The memory barrier inside __folio_mark_uptodate makes sure that + * preceding stores to the page contents become visible before + * the set_pte_at() write. + */ + __folio_mark_uptodate(folio); + + ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, + &folio->page, true, 0); + if (ret) + goto out_put; + + return 0; +out_put: + folio_put(folio); + return ret; +} + static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd, struct vm_area_struct *dst_vma, unsigned long dst_addr) @@ -324,6 +356,9 @@ static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd, spinlock_t *ptl; int ret; + if (mm_forbids_zeropage(dst_vma->mm)) + return mfill_atomic_pte_zeroed_folio(dst_pmd, dst_vma, dst_addr); + _dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr), dst_vma->vm_page_prot)); ret = -EAGAIN; -- 2.43.2