From patchwork Fri Dec 16 15:50:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13075135 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 61AE9C4332F for ; Fri, 16 Dec 2022 15:51:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E642A8E000B; Fri, 16 Dec 2022 10:51:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DED278E0008; Fri, 16 Dec 2022 10:51:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C3FD38E000B; Fri, 16 Dec 2022 10:51:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B15A18E0008 for ; Fri, 16 Dec 2022 10:51:17 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 88FDA161163 for ; Fri, 16 Dec 2022 15:51:17 +0000 (UTC) X-FDA: 80248608594.09.ED901E3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf13.hostedemail.com (Postfix) with ESMTP id 716C020002 for ; Fri, 16 Dec 2022 15:51:15 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fFpGvVIO; spf=pass (imf13.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=1671205875; 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=KF8YBIr+SenpQMPOCGcYKB11m6lZYSBtX9iYqo4LY9E=; b=hEraS+PKenkG8/qInhiCUDw4lCJs8E4h3qn4a34wHCt8mtZXsP4jQsUo1Xb/ZgcnqQLkot IbrUIcWkLoOQCgRB8GGwh2MmsqvH4Vn7HkRpnmZUmX8Cn5YYLPCqk+5PNwuVZAG2tI1xOq HQcaUCJvyZZtdWJj/PL0g19j8Q3hOHM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=fFpGvVIO; spf=pass (imf13.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=1671205875; a=rsa-sha256; cv=none; b=j5ItWDrg/EnCwOzBF88oICV1iqgTGE5pig8N9yLUv95nVS0RBEmjfWmGIhENtFHnv9XPY9 Eo6zhvIj7lLIHPRdCEjNlVREqIoqaP3S/qwSjwXELHB//jgmBOwwU5dRb47stdfmrSyzJx qCEJFceTOTTFcg2xe9ia+LPo1/+4PW0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671205874; 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=KF8YBIr+SenpQMPOCGcYKB11m6lZYSBtX9iYqo4LY9E=; b=fFpGvVIO9F6Z+ib+i60gCNJkKYejSxMkfzCK6txsTHZnQGagCwP65sGrrFoHaQe4QS7McM RyNZoutjNdynSV4c48raNHZqopUU7Dz7jOanOexboKrN5giFZYz4YoiedmLBi8FeEDAbo5 5ZUdYQdHiOg+H9monTmyma24ZvWPQqo= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-259-gAvNHcyZPwyKlmZVpPge_w-1; Fri, 16 Dec 2022 10:51:13 -0500 X-MC-Unique: gAvNHcyZPwyKlmZVpPge_w-1 Received: by mail-qk1-f200.google.com with SMTP id bm30-20020a05620a199e00b006ff813575b1so2066751qkb.16 for ; Fri, 16 Dec 2022 07:51:13 -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=KF8YBIr+SenpQMPOCGcYKB11m6lZYSBtX9iYqo4LY9E=; b=cFn1VJeXjPYs2OT5ah1cugjsFF3OKiVV+daMpXdV+dsn04cdO8BMpmdH8wGH76ryYq 2/yneyKc7TKeeSPSkRrhilxgjc0rmxpo54kB3TI7A3Tp3P5XqmAqr71UAzB2FQB5ixPt rjnHfbI0eiyrB05TwU7OQnIh1CSAJ3rq414qdqOob7mhy7sQPn//LfJ7qgmXhPo4BNCE eBlOpK6HjH+boYdyjySOES1Of9+3HLW6T9Sy/B8ysqK7l+IasRe9IyWVaJc83hCqNrvH U/HPuJa4MCXNgfWX1st2+2OzLbuy+q8BAgiOe+nlQCSQug/07HekXQ5TeqdSRYpdioHb 43Mw== X-Gm-Message-State: ANoB5pm/imjFIbNrcikIN9tp6nIdZvEmuNXq/WLbFTibQixja5P381dr DfZmiBJe2IhMRFxD5Kqgu32ArD5BuXpFEeLX8cywKIBJJ0BezNwgZ0TREfik42Fmgq6nZlzXlZ+ 5vmS6h5go8ak= X-Received: by 2002:a05:622a:4d8e:b0:3a6:258d:4387 with SMTP id ff14-20020a05622a4d8e00b003a6258d4387mr42497434qtb.13.1671205873201; Fri, 16 Dec 2022 07:51:13 -0800 (PST) X-Google-Smtp-Source: AA0mqf5Jn2Uhau9BzqMwoo3qPaHalj0PlAXaiA0mx+xscLlNHjTHI5SgMhkHZ6FN0+QlzD5tFFX+mQ== X-Received: by 2002:a05:622a:4d8e:b0:3a6:258d:4387 with SMTP id ff14-20020a05622a4d8e00b003a6258d4387mr42497407qtb.13.1671205872924; Fri, 16 Dec 2022 07:51:12 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-45-70-31-26-132.dsl.bell.ca. [70.31.26.132]) by smtp.gmail.com with ESMTPSA id s81-20020a37a954000000b006eeb3165554sm1682297qke.19.2022.12.16.07.51.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Dec 2022 07:51:12 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Muchun Song , Miaohe Lin , Andrea Arcangeli , Nadav Amit , James Houghton , peterx@redhat.com, Mike Kravetz , David Hildenbrand , Rik van Riel , John Hubbard , Andrew Morton , Jann Horn Subject: [PATCH v4 3/9] mm/hugetlb: Document huge_pte_offset usage Date: Fri, 16 Dec 2022 10:50:54 -0500 Message-Id: <20221216155100.2043537-4-peterx@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221216155100.2043537-1-peterx@redhat.com> References: <20221216155100.2043537-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Queue-Id: 716C020002 X-Stat-Signature: hbbp6rut3fn9jocmsphxc6dqsseea83s X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1671205875-527151 X-HE-Meta: U2FsdGVkX187o7QD2jm3f4ANKIxy/G8TGXbECNrisKYiu86KnfRvNZrjnCjzESmTF7mOkYcjuBB+ZoJyRs9Kbd9sJ6Zj8BaxDt3i45Jv3J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nRA8h+ya6iBYHX 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: huge_pte_offset() is potentially a pgtable walker, looking up pte_t* for a hugetlb address. Normally, it's always safe to walk a generic pgtable as long as we're with the mmap lock held for either read or write, because that guarantees the pgtable pages will always be valid during the process. But it's not true for hugetlbfs, especially shared: hugetlbfs can have its pgtable freed by pmd unsharing, it means that even with mmap lock held for current mm, the PMD pgtable page can still go away from under us if pmd unsharing is possible during the walk. So we have two ways to make it safe even for a shared mapping: (1) If we're with the hugetlb vma lock held for either read/write, it's okay because pmd unshare cannot happen at all. (2) If we're with the i_mmap_rwsem lock held for either read/write, it's okay because even if pmd unshare can happen, the pgtable page cannot be freed from under us. Document it. Reviewed-by: John Hubbard Reviewed-by: David Hildenbrand Signed-off-by: Peter Xu --- include/linux/hugetlb.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 551834cd5299..d755e2a7c0db 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -192,6 +192,38 @@ extern struct list_head huge_boot_pages; pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, unsigned long sz); +/* + * huge_pte_offset(): Walk the hugetlb pgtable until the last level PTE. + * Returns the pte_t* if found, or NULL if the address is not mapped. + * + * Since this function will walk all the pgtable pages (including not only + * high-level pgtable page, but also PUD entry that can be unshared + * concurrently for VM_SHARED), the caller of this function should be + * responsible of its thread safety. One can follow this rule: + * + * (1) For private mappings: pmd unsharing is not possible, so holding the + * mmap_lock for either read or write is sufficient. Most callers + * already hold the mmap_lock, so normally, no special action is + * required. + * + * (2) For shared mappings: pmd unsharing is possible (so the PUD-ranged + * pgtable page can go away from under us! It can be done by a pmd + * unshare with a follow up munmap() on the other process), then we + * need either: + * + * (2.1) hugetlb vma lock read or write held, to make sure pmd unshare + * won't happen upon the range (it also makes sure the pte_t we + * read is the right and stable one), or, + * + * (2.2) hugetlb mapping i_mmap_rwsem lock held read or write, to make + * sure even if unshare happened the racy unmap() will wait until + * i_mmap_rwsem is released. + * + * Option (2.1) is the safest, which guarantees pte stability from pmd + * sharing pov, until the vma lock released. Option (2.2) doesn't protect + * a concurrent pmd unshare, but it makes sure the pgtable page is safe to + * access. + */ pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, unsigned long sz); unsigned long hugetlb_mask_last_page(struct hstate *h);