From patchwork Thu Mar 3 21:29:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Weiner X-Patchwork-Id: 12768175 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 67D14C433F5 for ; Thu, 3 Mar 2022 21:30:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B5E308D0002; Thu, 3 Mar 2022 16:29:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B0D658D0001; Thu, 3 Mar 2022 16:29:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D5CB8D0002; Thu, 3 Mar 2022 16:29:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0149.hostedemail.com [216.40.44.149]) by kanga.kvack.org (Postfix) with ESMTP id 8FC8E8D0001 for ; Thu, 3 Mar 2022 16:29:59 -0500 (EST) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 40EED80D4615 for ; Thu, 3 Mar 2022 21:29:59 +0000 (UTC) X-FDA: 79204367718.31.987B7B6 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) by imf23.hostedemail.com (Postfix) with ESMTP id 7C28F140009 for ; Thu, 3 Mar 2022 21:29:58 +0000 (UTC) Received: by mail-qk1-f178.google.com with SMTP id j78so5029690qke.2 for ; Thu, 03 Mar 2022 13:29:58 -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=AJm6rEfe6QChserQuJSZLkAQP22tqq4oajdg4WrPTzA=; b=rjFirQQfFh4ulxYx7aVCojr3fd5pqh1Jw1kbTCIPcKS70KJAgNT0fOilTmG98RH+Aq 2IADMLT0rYcG5f0jdWo5lNufgz52zYYQX0a0P4cPl3zvCXwCy3Oou5bikCHmP4ay00Q+ R5jS1FKoAYpdD442M8hB16LMhO2omPHD8kH7PBZbxAO3ncOJok0pR7hN6HvIk5zGrGcJ aFHZVDjib3ELve/RXF8kOB8h1I0haoiDZgdyDmgCvbSEDqWOXhSGLqXjJSSg0x2xTfE8 oxCkGpAk98F6NDIUdU87Ohk79PT8D1l9pDkinvBL/HFxKmWRsBvvzqFkX4XguVJKP6Ig qyeA== 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=AJm6rEfe6QChserQuJSZLkAQP22tqq4oajdg4WrPTzA=; b=X2vgImxa9UNxMuTVVb4fcfrLLl/txB5QMbt3WXj0co2zlBFTmNUyXXe9jLLdVdmvtZ wTODM/loJ9mzy1CQuwrzoKUdRwLW60GIV2mnL5wMRy24c5u3Z7qfzem8nHVdn8iNO+2k CUGfrzeKamFineHRxaPUl69kT+xZKgnLQ17GgLtq7Zudek+Rlu9R+Ll4LgzWg41oERrI /d4con0Mj8R7QtyiAhn7hujmSFdKfCzQdb+JKd7LHf+G2qg+t0JEV0amG70NnnEtFNVq dCPFfnDvw5Qz75su0X6hubmtkCxmFCsqCb8+yPkul5M4aKvQ5pY5j4jfAiYhHbOcoeOC b+6Q== X-Gm-Message-State: AOAM533ZCEv7XwYgNCPtS89jP/3IlnYNo6SwC7tqEcwXfgxbzkTZm1Gv JJsFj9oZKuen9QJFWMCTKKFFhA== X-Google-Smtp-Source: ABdhPJzmib5vwTvU2gQ6huUHKU/Ce1Z7GZasquxbAu5zYSlWbMi8LHHZAmVedXMj3aKijmI2McBtIg== X-Received: by 2002:a05:620a:c44:b0:508:201b:39d0 with SMTP id u4-20020a05620a0c4400b00508201b39d0mr726361qki.437.1646342997651; Thu, 03 Mar 2022 13:29:57 -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 x12-20020ac85f0c000000b002de8931d4d6sm2206171qta.77.2022.03.03.13.29.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Mar 2022 13:29:57 -0800 (PST) From: Johannes Weiner To: Andrew Morton Cc: Michal Hocko , Vlastimil Babka , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm: madvise: MADV_DONTNEED_LOCKED Date: Thu, 3 Mar 2022 16:29:56 -0500 Message-Id: <20220303212956.229409-1-hannes@cmpxchg.org> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 7C28F140009 X-Stat-Signature: 44og5b8iyip8bwaa6sycho65736bc47q Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=rjFirQQf; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf23.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.222.178 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org X-HE-Tag: 1646342998-383555 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 --- include/uapi/asm-generic/mman-common.h | 2 ++ mm/madvise.c | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) 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 5604064df464..12dfa14bc985 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -800,6 +800,13 @@ static long madvise_dontneed_single_vma(struct vm_area_struct *vma, return 0; } +static bool can_madv_dontneed_free(struct vm_area_struct *vma, int behavior) +{ + if (behavior == MADV_DONTNEED_LOCKED) + return !(vma->vm_flags & (VM_HUGETLB|VM_PFNMAP)); + 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 +815,8 @@ 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 (!can_madv_dontneed_free(vma, behavior)) return -EINVAL; if (!userfaultfd_remove(vma, start, end)) { @@ -830,7 +838,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, */ return -ENOMEM; } - if (!can_madv_lru_vma(vma)) + if (!can_madv_dontneed_free(vma, behavior)) return -EINVAL; if (end > vma->vm_end) { /* @@ -850,7 +858,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); @@ -988,6 +996,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: @@ -1113,6 +1122,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: