Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp1035581rdb; Tue, 19 Sep 2023 19:19:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFn0RwlLGgvG2gmtodFNCPiLp5D3iCGfazeZuyiQAYlRxaEMueyNR2cuFfo3ORPMxMBqe9d X-Received: by 2002:a05:6808:210e:b0:3a3:640d:ed71 with SMTP id r14-20020a056808210e00b003a3640ded71mr1292430oiw.10.1695176354260; Tue, 19 Sep 2023 19:19:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695176354; cv=none; d=google.com; s=arc-20160816; b=ECawsMXfDqnXmw4YEhgrxRuhWW5fta5KY6jdHPMwdYQgOgybGXJLPE7PEBCD03pYvI T+EN+YAoYCVA8nTd+17i2/qoafYwyvYkeD3fcJ6Af8eBrJMVBGogW9+55zAHbup60S7F BE197XKPDPM1gOJY5ChYwE+tBrTvBfI37A/gKSEgGerfMGTqOn40FnpiWnS4C8l9UQq2 OOoaEFiyB4yvAxcJii63go2Ux2xdqfxjGYKRh6Xi/FZ0qZPucP/r8kb22G3zSZTqiv9P oaWsPVKECiDOia5DjNFr6+5JasFRjWe03/zl8fo52HgeqW728GwE4BpwFdMehN2sku5C Ya8Q== 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=UXByQc3t3A49Pgq8d2E1JEtcEwQkNM/QU0Xg3xEoGLo=; fh=eSzpgospgMU8oOGgEUSakcgwfntwwBOKwj4f1IHW3hI=; b=qkkJrJkjN6fDRRfcJp9t4KDfBgYnK/ForOaVktwK4MI1Phc5EVC9twWbhi/i5tMlgZ VbElODGDusqTL6aNqvPF9yAyjrwkvW5YUho0ZRLMnoTbKETzC3EW9qGFopqBpJd/UWm5 Ilf/MXXFhN5tGMbjFi31R3LX5Rjv0O7x1Q5o07l8iNarQ6HROwHQP6SrJkYo22aupvbs msrZ5MZfslIkmipWKwDZHN6TxwZ0kij6Ed3k+EUJXEN8WMIEtpTpGAlhmKfVmyhE3XA3 WrgpiQ7Y6PvwDTWKq8+XG9fcxYJrg5bbr3/eG7d2wg/+597dffQacmp0l4cRZw0FGr55 QlQg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id z1-20020a633301000000b00578af1e2f3dsi2451172pgz.527.2023.09.19.19.18.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 19:19:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id A447E8255795; Tue, 19 Sep 2023 19:18:45 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231260AbjITCSp (ORCPT + 99 others); Tue, 19 Sep 2023 22:18:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229521AbjITCSp (ORCPT ); Tue, 19 Sep 2023 22:18:45 -0400 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB7C1BE for ; Tue, 19 Sep 2023 19:18:39 -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 1qimn9-0006me-2W; Tue, 19 Sep 2023 22:18:15 -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 Subject: [PATCH 0/2] hugetlbfs: close race between MADV_DONTNEED and page fault Date: Tue, 19 Sep 2023 22:16:08 -0400 Message-ID: <20230920021811.3095089-1-riel@surriel.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 morse.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 (morse.vger.email [0.0.0.0]); Tue, 19 Sep 2023 19:18:45 -0700 (PDT) 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.