From patchwork Wed Oct 4 03:25:02 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: 13408257 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 C29EAE8FDC6 for ; Wed, 4 Oct 2023 03:28:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0CAF88D0003; Tue, 3 Oct 2023 23:28:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 07B4D8D0002; Tue, 3 Oct 2023 23:28:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E858C8D0003; Tue, 3 Oct 2023 23:28:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D62768D0002 for ; Tue, 3 Oct 2023 23:28:24 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A3DDE80523 for ; Wed, 4 Oct 2023 03:28:24 +0000 (UTC) X-FDA: 81306346128.28.F60AADB Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf26.hostedemail.com (Postfix) with ESMTP id 2E3C7140015 for ; Wed, 4 Oct 2023 03:28:22 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; dmarc=none; spf=none (imf26.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696390103; 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=/zHmMkPgzKcyRWwi6WQNRMOs20bt61liBgfsWbPu8N8=; b=i/nQbpudklehbBagxElegt8UKp6KvXVcje66Qz4wYFnCl9yDvdRmOObOYPpHT07xCLkP/a pj0JOFXQY3qcgcA7yqR+k6n1GUADACYZYia6lAb80D7qMQFGubFCYFOTeI72dQjXoG9Drv 6T3KRYoYluL8U8v53L0Y/vkqaUcMz7g= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; dmarc=none; spf=none (imf26.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696390103; a=rsa-sha256; cv=none; b=OvVZ/RgeKVKqu+s1o1BRhxjtO3T83aKgahLWVgp9hRW0hvyvPO830iUNVokr+2lbieIt+t 1mN8eXWosSnjQOdvV2PojHNrHiOdxM/D8AWel3aQLP7DBWbM3S2hhbny/5C7xRrHdZBhm/ /DwCfJZ3+/Ojc8ZzVJFsvbvoICsvZsE= 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 1qnsYb-0005Bb-0H; Tue, 03 Oct 2023 23:28:17 -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 v6 0/3] hugetlbfs: close race between MADV_DONTNEED and page fault Date: Tue, 3 Oct 2023 23:25:02 -0400 Message-ID: <20231004032814.3108383-1-riel@surriel.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: 2E3C7140015 X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Dv1t+K953v5IVF2jxml ozmcxdPN M/iHkiQWGqYbQvewSgVFdRpxdf+70OIkVlfufGREmhrS/jyjjZq2G3qpEyP00rh7TLqaaSCvgVncK1IPOZ89+ANHWc4oifdKXkzbBw0eYeCBjSyFIwx7NXX4e6u8o5wzpTMLBIhcXNPa1thJK2llSpAHrmeW3Q2tcFsxvCL7ML1qEQ17Xee2qwxDQXfWY83HcmP1+4CfnRHEnjV7G51JsO8lRJdH71Wry1ysbgUI2uUVoEWLQxJXueZTN/9LHVpxphEjSoRFWZk6BUuoOzy3o2L/0eg== 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: v6: move a fix from patch 3 to patch 2, more locking fixes 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.