Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp139322rdb; Thu, 5 Oct 2023 21:02:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEkex2P6v0yTwcm4mZ0kTF2eqqyJZ/w3wSSircHgkkZ4qURsHVjXGI1wGZqTXM83LZtR0U8 X-Received: by 2002:a05:6a20:3c89:b0:161:2df0:eadf with SMTP id b9-20020a056a203c8900b001612df0eadfmr8060331pzj.24.1696564923873; Thu, 05 Oct 2023 21:02:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696564923; cv=none; d=google.com; s=arc-20160816; b=AdNMhzZHXFCwsMCtXkV4deewrBK2TCpB+C2+kUPHK48cWwPR7jYCeCcOijyjL/L278 0yNYwT3z2QxFgjjFKTsl1NjywQT1JuJuAqgdBkO64yRz18XmqTZ1XjMTJdP3dB17lMna WKdPFjm8YSrNyiNum7k5ZcqcWf2wkG12mcPbJMtXW763f43Q/rzl1wBLJwjwCAV5GokS 4I2kEaecqZQlwN5Tp7eZYZOv7fjtqgyV86sPDPvDHvGiK0Oypb33geg6zDrlhB88JUPw JG1S0iblCTjTlmDLL/W4HkQLIJYAvonZX43oNy66ylv3+2ojED+Q96Uc+HrCCwfcKVs3 J/vQ== 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=Cdi1VObhi9/RrxVkC71uC7OCN+ZfJbW9EoREPRpsFUU=; fh=q9SQMO40xEi/EnFYzkYLxDJR2yDm5elEChUbTy2RFH8=; b=wTJsDVV9+VL0TFuLI6bBd4/l1MsEwSWknTzFjorTWvyof1sn559r7raorUBxrPOK+G OLi9refgvZpVSUYsT1KJAHb6DW9BH+c3YAM0UfQ0kjTmq3VWJLhKJvKH5WmYip0klC1/ rKoR22oIuanfBGLG1SQ6jAW+p1+DZn9Xrem+zb63SG7QxrNs2PDrQWKyYzAkE/wTnWo0 LkOTywVDcw58KuO0tMDof8kroHEzLrD2qii6K8U5LIgSsIDRiw0x7qh3v70g1L+YJPFf ribzHmKScb/XIwAoIACKrGyzkirTUzqEYDt0jmv9D+WbpTZdf3jYVCFMDhAeb3DvNAdb IdCA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id kl8-20020a170903074800b001c746bca061si2779889plb.74.2023.10.05.21.02.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 21:02:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id EFDBA8082DCD; Thu, 5 Oct 2023 21:00:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229914AbjJFEA1 (ORCPT + 99 others); Fri, 6 Oct 2023 00:00:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229876AbjJFEAZ (ORCPT ); Fri, 6 Oct 2023 00:00:25 -0400 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7F58DE for ; Thu, 5 Oct 2023 21:00:24 -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 1qoc0k-0000mf-2n; Fri, 06 Oct 2023 00:00:22 -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 v7 0/4] hugetlbfs: close race between MADV_DONTNEED and page fault Date: Thu, 5 Oct 2023 23:59:05 -0400 Message-ID: <20231006040020.3677377-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 agentk.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 (agentk.vger.email [0.0.0.0]); Thu, 05 Oct 2023 21:00:37 -0700 (PDT) v7: fix !vma->vm_file and hfill2 cases 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.