From patchwork Fri Jun 7 18:20:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Donet Tom X-Patchwork-Id: 13690592 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ECEAAC27C53 for ; Fri, 7 Jun 2024 18:20:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 167A36B0088; Fri, 7 Jun 2024 14:20:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 118146B0089; Fri, 7 Jun 2024 14:20:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EFA256B008C; Fri, 7 Jun 2024 14:20:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D0E766B0088 for ; Fri, 7 Jun 2024 14:20:33 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 77946120122 for ; Fri, 7 Jun 2024 18:20:33 +0000 (UTC) X-FDA: 82204907946.07.1A0A29B Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by imf10.hostedemail.com (Postfix) with ESMTP id 24136C000D for ; Fri, 7 Jun 2024 18:20:30 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=nQuJRPll; dmarc=pass (policy=none) header.from=ibm.com; spf=pass (imf10.hostedemail.com: domain of donettom@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=donettom@linux.ibm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717784431; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=8Of3jtOtNBCqPysJASfdDL2S3JSFKdLpSXFzpLYj2PY=; b=bVx5DBNPA/gy+3jM47mHzvemfMiM/uOvs2PSQaXPdOLkcIQMzoZShveth/xea9ta6/MiDk vSmW8rbLBPLI9w1t4ODa8s52oZgvNyodqG5WYrxWI9DW46+UG5MFTFA4V+iwEEPNHxXoUi ZwhhLEx7QVyv1qefgl+VUWyigtj0p8k= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=nQuJRPll; dmarc=pass (policy=none) header.from=ibm.com; spf=pass (imf10.hostedemail.com: domain of donettom@linux.ibm.com designates 148.163.156.1 as permitted sender) smtp.mailfrom=donettom@linux.ibm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717784431; a=rsa-sha256; cv=none; b=0EEFvYLzqx0Vxrm5dSZkf+HnKtg1Ji7XP62Zm45zXShUaocr8jHxtugjOMxUMW/bq2/B+0 1ZlnP4uwoE4M4D/XrT+JV2iqog9Lz9xtKPSSvxmGhDuDVhHlvg5F9Dd3q/78P0dD8zehjX PaKtU6yn8bHsQvEK9yo0X4d5c9SPufw= Received: from pps.filterd (m0353727.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 457IBJGK028460; Fri, 7 Jun 2024 18:20:10 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc : content-transfer-encoding : date : from : message-id : mime-version : subject : to; s=pp1; bh=8Of3jtOtNBCqPysJASfdDL2S3JSFKdLpSXFzpLYj2PY=; b=nQuJRPllzByViz+Rnhu6mjt/SumqpxoyCeOwsm+X8P6K+7I7izig3HceiEPOIBn0OiFO T7OyKtbvbPwbSnEygyzWU4BFp0hiBSwRDjEBH3hrw0CMlr2RULEKO/hfhrfDzvq3bVCz Tyyy+KC+ORFXzmNHTXQQ859t5TRufnLf8rwIFLDGPZ205+AJu1K+kobAwQDs0lWX6p0d pce0J9L4MkHMVDS03zPowdZe9O+MkmK+tYKFESvoWelMdGN7ou+vEsYASOlFMz3TpgSx 9k+fl2ZxV+5+ToIdOSM/Jn/95rYr62evb+SJZkMzJ7yja5oS9HuA6TdEVYuv4VO5Q22r 7w== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3ym6yx030s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Jun 2024 18:20:09 +0000 Received: from m0353727.ppops.net (m0353727.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 457IK9Km015675; Fri, 7 Jun 2024 18:20:09 GMT Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3ym6yx030n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Jun 2024 18:20:09 +0000 Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 457GnIBP026671; Fri, 7 Jun 2024 18:20:07 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3ygffnj0j7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 07 Jun 2024 18:20:07 +0000 Received: from smtpav06.fra02v.mail.ibm.com (smtpav06.fra02v.mail.ibm.com [10.20.54.105]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 457IK31D44826968 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 7 Jun 2024 18:20:05 GMT Received: from smtpav06.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A2AD520049; Fri, 7 Jun 2024 18:20:03 +0000 (GMT) Received: from smtpav06.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A62D520040; Fri, 7 Jun 2024 18:20:01 +0000 (GMT) Received: from ltczz402-lp1.aus.stglabs.ibm.com (unknown [9.40.194.31]) by smtpav06.fra02v.mail.ibm.com (Postfix) with ESMTP; Fri, 7 Jun 2024 18:20:01 +0000 (GMT) From: Donet Tom To: Andrew Morton , Shuah Khan , Matthew Wilcox , Tony Battersby Cc: linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Ritesh Harjani , Mike Rapoport , Muchun Song , David Hildenbrand , p.raghav@samsung.com, usama.anjum@collabora.com Subject: [PATCH v4] selftest: mm: Test if hugepage does not get leaked during __bio_release_pages() Date: Fri, 7 Jun 2024 13:20:00 -0500 Message-ID: <20240607182000.6494-1-donettom@linux.ibm.com> X-Mailer: git-send-email 2.43.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: Mt-ewEyqWzwNqnyY4Mdcwcwb3rVuufCr X-Proofpoint-GUID: H5jiUjmYy4Zoi_JUgEFfVW_eamQBcxgh X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-07_11,2024-06-06_02,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 adultscore=0 clxscore=1015 mlxscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 priorityscore=1501 bulkscore=0 spamscore=0 phishscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2405010000 definitions=main-2406070135 X-Rspamd-Queue-Id: 24136C000D X-Stat-Signature: odgb81pyso539ysgtw3h8phsnwr4k9y3 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1717784430-924362 X-HE-Meta: U2FsdGVkX19HP7Z4xMzPi/g0BYnFtT92sCqOsBbT6Mh0Ra96SG1JkSTqxJjEKbDnXeKUF5tL0Pz9tVtro99BRXeNysa84pIEN9kXZemhijvitKntXCbRUGeDRl7zutr4LS+TFE793SKcH532FyiZH3vt6zMLQiz7pkvI0+WO/VTJQu1ZlKzecIReKQIoDs1Pi4s4oftNxcPJfmWnP93zPzb+Bgn6O8kL5hI01hL2nEuvlAD14cJASQCAMSUYpxd5/JIM6XWt9BnxdeOf5+O8uddMy+qt5DjTNpw6t50cD4P4xov+gTRDNECnZltM7T7hOc9OPCEwvlXTu7BvLFRnFqTHMRCeCRSkRQ75L9gXUUGd7vjFiJde6ZQndkZkyHk7KKuIVmnqDwl90bRZuYzGL40fq4hfkItfOXBGr2KnATUnKb2MUtTV1HXn4c+P/avdzc4Jlq5J6ZskmUt7ixIrMFHLV6D6T63XJaqqwa9p2a8V23zSMsKRRWGFqcybBnjVOr1j3vAcde+lpYKXGxQlvkOO2eaoAPFq//kw3PaIaJvVE3bxTE/hrkGjAruuGfWZUkSYXMDB0lAxYVZbvz9dGTPMLfOlPj87WlNDGVAjyG3BiBl4X+JbX9AGafCuKv3TQ9tVgltVdOekwLEhvUq3vSFsUd5fO1iU/6F51lOxhd9ejwMZ9eG+45jdCQDxd6Y2drJSpKI15WpNFyJUgNW59IGy6KYJ5PCVBrMhR6NrB21x7tMNVuHIjic0Ha4D+1G+bbdZjZ3tnvHE7qm4j9Toh+yoULEK7dbgIO+ukwIX8q5XGlcoELiGjC9KKAtcCwnEmkSP6nJmvRpLSyNQ4v2gsVzx1D731TDd9XU294eF6qboovEL860WpPaFLooNwA2/SER5tyBWUxtute1IGwwQv1V04/Ds8pu46CiuObj9Gqb8/fSY88emrdiGpTu/9Bl2kRoU0OMF0DtBuUjaH9b KoNFJQwp txhcPl7YACd8/kY+rOy5X0z3dOLsktjJA/lTXPNitPYJ6RwNQbKe1tcqem24C+LwbP+Z0hirJqeTS8ph+ewzAymgTpbP9fGMmC0uvfKLB8iklhJhCVWdbl/1ZBaawP6o3gc4oHEZOD0r6FCb5UFCapTf+S3yRLK9aFAZfSHLRxgckiLdy2pwKMEnpuwdQk1ONw0oTbenFItx+HXWU8hIy10qCVnE6jCoXUmEIw9GXFnlmqDrplCxg8lQXeE2++lWPRJQJSZXb8ZrKdharuWL+DHSrPtjyzbptNI7hdHL2ZVWJWfVnSGTOk9L2Md3rPz782pHcsD3YpOXskx557D6fXLL/bQVXCzbkQjrqaxrDGOUpPFEK4YjW7laNllXoHloM7vCSKblBkCZnd1C05qFJlzuTZk6Mm7rAUdMEKAvqYtn4LGvh/jkLMUlrKQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Commit 1b151e2435fc ("block: Remove special-casing of compound pages") caused a change in behaviour when releasing the pages if the buffer does not start at the beginning of the page. This was because the calculation of the number of pages to release was incorrect. This was fixed by commit 38b43539d64b ("block: Fix page refcounts for unaligned buffers in __bio_release_pages()"). We pin the user buffer during direct I/O writes. If this buffer is a hugepage, bio_release_page() will unpin it and decrement all references and pin counts at ->bi_end_io. However, if any references to the hugepage remain post-I/O, the hugepage will not be freed upon unmap, leading to a memory leak. This patch verifies that a hugepage, used as a user buffer for DIO operations, is correctly freed upon unmapping, regardless of whether the offsets are aligned or unaligned w.r.t page boundary. Test Result Fail Scenario (Without the fix) -------------------------------------------------------- []# ./hugetlb_dio TAP version 13 1..4 No. Free pages before allocation : 7 No. Free pages after munmap : 7 ok 1 : Huge pages freed successfully ! No. Free pages before allocation : 7 No. Free pages after munmap : 7 ok 2 : Huge pages freed successfully ! No. Free pages before allocation : 7 No. Free pages after munmap : 7 ok 3 : Huge pages freed successfully ! No. Free pages before allocation : 7 No. Free pages after munmap : 6 not ok 4 : Huge pages not freed! Totals: pass:3 fail:1 xfail:0 xpass:0 skip:0 error:0 Test Result PASS Scenario (With the fix) --------------------------------------------------------- []#./hugetlb_dio TAP version 13 1..4 No. Free pages before allocation : 7 No. Free pages after munmap : 7 ok 1 : Huge pages freed successfully ! No. Free pages before allocation : 7 No. Free pages after munmap : 7 ok 2 : Huge pages freed successfully ! No. Free pages before allocation : 7 No. Free pages after munmap : 7 ok 3 : Huge pages freed successfully ! No. Free pages before allocation : 7 No. Free pages after munmap : 7 ok 4 : Huge pages freed successfully ! Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0 V4: - Added this test to run_vmtests.sh. V3: - Fixed the build error when it is compiled with _FORTIFY_SOURCE. V2: - Addressed all review commets from Muhammad Usama Anjum https://lore.kernel.org/all/20240604132801.23377-1-donettom@linux.ibm.com/ V1: https://lore.kernel.org/all/20240523063905.3173-1-donettom@linux.ibm.com/#t Signed-off-by: Donet Tom Co-developed-by: Ritesh Harjani (IBM) Signed-off-by: Ritesh Harjani (IBM) Reviewed-by: Muhammad Usama Anjum --- tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/hugetlb_dio.c | 118 ++++++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 1 + 3 files changed, 120 insertions(+) create mode 100644 tools/testing/selftests/mm/hugetlb_dio.c diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index 3b49bc3d0a3b..a1748a4c7df1 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -73,6 +73,7 @@ TEST_GEN_FILES += ksm_functional_tests TEST_GEN_FILES += mdwe_test TEST_GEN_FILES += hugetlb_fault_after_madv TEST_GEN_FILES += hugetlb_madv_vs_map +TEST_GEN_FILES += hugetlb_dio ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty diff --git a/tools/testing/selftests/mm/hugetlb_dio.c b/tools/testing/selftests/mm/hugetlb_dio.c new file mode 100644 index 000000000000..986f3b6c7f7b --- /dev/null +++ b/tools/testing/selftests/mm/hugetlb_dio.c @@ -0,0 +1,118 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This program tests for hugepage leaks after DIO writes to a file using a + * hugepage as the user buffer. During DIO, the user buffer is pinned and + * should be properly unpinned upon completion. This patch verifies that the + * kernel correctly unpins the buffer at DIO completion for both aligned and + * unaligned user buffer offsets (w.r.t page boundary), ensuring the hugepage + * is freed upon unmapping. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include "vm_util.h" +#include "../kselftest.h" + +void run_dio_using_hugetlb(unsigned int start_off, unsigned int end_off) +{ + int fd; + char *buffer = NULL; + char *orig_buffer = NULL; + size_t h_pagesize = 0; + size_t writesize; + int free_hpage_b = 0; + int free_hpage_a = 0; + const int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB; + const int mmap_prot = PROT_READ | PROT_WRITE; + + writesize = end_off - start_off; + + /* Get the default huge page size */ + h_pagesize = default_huge_page_size(); + if (!h_pagesize) + ksft_exit_fail_msg("Unable to determine huge page size\n"); + + /* Open the file to DIO */ + fd = open("/tmp", O_TMPFILE | O_RDWR | O_DIRECT, 0664); + if (fd < 0) + ksft_exit_fail_perror("Error opening file\n"); + + /* Get the free huge pages before allocation */ + free_hpage_b = get_free_hugepages(); + if (free_hpage_b == 0) { + close(fd); + ksft_exit_skip("No free hugepage, exiting!\n"); + } + + /* Allocate a hugetlb page */ + orig_buffer = mmap(NULL, h_pagesize, mmap_prot, mmap_flags, -1, 0); + if (orig_buffer == MAP_FAILED) { + close(fd); + ksft_exit_fail_perror("Error mapping memory\n"); + } + buffer = orig_buffer; + buffer += start_off; + + memset(buffer, 'A', writesize); + + /* Write the buffer to the file */ + if (write(fd, buffer, writesize) != (writesize)) { + munmap(orig_buffer, h_pagesize); + close(fd); + ksft_exit_fail_perror("Error writing to file\n"); + } + + /* unmap the huge page */ + munmap(orig_buffer, h_pagesize); + close(fd); + + /* Get the free huge pages after unmap*/ + free_hpage_a = get_free_hugepages(); + + /* + * If the no. of free hugepages before allocation and after unmap does + * not match - that means there could still be a page which is pinned. + */ + if (free_hpage_a != free_hpage_b) { + ksft_print_msg("No. Free pages before allocation : %d\n", free_hpage_b); + ksft_print_msg("No. Free pages after munmap : %d\n", free_hpage_a); + ksft_test_result_fail(": Huge pages not freed!\n"); + } else { + ksft_print_msg("No. Free pages before allocation : %d\n", free_hpage_b); + ksft_print_msg("No. Free pages after munmap : %d\n", free_hpage_a); + ksft_test_result_pass(": Huge pages freed successfully !\n"); + } +} + +int main(void) +{ + size_t pagesize = 0; + + ksft_print_header(); + ksft_set_plan(4); + + /* Get base page size */ + pagesize = psize(); + + /* start and end is aligned to pagesize */ + run_dio_using_hugetlb(0, (pagesize * 3)); + + /* start is aligned but end is not aligned */ + run_dio_using_hugetlb(0, (pagesize * 3) - (pagesize / 2)); + + /* start is unaligned and end is aligned */ + run_dio_using_hugetlb(pagesize / 2, (pagesize * 3)); + + /* both start and end are unaligned */ + run_dio_using_hugetlb(pagesize / 2, (pagesize * 3) + (pagesize / 2)); + + ksft_finished(); +} + diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh index 3157204b9047..5698d519170d 100755 --- a/tools/testing/selftests/mm/run_vmtests.sh +++ b/tools/testing/selftests/mm/run_vmtests.sh @@ -265,6 +265,7 @@ CATEGORY="hugetlb" run_test ./map_hugetlb CATEGORY="hugetlb" run_test ./hugepage-mremap CATEGORY="hugetlb" run_test ./hugepage-vmemmap CATEGORY="hugetlb" run_test ./hugetlb-madvise +CATEGORY="hugetlb" run_test ./hugetlb_dio nr_hugepages_tmp=$(cat /proc/sys/vm/nr_hugepages) # For this test, we need one and just one huge page