From patchwork Fri Oct 6 03:59:05 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: 14997 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp72948vqo; Thu, 5 Oct 2023 21:00:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE6tMYRBfMXm5vYQrs1Xw8UooWwDucUt9u7jyazB4zn3NzEIyhttDCiQfMFSl0gjJs0V3Ct X-Received: by 2002:a17:902:e803:b0:1c6:2acc:62ea with SMTP id u3-20020a170902e80300b001c62acc62eamr8160571plg.57.1696564847674; Thu, 05 Oct 2023 21:00:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696564847; cv=none; d=google.com; s=arc-20160816; b=g91CIPPqQXo54z6TI/haFa1XrZcKeHoaq7hREnPA9wcipmF+2xg/j9pxE6011aGvkn hLgrQeSstKpjc2sAJ3Fs0KoxilEcMR7YZHKdZG+zlvMJlm+NgCWcvrAkyAoOebznVbre 7i1CxYn+KtZUZKQ0C3g6fUp5wlp7oky17yYpGZU8b/JGP0zng8IPNovGPlk2sqyXfNx6 gj6bwRZ6lhRmOfxf8TwJ97kATkSEsq6PVOX1uywrs3SFuBJg/P9sOGzLMMv24/nI59UG XCwTcrTeW6CFh5bQM8EnQ0xwchdEKNBgcXj9M7tMQkbhDiRdyBymunrIxNMDQABcdGrW q0lg== 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:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id ik5-20020a170902ab0500b001c88be7cdbfsi1431413plb.387.2023.10.05.21.00.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 21:00:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id 24F008028B6F; Thu, 5 Oct 2023 21:00:45 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229918AbjJFEA2 (ORCPT + 18 others); Fri, 6 Oct 2023 00:00:28 -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 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Thu, 05 Oct 2023 21:00:45 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778977181983955604 X-GMAIL-MSGID: 1778977181983955604 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.