From patchwork Mon May 22 08:24:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13249979 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 A72F5C7EE26 for ; Mon, 22 May 2023 08:24:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EBC07900002; Mon, 22 May 2023 04:24:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E6C9C6B0075; Mon, 22 May 2023 04:24:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3436900002; Mon, 22 May 2023 04:24:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id C1A976B0074 for ; Mon, 22 May 2023 04:24:19 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 86654C0C7F for ; Mon, 22 May 2023 08:24:19 +0000 (UTC) X-FDA: 80817203838.08.08A9853 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) by imf13.hostedemail.com (Postfix) with ESMTP id AB35E20011 for ; Mon, 22 May 2023 08:24:17 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=liQ5OOFc; spf=pass (imf13.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.45 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684743857; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=5P55SeVZi8i0w++ACS66BUPKb/klhNtkg+RLR5cpX8k=; b=gi9t8MmXJrsml6d4BTed5wUW2DRqxnfLUBSNiw3xg3eqn6BDVJhKNbUkpei2PTET3gDFGW SXvkBY2G1Lb26TUdoxVpV52Kao6g7a+aFuWK9wsbl03tq+u9QtunhrErpNwfzklY2QOWoX Mz4SZP1EeOQcSAYR2Vvn2tCiWLyHG2g= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684743857; a=rsa-sha256; cv=none; b=7hGS9hat22g1LDxuFbUSZA1XSeNm58lPBgkRAA43MYvHflhbxFBQOLaAfVPff2HID3HUio bBoB2I0mlQ8ZMdJKizvVlL+2Eymh6fe2/0D4Xw7PcR40n1112AcSKLxf3nIngEj2/p+ZEZ kJ+VHDZksVWc9R7Y6XpkgjY3VXnVpjc= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=liQ5OOFc; spf=pass (imf13.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.45 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-3095557dd99so3029303f8f.1 for ; Mon, 22 May 2023 01:24:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684743856; x=1687335856; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=5P55SeVZi8i0w++ACS66BUPKb/klhNtkg+RLR5cpX8k=; b=liQ5OOFc1KNu/9WLga5ETHW9Gc2ze3aZV9Fy1QKL0qLhSXtmQ1QZhILOo+9hG9VvH3 +HFxbgphbR2Ngr1nE0C6WlxJcRb8PoDx72vWW2fs7XsQfacxn1T32OKKVTnGfE5yEJrB IlIs2iBFppPq1Ucp/6rac1yi5GqGNB4CpjWjilToUd3T1drgmBDSKTTfJPjnC5qn25ul RaUJJSjIrNitD4w46OlGPby1QxOXjsnWmRKkYe+l3+wr35gv7yowdyVGq28HOV5acKXS B/F+dy/A1XHwBLUtTWfRUo0FRSEIrjsetcuHS40VnYtRJI6P0iK+M0BiIvzmejC9qA/B xHSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684743856; x=1687335856; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5P55SeVZi8i0w++ACS66BUPKb/klhNtkg+RLR5cpX8k=; b=HkJujlCcn/unieWCxg6GJbXt3AbOixNlCzNvrBOqvMZYwIdZTbAhfw2CjdpoOZmmrL 2kskizueTlrOaiTQMrD1vklxAFbEYFmmQXBBr1UvCholztTtbwyTMUns+ZSPL1QL/h70 wFXI0/BL91qoK97k+nNLcfs3lRNrTvrBQQYJIqLnGuf1oMao4rSHM/fNFWrJU1HBeLwo 4pOlAgK4YMiz1ywd4ETGc0wizdvCbq7ba5UutPe1DFPTLeo5BAOXnZ6aIRvCFMAhxLp/ R1QiyxwZkqmDQYNT4FnTYUJYYMmvoUgv6mf5OpVUp+9ArNg9JxN1mxjFOWA7TfjD4r9C p63w== X-Gm-Message-State: AC+VfDwhfwnB5oCjR7r3g3SP+SC/XIcdrLhIDVFCHJgYdBwRHt0qpgAY W1KdefXxfOqH5SiHUAZW7tTHlAVeG7k= X-Google-Smtp-Source: ACHHUZ6xkVk5ZDc1iRaohsXE7LCPcSRoYMlsLeU+/KiEiMsvQLJkj1AqEW8V2Ha6BnEMOK7XxGuM6Q== X-Received: by 2002:adf:f542:0:b0:309:4368:a8a0 with SMTP id j2-20020adff542000000b003094368a8a0mr6133123wrp.68.1684743855512; Mon, 22 May 2023 01:24:15 -0700 (PDT) Received: from lucifer.home (host81-154-179-160.range81-154.btcentralplus.com. [81.154.179.160]) by smtp.googlemail.com with ESMTPSA id j3-20020a5d4483000000b003062b57ffd1sm6900600wrq.50.2023.05.22.01.24.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 May 2023 01:24:14 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Mike Rapoport , "Liam R . Howlett" , Vlastimil Babka , Lorenzo Stoakes Subject: [PATCH] mm/mmap: refactor mlock_future_check() Date: Mon, 22 May 2023 09:24:12 +0100 Message-Id: <20230522082412.56685-1-lstoakes@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: AB35E20011 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: s54qoi7dt153ixo7bg5r3xd1dc3nhfqw X-HE-Tag: 1684743857-701624 X-HE-Meta: U2FsdGVkX1+Pue4Bkj0/yzzOCdFNAP9O5ZKx9NZFGLhnQgQhQL5pb7cPM+beeY+B4+bWc2pOtIWZ9kdWUAsF3g8970Ba3S/hvSBcEJVVtNQgHEiKEmRIwJ1W2C9Vwjth6+4CDfy3kaHO/+T91oWd0Y1Wtyjx6urEykKdcjwgpo51APjxUaGbcobKK4EgLA2iOClrcL7gDldklrjKgCgqhBkNePr/5b2eeflbrJmfz0Or3EA9/Ofw5tQQ2e6KdOTnzjeb80kglEu6wPy2w3At8SMkRAbLN7E959fgIsFZ38P4O4JnTxjRNl3SRfBcajuihKBatS4vy6de8O9zOqJz84r21wtewHMA2eRg+zCiiZtfAEwDeQJUs6dBLc6t/z+PKpFNkJugIr9DzNWmLe8az34lEGYKKSA/LjqeijFte6/pXC0ByU5IcKGVl23bz4boCDi3MnjuglkmwRWgfic+THDjSV/hvi4y/XAuw48QEKjY2nU+1JLIwdbRFcthJCzjaIArpIQxX+bYnSBgd0d1d8gBLZEFtMOQbw5ydNOvo2C1aVSlWxGn4AbDC32tZtuXIXY4xXpXRR9XujHYgTy3/Iq9d5R8sZkMVw2nTElt9JhkjEcWX7ZyW8an26tcqrxdFTNlASiJNQ960dFaq3fHWsQfKLYZL2GWZc5s0kDIEPaBvaWL2mFPxt6ZG3l9NW+Ctm/IDySU1+QWUbacr0fzndE8yowDqOZycmEpkbdiIuR4C2JC7IhYrLNCIYNUsvH4VI/+MoSt9shVlFFpJ/piKmSpeTjQwwHI5QqYAkGi3Oyov0SiTUiwYUA+WaFZWvPWKwpYef8mRcrj645AeSOS+NimLPcN4TWCqZLdyv7XiQhOOwmOIaeyOsk7ECJiHICD3++NyXp2fngSmEddRyGlxhKF2Fy6VLkCrp+UVEGViaNyZUmNGvMTTEgWFReNGt3Pdl6IEc/YRHimqk9jTOW yEDjaumF Dct2aMdNKH3gAqrqJqWQLsHc4bvbkJ9jIIA/7Olo7DS+QCIhQ4YjtIQ3OqjF2tF+GT4OfVr1CzqMf/hDXZCPN8iIckA0/Ip0TcPnp3ZBrcCORK+vBKOQdxJmPTpfAxBNCSH8ek3ab0wBJeT7lJGNj0h/qrEawVSkV2fs9jtI89uFJVjvjUXdxpFxpnJawMW95EJ7oycUVnVQVOfsDcjdwWrUVMIqchlRghOJww1S45jO/IqllcyXhN7v+DUjwEZVqnKeDPYozguMRQJoyLJ4h88JxtKntSZRybLX/ijWCb4jzRnyDDCMiw1koZT1XSwvg+/b45rTAREXYQuueThIU63mWuo/JgIa3NmtC7Oj+QjZFzZ1J93nV/dlhORxiIji32hsK22x4/gs5ZvRCTkCrTEnitUVEhXQeZzUtBTJPGioECWFEAYtX2o0Bg2k8gyRMiYxnbD5rpAnpv+vMIHTTpSt3z4/nD5nNSZpECkdiCYxNI9k= 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: In all but one instance, mlock_future_check() is treated as a boolean function despite returning an error code. In one instance, this error code is ignored and replaced with -ENOMEM. This is confusing, and the inversion of true -> failure, false -> success is not warranted. Convert the function to a bool, lightly refactor and return true if the check passes, false if not. Signed-off-by: Lorenzo Stoakes Signed-off-by: Andrew Morton Acked-by: Vlastimil Babka --- mm/internal.h | 4 ++-- mm/mmap.c | 33 +++++++++++++++++---------------- mm/mremap.c | 2 +- mm/secretmem.c | 2 +- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index bb6542279599..66dd214b302a 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -576,8 +576,8 @@ extern long populate_vma_page_range(struct vm_area_struct *vma, extern long faultin_vma_page_range(struct vm_area_struct *vma, unsigned long start, unsigned long end, bool write, int *locked); -extern int mlock_future_check(struct mm_struct *mm, unsigned long flags, - unsigned long len); +extern bool mlock_future_check(struct mm_struct *mm, unsigned long flags, + unsigned long bytes); /* * mlock_vma_folio() and munlock_vma_folio(): * should be called with vma's mmap_lock held for read or write, diff --git a/mm/mmap.c b/mm/mmap.c index 877696464c09..10f2d5d689da 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -182,7 +182,8 @@ static int check_brk_limits(unsigned long addr, unsigned long len) if (IS_ERR_VALUE(mapped_addr)) return mapped_addr; - return mlock_future_check(current->mm, current->mm->def_flags, len); + return mlock_future_check(current->mm, current->mm->def_flags, len) + ? 0 : -EAGAIN; } static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma, unsigned long addr, unsigned long request, unsigned long flags); @@ -1148,21 +1149,21 @@ static inline unsigned long round_hint_to_min(unsigned long hint) return hint; } -int mlock_future_check(struct mm_struct *mm, unsigned long flags, - unsigned long len) +bool mlock_future_check(struct mm_struct *mm, unsigned long flags, + unsigned long bytes) { - unsigned long locked, lock_limit; + unsigned long locked_pages, limit_pages; - /* mlock MCL_FUTURE? */ - if (flags & VM_LOCKED) { - locked = len >> PAGE_SHIFT; - locked += mm->locked_vm; - lock_limit = rlimit(RLIMIT_MEMLOCK); - lock_limit >>= PAGE_SHIFT; - if (locked > lock_limit && !capable(CAP_IPC_LOCK)) - return -EAGAIN; - } - return 0; + if (!(flags & VM_LOCKED) || capable(CAP_IPC_LOCK)) + return true; + + locked_pages = bytes >> PAGE_SHIFT; + locked_pages += mm->locked_vm; + + limit_pages = rlimit(RLIMIT_MEMLOCK); + limit_pages >>= PAGE_SHIFT; + + return locked_pages <= limit_pages; } static inline u64 file_mmap_size_max(struct file *file, struct inode *inode) @@ -1274,7 +1275,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr, if (!can_do_mlock()) return -EPERM; - if (mlock_future_check(mm, vm_flags, len)) + if (!mlock_future_check(mm, vm_flags, len)) return -EAGAIN; if (file) { @@ -1926,7 +1927,7 @@ static int acct_stack_growth(struct vm_area_struct *vma, return -ENOMEM; /* mlock limit tests */ - if (mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) + if (!mlock_future_check(mm, vma->vm_flags, grow << PAGE_SHIFT)) return -ENOMEM; /* Check to ensure the stack will not grow into a hugetlb-only region */ diff --git a/mm/mremap.c b/mm/mremap.c index b11ce6c92099..bcfcb8df5875 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -775,7 +775,7 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr, if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)) return ERR_PTR(-EFAULT); - if (mlock_future_check(mm, vma->vm_flags, new_len - old_len)) + if (!mlock_future_check(mm, vma->vm_flags, new_len - old_len)) return ERR_PTR(-EAGAIN); if (!may_expand_vm(mm, vma->vm_flags, diff --git a/mm/secretmem.c b/mm/secretmem.c index 974b32ba8b9d..58d2af12df4f 100644 --- a/mm/secretmem.c +++ b/mm/secretmem.c @@ -125,7 +125,7 @@ static int secretmem_mmap(struct file *file, struct vm_area_struct *vma) if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0) return -EINVAL; - if (mlock_future_check(vma->vm_mm, vma->vm_flags | VM_LOCKED, len)) + if (!mlock_future_check(vma->vm_mm, vma->vm_flags | VM_LOCKED, len)) return -EAGAIN; vm_flags_set(vma, VM_LOCKED | VM_DONTDUMP);