From patchwork Fri Sep 22 19:02:28 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: 14400 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5814476vqi; Fri, 22 Sep 2023 12:20:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFfuq8BoMyJvWhGjmXPJAORGL7rZUHyHHanEA5FAJC3uEWk4Ic2JgYQ6sL99DyydaHIdFiy X-Received: by 2002:a05:6a20:f3a6:b0:15a:592c:131a with SMTP id qr38-20020a056a20f3a600b0015a592c131amr485309pzb.30.1695410434751; Fri, 22 Sep 2023 12:20:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695410434; cv=none; d=google.com; s=arc-20160816; b=XXsQ7aKlcpIXuKxerRPIGe40uWSMag7GAoB4LzPd5Ckjhg/MhKLNEyQ7TRySGJYEli 1yiWiNCkfCJiYzeQ+BWnhYjQcscbb88pHUiEkEVxmnrnbZ1U/Rqh5hLXdU+TsxReni+Y jZvbfX2ylWu4FCwZBK4nCgQsJrA/a4YLmD2mc653AG7Yf+uV58LhEpL8wgP1nrFWd8Cn RSjX9A1quKZ+ThY8jNxbUxx22mEuQBMHHFnnPXO4Y4BI60a9Huw7uWskQkw6agkuNBDK 1GxtcjHzYrZBHHHymNTiYfQNwMBbXgVerbDSoe/KQsY2CNQ9NxY6B3fxhO7XyGnwIws4 +ndg== 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=ZWOBUzel0hTP5zlz7y2Skk8tQ+ei1TM5PAGkEPtLeRI=; fh=q9SQMO40xEi/EnFYzkYLxDJR2yDm5elEChUbTy2RFH8=; b=WFOTqiZeoS/0Rbee6QEzD0PiqhWyj8IAMnUzh0rkDbw5WeHRFypawFbBqk2TXqk1ET rzVwME+D4FTjyu3EeQ5jp0R7/GmzFSOnRhOow1dmVZHF3heq5vEK1JM6A4DahS2dzQjY w2fWYUVr+wEk7ca6syjCd9t4+3UioOIeTt7di5fUfZAF3juuDCncgwwmgvSraP6PZ6K7 2Ul8AuhL3EMRDKw5ZfIRcd2XAx+vLHSEVh4irEB7igsnhRpkmVxR8MQDOlghz4GUtO1P lgWGIlKfhkF/3FJjaiv61uXUjEorHxoO0jgeamCy3DVBze1LxNJPlX0CB9KUhLo53Mqp f80w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id m11-20020a633f0b000000b00565f24af893si4279106pga.22.2023.09.22.12.20.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 12:20:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id B581580A73DC; Fri, 22 Sep 2023 12:06:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233778AbjIVTG3 (ORCPT + 28 others); Fri, 22 Sep 2023 15:06:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233756AbjIVTG1 (ORCPT ); Fri, 22 Sep 2023 15:06:27 -0400 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E6C0AC for ; Fri, 22 Sep 2023 12:06:21 -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 1qjlTP-0002Wy-1G; Fri, 22 Sep 2023 15:05:55 -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 v2 0/3] hugetlbfs: close race between MADV_DONTNEED and page fault Date: Fri, 22 Sep 2023 15:02:28 -0400 Message-ID: <20230922190552.3963067-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=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Fri, 22 Sep 2023 12:06:31 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777766341909566476 X-GMAIL-MSGID: 1777766692232239132 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.