From patchwork Wed Jan 4 22:52:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13089174 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 AFF40C46467 for ; Wed, 4 Jan 2023 22:52:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C0A68E0003; Wed, 4 Jan 2023 17:52:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0726B8E0001; Wed, 4 Jan 2023 17:52:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7AE88E0003; Wed, 4 Jan 2023 17:52:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id DB7668E0001 for ; Wed, 4 Jan 2023 17:52:17 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B34BDA01AF for ; Wed, 4 Jan 2023 22:52:17 +0000 (UTC) X-FDA: 80318616714.23.CAA79C3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf10.hostedemail.com (Postfix) with ESMTP id D1EBEC000F for ; Wed, 4 Jan 2023 22:52:15 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hjZ2GtNo; spf=pass (imf10.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@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=1672872735; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iUQecF0p0KDaGb3ic+3dlRjRyqZfOtSt4XLNwuIXR0Y=; b=OqQAzYBBnvAagkTdkbjRD0uuNe405PklQ/8+3pTdlv71ckRcWCeMcOK7lyUJf/TPP3SiLl kEe1t9ID91SzTnLceTTTZRZDFrqT8ddWgOEjgijzPD4y+y4mYtjyZ2jbgeIWM0bIzndWNI UjYWOQfLzXnEekt4UXhf+iYLxrRAVf0= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hjZ2GtNo; spf=pass (imf10.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672872735; a=rsa-sha256; cv=none; b=LwXmHCb2CphdEgUWY/IYmmVLex33UohBOu88iEtNjDo4TlcZdwouEPy/TmZdGG3saUjv4Z F5MWqCKUnM0hVusyHgRkWZV0hcGSLb4YLxAkUHX4aFEGz5ptLlh/NtImBsCcQnN5Jy8Uso 9TKCA3cEBMnruyrsyrTJIX8pvnxG3X4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672872735; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iUQecF0p0KDaGb3ic+3dlRjRyqZfOtSt4XLNwuIXR0Y=; b=hjZ2GtNoQ6UeWrzJFIc2rNsYL00+stijIi8F5XWxNdYvVWYTF28FGP1vDoTQAQYVHZ5xXw 8ri9c9KUmKV6uTuXrhtYlUjlVAR0L8+q/DglzRuNHog2mToFJCOx7rrIcoJI0igmNPGxmS E+R2yn+dHbULLv5GidC/NKPvAYq+oaM= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-41-q7-ZY4wWNPWuScvdBAviXA-1; Wed, 04 Jan 2023 17:52:14 -0500 X-MC-Unique: q7-ZY4wWNPWuScvdBAviXA-1 Received: by mail-qv1-f69.google.com with SMTP id ob12-20020a0562142f8c00b004c6c72bf1d0so18224471qvb.9 for ; Wed, 04 Jan 2023 14:52:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iUQecF0p0KDaGb3ic+3dlRjRyqZfOtSt4XLNwuIXR0Y=; b=rD/oetmLbYn8k2gNIl1I8Xbvz+nam3xqx4L7u6nS2jmEfQzcT8xvJOVPfLyZya7ukq 3/48NR1TRAEr85PlCtNNuD1KtIEDvc7HEhDB/XAMdjp+p/tLhR8RSt93GqiLCHrWAnfw tEKFjL6tA5ZO1fLWn8ytFzjQOKHx+hrfLemTeVUXX6Kwxk9oGeB5lUyOVy+cMb6fnh5i QAvYtsxDnmm17K8UkpX/c/7rijgFdYVbvAhI2pVvw/8JWPrjBj+wHUsexBf732pZU71G zME6eJu1KvkjqUB2MDJCblqoPpiHBjnhl/gJpj3k1iWufvnU37eNmw3TO+lyfKqYKp6B m3Gg== X-Gm-Message-State: AFqh2krwcax90f4F7Yeg/H/1BdW3nHVnlP8JM4tGjEwzZKeH0Hd5TJz0 fB1i3IsV1pA8w2Gm+20qBHStkcbK4IsnpYf2eYMeD2FZo0bDFL60Mtf4FZV+TO0tjjPPi3nLxOM C0agJkrtAAtU4nYJ+Pyz4hpYL9q1CdGduUjOKfBe8qpqZphIdmtz2XlfWwB/u X-Received: by 2002:a0c:f3ce:0:b0:531:7b1e:cdaa with SMTP id f14-20020a0cf3ce000000b005317b1ecdaamr56474342qvm.44.1672872732944; Wed, 04 Jan 2023 14:52:12 -0800 (PST) X-Google-Smtp-Source: AMrXdXvpKKemP1V8IrIbogqNKolIRqZbJHS4f3LCuRtRb0RPSmf1nkmMiOVux8MqIUMl3pEtxplS2A== X-Received: by 2002:a0c:f3ce:0:b0:531:7b1e:cdaa with SMTP id f14-20020a0cf3ce000000b005317b1ecdaamr56474310qvm.44.1672872732634; Wed, 04 Jan 2023 14:52:12 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-39-70-52-228-144.dsl.bell.ca. [70.52.228.144]) by smtp.gmail.com with ESMTPSA id r1-20020a05620a298100b006eeb3165565sm24654710qkp.80.2023.01.04.14.52.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Jan 2023 14:52:11 -0800 (PST) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Mike Kravetz , Muchun Song , peterx@redhat.com, Nadav Amit , Andrea Arcangeli , David Hildenbrand , James Houghton , Axel Rasmussen , Andrew Morton , linux-stable Subject: [PATCH 1/3] mm/hugetlb: Pre-allocate pgtable pages for uffd wr-protects Date: Wed, 4 Jan 2023 17:52:05 -0500 Message-Id: <20230104225207.1066932-2-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20230104225207.1066932-1-peterx@redhat.com> References: <20230104225207.1066932-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Stat-Signature: zszw9nd47wjtpkcubtipren8j9495fsb X-Rspam-User: X-Rspamd-Queue-Id: D1EBEC000F X-Rspamd-Server: rspam06 X-HE-Tag: 1672872735-447869 X-HE-Meta: U2FsdGVkX19OTmfWybToCulhrqy/JxmGkRsgNfo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chr+qzGvDfuK17UAhAqDDMwGwDkzSZJpb1E0XpkvrvHgonxqHTz7ieCePUSQ== 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: Userfaultfd-wp uses pte markers to mark wr-protected pages for both shmem and hugetlb. Shmem has pre-allocation ready for markers, but hugetlb path was overlooked. Doing so by calling huge_pte_alloc() if the initial pgtable walk fails to find the huge ptep. It's possible that huge_pte_alloc() can fail with high memory pressure, in that case stop the loop immediately and fail silently. This is not the most ideal solution but it matches with what we do with shmem meanwhile it avoids the splat in dmesg. Cc: linux-stable # 5.19+ Fixes: 60dfaad65aa9 ("mm/hugetlb: allow uffd wr-protect none ptes") Reported-by: James Houghton Signed-off-by: Peter Xu Acked-by: James Houghton Acked-by: David Hildenbrand Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index bf7a1f628357..017d9159cddf 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6649,8 +6649,17 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, spinlock_t *ptl; ptep = hugetlb_walk(vma, address, psize); if (!ptep) { - address |= last_addr_mask; - continue; + if (!uffd_wp) { + address |= last_addr_mask; + continue; + } + /* + * Userfaultfd wr-protect requires pgtable + * pre-allocations to install pte markers. + */ + ptep = huge_pte_alloc(mm, vma, address, psize); + if (!ptep) + break; } ptl = huge_pte_lock(h, mm, ptep); if (huge_pmd_unshare(mm, vma, address, ptep)) {