From patchwork Fri Jun 24 17:36:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 12894947 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 36ED0C43334 for ; Fri, 24 Jun 2022 17:37:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4A2C88E0254; Fri, 24 Jun 2022 13:37:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DD3D8E0244; Fri, 24 Jun 2022 13:37:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1BAAA8E0254; Fri, 24 Jun 2022 13:37:42 -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 F33048E0244 for ; Fri, 24 Jun 2022 13:37:41 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D4CE260B6F for ; Fri, 24 Jun 2022 17:37:41 +0000 (UTC) X-FDA: 79613836722.25.0C5FA14 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf29.hostedemail.com (Postfix) with ESMTP id 6263B120010 for ; Fri, 24 Jun 2022 17:37:41 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-317ab78a345so26751437b3.10 for ; Fri, 24 Jun 2022 10:37:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=HisKs0OAYNP82NuB/Frk0iPqJLb2lc+sAZjNm287jcQ=; b=P/aHZ9fJlVwvkJfaZVFajZm98KOIfMj2IYppFM5LuQ1zf9EsSZ8qMdlGw+rfUUepxT Rk4rneqzgNI3hBkLTvsDOw/sovXOPg7QHxBlzbSMDv947qVGHCYD/AKf6PpAucF+SB8+ CPR/TNLo08h/tyhtM460hIdHHPcABjdzuulR5lTk4iFehrldnIL1GfqZda2In7YFtggB usvpcPQWqZ0WphYnX3EhkMBx+Typu02xyZyvlY2VIUMvBbNVFv8qTl1N/MQr/QOO/hLC HQXC2pLyWYxCtUrqnUx0Na56WcsA/KgE6sPe3nZndOHiUWkRsKxAtTwz+BZWdqDY7Yot OYtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=HisKs0OAYNP82NuB/Frk0iPqJLb2lc+sAZjNm287jcQ=; b=PUhnka9CJFqcMn3z/UG5Zbep+CjOKZ5H8p35XZ7P69YUNadsHFXfQ/585McCJ3Gdb3 hwj6K1IytY0qrD5GRiah3C6wJSSDVds7rkjBvxigl4VobZpUx81TU2/u6h8PRu7ls9hQ dClN/h6VU0L4KCaJpztAU4kwgZrlWDSj01sAIFeTmIjFBc2asIfOG+1MsM/B6FcBA2X7 cwUtKc2/sRpcwbJhedBH+9VoRgeXa1DF7mx06xHe1k+hV0IdUuM82Jj2OBNWALqpRLKC QDaP/GtYy3w/kyTKcJwcC1FWSIlmSCc4/OKnkimJ2+T7YyBpXwLLUvYEg1rn0GpdkulL DIaw== X-Gm-Message-State: AJIora+daE/j9Iy6ikeSyAJIFnra1EyQ2I5FO9zVQwQfFIJEWEOK4CQo D2l7lXloYGm8/M2A/K75ds/HsjEyale9Xyfy X-Google-Smtp-Source: AGRyM1tzk7GXyftSzcieFt/kefrccg/jUZ3bHYFjHpboz2q85ML1dIMb8Z3beFS8X7RsMAKgtTvTxLeNH4OzIoqy X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a0d:e60e:0:b0:317:893c:90a3 with SMTP id p14-20020a0de60e000000b00317893c90a3mr18394744ywe.241.1656092260720; Fri, 24 Jun 2022 10:37:40 -0700 (PDT) Date: Fri, 24 Jun 2022 17:36:52 +0000 In-Reply-To: <20220624173656.2033256-1-jthoughton@google.com> Message-Id: <20220624173656.2033256-23-jthoughton@google.com> Mime-Version: 1.0 References: <20220624173656.2033256-1-jthoughton@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [RFC PATCH 22/26] madvise: add uapi for HugeTLB HGM collapse: MADV_COLLAPSE From: James Houghton To: Mike Kravetz , Muchun Song , Peter Xu Cc: David Hildenbrand , David Rientjes , Axel Rasmussen , Mina Almasry , Jue Wang , Manish Mishra , "Dr . David Alan Gilbert" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1656092261; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HisKs0OAYNP82NuB/Frk0iPqJLb2lc+sAZjNm287jcQ=; b=69kDR0zIqgx9yX1JCkXot1yqVFDYINbYW7Qh548ADUKFGCQoZ3UqwIzQYfXvOznl6AOCks S6Ps46SqOhCQuNJKQ2Gb0Zir/zh2sRf/GYlhDLvtW5dTLMRn89Lu6wumWx1UASz2SwyNEj ZXFNo14DZwzIQFao1sL7KAgPd6/8dOY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1656092261; a=rsa-sha256; cv=none; b=y2X4YzzmlKRq2d8UpvOA48vloNzCJXf0usO0L0cnuFgL9iKuZnuQA/OhM2DoV1BkPhknBY XZ4mrKtxkPgQe9m6L3iCbL0jTXi6yQjLBlH6+ecqU17ylsp3f04NmpsEs86HKC/lSN00AO FbTP/Afu1zW32nvAVCOdqMTcdTb2/5w= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="P/aHZ9fJ"; spf=pass (imf29.hostedemail.com: domain of 3ZPa1YgoKCEUq0ov1no0vunvvnsl.jvtspu14-ttr2hjr.vyn@flex--jthoughton.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3ZPa1YgoKCEUq0ov1no0vunvvnsl.jvtspu14-ttr2hjr.vyn@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="P/aHZ9fJ"; spf=pass (imf29.hostedemail.com: domain of 3ZPa1YgoKCEUq0ov1no0vunvvnsl.jvtspu14-ttr2hjr.vyn@flex--jthoughton.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3ZPa1YgoKCEUq0ov1no0vunvvnsl.jvtspu14-ttr2hjr.vyn@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 6263B120010 X-Stat-Signature: iuhmha9z7ror8ojffboi435yw3xs8sso X-HE-Tag: 1656092261-121895 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: This commit is co-opting the same madvise mode that is being introduced by zokeefe@google.com to manually collapse THPs[1]. As with the rest of the high-granularity mapping support, MADV_COLLAPSE is only supported for shared VMAs right now. [1] https://lore.kernel.org/linux-mm/20220604004004.954674-10-zokeefe@google.com/ Signed-off-by: James Houghton --- include/uapi/asm-generic/mman-common.h | 2 ++ mm/madvise.c | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h index 6c1aa92a92e4..b686920ca731 100644 --- a/include/uapi/asm-generic/mman-common.h +++ b/include/uapi/asm-generic/mman-common.h @@ -77,6 +77,8 @@ #define MADV_DONTNEED_LOCKED 24 /* like DONTNEED, but drop locked pages too */ +#define MADV_COLLAPSE 25 /* collapse an address range into hugepages */ + /* compatibility flags */ #define MAP_FILE 0 diff --git a/mm/madvise.c b/mm/madvise.c index d7b4f2602949..c624c0f02276 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -59,6 +59,7 @@ static int madvise_need_mmap_write(int behavior) case MADV_FREE: case MADV_POPULATE_READ: case MADV_POPULATE_WRITE: + case MADV_COLLAPSE: return 0; default: /* be safe, default to 1. list exceptions explicitly */ @@ -981,6 +982,20 @@ static long madvise_remove(struct vm_area_struct *vma, return error; } +static int madvise_collapse(struct vm_area_struct *vma, + struct vm_area_struct **prev, + unsigned long start, unsigned long end) +{ + bool shared = vma->vm_flags & VM_SHARED; + *prev = vma; + + /* Only allow collapsing for HGM-enabled, shared mappings. */ + if (!is_vm_hugetlb_page(vma) || !hugetlb_hgm_enabled(vma) || !shared) + return -EINVAL; + + return hugetlb_collapse(vma->vm_mm, vma, start, end); +} + /* * Apply an madvise behavior to a region of a vma. madvise_update_vma * will handle splitting a vm area into separate areas, each area with its own @@ -1011,6 +1026,8 @@ static int madvise_vma_behavior(struct vm_area_struct *vma, case MADV_POPULATE_READ: case MADV_POPULATE_WRITE: return madvise_populate(vma, prev, start, end, behavior); + case MADV_COLLAPSE: + return madvise_collapse(vma, prev, start, end); case MADV_NORMAL: new_flags = new_flags & ~VM_RAND_READ & ~VM_SEQ_READ; break; @@ -1158,6 +1175,9 @@ madvise_behavior_valid(int behavior) #ifdef CONFIG_MEMORY_FAILURE case MADV_SOFT_OFFLINE: case MADV_HWPOISON: +#endif +#ifdef CONFIG_HUGETLB_HIGH_GRANULARITY_MAPPING + case MADV_COLLAPSE: #endif return true; @@ -1351,6 +1371,9 @@ int madvise_set_anon_name(struct mm_struct *mm, unsigned long start, * triggering read faults if required * MADV_POPULATE_WRITE - populate (prefault) page tables writable by * triggering write faults if required + * MADV_COLLAPSE - collapse a high-granularity HugeTLB mapping into huge + * mappings. This is useful after an entire hugepage has been + * mapped with individual small UFFDIO_CONTINUE operations. * * return values: * zero - success