From patchwork Sun Oct 1 00:55:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13405268 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 CAFD3E748F1 for ; Sun, 1 Oct 2023 00:57:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E21506B01F0; Sat, 30 Sep 2023 20:57:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DD0A46B01F1; Sat, 30 Sep 2023 20:57:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CBF096B01F2; Sat, 30 Sep 2023 20:57:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BBEF76B01F0 for ; Sat, 30 Sep 2023 20:57:58 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7F8E1B36F9 for ; Sun, 1 Oct 2023 00:57:58 +0000 (UTC) X-FDA: 81295080636.23.2AC701A Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf21.hostedemail.com (Postfix) with ESMTP id 04F111C0006 for ; Sun, 1 Oct 2023 00:57:56 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; spf=none (imf21.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696121877; h=from:from:sender: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:references; bh=q74vk6NjIVCnnLqm8nJWmYw2+j7Q4Kywks0MXA0rCTw=; b=F2VboH3LA3LpZNc4ht5+mJ6pi25tKozmQ7VlnpO0YEipCRCp2/8qKyu76weTVmN2wzAHdn 4RhRSQwJo4xvUSM1W/yu6pGEl07vmgpSTwp4zsmY/JGSetMSZavviQYrPF23QqawGA8u6I SOtu6ttXWZ6xoMRWbPROQVpWo8xLgg4= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=none (imf21.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696121877; a=rsa-sha256; cv=none; b=EESP8ThlREK4etJHqVDKQHah6/zZkdHMB9FybYW7VlgVTF5q0atP+bq8YETDLvlh5MTStq 93q5Oefb10rlp9M7rDesBRnQt5WrcTin4ZJ97WGnne/YyWxEPu5u3n1sthiYMKqp5KNZAs K59eK1cRlYEJkxvvq4T9zKliHUIqEnc= Received: from imladris.home.surriel.com ([10.0.13.28] helo=imladris.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1qmklZ-0008G8-0C; Sat, 30 Sep 2023 20:57:01 -0400 From: riel@surriel.com To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, linux-mm@kvack.org, akpm@linux-foundation.org, muchun.song@linux.dev, mike.kravetz@oracle.com, leit@meta.com, willy@infradead.org Subject: [PATCH v5 0/3] hugetlbfs: close race between MADV_DONTNEED and page fault Date: Sat, 30 Sep 2023 20:55:47 -0400 Message-ID: <20231001005659.2185316-1-riel@surriel.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: 04F111C0006 X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OJHB jQYxNflH 5CrwjtjE3oVXc/+RcWI3k9iDeGlW/1XNmKukGYfFfGUOLfe/xCAbJnC283hDAAPanB8wIUiwSi7CRUa4FyLWVYL0sHCE1gqecDtlrxwCZk6q4TwuCzdnGS/kUFbn+kfPgQUpM/ABGJxaiMJ5F+zdBO/N8e7J7byCwfhh2cA260y3bBSJ5QpGOvmKr7rFNRfIqTMm3jf2Ku2D947Zi5ePJmOvMfGVdWAjyRFmxU4IX9DkG6paChY7T3fD+bavwym3MLLoNCYKEZ0jr5Pt0Bj19IZPXEw== 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: v5: somehow a __vma_private_lock(vma) test failed to make it from my tree into the v4 series, fix that v4: fix unmap_vmas locking issue pointed out by Mike Kravetz, and resulting lockdep fallout v3: fix compile error w/ lockdep and test case errors with patch 3 v2: fix the locking bug found with the libhugetlbfs tests. Malloc libraries, like jemalloc and tcalloc, take decisions on when to call madvise independently from the code in the main application. This sometimes results in the application page faulting on an address, right after the malloc library has shot down the backing memory with MADV_DONTNEED. Usually this is harmless, because we always have some 4kB pages sitting around to satisfy a page fault. However, with hugetlbfs systems often allocate only the exact number of huge pages that the application wants. Due to TLB batching, hugetlbfs MADV_DONTNEED will free pages outside of any lock taken on the page fault path, which can open up the following race condition: CPU 1 CPU 2 MADV_DONTNEED unmap page shoot down TLB entry page fault fail to allocate a huge page killed with SIGBUS free page Fix that race by extending the hugetlb_vma_lock locking scheme to also cover private hugetlb mappings (with resv_map), and pulling the locking from __unmap_hugepage_final_range into helper functions called from zap_page_range_single. This ensures page faults stay locked out of the MADV_DONTNEED VMA until the huge pages have actually been freed. The third patch in the series is more of an RFC. Using the invalidate_lock instead of the hugetlb_vma_lock greatly simplifies the code, but at the cost of turning a per-VMA lock into a lock per backing hugetlbfs file, which could slow things down when multiple processes are mapping the same hugetlbfs file.