From patchwork Wed Mar 27 17:17:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13607117 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56598C47DD9 for ; Wed, 27 Mar 2024 17:18:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE8976B009C; Wed, 27 Mar 2024 13:18:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9B066B009D; Wed, 27 Mar 2024 13:18:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C38AE6B009E; Wed, 27 Mar 2024 13:18:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A68906B009C for ; Wed, 27 Mar 2024 13:18:03 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E83F5A1325 for ; Wed, 27 Mar 2024 17:17:59 +0000 (UTC) X-FDA: 81943476678.02.404215E Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 286B818001B for ; Wed, 27 Mar 2024 17:17:58 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Orvc7TZr; spf=pass (imf06.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711559878; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=G8ccLk4IwuzIQJFrPipCiLzVcTODVh2CvV2jxW3RkMc=; b=a5fPcfn0CD144/w5oUPUwmP1SZ5A0u1R5Ky16klMVIYmfQj42xgFmd3VwbTYaBlI0XO4ak B+/zKqz3hb1+SHjp7CThjbvBF3V+xe0IgApmyda8WkkbZHijNOjFKo2Vhm8UC1VBl7r45R bCnSC2riDZKgpNJts4bRoB8DaXNKB0I= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Orvc7TZr; spf=pass (imf06.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711559878; a=rsa-sha256; cv=none; b=rO8c9Xhm7RENm0HfBok72Sb4y7vJ2B4s/0lU6d5zfhixYGQNZxmMsL9wKNyZfo6dh/iyCL wFOCxghp/Ynho3VA+fSSjmlr+BwUIby0rnPHyhfnChQGiz10Xxm/DlMjY4H/OkOz7TUysu bqN5NOHtd7W1oCjoXRnyiBfGILIqrWY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711559877; 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=G8ccLk4IwuzIQJFrPipCiLzVcTODVh2CvV2jxW3RkMc=; b=Orvc7TZr7RBLTBXSXoz7sA1sdhrpe883B6ZsVs3tqe+FVdkCS+HDhEg7D18u4gXrH4OGXF rlCqYFrDp82USUFRSrLJJ92pedY5usdBnCtqwMavP6a7ltQI/wgS1nWcCgRMlgMYTo/svk +TbZ3ydqmi7PyO79CXhN3EnWm34AAcU= 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-551-UN5Oy67YMRqj2AlkFHDtAg-1; Wed, 27 Mar 2024 13:17:53 -0400 X-MC-Unique: UN5Oy67YMRqj2AlkFHDtAg-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 050523C02B6C; Wed, 27 Mar 2024 17:17:52 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.193.208]) by smtp.corp.redhat.com (Postfix) with ESMTP id DD695111E40C; Wed, 27 Mar 2024 17:17:48 +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 v2 1/2] mm/userfaultfd: don't place zeropages when zeropages are disallowed Date: Wed, 27 Mar 2024 18:17:36 +0100 Message-ID: <20240327171737.919590-2-david@redhat.com> In-Reply-To: <20240327171737.919590-1-david@redhat.com> References: <20240327171737.919590-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Rspamd-Queue-Id: 286B818001B X-Rspam-User: X-Stat-Signature: 71488rfnaqzywpnh8ostq64dinzj44ru X-Rspamd-Server: rspam01 X-HE-Tag: 1711559878-270103 X-HE-Meta: U2FsdGVkX1+xobac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FPO9Han/brD8/lcGBdrYSWBL/hsOXfQq1lqdwhTTkuUUP7Xs1ymkpFNSgAST56om9haqLPfT4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 712160cd41ec..9d385696fb89 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->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;