From patchwork Fri Oct 21 23:28:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 7026 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp951259wrr; Fri, 21 Oct 2022 16:33:31 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5QOn8lp8kP7fNDDFlaSThRiUKxaBhloxvsAmuyiIaTQuLfuhs9Nw5FWZR1eXX9WIDUi0Iw X-Received: by 2002:a05:6a00:c91:b0:562:aa06:2848 with SMTP id a17-20020a056a000c9100b00562aa062848mr22092396pfv.2.1666395211275; Fri, 21 Oct 2022 16:33:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666395211; cv=none; d=google.com; s=arc-20160816; b=i3xqOL7BSoflcdr3mPDnoWDvQYs3143Y8IPFoXP02Aqmhsds/oh00gg43tS1E0vk6b 4d2F4boJLNfXw6zeCaU9+NoEj3+5+o0qQrxzod1UFYcNnrH8S7189+/bLuyZBecUfXuA s89IH7jFNIoz9CtXeHGospWlQ6rQTvNFCFL8pxpXPNMAfrTqPndyUdUjWkO/NmORNMXY faoLHU6U5BFvBA1rxOMCXsgoxHiCPvSuGPmijPl5JSlGWc6WCiYSQZbsLyknp82wCBme dcVFoky5AOMVSSe8Nn2qzbswr75XEylvf2pfv9Gk3K1AJ8fyjsE3nV/VE656CIh/UZ5n JquA== 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:subject:cc:to:from:date; bh=NNIjsxsXvHppPN2twb3s9Oh/SYdaL1myuKZJ0aPS8/c=; b=elSS3Hr2Cj08CDkPsIiz6mWKgavppvDSlYdZI+GPesuhDUELqakesfhB/n40kvkAfL WI/r0CBGGO8rlo7Ss4dM4Vmps9L5EsvY8VlzuOJlBHG+nPOG+1eEYPQChC4hxHzgKDCH 4S3OKkg+hax/Kf7dUsYLOTGCC11CftBp6huANF/jx5R5VsOXvqug9Cn29XPMm/sybo1O T+Xv18ejyVcXDnyqmQe9XrGYeYt9O6U7Rg14cJk/M43pLXunwf39LZy7sFLZgPjsLQ+d Xs7NGFBt7bUV8p0LMDtS/fRdEtLr/6lcvhrVLMI4l5SCtYbkdFbTefJU95CqcLbIMogM HQ3A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n1-20020a6546c1000000b0041c8dfb29f0si29375227pgr.705.2022.10.21.16.33.18; Fri, 21 Oct 2022 16:33:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229744AbiJUX2L (ORCPT + 99 others); Fri, 21 Oct 2022 19:28:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229736AbiJUX2J (ORCPT ); Fri, 21 Oct 2022 19:28:09 -0400 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7605E2A3882 for ; Fri, 21 Oct 2022 16:28:08 -0700 (PDT) Received: from [2603:3005:d05:2b00:6e0b:84ff:fee2:98bb] (helo=imladris.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1om1Qr-0000ST-2D; Fri, 21 Oct 2022 19:28:05 -0400 Date: Fri, 21 Oct 2022 19:28:05 -0400 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, kernel-team@meta.com, stable@kernel.org, Mike Kravetz , Andrew Morton , David Hildenbrand Subject: [PATCH v2] mm,madvise,hugetlb: fix unexpected data loss with MADV_DONTNEED on hugetlbfs Message-ID: <20221021192805.366ad573@imladris.surriel.com> X-Mailer: Claws Mail 4.1.0 (GTK 3.24.34; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Sender: riel@shelob.surriel.com X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747342025578787361?= X-GMAIL-MSGID: =?utf-8?q?1747342025578787361?= A common use case for hugetlbfs is for the application to create memory pools backed by huge pages, which then get handed over to some malloc library (eg. jemalloc) for further management. That malloc library may be doing MADV_DONTNEED calls on memory that is no longer needed, expecting those calls to happen on PAGE_SIZE boundaries. However, currently the MADV_DONTNEED code rounds up any such requests to HPAGE_PMD_SIZE boundaries. This leads to undesired outcomes when jemalloc expects a 4kB MADV_DONTNEED, but 2MB of memory get zeroed out, instead. Use of pre-built shared libraries means that user code does not always know the page size of every memory arena in use. Avoid unexpected data loss with MADV_DONTNEED by rounding up only to PAGE_SIZE (in do_madvise), and rounding down to huge page granularity. That way programs will only get as much memory zeroed out as they requested. Cc: Mike Kravetz Cc: David Hildenbrand Cc: Andrew Morton Cc: stable@kernel.org Fixes: 90e7e7f5ef3f ("mm: enable MADV_DONTNEED for hugetlb mappings") Reviewed-by: Mike Kravetz --- v2: split out the most urgent fix for stable backports mm/madvise.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mm/madvise.c b/mm/madvise.c index 2baa93ca2310..c7105ec6d08c 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -813,7 +813,14 @@ static bool madvise_dontneed_free_valid_vma(struct vm_area_struct *vma, if (start & ~huge_page_mask(hstate_vma(vma))) return false; - *end = ALIGN(*end, huge_page_size(hstate_vma(vma))); + /* + * Madvise callers expect the length to be rounded up to PAGE_SIZE + * boundaries, and may be unaware that this VMA uses huge pages. + * Avoid unexpected data loss by rounding down the number of + * huge pages freed. + */ + *end = ALIGN_DOWN(*end, huge_page_size(hstate_vma(vma))); + return true; } @@ -828,6 +835,9 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, if (!madvise_dontneed_free_valid_vma(vma, start, &end, behavior)) return -EINVAL; + if (start == end) + return 0; + if (!userfaultfd_remove(vma, start, end)) { *prev = NULL; /* mmap_lock has been dropped, prev is stale */