From patchwork Mon Sep 25 20:28:49 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: 14490 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp1467549vqu; Mon, 25 Sep 2023 13:34:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHQAEbeiIw0D7aGnDF3WYlhZIuTQGr144N6iDJw9YJxDpH6QMin9jFnX+5qs2Q97VaGXRCZ X-Received: by 2002:a92:1a08:0:b0:351:4f0c:b959 with SMTP id a8-20020a921a08000000b003514f0cb959mr1095793ila.21.1695674052634; Mon, 25 Sep 2023 13:34:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695674052; cv=none; d=google.com; s=arc-20160816; b=0Z4K+cwK8kRSSjLA+fPht0wPxstDoDTXhRAXlbyLC/lYWHUcHUaDSk70RAnHkouBfU I4jorP3CN4rLyquFDklE+p/G5Wl4uQzLnXmN4n72yZCOB3yNY2K9L+AJQngePzIHCsGi 6A7F8cJoHZq/uifr3EMoFivOIDE31VqB4QDG7GXCgjurYa2h/HpNEWZvBAfQxGJyaLaO LkqIO+zQ5h4zFWRIG+CjucmKoZ9AIIgkYyxIto1JKi/n6O7rNQR7M6Jxp4SKmrFSY+2F wVtC6CIhSd88lComtYy0TgXcNY6FlDb6HsbFU1br2UMyqWMasuSexp6aEn36VGFeNvc1 vLuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=XioVnARdLB6bwJ7fgwz2gXhIl2/LID0ZzLzrIMgjZHA=; fh=q9SQMO40xEi/EnFYzkYLxDJR2yDm5elEChUbTy2RFH8=; b=AOTbJ7OJNC9u/046K5uo+Rwt4C+6YzzVMYK0iJ6eAkJ+ZF8GYQTRxeWkzua9SmQmH4 zfOWwNSbpAndbyDwUbds+ZCLQ+9cx6kQJTfExgmRp3jib45MviJ4IO7A6/8phe+m930L x1W9liEjK8/Fp/bFilnuZyFvVUMPfqGsTEkV9ILc6Y7+/L0NWyMQk3Dp5u3HXcWH5TCe S23Q8S2Z0lNg4Yai1CQLoYboy8LKuV/ayIzhom/pZsdtK6Qh+bwcA6DsbeNbn1dBFRxn +Tr5KvFhVHdXZ9LJXxSDFyH/gP2dxNwDRRargBzQGYzWU0LPp8gyQuREZqt5FxuXY/1q JkHg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id e23-20020a637457000000b0057d08dac754si8201163pgn.671.2023.09.25.13.34.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 13:34:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 124B180BC113; Mon, 25 Sep 2023 13:31:46 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229852AbjIYUbe (ORCPT + 28 others); Mon, 25 Sep 2023 16:31:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229481AbjIYUbc (ORCPT ); Mon, 25 Sep 2023 16:31:32 -0400 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 249E010C for ; Mon, 25 Sep 2023 13:31:26 -0700 (PDT) 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 1qksDy-0006Gb-07; Mon, 25 Sep 2023 16:30:34 -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 v3 0/3] hugetlbfs: close race between MADV_DONTNEED and page fault Date: Mon, 25 Sep 2023 16:28:49 -0400 Message-ID: <20230925203030.703439-1-riel@surriel.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Sender: riel@surriel.com X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 25 Sep 2023 13:31:46 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778043115185562577 X-GMAIL-MSGID: 1778043115185562577 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.