From patchwork Thu Oct 7 18:19:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Kravetz X-Patchwork-Id: 12542597 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5500DC433FE for ; Thu, 7 Oct 2021 18:19:44 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EA74261263 for ; Thu, 7 Oct 2021 18:19:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org EA74261263 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 838D7900003; Thu, 7 Oct 2021 14:19:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B3646B0078; Thu, 7 Oct 2021 14:19:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 606046B0075; Thu, 7 Oct 2021 14:19:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0062.hostedemail.com [216.40.44.62]) by kanga.kvack.org (Postfix) with ESMTP id 528F86B0072 for ; Thu, 7 Oct 2021 14:19:43 -0400 (EDT) Received: from smtpin32.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 001C52702B for ; Thu, 7 Oct 2021 18:19:42 +0000 (UTC) X-FDA: 78670454646.32.7539975 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf29.hostedemail.com (Postfix) with ESMTP id 83783900259F for ; Thu, 7 Oct 2021 18:19:42 +0000 (UTC) Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 197I5bc8025160; Thu, 7 Oct 2021 18:19:40 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=ztRI9qgc0t3v+U02wSqBcenJLvI0kf9QUfmuM/9rRZg=; b=cz9TvFrZKFxVCKNXnlxPtP6sEXubOpyWpBC3WJBW6R5jfGT6/kr2Pn5559gsu5oMeCjK yU+blKTr9+DYeF0WqciU+YrfQHkueQYJdIywRvwe9R5pdgDCkWAybnGyzqVbSUzRthwp z+LJBln4Jjj33H4Qr0y5eZ3cN3e2vnRfHN8lspKd8dyKn8dufrr+wrkD3TXB9oHGuKTs T/vK/d6O5+m8qJcU7wMXUP11/KoOfCEKtQdmamuUuOr1V9mTTdWEOuq3DDW/JxqxrQnR aGVmLJvuWWC9vb+djf1/dBH8T0y9iio2jGUUxrpTI9AvWXAauqgaZz9iYTpR+BS71loX dw== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3bj0pw2vfa-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 07 Oct 2021 18:19:39 +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 197IA8BP047007; Thu, 7 Oct 2021 18:19:39 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2042.outbound.protection.outlook.com [104.47.66.42]) by aserp3030.oracle.com with ESMTP id 3bev7ww3ea-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 07 Oct 2021 18:19:38 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=On3VyucICEzczI4ZX+Tr85bWNvqFceqkdwHYqdoNfuM8g+HLJgIUNq/4SCF1PG/FuA0yUvEAsctz8DrGm+/bVaQkH6QGQg7hXM/6IriCyfaORnSg0iw0i5Dm+zXAjq33BCzASXSGhLmO9/k2W0oTXu+9n3CYkZ3nRaleUpJ58IrK94RShHSodeUaLhKsCiekhDafP84p9IfiQv/d3HPIeqfEU3oEWXYYNHRlsgcXWwCpmTAt0nFWkNjWxO9jmzh4hLNkTwGqlqodG+lbHRCHDTZxsRFs/xP3jjjO+IBF/FJXJb4F6O3Hu0UI/h9EVo5ECaTKmsYtkYWEp81dh/bMrw== 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=ztRI9qgc0t3v+U02wSqBcenJLvI0kf9QUfmuM/9rRZg=; b=dnbmizEcDEX9cij/r3IDe7y9vq8/+59/Ll9CvggU2ErVOdLrbu2DnT+2tuM4cEDDivuzBHK+BRrWEWMRcwmNmNvaccdirg08AjHj2xOdE7/fFKyoWibkunvgDpga0NH/jHZiX6lzT6Fbo8nX8jwyZVggZ0/whX+Oh1EjaKJ37tddgGI0p7xmVCvD186s5qCbbC1yExgzNi2ao06gE7UnqtINLSCpo7a3FEirrhbgRF+qTjej7VCJ+iv90Be1V4NGRwRuTwyGOX15tZLmrXckIy6Q8qF25UHxvuKHtlPg4gKD8AX079bamTrd5KgZrC46xDneNYcYZ3ycSI97hOnIcA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; 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=ztRI9qgc0t3v+U02wSqBcenJLvI0kf9QUfmuM/9rRZg=; b=oqADCcqSsTP7SJPFRJs0TOq8KYP42fOrrgLcZKEiBg3TD4npy7kvqV1caHZIl+70C51MEZ9wfJj6jeIzF4VBBzLeoWmjWrbYNXnFgb10QHOSUR9kOV8OwBo+W/ZjvSkNbVcO4W+O1GgZFjwH1a8pdwTjx8JCMU2Oe5j3CzKut1A= Received: from BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) by BYAPR10MB2552.namprd10.prod.outlook.com (2603:10b6:a02:ad::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4587.18; Thu, 7 Oct 2021 18:19:36 +0000 Received: from BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::bc59:71de:1590:cbf5]) by BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::bc59:71de:1590:cbf5%7]) with mapi id 15.20.4587.020; Thu, 7 Oct 2021 18:19:36 +0000 From: Mike Kravetz To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , Michal Hocko , Oscar Salvador , Zi Yan , Muchun Song , Naoya Horiguchi , David Rientjes , "Aneesh Kumar K . V" , Nghia Le , Andrew Morton , Mike Kravetz Subject: [PATCH v4 1/5] hugetlb: add demote hugetlb page sysfs interfaces Date: Thu, 7 Oct 2021 11:19:14 -0700 Message-Id: <20211007181918.136982-2-mike.kravetz@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211007181918.136982-1-mike.kravetz@oracle.com> References: <20211007181918.136982-1-mike.kravetz@oracle.com> X-ClientProxiedBy: MWHPR1701CA0012.namprd17.prod.outlook.com (2603:10b6:301:14::22) To BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) MIME-Version: 1.0 Received: from monkey.oracle.com (50.38.35.18) by MWHPR1701CA0012.namprd17.prod.outlook.com (2603:10b6:301:14::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4587.19 via Frontend Transport; Thu, 7 Oct 2021 18:19:35 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f9e528f3-5d95-40cb-191a-08d989bf04bb X-MS-TrafficTypeDiagnostic: BYAPR10MB2552: X-MS-Exchange-Transport-Forked: True 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: JY9zgc9p5ZQ4MGU+H7SWhlPpI0FPHRgIENLz1dS7k3ufW6kMXfdfm09JNVMn8i1suffa/6qZT2B3DY2ZHHHy7mDdYmK54PPmP4PzwgRGtlrEF/pkYqjKyOLVtZG4bWxcP0Y6Ddbo2bjkzKFNLrxECdJwXIfj1jtA2Am1J2FeGRSey3x59vJCLOdaGrb7s6lmp2eJ2WeuYcIjZUMYRaUsvW4Ys1TVVwAWHU5vs9dKiinTJSdaYSVTPs1CAyS8lWv5Ka6ias4Ycde8hDXf9rxhdv51MFjOG9Lmc6sv8buOD2haPZV3HfRNZmJ2Zphz03WWhtcIyv/tbhCTv1uM3Ib/E8mNHYaTzb+AlTn9yKQ44nX5W4daiguJlOxxzSiqQOqyldMvCG7TlmdKhxYpfkkkie+yYjHWp8t4mgUsBU5pekPvhm3MPjRYQ7tG0XyhxUvhSv5j+nGTfJvC3X40kaxMYJ6cQS0uhEyacPhjtZNT3/WX2ipGw3M9bLR+ppJFP+7Ie8+xGuaAmjBuU0z+8u5lzTesXSjuLNkzyq+vtitJvXzSQL+9nqg44ZNG6jwb/wUiOFGR4SNsxjmup4J0+Ql0AM4jfWiEg06ng7TDRGOWuw/udazRwFHNZJZOn7J8hDTHXW29Ku4mlipFg7rNcX8HNvdOXzUmOJ671HGHp+cm2bdTBRf3bVkjDIaCoajog7saYjdPT6QoMSE4By19wC7iUw== 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:(366004)(86362001)(38350700002)(316002)(7416002)(44832011)(5660300002)(6486002)(107886003)(2906002)(1076003)(186003)(508600001)(54906003)(38100700002)(6666004)(66476007)(36756003)(66946007)(4326008)(66556008)(52116002)(7696005)(956004)(2616005)(26005)(8676002)(83380400001)(8936002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: bYSrn1gd/h+jzt4EN4md/QoAF2ZYI6WwcTsb+ZWSPe3UqYQaQpShaR3hfLqhUZZrw1FqL91mjwauCH1vIbLKBsbo2KqTzBc1j6/qKiHb4B/i5oZTEBpTDxTU9+KBFnOGLheA/CXezAMBgkXa0poHRJjnq1IzRT0ruq9Sit69jc60LGASs2qQz0t9UsRNiuzYcuZZQ/LeKSJFlB5mEVOU+9iVR9NzvbAxTv876CHNpF1fw/08oDZP3AE/lBwZIesE+6EbLHT1dS9bN+H3b8+9uXTOFwfcGbMx8zqNhSZ44lT+H4u7VFQlUWUYiRXSup2edZ9Kvrm/6dNO2wRGpB6Zpi6J1kVZX0qYjinC8GWyDEEkRlC6OdSLBJE06rXpuygz5unl6hs55lPHZWkqilikOmZyuSjR14YkgFf3lG67FU2H0kXk0K3hXoZsBqCDd8lY/F7LtdYh2iNz/r+vaBPYi0y84tGsNDFgAWlPmRaGLJe2oOrv62Hb9HBRoh16mFv3EKQc0rToVCcISWPma3QBh4WRECrFonduqRIw5jxNIIQ3NSpHWiw4okzZOrkXjMjlZIe5Nhnp3VcaVwOMPqeeN0nsScMANOGx6Lix3Ut7YaTqJ7ckqf9vmXdJ63otzSYTRF6lVcegyFW6cPRrWjZQsmtsbtZHKLi1WcQbaheF31YsPadFnQBIOkbbwjYpK45hglAXeZuhEiWGpody57fC7I4XdCacYTflKGs4NRDsP+dXq/QGjJPtvUj00szHARPE3XLA0R7/aiLObHvpvrhm/ehcMcIelYRNwm37zlFgv4G0OamAlDVf86sKELBQEoq5h9pEkka59nWKazAGNKpvLdFfw4D3YJeFMTu1xq+ec/RsWKXIvRy9VnjHOk4xMh7zk3lo/pD2bEK6UBYdWPPXAvT3XeNHgXuTElKLCJw1Sd/CbTG7t3hMWY1gni+I5cCucCQtJ0jphGPEFZxyFJ8WYx97ZvSZo49Cor85uaTbtOe8gbDWPMbog2Jg2BCSBLP2JxgdCpvSpJNqG9/yiJ/3KO0+zyVZD/si6ozMb5cDVjVOK79rWWRLvXVVdAyyb/ACbwhCotPqGQe592Ek7Xs0buA1buRg7mzX+2HnIUQxB10lRRDnfMt2MmNdiiBv1kIRkWB467+zOEC/vAAVYoaTOsBrmFo0DFnRsM0lr+0Hex+ZB7ii2woBw1/iQdx5BjaXxjhi6sBjWIpnJjZmyJJzP065YSHQ9bdpewPepRy4mDIScem3Wk07fNn3iDxPjok59c0aOikCx/nKvxTE/Ht2R3H7eKIOaI/sAoDssdGIsFtMk7fHQQ/iwXABPh/j9zKx X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f9e528f3-5d95-40cb-191a-08d989bf04bb X-MS-Exchange-CrossTenant-AuthSource: BY5PR10MB4196.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Oct 2021 18:19:36.1770 (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: jPzxH/JerXjmRxFkQkeDtfnoQfYQkOOOd+G23IZzvrr5rRVEZ/HsYC97B/Vwv5cPne9AqMnkiTTG4pXa+J/JVw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR10MB2552 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10130 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 mlxlogscore=999 adultscore=0 bulkscore=0 suspectscore=0 malwarescore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2109230001 definitions=main-2110070118 X-Proofpoint-ORIG-GUID: SNwzrWpT0vRQtK854lBRoxbbgw5hLTEx X-Proofpoint-GUID: SNwzrWpT0vRQtK854lBRoxbbgw5hLTEx Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b=cz9TvFrZ; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=oqADCcqS; spf=none (imf29.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: rspam04 X-Rspamd-Queue-Id: 83783900259F X-Stat-Signature: ag4icn5j1fdz16tueu46nf1owjb3ffdb X-HE-Tag: 1633630782-530725 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: Two new sysfs files are added to demote hugtlb pages. These files are both per-hugetlb page size and per node. Files are: demote_size - The size in Kb that pages are demoted to. (read-write) demote - The number of huge pages to demote. (write-only) By default, demote_size is the next smallest huge page size. Valid huge page sizes less than huge page size may be written to this file. When huge pages are demoted, they are demoted to this size. Writing a value to demote will result in an attempt to demote that number of hugetlb pages to an appropriate number of demote_size pages. NOTE: Demote interfaces are only provided for huge page sizes if there is a smaller target demote huge page size. For example, on x86 1GB huge pages will have demote interfaces. 2MB huge pages will not have demote interfaces. This patch does not provide full demote functionality. It only provides the sysfs interfaces. It also provides documentation for the new interfaces. Signed-off-by: Mike Kravetz Reviewed-by: Oscar Salvador Signed-off-by: Mike Kravetz Reviewed-by: Oscar Salvador --- Documentation/admin-guide/mm/hugetlbpage.rst | 30 +++- include/linux/hugetlb.h | 1 + mm/hugetlb.c | 155 ++++++++++++++++++- 3 files changed, 183 insertions(+), 3 deletions(-) diff --git a/Documentation/admin-guide/mm/hugetlbpage.rst b/Documentation/admin-guide/mm/hugetlbpage.rst index 8abaeb144e44..bb90de3885d1 100644 --- a/Documentation/admin-guide/mm/hugetlbpage.rst +++ b/Documentation/admin-guide/mm/hugetlbpage.rst @@ -234,8 +234,12 @@ will exist, of the form:: hugepages-${size}kB -Inside each of these directories, the same set of files will exist:: +Inside each of these directories, the set of files contained in ``/proc`` +will exist. In addition, two additional interfaces for demoting huge +pages may exist:: + demote + demote_size nr_hugepages nr_hugepages_mempolicy nr_overcommit_hugepages @@ -243,7 +247,29 @@ Inside each of these directories, the same set of files will exist:: resv_hugepages surplus_hugepages -which function as described above for the default huge page-sized case. +The demote interfaces provide the ability to split a huge page into +smaller huge pages. For example, the x86 architecture supports both +1GB and 2MB huge pages sizes. A 1GB huge page can be split into 512 +2MB huge pages. Demote interfaces are not available for the smallest +huge page size. The demote interfaces are: + +demote_size + is the size of demoted pages. When a page is demoted a corresponding + number of huge pages of demote_size will be created. By default, + demote_size is set to the next smaller huge page size. If there are + multiple smaller huge page sizes, demote_size can be set to any of + these smaller sizes. Only huge page sizes less than the current huge + pages size are allowed. + +demote + is used to demote a number of huge pages. A user with root privileges + can write to this file. It may not be possible to demote the + requested number of huge pages. To determine how many pages were + actually demoted, compare the value of nr_hugepages before and after + writing to the demote interface. demote is a write only interface. + +The interfaces which are the same as in ``/proc`` (all except demote and +demote_size) function as described above for the default huge page-sized case. .. _mem_policy_and_hp_alloc: diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 1faebe1cd0ed..f2c3979efd69 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -596,6 +596,7 @@ struct hstate { int next_nid_to_alloc; int next_nid_to_free; unsigned int order; + unsigned int demote_order; unsigned long mask; unsigned long max_huge_pages; unsigned long nr_huge_pages; diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 95dc7b83381f..44d3c9477635 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2986,7 +2986,7 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h) static void __init hugetlb_init_hstates(void) { - struct hstate *h; + struct hstate *h, *h2; for_each_hstate(h) { if (minimum_order > huge_page_order(h)) @@ -2995,6 +2995,22 @@ static void __init hugetlb_init_hstates(void) /* oversize hugepages were init'ed in early boot */ if (!hstate_is_gigantic(h)) hugetlb_hstate_alloc_pages(h); + + /* + * Set demote order for each hstate. Note that + * h->demote_order is initially 0. + * - We can not demote gigantic pages if runtime freeing + * is not supported, so skip this. + */ + if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) + continue; + for_each_hstate(h2) { + if (h2 == h) + continue; + if (h2->order < h->order && + h2->order > h->demote_order) + h->demote_order = h2->order; + } } VM_BUG_ON(minimum_order == UINT_MAX); } @@ -3235,9 +3251,31 @@ static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid, return 0; } +static int demote_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed) + __must_hold(&hugetlb_lock) +{ + int rc = 0; + + lockdep_assert_held(&hugetlb_lock); + + /* We should never get here if no demote order */ + if (!h->demote_order) { + pr_warn("HugeTLB: NULL demote order passed to demote_pool_huge_page.\n"); + return -EINVAL; /* internal error */ + } + + /* + * TODO - demote fucntionality will be added in subsequent patch + */ + return rc; +} + #define HSTATE_ATTR_RO(_name) \ static struct kobj_attribute _name##_attr = __ATTR_RO(_name) +#define HSTATE_ATTR_WO(_name) \ + static struct kobj_attribute _name##_attr = __ATTR_WO(_name) + #define HSTATE_ATTR(_name) \ static struct kobj_attribute _name##_attr = \ __ATTR(_name, 0644, _name##_show, _name##_store) @@ -3433,6 +3471,105 @@ static ssize_t surplus_hugepages_show(struct kobject *kobj, } HSTATE_ATTR_RO(surplus_hugepages); +static ssize_t demote_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t len) +{ + unsigned long nr_demote; + unsigned long nr_available; + nodemask_t nodes_allowed, *n_mask; + struct hstate *h; + int err = 0; + int nid; + + err = kstrtoul(buf, 10, &nr_demote); + if (err) + return err; + h = kobj_to_hstate(kobj, &nid); + + /* Synchronize with other sysfs operations modifying huge pages */ + mutex_lock(&h->resize_lock); + + if (nid != NUMA_NO_NODE) { + init_nodemask_of_node(&nodes_allowed, nid); + n_mask = &nodes_allowed; + } else { + n_mask = &node_states[N_MEMORY]; + } + + spin_lock_irq(&hugetlb_lock); + while (nr_demote) { + /* + * Check for available pages to demote each time thorough the + * loop as demote_pool_huge_page will drop hugetlb_lock. + * + * NOTE: demote_pool_huge_page does not yet drop hugetlb_lock + * but will when full demote functionality is added in a later + * patch. + */ + if (nid != NUMA_NO_NODE) + nr_available = h->free_huge_pages_node[nid]; + else + nr_available = h->free_huge_pages; + nr_available -= h->resv_huge_pages; + if (!nr_available) + break; + + err = demote_pool_huge_page(h, n_mask); + if (err) + break; + + nr_demote--; + } + + spin_unlock_irq(&hugetlb_lock); + mutex_unlock(&h->resize_lock); + + if (err) + return err; + return len; +} +HSTATE_ATTR_WO(demote); + +static ssize_t demote_size_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + int nid; + struct hstate *h = kobj_to_hstate(kobj, &nid); + unsigned long demote_size = (PAGE_SIZE << h->demote_order) / SZ_1K; + + return sysfs_emit(buf, "%lukB\n", demote_size); +} + +static ssize_t demote_size_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + struct hstate *h, *demote_hstate; + unsigned long demote_size; + unsigned int demote_order; + int nid; + + demote_size = (unsigned long)memparse(buf, NULL); + + demote_hstate = size_to_hstate(demote_size); + if (!demote_hstate) + return -EINVAL; + demote_order = demote_hstate->order; + + /* demote order must be smaller than hstate order */ + h = kobj_to_hstate(kobj, &nid); + if (demote_order >= h->order) + return -EINVAL; + + /* resize_lock synchronizes access to demote size and writes */ + mutex_lock(&h->resize_lock); + h->demote_order = demote_order; + mutex_unlock(&h->resize_lock); + + return count; +} +HSTATE_ATTR(demote_size); + static struct attribute *hstate_attrs[] = { &nr_hugepages_attr.attr, &nr_overcommit_hugepages_attr.attr, @@ -3449,6 +3586,16 @@ static const struct attribute_group hstate_attr_group = { .attrs = hstate_attrs, }; +static struct attribute *hstate_demote_attrs[] = { + &demote_size_attr.attr, + &demote_attr.attr, + NULL, +}; + +static const struct attribute_group hstate_demote_attr_group = { + .attrs = hstate_demote_attrs, +}; + static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, struct kobject **hstate_kobjs, const struct attribute_group *hstate_attr_group) @@ -3466,6 +3613,12 @@ static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, hstate_kobjs[hi] = NULL; } + if (h->demote_order) { + if (sysfs_create_group(hstate_kobjs[hi], + &hstate_demote_attr_group)) + pr_warn("HugeTLB unable to create demote interfaces for %s\n", h->name); + } + return retval; }