Received: by 2002:ab2:7988:0:b0:1f4:b336:87c4 with SMTP id g8csp98371lqj; Thu, 11 Apr 2024 10:53:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXXEcSua2V8yqHyXTbc46ylrRmOmtNVJlTzha1g0PDNp2YBMAtfJJ1qtMHtaINiee3kpUMfx0QwijxEpkT1lsTmkie8aFUYS3z14dYb7g== X-Google-Smtp-Source: AGHT+IHYYJgd/DGXJ5Z/NdKke80vWDav3RnXa4Mj20CLUIasmSw8LQPUAUTU+uVErm1Oe8gDZPIv X-Received: by 2002:a50:d594:0:b0:56e:c16:fbf2 with SMTP id v20-20020a50d594000000b0056e0c16fbf2mr317656edi.23.1712857993427; Thu, 11 Apr 2024 10:53:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712857993; cv=pass; d=google.com; s=arc-20160816; b=uhLPPn8uRvG5qrmWiZiDP+pRE6/0aYVY0J+IXKDl8poxB+t549NGOKp18CPdJ7xFpk ERJZXf3LTOX3mh652w4H0GfnTgv6+GOfH6GOS7i5t40KuMJ7AwFmLF7//u9hPNTepJh9 EfcTVm0RN81FEDGLVk0hc6KxXdyygSbm32L1GmOCoQf0G1m7p7bOOBnDlS5fnpzFaaZS RyxMPB0ls4Mk5yTcRxUa4uk1wRldtLSydHtSu+kPUPPc17oAMPW9lxMtWbh5AW1Oyj6+ 53kJ8TgBhvUuRCdQy/6EHXOE1ghFU8VT+3/nhc/dVLfeE753gaGY44o6wRcerVq2qP4C FXdQ== 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=5Gc5EzQD4KCJRDKgqgJn1MWb8b3Qcq3MV0Fega+6Dww=; fh=lTN169O21rOOr7J+7lxx3V02qSPUFVwSLBMTyvj5SSQ=; b=JTw8OQZpnxz9bItAocxxk/m6Livvr4DiVkQ3JG4BrJOwmjVlLle0V+uXH7bky2Gy5K Pf6am/uLEsXaNH/pXC7//6KMNTBkosdZAh0MdJWy/bGe+YLNu7asUtBtl2G6WDXDgJvP 41Z9ivsbs1UQJ1zcBknPI6/zDGejrRoBF5USlfpPQEnoQVhSaeO8J2rZzNbqTXsvm+5u RQ6wTduaV2Ks3h7tkZXCN4dAXdGpSvqXfFgdkb+nbRLz/UBxzQOQPzce0Jv83dKwa61x SB+uJuhsuRzxc+w2ni85ct0cqq0VyC5SfMBMUORW5ruDqN6RITkjWqeyyFFp0mMcxUKk V2+w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=hBl09Lms; 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-141302-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141302-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 h15-20020a056402280f00b0056e679d34d8si985136ede.487.2024.04.11.10.53.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 10:53:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-141302-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=hBl09Lms; 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-141302-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141302-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 11EF61F27C99 for ; Thu, 11 Apr 2024 17:44:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 948A515E20E; Thu, 11 Apr 2024 16:15:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="hBl09Lms" 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 033F515E215 for ; Thu, 11 Apr 2024 16:15:06 +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=1712852108; cv=none; b=ddmuy+iK+aW0YPAQ72N18sO0CGFBKLma3qeuh2Z2WvDm3M7mVn7p4HseY1W5GrqTZPehS0CpxlID0SotZBx/HpkVLWX5wAKkeoTq1cXztWM1Jx1XwN2p1m75igP8bvcmJ+RGLXPMbogEA3G3fhp3xIlhGjo9z5HHl36g9p8T1GU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712852108; c=relaxed/simple; bh=EffdTTq97YcuRZ8vDwF+UAMXSPioZwk+HMzUFS3/S+I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gF37Awl4/OY4Kc+Ucf8kuiRi56VjqYVVsO8xnrzG1ahTdxlbkB4O10S2ZPkb5MJrljgg0hUOrDTjrBorDNmOq1fjbG2XI0quVAY5kw6IDI8mwpXr+yugmTbhTX0ts1FnbHiWCt8l+D1WxtbGm7yMr0icBU66Qql1dymZqOvEh+0= 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=hBl09Lms; 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=1712852104; 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=5Gc5EzQD4KCJRDKgqgJn1MWb8b3Qcq3MV0Fega+6Dww=; b=hBl09LmsIRR3V4/Mq8krhx0zvvKV5YWnGs7D9u8e3FWxBKrNwbidX1Vr8QNP7+IB4CBpIl TyOwkiETZJX224w+fTZiXDykGs2jUEOMebfqty1sizJGAAi4j8MDSnFc/F059ixoOY4A7G uC+UaXL3PdkXafgEHRGST/0ONz3iBzk= 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-237-AyIYVHnbPImCEUVwQUyQAA-1; Thu, 11 Apr 2024 12:14:55 -0400 X-MC-Unique: AyIYVHnbPImCEUVwQUyQAA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 926B71887313; Thu, 11 Apr 2024 16:14:54 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.194.173]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8FC1D1074E; Thu, 11 Apr 2024 16:14:50 +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 v3 1/2] mm/userfaultfd: don't place zeropages when zeropages are disallowed Date: Thu, 11 Apr 2024 18:14:40 +0200 Message-ID: <20240411161441.910170-2-david@redhat.com> In-Reply-To: <20240411161441.910170-1-david@redhat.com> References: <20240411161441.910170-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.5 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") Reviewed-by: Peter Xu Signed-off-by: David Hildenbrand --- mm/userfaultfd.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 3c3539c573e7..7eb3dd0f8a49 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -316,6 +316,37 @@ 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 = -ENOMEM; + + folio = vma_alloc_zeroed_movable_folio(dst_vma, dst_addr); + if (!folio) + return ret; + + if (mem_cgroup_charge(folio, dst_vma->vm_mm, GFP_KERNEL)) + goto out_put; + + /* + * The memory barrier inside __folio_mark_uptodate makes sure that + * zeroing out the folio become visible before mapping the page + * using set_pte_at(). See do_anonymous_page(). + */ + __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 +355,9 @@ static int mfill_atomic_pte_zeropage(pmd_t *dst_pmd, spinlock_t *ptl; int ret; + if (mm_forbids_zeropage(dst_vma->vm_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.44.0