From patchwork Fri Jan 28 22:26:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Kravetz X-Patchwork-Id: 12729205 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 C27CBC433F5 for ; Fri, 28 Jan 2022 22:26:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D4526B00EC; Fri, 28 Jan 2022 17:26:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 183AB6B00EF; Fri, 28 Jan 2022 17:26:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F18826B00F1; Fri, 28 Jan 2022 17:26:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0168.hostedemail.com [216.40.44.168]) by kanga.kvack.org (Postfix) with ESMTP id E175A6B00EC for ; Fri, 28 Jan 2022 17:26:32 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 8C5CA181F6001 for ; Fri, 28 Jan 2022 22:26:32 +0000 (UTC) X-FDA: 79081131024.07.7D24C7A Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf26.hostedemail.com (Postfix) with ESMTP id 090AC140003 for ; Fri, 28 Jan 2022 22:26:31 +0000 (UTC) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20SMCt4l020724; Fri, 28 Jan 2022 22:26:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=JbNnOAY7/pNJVSMUUNZ+y8NOLvEedODea2X/9pS9EVw=; b=et2YnEDzk1j7yeMbTB8+Cvlsx/OoTbEoC3Iz5MYHA2PFhTDTtLcld0/nh5iqReYFw9mC 6YnpMWPCHymDpxhRM+GYiyuMuLdBMM5KZ7q3szqT/PY3po5W0XgtkJiEkFQqta19IpHq U4cd2GpIf0uSY17u42ja47ysDMzvSdebe2jsOsblyXdb7+C/e5FZ9LiwVOIPP9mxI8U1 Hxp0eyen+Z/nhRYlGI8Uzv/wxFRuS5ppPzv6pBq10y+wDCz9kS1YpDXanxpc5HA2Q+yS yEY2z+ZPBaFpACG6+tDVW3Bro3Ke64pwKGOi5aMZLf2ntqcCb5p1NQPEkKkI37lWy6mc HA== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by mx0b-00069f02.pphosted.com with ESMTP id 3duvsj4j7f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Jan 2022 22:26:27 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20SMHRA3142671; Fri, 28 Jan 2022 22:26:26 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2168.outbound.protection.outlook.com [104.47.56.168]) by userp3030.oracle.com with ESMTP id 3dr726qbsh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Jan 2022 22:26:26 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=F/syO7hxFpP/yvEYhRhBdWOCDru/5nNgiYqu3qs8vOjV/GPyX6cixtv/S3s6/GbHWUPNcul5acdeBEGDna+3JSbghmWUcNC8tqG4wlCEw6OHF++nwnjiiFhQd37OalMIZPQSwzOGaqegmt1rnD6M+t5Cx5gTe9KRmSzPp5rgvZ8Wa62yGtgR76sxjK08uMP+famDX7634MsfAuGYu49GEySefpydWBiM0jWgjH49qavN5v4AsfCX4epoYLDPjSe6rL1KNXlXLnLZsHaCbSwfQ4osez59ta0qPHJ5SzQVRBQXyZ18tBtHR1mzBNJTQkIse/y8KgR3OAgF3/l98ujhlQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JbNnOAY7/pNJVSMUUNZ+y8NOLvEedODea2X/9pS9EVw=; b=RX3w0lgvyIQJMd9vjspr/7T6Fwyjke6Y07AEmuVkglNre9jtZY5cjJ0ETKzCD4gkGWFnc36me0ead/jI/8+qVk77VyfDi4eqpleHoW9d6qXSAcefCdrDU9fy0LFJOuVs0nHv0iX03IRpPwjf93HPdxsX9LFOJ5laWpBPuR+c941s7FJFHxHQVsBwlNmDyN21La2g4VFK5S4ZHrz1refFNrw+TEAPNixoNJasSBqO3ltTiXxJ/4N02aW/l+axV8sqxT9ArLE2IQs3FMEmcwYhLoxwValxGxa1qtNyEPy3TrA0I3eRYjQJxSI6tSSdOiVyRtV+8UAVYPuXeodcLmz0Tw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JbNnOAY7/pNJVSMUUNZ+y8NOLvEedODea2X/9pS9EVw=; b=bUnWj5+j7Mjjz8Gig82LK0sGQLcNsekmTep2GIHUZNiQvu3m1ISRjebVjWGjKz9zMMJAfjVt1gM/cZHE6J2/287jmalmqEIccPNCXSP8NOG3T4l1EIlq6d/Ee1f7YmXuRBEfVca05Mle7v0o5oe36JfRiof6+64409vnccajBFk= Received: from BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) by DM6PR10MB2587.namprd10.prod.outlook.com (2603:10b6:5:b1::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.19; Fri, 28 Jan 2022 22:26:23 +0000 Received: from BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::90eb:bb07:608b:7556]) by BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::90eb:bb07:608b:7556%7]) with mapi id 15.20.4930.019; Fri, 28 Jan 2022 22:26:23 +0000 From: Mike Kravetz To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Naoya Horiguchi , David Hildenbrand , Axel Rasmussen , Mina Almasry , Michal Hocko , Peter Xu , Andrea Arcangeli , Shuah Khan , Andrew Morton , Mike Kravetz Subject: [PATCH 1/3] mm: enable MADV_DONTNEED for hugetlb mappings Date: Fri, 28 Jan 2022 14:26:03 -0800 Message-Id: <20220128222605.66828-2-mike.kravetz@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220128222605.66828-1-mike.kravetz@oracle.com> References: <20220128222605.66828-1-mike.kravetz@oracle.com> X-ClientProxiedBy: MWHPR07CA0016.namprd07.prod.outlook.com (2603:10b6:300:116::26) To BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 31a24521-3813-4d2f-0f6d-08d9e2ad3703 X-MS-TrafficTypeDiagnostic: DM6PR10MB2587:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 7jy52z6mKJc7zK+yrrJeCX9IpvOkCzCDkXLXqPrb4w1rTfDNQtnuOeJqdVZ8jIsxbqOGMdIngLDqQJnzhzGhVLJ4XuREbay4ex044v5Bxy2xFXY3lIc9ZxZKQB/G3R7Xh+JZ1xvRVnAe0qyRttfpoHtx4mV/U6nDpxaiP2dIMaqOpvYN+F1PILXJrJiRgGokrkSwxZXF07K3oxSiAytr21amYpOV6+yhBqdvdsaVTim0Rs4oUvlOFcBHisyPqBii1BBV+femJG1FpiNaaF+LmGGCuBZKck0Nw3Wmg54a2/gK/3hqRPrvIBlCjv5G1r7Q0Aj6nqnDd48FG/j2in/Y0Kr6mOTLPEqnJPPJzxC/wevzifeW6SOiQ7UQrlRTAkEdGUEvZwmIPMRV9y7UGSg3eYnUZrzG6r9xkIBDkc4i0pNmDqpn5zsA2h6opsX1LPDfF2r1HE00Vw3clTujCs1on3inNgDSi9krKnRm6qqNwnanIrwkLZrD15aS2qiQpBdb0mfMLNPSePhTlp/shj83RvhsKWBOGzf1Nc8AcBH5Z8GnRjzzFJ55W5yLQD6d33pBIDich9/Z0DyYmDcYqJvVIMhTCsSKhZeuoUHJobr3D+skkeK8ufaOGBE5U4x0TJsylfNMTzBeg63+emxMAJ5Ntyg6jUk1Ymz9ZNoC0nEt9QqmR5KPuip7eoj9uiUdY8jVEjyDlJkBW8vGWYrYerLotA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BY5PR10MB4196.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(8676002)(7416002)(66556008)(66476007)(8936002)(36756003)(66946007)(44832011)(38100700002)(38350700002)(54906003)(316002)(4326008)(5660300002)(6506007)(6512007)(6666004)(52116002)(107886003)(2906002)(83380400001)(508600001)(86362001)(6486002)(2616005)(1076003)(186003)(26005)(20210929001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: /HZyjxlSx7lYzQ/6qcq11/IpfMfBhJtLnIBa8RwZx2erWuJkmq67HDNFFP7P+x07JDPP0610GAesNayMAn+LjHRLEqufbkBA7FC816/wRHUdVStUGWatsFb/miwnjpdRn1SEI1mP5WxmoUtHsJ/W7+ovxM7Qu34yMNpW4KuIyErSEuiUubfojF/yyqnGLRs0TCCneX7RfxoByenVylQS9ujESetjumVn9yMYd8pwkYYU5bNjrMDQkm3j04p1FE3GMxeB6Ltb1POSlVne456vT1MhtGLsM0OP5xJEvBaiz7K+1x3IE38Tf35zWVyplQRgtvlRBuY0Cp1+qT09fkX1c82gHOZFqqp/2vXbviWUvBQ0z1sZlVtrvKOAtcmyEn/x0eY0URuLW2bKOojMQO8iA4NGafHpGUTfS49wByi9ovv13lxVpOA1Evnb+LvAH9yJBy/U6O0kp8BxUJGvntGSH3E+dOxPPx///2FpEUisuiXvUNE/f0vkktyFLcm8AA9Orgbf5jkUOCLB9kdXwblgand1IdNx3zfSleYIT/PnyK6F/6RKfAR2YpWJ9eqTuY4qeTrjHZy5ehXAamvVi2WXeIhAX/njKNxDBzpUcq8fteZ69CvI4sBhoAGR8SCNbLzSo9eSjJ6HtJcugjv/qDbA1BNO4qDoeZ5l56v6d3xoAi4/6q0DRQ12LxVFpdq48iNJ2gdRbuyfex+ni+S/YX63JFBqbkVfJJWIDzVY5InmjuYa6IIhWsC8toVIHIlqTPP3aRUSn+hSWH/k/6HbQv1nSlCbgA6pQtSEtvlN0dggMvsW8ZSEc2PO4PgsA/zxGeW2ptBDQxPgXOqGJnkg8MytHiDudz1s2BnaJo3QKa27BRZukj7cHh+F7qUbw1DJuH42ykrocvDxRTE3Nk1X5AJqZrK21tJChqHdbrDBRIcU5Rnb1vPLmuVqMOmFY1IPy366cEHicIl/Ei7K4Nd9VMx/tke8IJ5rEhVh8ilfPoWjyMJt8YXcR+SYeI+kzSGaL6M1ni9O73PpUzcJHc7yyDnxijOeebg1jmGQKMXjbm0Uej+0HFV0FxlT3c/cWvNcu6ZoKKuqz4HNzqrRaJGRBkfwApzbeTCy+7hcMPEVdbzUg6WRG+IAlZDveVLiuX3jJwEhUzk5UCnzk5iUu7jB1oGopjq4Aq2jJ7SkVNELJ/QEdUqSqWDMv0aGy9Yw9dw45C3jyizAdWaNoRze+MFAtRthl8hGHSjFnzaJSmB6/DFm0z1s2SaBRTzSvd2E3kMn+v6JMkjnNlklgGnSUKhZBnEovkHspZmTRxaUeU1uF8CpMb/my573Fl8bbDCiAjk5XvYPD8Ek/yO5mL8YK2aqwFO+orwovj28ZmESJMFY6wKAILCTTX01tZpuzZohi8vi+NCl55qPvOYTP1jSGoECk96kkiXEnmNvtVJvoOOVVRATBepSJ6WJx1v9a41ah3ozZhqPlvuxE7g+HXs35JW9/Is7vcI8b1vp5nvQlszasJHXIeOwpmsHrIaCsfkXP9dznzmDXu8ISR5VZhM23LjB47oRb4saS8ce0fKy+qvDpe7HjEwwY0PhX8UI5rskY1k5vVC48v8VO6IV/hqP6fXhnY1Reqfq2+G3Fsrlw8M79Uy4paE= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 31a24521-3813-4d2f-0f6d-08d9e2ad3703 X-MS-Exchange-CrossTenant-AuthSource: BY5PR10MB4196.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jan 2022 22:26:23.1521 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 3UnkqTiU/Il5pBmatQfASF2fq/d3tnT4HUa9UXE7oEYY+DJ6Ieeez08kRkxfwUSYbWCdw+I+bA14UeUQuDEoNA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB2587 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10241 signatures=673430 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 mlxlogscore=861 adultscore=0 spamscore=0 bulkscore=0 mlxscore=0 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2201280125 X-Proofpoint-ORIG-GUID: 69E9sNJSnegydv5YGvUvixRDFYf_G7Ol X-Proofpoint-GUID: 69E9sNJSnegydv5YGvUvixRDFYf_G7Ol X-Stat-Signature: bwu14hh1ojtuocwc9nsym3398sekb6qw X-Rspam-User: nil Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b=et2YnEDz; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=bUnWj5+j; spf=none (imf26.hostedemail.com: domain of mike.kravetz@oracle.com has no SPF policy when checking 205.220.177.32) smtp.mailfrom=mike.kravetz@oracle.com; dmarc=pass (policy=none) header.from=oracle.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 090AC140003 X-HE-Tag: 1643408791-551280 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: MADV_DONTNEED is currently disabled for hugetlb mappings. This certainly makes sense in shared file mappings as the pagecache maintains a reference to the page and it will never be freed. However, it could be useful to unmap and free pages in private mappings. The only thing preventing MADV_DONTNEED from working on hugetlb mappings is a check in can_madv_lru_vma(). To allow support for hugetlb mappings create and use a new routine madvise_dontneed_free_valid_vma() that will allow hugetlb mappings. Also, before calling zap_page_range in the DONTNEED case align start and size to huge page size for hugetlb vmas. madvise only requires PAGE_SIZE alignment, but the hugetlb unmap routine requires huge page size alignment. Signed-off-by: Mike Kravetz --- mm/madvise.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/mm/madvise.c b/mm/madvise.c index 5604064df464..01b4d145d8f0 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -796,10 +796,30 @@ static int madvise_free_single_vma(struct vm_area_struct *vma, static long madvise_dontneed_single_vma(struct vm_area_struct *vma, unsigned long start, unsigned long end) { + /* + * start and size (end - start) must be huge page size aligned + * for hugetlb vmas. + */ + if (vma->vm_flags & VM_HUGETLB) { + struct hstate *h = hstate_vma(vma); + + start = ALIGN_DOWN(start, huge_page_size(h)); + end = ALIGN(end, huge_page_size(h)); + } + zap_page_range(vma, start, end - start); return 0; } +static bool madvise_dontneed_free_valid_vma(struct vm_area_struct *vma, + int behavior) +{ + if (vma->vm_flags & VM_HUGETLB) + return behavior == MADV_DONTNEED; + else + return can_madv_lru_vma(vma); +} + static long madvise_dontneed_free(struct vm_area_struct *vma, struct vm_area_struct **prev, unsigned long start, unsigned long end, @@ -808,7 +828,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, struct mm_struct *mm = vma->vm_mm; *prev = vma; - if (!can_madv_lru_vma(vma)) + if (!madvise_dontneed_free_valid_vma(vma, behavior)) return -EINVAL; if (!userfaultfd_remove(vma, start, end)) { @@ -830,7 +850,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, */ return -ENOMEM; } - if (!can_madv_lru_vma(vma)) + if (!madvise_dontneed_free_valid_vma(vma, behavior)) return -EINVAL; if (end > vma->vm_end) { /* From patchwork Fri Jan 28 22:26:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Kravetz X-Patchwork-Id: 12729207 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 38C06C433FE for ; Fri, 28 Jan 2022 22:26:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A3A166B00F1; Fri, 28 Jan 2022 17:26:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A125E6B00F2; Fri, 28 Jan 2022 17:26:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6650F6B00F4; Fri, 28 Jan 2022 17:26:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0101.hostedemail.com [216.40.44.101]) by kanga.kvack.org (Postfix) with ESMTP id 4E50A6B00EF for ; Fri, 28 Jan 2022 17:26:35 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id F1D8882D6B77 for ; Fri, 28 Jan 2022 22:26:34 +0000 (UTC) X-FDA: 79081131108.14.1870034 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf25.hostedemail.com (Postfix) with ESMTP id 799BDA0007 for ; Fri, 28 Jan 2022 22:26:34 +0000 (UTC) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20SMCtG1026831; Fri, 28 Jan 2022 22:26:30 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=IyGFvMgZRc4OkC4fwbRQpZz7cSALeDUuu6FeJv83v6k=; b=AuHOX36uziXV6Nl1Lqqw+n9b0konr58LueLzB2KQRXYGHrJmY0sdnH2tco64YngCFPeS hE773okP7xxqt7L+GjLKH7Zur7XhFMLobE0GCsuu/920gKTxiie4P6PbJ4oXJIN6Xn+a /778cThqPv/SjCGB1liyIuVOLcZaX9T3Qg4m049FGMokY0iXtRkal7Hlkz+HtV7ahceQ jJ2NjMapYKgKD/iK2gHUCkX3d685V12vgXiXUdkSXHsUom4GsA4pbFVdUnGKs/SpgIgu 2E5Mwt83Dd62LkdNSNIl/AQmranaA9qxL47JI3ae8EMS2DFcA/QZHC4OM7BdiRnZxY/D QQ== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3duwub4g2n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Jan 2022 22:26:30 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20SMFiJ1102183; Fri, 28 Jan 2022 22:26:29 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2170.outbound.protection.outlook.com [104.47.56.170]) by aserp3030.oracle.com with ESMTP id 3dr7ypma8r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Jan 2022 22:26:29 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ISA5tksYvz7oPcCxHmvqnshdfTycgbJzMgT8XJa++4c0t82H12jI4X1wUlFeGkUf9RgSb+Ubj3cwe6+vykwQVXOaFLmQ7bJg338+dEXgIYf+dfQnsKo+x/2BY8chWLSPvhhBJMMdPeHP6hnA6/e/qanLhorvZh2jP6qkhk3VqWlbCOQF30tixR9wO0y5zO2hGt1Tt/QIwAvX/7P5y2Uj/C2aMTxdLXkMXaMgQ041sErOvK9pJ4Jmosn7CiS0Nm3uMH3anMkOShjByAIDMw4eM/GY9eoqA8d1G+XuuKPjE5r+6FIhFnDs1H+ZqmCfaYSg8mPi/NTLUiJNPpsILJHTRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=IyGFvMgZRc4OkC4fwbRQpZz7cSALeDUuu6FeJv83v6k=; b=Md1iLYuZIk/32Af0wD0KT72w8z2Ia418iKdLKWEpwjjkge75M8R2DgLiywm/EFzwdsNhvepRJaOty7ij4wYSNNnJL3lokNRK+CwX4ZUnMFq7eVpwSX+zJjI8wdDUVqhqdK/BbKF1qN8BiRXTiCTJvZs/7yDjHBUjaInAzUGQuJDpyNYxLzOUs0/zRUzzoaStk7BRrGyRGPwfWxe4EyNStnwdXUkau4DE+yV5Idp6QLTPaKdBLH02kIqmqIkC+2iMTLlC4cXclQsVyoQ2QhRcKVpIyIm4kHZ+AYyroGcdlAmqD3lyeR40EcMH58+MiSPiQgyFmHxdVz5jxa9XYTBSzw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=IyGFvMgZRc4OkC4fwbRQpZz7cSALeDUuu6FeJv83v6k=; b=uUeBvngv1qT8CYxKlYm1rQ9EdFhzlFz3qjiLUkjwNUPlInB6UeXVUO0tKaozrU7RibiU6br54ZQJjRjhvo/KkmOEqyQvuC/Rpvm4l9NFPtF87aAgDLv454sw20vabZMj3D3m1V62gTrn7KEM+LnUIS6lWiL8MjGlE8anWuZ1xpE= Received: from BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) by DM6PR10MB2587.namprd10.prod.outlook.com (2603:10b6:5:b1::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.19; Fri, 28 Jan 2022 22:26:25 +0000 Received: from BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::90eb:bb07:608b:7556]) by BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::90eb:bb07:608b:7556%7]) with mapi id 15.20.4930.019; Fri, 28 Jan 2022 22:26:25 +0000 From: Mike Kravetz To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Naoya Horiguchi , David Hildenbrand , Axel Rasmussen , Mina Almasry , Michal Hocko , Peter Xu , Andrea Arcangeli , Shuah Khan , Andrew Morton , Mike Kravetz Subject: [PATCH 2/3] selftests/vm: add hugetlb madvise MADV_DONTNEED MADV_REMOVE test Date: Fri, 28 Jan 2022 14:26:04 -0800 Message-Id: <20220128222605.66828-3-mike.kravetz@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220128222605.66828-1-mike.kravetz@oracle.com> References: <20220128222605.66828-1-mike.kravetz@oracle.com> X-ClientProxiedBy: MWHPR07CA0016.namprd07.prod.outlook.com (2603:10b6:300:116::26) To BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4dd2e40a-9162-47dc-8689-08d9e2ad3842 X-MS-TrafficTypeDiagnostic: DM6PR10MB2587:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: F+qujmYDJ2V3f0fNvkGlBkHByNbv+vf12UrA10M99No1KzreajG17m31GInDl0wSnTxebczY6gg1XaoKLmGpJWLevmlXk1UjaDKcqpjDfEdEc6RuIIEFCE9g9BWZGj+32lDYCoW6xCgPOIHCdkrq0XBDP+gwUhzJgJV7EAjs8/ImDOCF6SbncOugLjSeQmEVQSqoZhTdG5qMzzGu+QwTjSqZTh7oenuxFWHwPFZ5mhjsJYLNVKJ3bFUsjUTQ7Wxj3z5RKHCqpzj1rJfdElzSC8fLShYI8j8F/MXBj+6i9v29VR7ENpMIGMLXrDDyQLQI0G4FqQ+nTpSqwm8yiYKnlQtSo4YPKfdNm0ja4XncR8Qdw8jtW28H4OWqYKddk2JVcRfRLsTk0aqwxN0nrxUBRyHeffxi7KFWbpZ9OBesq6jHuVPg1R+bInWibB6B+v1caXJ22eOzhcihLmr/1g5cqP9tzYJpTt94Q5fRdyhfO/kv9eZVy1VrEyTZeh/pzcg64VDEm0VXpIt5HqrZel8D4j2bAYPTmAwSvA9Kme4PeBDk/b6inJY7V1EP4J1q5CwRMLTybUEYAvUuIcHkgLaLtnLY8d7O4NltAvkZpO6+KQ0HWoeWe7agtGf/hO+07tShokn7d7ZKSmqEIKvsPSm+5ryaNEs6KCVKgeZ/rv23WYhdrIf4xe1SFjuIZApuZdM3YYloEmzzW5huX0wTW5FFQFv7dDw9xf7JgRVglX/pmWh4X78NDSH31AsGLmIfk3Sv9DTEYzNrIGyYubLehc9kRjvA7foGCguWXuoZUiWmJlk= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BY5PR10MB4196.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(30864003)(8676002)(7416002)(66556008)(66476007)(8936002)(36756003)(66946007)(44832011)(38100700002)(38350700002)(54906003)(316002)(4326008)(5660300002)(6506007)(6512007)(6666004)(52116002)(107886003)(2906002)(83380400001)(508600001)(86362001)(966005)(6486002)(2616005)(1076003)(186003)(26005)(20210929001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: yM+fsScygWHXP1zDUPrZ83MAV3msiEInyS5CbCdfLLXz426V8v+jSGE59QIQqNqNmRoFHwaD0MVD8fNiYMJs/qHMmNCM5An9L6ZBt+e+9w3Ri69ANjvpTywcZ8fVJh+gHSm2DBIuM/vxqHSNbKNHuHdHTmUG15zEDNp3jQPC/H3NshgSLOxxUPe6qFh5mFcakanOQVIUvqML6F9Mrdh5z2pQ/j//vnxkPYBu8OvQsWSXJojj59n882llFCesavLc30JyeZXw3qpy2hE7cAlWGTJnlG97m4+q9KWxO5qHsADOwEU+rNpL2GNfJmhhRW3NhZvmdBAaeAVb5r2dBBLtnn0DxhKNL6DVt1chBx3BMsP4RzD0VcOJD6LallZZTG/w7kBOv6Gdg+Yq1qUfUmFH95nP7CW1GsuLIkwioHN+2EY+iPIYp3lt9Fkdpgx3TNAulBpIykl3fn0OfUBBuEOE40aD4l1gSCeFgZZrCX6p14RJr8GswfMT4ZFLj6105XySrDpBmabVNwxWfrKdW1gHmprn+MG2zqqzDTb+9i2t3tiTetpa6TXW9XEV0wPLmmbdKB+TZTPrppz+BbY1T16497caT9DeJ3nolpHO5mS2SwyJmcgrpq2le6B1AhM/h+SKpYgxdpW3nq5CAGRCUYQDUAYTavi2iyo2f5CUB9d4WVWkoRzr4ZKAzmJPI3RclfvxOU6WlXfY7Zq8Rt3vkZMYyprjiLvrtC/zkJi5b4jvHIHsdjXoRc2Ap3T24jVWIvYvFjrbT7PkaXnns189cJdrOeXh/riK0+aXjZ1cYMKM38gdXkSmWgdoA/zkpcPj1ljhRwhtQgWrWmOGJOKFpQ1afix72EEvD8qRrfQtmtmz+V0hQrHWIrx3Xb86joGJCkpzmPP8HpTVtkkptEbn1qpn5wPDXSSqeG1V+TbSDF3fxHcCiUNkuXFxJqaonsIn5+8edzcqn61tO5eBVHq4GJgKxKsi/pl1At8FhaWt/FRx4N43H1fSS9sUBLPxL0BcbPu0j1XYmwAuD5WrZIX8abAqPX0HAbNNu/MavBgCPilzz9HC3vT2ZZEjMVWiCXsqgvmen7uXD85JBDSFBX1gT10FQo+xOkYrY8qkOR4bulKmPIhY48myKSGn0IQTcNxWzIOcv8BSUiOXlmmT7JgiHEA/UWlHKB6NjwheZbfm9MK5IPMtrLdtpXCGOwGCRPoMHE5jC8SytYdt6vS/4zJxKLrKnGVXCKT7bCUCFGqrtcfPHR3AYYFVzXo5tZBuTpkWsD3qrf055s2x8cYo7rIQ5c+OhiWUv7HzU7ozzvMiBIcKXdGR9yDBpuvJPFFg2V4pJ3iFpmJj16fPg7x2Z91T3SqGJXIoYWcZTHCU0/kGS5+lF+80CAKvlOZGtpiPrnWSOtvb7KkRPTzk+dZvbm/T80lD6jImV8p6ICXTnKUKar/aMGeuIbCH6EBbtIUqDAX21eiu0hPEq5oZnk1sMsJHusaBJl1BBDzwzrmhvYjcuHa1uD/bK0Q2W6xaXF6Mrj7IW4L32klMrs7sUE9MFqVy2lmqlmaPZRz4iVIjay53I9Ww3gSE7YmfiQCSsy3ytHagrl3gKO7R8S90FTNj1wvNMFW9Kjgxf4LlOxGaJoDH5bHs7yM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4dd2e40a-9162-47dc-8689-08d9e2ad3842 X-MS-Exchange-CrossTenant-AuthSource: BY5PR10MB4196.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jan 2022 22:26:25.1832 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: QycZUBtA/OQpxIUmXZFPlprHmZw0WG0KvTKV4n3cvG8Z/KGY9C2MtBdMDUsytscKV2k1DonHgGZlw5Pj3dv0bA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB2587 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10241 signatures=673430 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 phishscore=0 bulkscore=0 suspectscore=0 malwarescore=0 adultscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2201280125 X-Proofpoint-GUID: K6bUSamWaAFSlX5aQ51vEEVwDXDtqqgB X-Proofpoint-ORIG-GUID: K6bUSamWaAFSlX5aQ51vEEVwDXDtqqgB X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 799BDA0007 X-Stat-Signature: 3zweepw54dk7gaygh66kwgwjrxpfuiqn Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b=AuHOX36u; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=uUeBvngv; dmarc=pass (policy=none) header.from=oracle.com; spf=none (imf25.hostedemail.com: domain of mike.kravetz@oracle.com has no SPF policy when checking 205.220.177.32) smtp.mailfrom=mike.kravetz@oracle.com X-Rspam-User: nil X-HE-Tag: 1643408794-972118 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: Now that MADV_DONTNEED support for hugetlb is enabled, add corresponding tests. MADV_REMOVE has been enabled for some time, but no tests exist so add them as well. Signed-off-by: Mike Kravetz Reviewed-by: Shuah Khan --- tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/hugetlb-madvise.c | 401 +++++++++++++++++++ tools/testing/selftests/vm/run_vmtests.sh | 12 + 3 files changed, 414 insertions(+) create mode 100644 tools/testing/selftests/vm/hugetlb-madvise.c diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index 1607322a112c..f60cf43bbf61 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -28,6 +28,7 @@ LDLIBS = -lrt -lpthread TEST_GEN_FILES = compaction_test TEST_GEN_FILES += gup_test TEST_GEN_FILES += hmm-tests +TEST_GEN_FILES += hugetlb-madvise TEST_GEN_FILES += hugepage-mmap TEST_GEN_FILES += hugepage-mremap TEST_GEN_FILES += hugepage-shm diff --git a/tools/testing/selftests/vm/hugetlb-madvise.c b/tools/testing/selftests/vm/hugetlb-madvise.c new file mode 100644 index 000000000000..31c302528f2c --- /dev/null +++ b/tools/testing/selftests/vm/hugetlb-madvise.c @@ -0,0 +1,401 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * hugepage-madvise: + * + * Basic functional testing of madvise MADV_DONTNEED and MADV_REMOVE + * on hugetlb mappings. + * + * Before running this test, make sure the administrator has pre-allocated + * at least MIN_FREE_PAGES hugetlb pages and they are free. In addition, + * the test takes an argument that is the path to a file in a hugetlbfs + * filesystem. Therefore, a hugetlbfs filesystem must be mounted on some + * directory. + */ + +#include +#include +#include +#include +#define __USE_GNU +#include + +#define USAGE "USAGE: %s \n" +#define MIN_FREE_PAGES 20 + +#define validate_free_pages(exp_free) \ + do { \ + int fhp = get_free_hugepages(); \ + if (fhp != (exp_free)) { \ + printf("Unexpected number of free huge " \ + "pages line %d\n", __LINE__); \ + exit(1); \ + } \ + } while (0) + +unsigned long huge_page_size; +unsigned long base_page_size; + +/* + * default_huge_page_size copied from mlock2-tests.c + */ +unsigned long default_huge_page_size(void) +{ + unsigned long hps = 0; + char *line = NULL; + size_t linelen = 0; + FILE *f = fopen("/proc/meminfo", "r"); + + if (!f) + return 0; + while (getline(&line, &linelen, f) > 0) { + if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) { + hps <<= 10; + break; + } + } + + free(line); + fclose(f); + return hps; +} + +unsigned long get_free_hugepages(void) +{ + unsigned long fhp = 0; + char *line = NULL; + size_t linelen = 0; + FILE *f = fopen("/proc/meminfo", "r"); + + if (!f) + return fhp; + while (getline(&line, &linelen, f) > 0) { + if (sscanf(line, "HugePages_Free: %lu", &fhp) == 1) + break; + } + + free(line); + fclose(f); + return fhp; +} + +void write_fault_pages(void *addr, unsigned long nr_pages) +{ + unsigned long i; + + for (i = 0; i < nr_pages; i++) + *((unsigned long *)(addr + (i * huge_page_size))) = i; +} + +void read_fault_pages(void *addr, unsigned long nr_pages) +{ + unsigned long i, tmp; + + for (i = 0; i < nr_pages; i++) + tmp += *((unsigned long *)(addr + (i * huge_page_size))); +} + +int main(int argc, char **argv) +{ + unsigned long free_hugepages; + void *addr, *addr2; + int fd; + int ret; + + if (argc != 2) { + printf(USAGE, argv[0]); + exit(1); + } + + huge_page_size = default_huge_page_size(); + if (!huge_page_size) { + printf("Unable to determine huge page size, exiting!\n"); + exit(1); + } + base_page_size = sysconf(_SC_PAGE_SIZE); + if (!huge_page_size) { + printf("Unable to determine base page size, exiting!\n"); + exit(1); + } + + free_hugepages = get_free_hugepages(); + if (free_hugepages < MIN_FREE_PAGES) { + printf("Not enough free huge pages to test, exiting!\n"); + exit(1); + } + + fd = open(argv[1], O_CREAT | O_RDWR, 0755); + if (fd < 0) { + perror("Open failed"); + exit(1); + } + + /* + * Test validity of MADV_DONTNEED addr and length arguments + */ + addr = mmap(NULL, 12 * huge_page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, + -1, 0); + if (addr == MAP_FAILED) { + perror("mmap"); + exit(1); + } + /* unmap first and last page so we know nothing is mapped there */ + if (munmap(addr, huge_page_size) || + munmap(addr + 11 * huge_page_size, huge_page_size)) { + perror("munmap"); + exit(1); + } + addr = addr + huge_page_size; + + write_fault_pages(addr, 10); + validate_free_pages(free_hugepages - 10); + + /* addr before mapping should fail */ + ret = madvise(addr - base_page_size, 10 * huge_page_size, + MADV_DONTNEED); + if (!ret) { + printf("Unexpected success of madvise call with invalid addr line %d\n", + __LINE__); + exit(1); + } + + /* addr + length after mapping should fail */ + ret = madvise(addr, (10 * huge_page_size) + base_page_size, + MADV_DONTNEED); + if (!ret) { + printf("Unexpected success of madvise call with invalid length line %d\n", + __LINE__); + exit(1); + } + + (void)munmap(addr, 10 * huge_page_size); + + /* + * Test alignment of MADV_DONTNEED addr and length arguments + */ + addr = mmap(NULL, 10 * huge_page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, + -1, 0); + if (addr == MAP_FAILED) { + perror("mmap"); + exit(1); + } + write_fault_pages(addr, 10); + validate_free_pages(free_hugepages - 10); + + /* addr should be aligned down to huge page size */ + if (madvise(addr + base_page_size, + 10 * huge_page_size - base_page_size, MADV_DONTNEED)) { + perror("madvise"); + exit(1); + } + + /* should free all pages in mapping */ + validate_free_pages(free_hugepages); + + write_fault_pages(addr, 10); + validate_free_pages(free_hugepages - 10); + + /* addr + length should be aligned up to huge page size */ + if (madvise(addr, (10 * huge_page_size) - base_page_size, + MADV_DONTNEED)) { + perror("madvise"); + exit(1); + } + + /* should free all pages in mapping */ + validate_free_pages(free_hugepages); + + (void)munmap(addr, 10 * huge_page_size); + + /* + * Test MADV_DONTNEED on anonymous private mapping + */ + addr = mmap(NULL, 10 * huge_page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, + -1, 0); + if (addr == MAP_FAILED) { + perror("mmap"); + exit(1); + } + write_fault_pages(addr, 10); + validate_free_pages(free_hugepages - 10); + + if (madvise(addr, 10 * huge_page_size, MADV_DONTNEED)) { + perror("madvise"); + exit(1); + } + + /* should free all pages in mapping */ + validate_free_pages(free_hugepages); + + (void)munmap(addr, 10 * huge_page_size); + + /* + * Test MADV_DONTNEED on private mapping of hugetlb file + */ + if (fallocate(fd, 0, 0, 10 * huge_page_size)) { + perror("fallocate"); + exit(1); + } + validate_free_pages(free_hugepages - 10); + + addr = mmap(NULL, 10 * huge_page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE, fd, 0); + if (addr == MAP_FAILED) { + perror("mmap"); + exit(1); + } + + /* read should not consume any pages */ + read_fault_pages(addr, 10); + validate_free_pages(free_hugepages - 10); + + /* madvise should not free any pages */ + if (madvise(addr, 10 * huge_page_size, MADV_DONTNEED)) { + perror("madvise"); + exit(1); + } + validate_free_pages(free_hugepages - 10); + + /* writes should allocate private pages */ + write_fault_pages(addr, 10); + validate_free_pages(free_hugepages - 20); + + /* madvise should free private pages */ + if (madvise(addr, 10 * huge_page_size, MADV_DONTNEED)) { + perror("madvise"); + exit(1); + } + validate_free_pages(free_hugepages - 10); + + /* writes should allocate private pages */ + write_fault_pages(addr, 10); + validate_free_pages(free_hugepages - 20); + + /* + * The fallocate below certainly should free the pages associated + * with the file. However, pages in the private mapping are also + * freed. This is not the 'correct' behavior, but is expected + * because this is how it has worked since the initial hugetlb + * implementation. + */ + if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + 0, 10 * huge_page_size)) { + perror("fallocate"); + exit(1); + } + validate_free_pages(free_hugepages); + + (void)munmap(addr, 10 * huge_page_size); + + /* + * Test MADV_DONTNEED on shared mapping of hugetlb file + */ + if (fallocate(fd, 0, 0, 10 * huge_page_size)) { + perror("fallocate"); + exit(1); + } + validate_free_pages(free_hugepages - 10); + + addr = mmap(NULL, 10 * huge_page_size, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) { + perror("mmap"); + exit(1); + } + + /* write should not consume any pages */ + write_fault_pages(addr, 10); + validate_free_pages(free_hugepages - 10); + + /* madvise should not free any pages */ + if (madvise(addr, 10 * huge_page_size, MADV_DONTNEED)) { + perror("madvise"); + exit(1); + } + validate_free_pages(free_hugepages - 10); + + /* + * Test MADV_REMOVE on shared mapping of hugetlb file + * + * madvise is same as hole punch and should free all pages. + */ + if (madvise(addr, 10 * huge_page_size, MADV_REMOVE)) { + perror("madvise"); + exit(1); + } + validate_free_pages(free_hugepages); + (void)munmap(addr, 10 * huge_page_size); + + /* + * Test MADV_REMOVE on shared and private mapping of hugetlb file + */ + if (fallocate(fd, 0, 0, 10 * huge_page_size)) { + perror("fallocate"); + exit(1); + } + validate_free_pages(free_hugepages - 10); + + addr = mmap(NULL, 10 * huge_page_size, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) { + perror("mmap"); + exit(1); + } + + /* shared write should not consume any additional pages */ + write_fault_pages(addr, 10); + validate_free_pages(free_hugepages - 10); + + addr2 = mmap(NULL, 10 * huge_page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE, fd, 0); + if (addr2 == MAP_FAILED) { + perror("mmap"); + exit(1); + } + + /* private read should not consume any pages */ + read_fault_pages(addr2, 10); + validate_free_pages(free_hugepages - 10); + + /* private write should consume additional pages */ + write_fault_pages(addr2, 10); + validate_free_pages(free_hugepages - 20); + + /* madvise of shared mapping should not free any pages */ + if (madvise(addr, 10 * huge_page_size, MADV_DONTNEED)) { + perror("madvise"); + exit(1); + } + validate_free_pages(free_hugepages - 20); + + /* madvise of private mapping should free private pages */ + if (madvise(addr2, 10 * huge_page_size, MADV_DONTNEED)) { + perror("madvise"); + exit(1); + } + validate_free_pages(free_hugepages - 10); + + /* private write should consume additional pages again */ + write_fault_pages(addr2, 10); + validate_free_pages(free_hugepages - 20); + + /* + * madvise should free both file and private pages although this is + * not correct. private pages should not be freed, but this is + * expected. See comment associated with FALLOC_FL_PUNCH_HOLE call. + */ + if (madvise(addr, 10 * huge_page_size, MADV_REMOVE)) { + perror("madvise"); + exit(1); + } + validate_free_pages(free_hugepages); + + (void)munmap(addr, 10 * huge_page_size); + (void)munmap(addr2, 10 * huge_page_size); + + close(fd); + unlink(argv[1]); + return 0; +} diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh index 75d401741394..e0daf9ff0cbe 100755 --- a/tools/testing/selftests/vm/run_vmtests.sh +++ b/tools/testing/selftests/vm/run_vmtests.sh @@ -119,6 +119,18 @@ else echo "[PASS]" fi +echo "-----------------------" +echo "running hugetlb-madvise" +echo "-----------------------" +./hugetlb-madvise $mnt/madvise-test +if [ $? -ne 0 ]; then + echo "[FAIL]" + exitcode=1 +else + echo "[PASS]" +fi +rm -f $mnt/madvise-test + echo "NOTE: The above hugetlb tests provide minimal coverage. Use" echo " https://github.com/libhugetlbfs/libhugetlbfs.git for" echo " hugetlb regression testing." From patchwork Fri Jan 28 22:26:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Kravetz X-Patchwork-Id: 12729208 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 EE5D7C433F5 for ; Fri, 28 Jan 2022 22:26:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7054B6B00F2; Fri, 28 Jan 2022 17:26:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B5386B00F4; Fri, 28 Jan 2022 17:26:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 509C96B00F6; Fri, 28 Jan 2022 17:26:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0216.hostedemail.com [216.40.44.216]) by kanga.kvack.org (Postfix) with ESMTP id 3AF706B00F2 for ; Fri, 28 Jan 2022 17:26:39 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id E5225972EA for ; Fri, 28 Jan 2022 22:26:38 +0000 (UTC) X-FDA: 79081131276.04.3D859D9 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf24.hostedemail.com (Postfix) with ESMTP id 3A970180007 for ; Fri, 28 Jan 2022 22:26:38 +0000 (UTC) Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 20SMClLO025308; Fri, 28 Jan 2022 22:26:31 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=yqpG1+R0S7YWx/GMZU+Cz0Xpv0yKbr+4G04dykhKLrE=; b=KNc9sqPIF0QqAF91IjE7C19qBoR+vfv4viO7wR9EYSuCtynooSi/VZ0McZehfM/6GZte fjuRy/Eupa7K6E0YM+TaIEjmuVGUu46aLZZSp8Kjp7kRmBhFQ9J840i9+L6jIbOqfqLP Vp/wLDr58Q/xm6ulawtWv1DnIRqSq4EE/T6OSqnBoAaJaakOwJ0zzPuEobBr9EH3eONk XFgcAGEUgHJNKX0QIMszQdgCL6spxwl2O4fRlC1oFE6Er/xrThTPZcye1PkvNrAU+p51 QuCd72iVPmBamEg7f+WVgkHgeoqZCeIlA36JpKex7GOX5N755z0FjE6WxP7KboMsucCO Bg== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3duwrxmmj8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Jan 2022 22:26:31 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 20SMFiJ3102183; Fri, 28 Jan 2022 22:26:30 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2170.outbound.protection.outlook.com [104.47.56.170]) by aserp3030.oracle.com with ESMTP id 3dr7ypma8r-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 28 Jan 2022 22:26:30 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ymk5EerJNYWnljgjCV7FDkh51DkBqJePwPPd5X4w9sUn2rHe1istCc0HGoLP7bKcVO+eNvoWj8BzjoAT4lly52yFJZtu+EuCU2mO9sDAt3D+mbSo5oCZYHGdtc3YLUyEagK3K1fu9ZIq3uMKy8MVFFIalDshRmFTnor/QdWVpkQL1tXcqzh3NpFmX96n7fBtswomSq2wGgdof38AhN8AirH7Nf8OQXGZ2hYSaaGprJMMHBwSVAPnZI/zBwL9UCphMObTLPzyRLpvQz0sUAfhJJFtYHg3Qsmlp4IvxcRGA36/q2/HalKwEKZ5OFAacoMoZyl4F04yY/pG60JnPGWebA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=yqpG1+R0S7YWx/GMZU+Cz0Xpv0yKbr+4G04dykhKLrE=; b=RaWJmzzWUncBQEArN+pvJ+gSE4gFPgMBrW4BZuMkFOARbGWxydCs/1mYHEWW6e2v8oD4Gls9sL6N24MhfOYJTpgUwOF/J4T5e6l3BD2aMoJ4P5XEyMOrtq+A1ZpRBRB1kS4WMXfG1i9OXOj5wuct5ELSijDWHUMzU/1WVJyMLhkR/2zOHVnfbHk9HsNS/XcbPUBHlztJDtn6m7UMbxmkuEO5r8koZMwQjHa59zTXuqOZrEOwk1c20EppBZLD3MBzXuBsO4Ry/Q6HKK9I2cCSTqgTqGQlviFpa/Oo5aQVQY1cmw0Jn2n4d5naCWjpdyWxAQqANuncDdoUQ6Ep4oCRUw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=yqpG1+R0S7YWx/GMZU+Cz0Xpv0yKbr+4G04dykhKLrE=; b=R5Fudtp6YUmO5uRj22+GP6zbUY2TV/Xv89zNsLO1giBrEr25s1NGkER2WPGYUiCtvcConc4i0fWGYDIFyp0LdT2Q392pEPnCdMR0nS0xZ+HIZiA/AIjBqiaO7nwL+TPK4X65cjIDR9CUfCedQ2gsQuZ7wJg1a6F7tHEt/GCrdOY= Received: from BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) by DM6PR10MB2587.namprd10.prod.outlook.com (2603:10b6:5:b1::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4930.19; Fri, 28 Jan 2022 22:26:27 +0000 Received: from BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::90eb:bb07:608b:7556]) by BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::90eb:bb07:608b:7556%7]) with mapi id 15.20.4930.019; Fri, 28 Jan 2022 22:26:27 +0000 From: Mike Kravetz To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Naoya Horiguchi , David Hildenbrand , Axel Rasmussen , Mina Almasry , Michal Hocko , Peter Xu , Andrea Arcangeli , Shuah Khan , Andrew Morton , Mike Kravetz Subject: [PATCH 3/3] userfaultfd/selftests: enable huegtlb remap and remove event testing Date: Fri, 28 Jan 2022 14:26:05 -0800 Message-Id: <20220128222605.66828-4-mike.kravetz@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220128222605.66828-1-mike.kravetz@oracle.com> References: <20220128222605.66828-1-mike.kravetz@oracle.com> X-ClientProxiedBy: MWHPR07CA0016.namprd07.prod.outlook.com (2603:10b6:300:116::26) To BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: dffc2cdc-efb5-4d58-5ccc-08d9e2ad399c X-MS-TrafficTypeDiagnostic: DM6PR10MB2587:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DTpHd0c9N+dGjGsR4lpf5MEIneq5Puy0aJZPqZA5NGR4ZN77jGYRZVP+5jFfPOa5x8ejAmm3JlUJdH8SlAyozYmjRhR0FuI+06nb32hj4K9JeXYkm98s26eZGeEaIwbp5lUWh/w/EFqGYAeh687tqQSxBTRIJoHcirw3zmK/YPpE+ohHye7MZ9/BsZDJODTDe8g+FPJCZIq+4aAyEqtdkeiou6T7gmeEACQtB2VCO/Ui7n0iiwBi/TTBhObO684Iv4rnu928jIm2QJjwiwf6pXgLvm1UZNOF9SWyjf2G68rCK+qWPLt7mPTouA/oqr4vmEu5zJNPIFLlHqd/UILR+KZQaVVtcEr7pPaF63/6o53GYABhlN+BD7k8j9o+uItSGmHAnNSFQ7QGK3O2+lkUzlRTRn38dD3kAzym3MOD8SZNZyh/fszPgfaWOk+S0KYkx2hNmKzyihSFzngl652QZw2gzAVPfEtq9025EroY4rxL+AWYbKG5Yb/ILYx/a9Q6WVEHU0qcXiNI82nq1wJxw2LFLeXpsx3bdiOU8gEceCj9eopyliIpeszd/tUNIzqoQmQyR4XkDNSmha4dWC30Gd+kt2i2P8PQaw0HgN3QL5uEQFUTUhH4bjh2mNUrKHM/SkpU6aTRFVnH7vcsK0spvnmDyxro5ZejXSyfjGv10dQl6zrKIZXPwOiqWyhl+ppiMuKLlYzAOrls6zsAConAtA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BY5PR10MB4196.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(8676002)(7416002)(66556008)(66476007)(8936002)(36756003)(66946007)(44832011)(38100700002)(38350700002)(54906003)(316002)(4326008)(5660300002)(6506007)(6512007)(6666004)(52116002)(107886003)(2906002)(83380400001)(508600001)(86362001)(6486002)(2616005)(1076003)(186003)(26005)(20210929001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9IkqiqXdBZgGmP9NUiGDpl8S+NC/VPLb44ikN6FhSZ8lPlw+p2Gjhm7Jm+uHGE2wk/04a6yYoTepHXnn8xX06iZxqiqU/YcQOktrDTwx1fX6DfBeP9c6AIeU0VKkv22PIf/Oha2ZPOq2nSS+qm//OrfuTzSY3d4ipzkbHWVyZIzyl4wDMhUjUpSAx/vxStqFf1AITa3GxtwQf7n4pqyC5kKjWmtfrNz35Dfc40fI99PCE6HDSztDuWdrlIveb0ECzYSdeSEje4IibZ/LI29RS7IxRNlobgSM1qhstIyywnbyg0EPmzrY5dqLPuisGQMinGOGh2c60PQKva2n0Apl/vXOZrBPQ2upDIf2sew2KAYDTl8WgpjXJhKxdv4qxT3q4jzBdCz6oBSAlq8Hrsz+aObBgPeXyzGtntTXQvpQA7wZKNjQXo5OAPXMgsOnmIFJLEEBmV717elZ1NQHnJh7E/o2oStrqlgQhrIrJIDIQTW96CVxM0pLwArdyhhu73+4W095aI9HeJhXpWbipia1gmIB/k/PfQBh4AGFBOOm87tQ0B90Fubo3aS6dpg0xsArV+zJX1S9OkuYn2F354CO8eGMgDkt3XPJE/2mkCvoUsLsCqyKhilDfv23Ddc3tORBGhr0Fqag+gK+y7qW1NbSitFsixVK1QGY/QFVvoIxg80oUf/tg8hSmZjzI8eLy2mevXCws47OVnlFZ+tLQxLRxFWb7uj4nHQjSS6wsGl11rymqdcwyve5aKE7EViT+LbC5E01LGfgAEm5+dyW5q6TMxY/RKaZPcG71nbV4Wg7IpAEbqVG4DzDtJmDBlM+kbfiS97tfxpr9oRIav84/oU6cy5czsjHazk5gK/iCkQSkT9nMC2OAA7kpOnshrY4mNZyBd+OTPaNF+icGXGplpTBlu7iUPqAjk74MXrctETYbSC2wW6GcWfXZKzeoYcjHLMMi/q4xjZzPbPFjNmutzE07d/Wjxnh+G93JBeKruTFfj8Pl+TKWNLHYJ/XApHiuUzsIhFj/0byOCodNz4O/80153uWhVAWw3I2yYq4OJm5liEg6hbLM5NzdqQt832SgY1ZSYrh3npaOm3U8NDm0VfYWjmOd51xteff7Q9/MINYgI98FOUMH7mOaN99JWz8LNOqXuIsOTncZ2DQs/+/AYDKbqL9EIyFQR622HUuPlBD9OmrdzwAwHJ1hm3UlXFCSLYACTxMcTalRnyT7fhW7kpyJh3o7INKyC5dpNFpyo826JQkrwJCDxtCBxsV8IxCZ041v4w6iO4jYZmLCuibUE/2aOATgoSNDxTxUmjN5fXjg1NyoGiAPuIer9lDG1Dq9tW6ojAv+6fOIX6zdiVtqw7065ZR8mjbUZ+b3OwAxhWJI7EDe7Cb1ZRasnYS4Xmv7pPTiov0AHP9TIPPaepQx2D5NjfFvY3Wy9FOcZrtfVS1Z+8RqGdelNJ5re29YrDb6+rdVUcenIrqS1nJ834LKpafWM4vk0MpGUKq6PEB9cWfHn12bQRKySIOQ+d5wuCVD9emItdJnogj7odZ3BLJrbYi5nPiyjP5On56v8vo8vbJwlTVXlp0u8jYwkr4W4AnlRFxE2mnytOwZqyQSh3vEp51IoRQGxCFQn1XX0LAavN5cpU= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: dffc2cdc-efb5-4d58-5ccc-08d9e2ad399c X-MS-Exchange-CrossTenant-AuthSource: BY5PR10MB4196.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jan 2022 22:26:27.6072 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 5YYOGcYIZO4JBVAP357LgA9gXG22HXj4nEdOz7FmJB8AgTKBnVii6ZI7JplWAw29dZ/79EaslUm8uyL34uJquw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB2587 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10241 signatures=673430 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 phishscore=0 bulkscore=0 suspectscore=0 malwarescore=0 adultscore=0 mlxlogscore=999 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2201280125 X-Proofpoint-GUID: p0VjFWuNcIM0NxSGoJp-e9OMb4gzw-_a X-Proofpoint-ORIG-GUID: p0VjFWuNcIM0NxSGoJp-e9OMb4gzw-_a X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 3A970180007 X-Stat-Signature: 9faqu7545hm1wizqcen5enbhqhf6bcat X-Rspam-User: nil Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b=KNc9sqPI; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=R5Fudtp6; spf=none (imf24.hostedemail.com: domain of mike.kravetz@oracle.com has no SPF policy when checking 205.220.165.32) smtp.mailfrom=mike.kravetz@oracle.com; dmarc=pass (policy=none) header.from=oracle.com X-HE-Tag: 1643408798-836920 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: With MADV_DONTNEED support added to hugetlb mappings, mremap testing can also be enabled for hugetlb. Modify the tests to use madvise MADV_DONTNEED and MADV_REMOVE instead of fallocate hole puch for releasing hugetlb pages. Signed-off-by: Mike Kravetz Reviewed-by: Axel Rasmussen --- tools/testing/selftests/vm/userfaultfd.c | 67 ++++++++++++------------ 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index d3fd24f9fae8..f5578ef85560 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -88,7 +88,6 @@ static bool test_uffdio_minor = false; static bool map_shared; static int shm_fd; static int huge_fd; -static char *huge_fd_off0; static unsigned long long *count_verify; static int uffd = -1; static int uffd_flags, finished, *pipefd; @@ -124,9 +123,9 @@ const char *examples = "./userfaultfd anon 100 99999\n\n" "# Run share memory test on 1GiB region with 99 bounces:\n" "./userfaultfd shmem 1000 99\n\n" - "# Run hugetlb memory test on 256MiB region with 50 bounces (using /dev/hugepages/hugefile):\n" + "# Run hugetlb memory test on 256MiB region with 50 bounces:\n" "./userfaultfd hugetlb 256 50 /dev/hugepages/hugefile\n\n" - "# Run the same hugetlb test but using shmem:\n" + "# Run the same hugetlb test but using shared file:\n" "./userfaultfd hugetlb_shared 256 50 /dev/hugepages/hugefile\n\n" "# 10MiB-~6GiB 999 bounces anonymous test, " "continue forever unless an error triggers\n" @@ -223,10 +222,13 @@ static void noop_alias_mapping(__u64 *start, size_t len, unsigned long offset) static void hugetlb_release_pages(char *rel_area) { - if (fallocate(huge_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, - rel_area == huge_fd_off0 ? 0 : nr_pages * page_size, - nr_pages * page_size)) - err("fallocate() failed"); + if (!map_shared) { + if (madvise(rel_area, nr_pages * page_size, MADV_DONTNEED)) + err("madvise(MADV_DONTNEED) failed"); + } else { + if (madvise(rel_area, nr_pages * page_size, MADV_REMOVE)) + err("madvise(MADV_REMOVE) failed"); + } } static void hugetlb_allocate_area(void **alloc_area) @@ -234,26 +236,37 @@ static void hugetlb_allocate_area(void **alloc_area) void *area_alias = NULL; char **alloc_area_alias; - *alloc_area = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE, - (map_shared ? MAP_SHARED : MAP_PRIVATE) | - MAP_HUGETLB | - (*alloc_area == area_src ? 0 : MAP_NORESERVE), - huge_fd, *alloc_area == area_src ? 0 : - nr_pages * page_size); + if (!map_shared) + *alloc_area = mmap(NULL, + nr_pages * page_size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | + (*alloc_area == area_src ? 0 : MAP_NORESERVE), + -1, + 0); + else + *alloc_area = mmap(NULL, + nr_pages * page_size, + PROT_READ | PROT_WRITE, + MAP_SHARED | + (*alloc_area == area_src ? 0 : MAP_NORESERVE), + huge_fd, + *alloc_area == area_src ? 0 : nr_pages * page_size); if (*alloc_area == MAP_FAILED) err("mmap of hugetlbfs file failed"); if (map_shared) { - area_alias = mmap(NULL, nr_pages * page_size, PROT_READ | PROT_WRITE, - MAP_SHARED | MAP_HUGETLB, - huge_fd, *alloc_area == area_src ? 0 : - nr_pages * page_size); + area_alias = mmap(NULL, + nr_pages * page_size, + PROT_READ | PROT_WRITE, + MAP_SHARED, + huge_fd, + *alloc_area == area_src ? 0 : nr_pages * page_size); if (area_alias == MAP_FAILED) err("mmap of hugetlb file alias failed"); } if (*alloc_area == area_src) { - huge_fd_off0 = *alloc_area; alloc_area_alias = &area_src_alias; } else { alloc_area_alias = &area_dst_alias; @@ -266,12 +279,7 @@ static void hugetlb_alias_mapping(__u64 *start, size_t len, unsigned long offset { if (!map_shared) return; - /* - * We can't zap just the pagetable with hugetlbfs because - * MADV_DONTEED won't work. So exercise -EEXIST on a alias - * mapping where the pagetables are not established initially, - * this way we'll exercise the -EEXEC at the fs level. - */ + *start = (unsigned long) area_dst_alias + offset; } @@ -424,7 +432,6 @@ static void uffd_test_ctx_clear(void) uffd = -1; } - huge_fd_off0 = NULL; munmap_area((void **)&area_src); munmap_area((void **)&area_src_alias); munmap_area((void **)&area_dst); @@ -922,10 +929,7 @@ static int faulting_process(int signal_test) struct sigaction act; unsigned long signalled = 0; - if (test_type != TEST_HUGETLB) - split_nr_pages = (nr_pages + 1) / 2; - else - split_nr_pages = nr_pages; + split_nr_pages = (nr_pages + 1) / 2; if (signal_test) { sigbuf = &jbuf; @@ -982,9 +986,6 @@ static int faulting_process(int signal_test) if (signal_test) return signalled != split_nr_pages; - if (test_type == TEST_HUGETLB) - return 0; - area_dst = mremap(area_dst, nr_pages * page_size, nr_pages * page_size, MREMAP_MAYMOVE | MREMAP_FIXED, area_src); if (area_dst == MAP_FAILED) @@ -1667,7 +1668,7 @@ int main(int argc, char **argv) } nr_pages = nr_pages_per_cpu * nr_cpus; - if (test_type == TEST_HUGETLB) { + if (test_type == TEST_HUGETLB && map_shared) { if (argc < 5) usage(); huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755);