From patchwork Wed Feb 2 01:40:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Kravetz X-Patchwork-Id: 12732500 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 5DF8DC433F5 for ; Wed, 2 Feb 2022 01:41:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BF7ED8D009E; Tue, 1 Feb 2022 20:41:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BA5DD8D0077; Tue, 1 Feb 2022 20:41:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D16D8D009E; Tue, 1 Feb 2022 20:41:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0100.hostedemail.com [216.40.44.100]) by kanga.kvack.org (Postfix) with ESMTP id 7BEC78D0077 for ; Tue, 1 Feb 2022 20:41:11 -0500 (EST) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 2E4D299EA5 for ; Wed, 2 Feb 2022 01:41:11 +0000 (UTC) X-FDA: 79096136742.10.6C8C8C7 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf23.hostedemail.com (Postfix) with ESMTP id 9E203140005 for ; Wed, 2 Feb 2022 01:41:10 +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 2120XCvT015791; Wed, 2 Feb 2022 01:41:07 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=BvqQ5y3f3dwZNLlUZ9/s580M/wXc76Av07SpkWkj8W4=; b=zpsylloabR5ouDmWX3WUhbmKt7BmBO2/eouDA5Tb9sfnG4jy16AIx70o6As9db9VYtSW r8Qph8u9zYT7H7lkrmdHma/Xreu9epBckgkAcDya5JgTlHkIIIukHW/mpTOGSYCabCVu Fy2dqfEuyCZf9C75ylAx05BrH6mE1DoY6dZDJayOgJ7noTg8okntaP4DDLRofmaL988G O+LzMrF2sY3N5oDCxE71Va1oi9MUsAqzDt60Sw/d+qfI8WxJIwVzyWMlQV+BeCLu+yig S20cC787vIzJkokblg+oHdsX/zoVXUM4cqbbxfHS9uUy2BcKfwlAonOgG2jO0L6oD+GC TA== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by mx0b-00069f02.pphosted.com with ESMTP id 3dxj9wcje7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 02 Feb 2022 01:41:06 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 2121VbwZ024096; Wed, 2 Feb 2022 01:41:05 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2175.outbound.protection.outlook.com [104.47.56.175]) by userp3020.oracle.com with ESMTP id 3dvy1r63uu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 02 Feb 2022 01:41:05 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DeprMzFyiaVxUpyhz4i8M/WVp12RkbiJz6YN7SLfSYinSlQucM5nBXqKGqqEMsmkAemmkMnZBSHRv5RRx04V1MPQoyr7BZFdZloT96L5HvPRsxWjJbsrIg7SQ3x5LISaotoBKZ1qPre5I980yKJYAaJuoy82FFk6g3+qKPzwcwdh1YvxgXcDgg+qS/ucS0GnFcAoIlJGK2Lt4mLn3ZV+kth3HhgdjUeoH0IrhS5MVcJ1a8XYQhXe5c11MAftrjJHffgd1obvQgaVUzwIgOC8M5wZmxgUxz0H0KHJ2xioXIFTa0sSJFWb1Pb7GSWVgqYCP4YVkqag7CDz4Gctechblw== 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=BvqQ5y3f3dwZNLlUZ9/s580M/wXc76Av07SpkWkj8W4=; b=hEjfwCm5nWmpu4nBTdp9QE9AbceGsMopyZGFYvOHo8DsI09THL0Fc1KX7ypfMNsH4+HJJQ+V7B3OR8ra2xzPu/faUcj4xuOMvusyCR5/X6U5mzWLRA7cAy2HUUjFS1cjDgZ/p6TYua0PdHONmhhDLFVcJ66U6FbV3/qmh1ZuT+j8Gg8AgPSzC5Ls/T8Uv/3iHdSBPfGOpTcWtGQkuHprBW7SekYHy1pA4o1H+eoEfoauj41DVnvPaLOxvs2xDkgqbAet8wl4csUefX/FXhFH3pUfRyev8zP5QU/NdP8HtFojq37u2RqhAJnxcGiVzVxCs7JL/+d3F/soi4eX6lcfPA== 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=BvqQ5y3f3dwZNLlUZ9/s580M/wXc76Av07SpkWkj8W4=; b=sry0/RUR63B6Zm390daNWgrLh05VwgG+U4qGYIVNARLB8BnKJ4qL05dIJQFbcb0uCWVZLCPH2c/Y0KtulQ7nN2KPJUI8Wk+LcgU+Pj36rQfHhzwfNGrFJJgqkI9flj8qo/cYwSTYf8g8oKu7Ahk+d+hRZeRYq0x5cvYeTKiGcpE= Received: from DM6PR10MB4201.namprd10.prod.outlook.com (2603:10b6:5:216::10) by DM6PR10MB3433.namprd10.prod.outlook.com (2603:10b6:5:61::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.11; Wed, 2 Feb 2022 01:41:03 +0000 Received: from DM6PR10MB4201.namprd10.prod.outlook.com ([fe80::1057:d8d1:8372:c8b4]) by DM6PR10MB4201.namprd10.prod.outlook.com ([fe80::1057:d8d1:8372:c8b4%8]) with mapi id 15.20.4930.022; Wed, 2 Feb 2022 01:41:03 +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 v2 1/3] mm: enable MADV_DONTNEED for hugetlb mappings Date: Tue, 1 Feb 2022 17:40:32 -0800 Message-Id: <20220202014034.182008-2-mike.kravetz@oracle.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202014034.182008-1-mike.kravetz@oracle.com> References: <20220202014034.182008-1-mike.kravetz@oracle.com> X-ClientProxiedBy: CO2PR05CA0085.namprd05.prod.outlook.com (2603:10b6:104:1::11) To DM6PR10MB4201.namprd10.prod.outlook.com (2603:10b6:5:216::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 63c47189-2d8b-451c-5648-08d9e5ed129c X-MS-TrafficTypeDiagnostic: DM6PR10MB3433: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: 9aIYk+h7GTkMJYfoNXq5Y1bhQloRFP+KQN3+dGexNA/nCMB0cTZWYkkn9KCG/WpAfT3UkCSMOFSLz0qBWqzALYAy5HfMAINWm6Bdwo1xBVP2v5wvtt9+D/mS1HF+rne67DlunjuaycI5ZgfgX3qa/nwE5tlAefrPqX9B38sZoZF4nCv4bUQ/jPz1st8Xpkvw3WYcK320qgaw21Igp2S0/GTs/xq1+6X7ERaMz9VbycTJt4IvNcriCdDkuoGgE1LLXQTgTD2ZRO0ADRvjTM1RrsDFhFIMfLn/1+tIx4ov4uNsjZSMNp17KXLxsPXPkWyBqGv9ZYjJ2fUJGXEiV9RhBL7hZBe5hbfsywz/yv8zDBR8yNqo9j9kCf50bDnAJibgmj+qbAmHbkEUsQGqisRj00RSkYhiW9BF3+0B6GNo6hRPmlZRzeDzKGatJIvS/uujOF8OAFs/dhCy6hlKJcuXMwYnvbAx+OEFMMCbYJ8eyrs4sf0ef4BJn8hmBk0gfiB+8dsKeghRgn9JdqRThuwZt5FiKaTnPErrU7OoTly8C/YGDQNVVOhNh6JeGDeIn6hymoBEnGv9jESpzBDWi6K4zazCUYVt6Pf3OlVJqfy0MR6ZGtQbCdQyQyjSYq8/xzh4nKgy9w9NIiyUUW4dd+Of+9tJtFQOtJoBDo5/aga4W9zHUy2MLcPaHMetzwznVqrdFUY0U4xj6hG1Uymng13qAg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR10MB4201.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(38350700002)(7416002)(54906003)(44832011)(5660300002)(86362001)(38100700002)(316002)(4326008)(8936002)(66946007)(66476007)(8676002)(66556008)(6486002)(508600001)(83380400001)(36756003)(52116002)(107886003)(2616005)(1076003)(6512007)(26005)(6666004)(186003)(6506007)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-Mess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m9BShx4M8AgdsP3+yiwY92/CgD3NWeBSf9sCg2wyRX4csdR5iYrcfyBGhHYGUgVa3ARhniPbhaUUwlH9oJHJgNpXCdKJeyRN1XBJREUhbdcaUHlw6TRMRB+9SSpnDP29VXrE9CwM6G4sxXv5PxZkoKn2PN6qm5hw0aKOvXhbNptUoeDU3emB7f9HbO6fA8tO9jIQ7oLTL9sstepKl3Fas0EF80UXhScgBJBRBgTXrc2LFR6zFjselGMGWGUQG5qE5SL29yF/eQt3jxa5mlipOyU68luwCjz/DQV5UY8iFXTf3Gafv4jouRJYFA2P2SxBaYH4AP1lUZijDNcm4NJswPBfKObuWImh0rI7q7YrHOpWGDaWTRawy6EKpTwHq+5zU7ayBR+QtPpHYiufY1+zvwmRgrfGE3WvBWM0nmwOuzP7Onr6F56O+Ofn4G2bAkxawwaXDYKrWrXrh4GDK1/aHJ2RhqCZUUHEJHLkELz7K1Ua03vyJdC9zIqFchgNjToA681bfVoVsr8GQl7NnTKIu+GPc1RVzpH4Ods/B5ujpiMnZcoLQEcTcD3hSjQUI= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 63c47189-2d8b-451c-5648-08d9e5ed129c X-MS-Exchange-CrossTenant-AuthSource: DM6PR10MB4201.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Feb 2022 01:41:03.3130 (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: A6om8MX0LGt2DlaRyVpCXXc8C8pZDIQL215JN763o7hUuBSIlaUCoCmMcq+3QLRh5mwJjCu3FZ3CXzNeCsrYVw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB3433 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10245 signatures=673430 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 mlxscore=0 spamscore=0 bulkscore=0 adultscore=0 mlxlogscore=918 phishscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202020004 X-Proofpoint-ORIG-GUID: 6gTJYGYbgXd2uZTpWJST8zAVIS9xH3QQ X-Proofpoint-GUID: 6gTJYGYbgXd2uZTpWJST8zAVIS9xH3QQ X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9E203140005 X-Rspam-User: nil Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b=zpsylloa; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b="sry0/RUR"; dmarc=pass (policy=none) header.from=oracle.com; spf=none (imf23.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-Stat-Signature: 4mth17ar3t66tgdbewwibrtuo7r3kf9m X-HE-Tag: 1643766070-179860 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..7ae891e030a4 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 (is_vm_hugetlb_page(vma)) { + 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 (is_vm_hugetlb_page(vma)) + 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) { /*