From patchwork Fri Mar 4 17:19:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Weiner X-Patchwork-Id: 12769497 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 1FFF9C433EF for ; Fri, 4 Mar 2022 17:19:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B241D8D0002; Fri, 4 Mar 2022 12:19:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AAD498D0001; Fri, 4 Mar 2022 12:19:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94DEF8D0002; Fri, 4 Mar 2022 12:19:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0082.hostedemail.com [216.40.44.82]) by kanga.kvack.org (Postfix) with ESMTP id 8478E8D0001 for ; Fri, 4 Mar 2022 12:19:16 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 41EC4181C9BA2 for ; Fri, 4 Mar 2022 17:19:16 +0000 (UTC) X-FDA: 79207364712.16.85FBEC0 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) by imf01.hostedemail.com (Postfix) with ESMTP id 326D24001C for ; Fri, 4 Mar 2022 17:19:15 +0000 (UTC) Received: by mail-qt1-f178.google.com with SMTP id e2so7920850qte.12 for ; Fri, 04 Mar 2022 09:19:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=gC8xukYRNS8d0cnjgjbmsbbS5jxefCyF6174YZWgnjY=; b=EN3R7adYfpnEOWaaYZXQKyz8kaKfr+Bd7zDtYsZ8ibpGqS9LczOy+G+udlplQP2aBG LkwI4eLYAVEl0JQPW+nHzg1CE9/iNKsDUDxhLDCwb60avkEMlniZADfF3ckIRer5vrL8 3LKvuk7tdZEk9VoRodQzciPB+WdLReJ64EhExsIu9iX6XbqRMgd5rmCg/fCFRTw0W6ZD D1o8O0uy3cDhrP+DLRG4uf8++dpyRC0cyqUEupVD0XeJcqqKXhG/KQ7T7+blVWAkDpvJ T10GcMNkGBWR4W7YX/Jccc/toKSAFTso8TUINpvPl2qmLf25L0/lOeK1P4XcnvmlF2PV bxBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=gC8xukYRNS8d0cnjgjbmsbbS5jxefCyF6174YZWgnjY=; b=tWvAYB4YqlgLXIsmVnk9FWW4ZiWPrptUE0lvucqPJEsw0FYdvlcn4s9GRRhrvN3PZ+ +M2j35zwdxTUnzImqn4fDqyNR8hjfAN751brtR6lhMU32r3F5HRHvlk9+ErOK9cA+/1o ymwiZgX3VK5eiR+ilX+opuXQggsiUu2yL/My+nrA12KeOdzMv4Z1MSXEuZthcvcM+gJ9 CyTLc9JkeS+AdXViQW1SrNctO69tsL54ESg9G7K/PaPS9f19bcACykwQcGPK2ZghyQc5 lgQNe1Hr9POJaGyGWbjMylThJ5hjvaGtbPEQPRm6PtLUWXhlzmaDD12Tseo/GRVqFSIs WVdA== X-Gm-Message-State: AOAM533T2pYlMVGi/BBAAGRoZSErvRX9dRz9M0YBRsBWa6aeqkbACTlO k2aFKWRkGER6fcFnJ6/ou0YP5w== X-Google-Smtp-Source: ABdhPJxPQ9l0ykDqVILGa4/U3r2RMyng9xdOipGnusx7p6vU8aXPKjqwclYZgDWhQeUi+ZvZ89KH7w== X-Received: by 2002:ac8:7d8f:0:b0:2de:2da6:945f with SMTP id c15-20020ac87d8f000000b002de2da6945fmr32279889qtd.351.1646414354358; Fri, 04 Mar 2022 09:19:14 -0800 (PST) Received: from localhost (cpe-98-15-154-102.hvc.res.rr.com. [98.15.154.102]) by smtp.gmail.com with ESMTPSA id bq42-20020a05620a46aa00b006494fb49246sm2550853qkb.86.2022.03.04.09.19.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 09:19:13 -0800 (PST) From: Johannes Weiner To: Andrew Morton Cc: Michal Hocko , Vlastimil Babka , Nadav Amit , David Hildenbrand , dgilbert@redhat.com, Mike Kravetz , linux-mm@kvack.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm: madvise: MADV_DONTNEED_LOCKED Date: Fri, 4 Mar 2022 12:19:12 -0500 Message-Id: <20220304171912.305060-1-hannes@cmpxchg.org> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 326D24001C X-Stat-Signature: 1is3poa4b7eyqkpe1dgs74ummf3iqdnq X-Rspam-User: Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=EN3R7adY; spf=pass (imf01.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.160.178 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org; dmarc=pass (policy=none) header.from=cmpxchg.org X-Rspamd-Server: rspam07 X-HE-Tag: 1646414355-347201 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 historically rejects mlocked ranges, but with MLOCK_ONFAULT and MCL_ONFAULT allowing to mlock without populating, there are valid use cases for depopulating locked ranges as well. Users mlock memory to protect secrets. There are allocators for secure buffers that want in-use memory generally mlocked, but cleared and invalidated memory to give up the physical pages. This could be done with explicit munlock -> mlock calls on free -> alloc of course, but that adds two unnecessary syscalls, heavy mmap_sem write locks, vma splits and re-merges - only to get rid of the backing pages. Users also mlockall(MCL_ONFAULT) to suppress sustained paging, but are okay with on-demand initial population. It seems valid to selectively free some memory during the lifetime of such a process, without having to mess with its overall policy. Why add a separate flag? Isn't this a pretty niche usecase? - MADV_DONTNEED has been bailing on locked vmas forever. It's at least conceivable that someone, somewhere is relying on mlock to protect data from perhaps broader invalidation calls. Changing this behavior now could lead to quiet data corruption. - It also clarifies expectations around MADV_FREE and maybe MADV_REMOVE. It avoids the situation where one quietly behaves different than the others. MADV_FREE_LOCKED can be added later. - The combination of mlock() and madvise() in the first place is probably niche. But where it happens, I'd say that dropping pages from a locked region once they don't contain secrets or won't page anymore is much saner than relying on mlock to protect memory from speculative or errant invalidation calls. It's just that we can't change the default behavior because of the two previous points. Given that, an explicit new flag seems to make the most sense. Signed-off-by: Johannes Weiner Acked-by: Michal Hocko Reviewed-by: Mike Kravetz Reviewed-by: Shakeel Butt Acked-by: Vlastimil Babka --- include/uapi/asm-generic/mman-common.h | 2 ++ mm/madvise.c | 24 ++++++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) v2: - mmap_sem for read is enough for DONTNEED_LOCKED, thanks Nadav - rebased on top of Mike's hugetlb DONTNEED patch in -mm diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h index 1567a3294c3d..6c1aa92a92e4 100644 --- a/include/uapi/asm-generic/mman-common.h +++ b/include/uapi/asm-generic/mman-common.h @@ -75,6 +75,8 @@ #define MADV_POPULATE_READ 22 /* populate (prefault) page tables readable */ #define MADV_POPULATE_WRITE 23 /* populate (prefault) page tables writable */ +#define MADV_DONTNEED_LOCKED 24 /* like DONTNEED, but drop locked pages too */ + /* compatibility flags */ #define MAP_FILE 0 diff --git a/mm/madvise.c b/mm/madvise.c index e4ddd00878b5..5b6d796e55de 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -52,6 +52,7 @@ static int madvise_need_mmap_write(int behavior) case MADV_REMOVE: case MADV_WILLNEED: case MADV_DONTNEED: + case MADV_DONTNEED_LOCKED: case MADV_COLD: case MADV_PAGEOUT: case MADV_FREE: @@ -502,14 +503,9 @@ static void madvise_cold_page_range(struct mmu_gather *tlb, tlb_end_vma(tlb, vma); } -static inline bool can_madv_lru_non_huge_vma(struct vm_area_struct *vma) -{ - return !(vma->vm_flags & (VM_LOCKED|VM_PFNMAP)); -} - static inline bool can_madv_lru_vma(struct vm_area_struct *vma) { - return can_madv_lru_non_huge_vma(vma) && !is_vm_hugetlb_page(vma); + return !(vma->vm_flags & (VM_LOCKED|VM_PFNMAP|VM_HUGETLB)); } static long madvise_cold(struct vm_area_struct *vma, @@ -787,10 +783,16 @@ static bool madvise_dontneed_free_valid_vma(struct vm_area_struct *vma, unsigned long *end, int behavior) { - if (!is_vm_hugetlb_page(vma)) - return can_madv_lru_non_huge_vma(vma); + if (!is_vm_hugetlb_page(vma)) { + unsigned int forbidden = VM_PFNMAP; + + if (behavior != MADV_DONTNEED_LOCKED) + forbidden |= VM_LOCKED; + + return !(vma->vm_flags & forbidden); + } - if (behavior != MADV_DONTNEED) + if (behavior != MADV_DONTNEED && behavior != MADV_DONTNEED_LOCKED) return false; if (start & ~huge_page_mask(hstate_vma(vma))) return false; @@ -854,7 +856,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, VM_WARN_ON(start >= end); } - if (behavior == MADV_DONTNEED) + if (behavior == MADV_DONTNEED || behavior == MADV_DONTNEED_LOCKED) return madvise_dontneed_single_vma(vma, start, end); else if (behavior == MADV_FREE) return madvise_free_single_vma(vma, start, end); @@ -993,6 +995,7 @@ static int madvise_vma_behavior(struct vm_area_struct *vma, return madvise_pageout(vma, prev, start, end); case MADV_FREE: case MADV_DONTNEED: + case MADV_DONTNEED_LOCKED: return madvise_dontneed_free(vma, prev, start, end, behavior); case MADV_POPULATE_READ: case MADV_POPULATE_WRITE: @@ -1123,6 +1126,7 @@ madvise_behavior_valid(int behavior) case MADV_REMOVE: case MADV_WILLNEED: case MADV_DONTNEED: + case MADV_DONTNEED_LOCKED: case MADV_FREE: case MADV_COLD: case MADV_PAGEOUT: