From patchwork Sat Aug 17 00:18:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13766876 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 AA8B8C531DC for ; Sat, 17 Aug 2024 00:18:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BFA9F6B018B; Fri, 16 Aug 2024 20:18:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A49776B020E; Fri, 16 Aug 2024 20:18:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 899F96B020F; Fri, 16 Aug 2024 20:18:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 6646C6B018B for ; Fri, 16 Aug 2024 20:18:42 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 14F601A03FC for ; Sat, 17 Aug 2024 00:18:42 +0000 (UTC) X-FDA: 82459826484.10.5C83D5E Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) by imf30.hostedemail.com (Postfix) with ESMTP id 38D6B8001B for ; Sat, 17 Aug 2024 00:18:39 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="A3ChME/X"; spf=pass (imf30.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.45 as permitted sender) smtp.mailfrom=pedro.falcato@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=1723853861; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=eE0tAujU+84Fl5Bml9LnGFFfJTwW8AbGe4iKDNDJLPI=; b=MWvVyzRX195DPFGNJUSClfVXQhx5/EKpyV38Yl0XP4gzXyM3qvcLTUpQ2rSaLpI15cZ7So OUjz8trlct0z6smfnanrfQXlRD7kY0HUIInmXtwxE1gAMl6mIlt21SPE9vxS9M6njv2jp7 6ht2FvYOT5LeElQpYby7qHXw0t76vYo= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="A3ChME/X"; spf=pass (imf30.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.45 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723853861; a=rsa-sha256; cv=none; b=StDiFbQ4d3eWSwXN+3AWJ5QphzUTAv7BI16pw/DbYlektEHcHiZvyGuTlUbcaAFWPuD3JV FN3HOW4FRF62Nulqo5vyDiWCxKjeK2PcQC9mhpVT2g0BoQ4IWwe1CqgjixV6Y/4tKMtLy/ sqtVHosBw6Xnx3tlTqgdMeeU2GgLark= Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-428101fa30aso18114545e9.3 for ; Fri, 16 Aug 2024 17:18:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723853919; x=1724458719; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=eE0tAujU+84Fl5Bml9LnGFFfJTwW8AbGe4iKDNDJLPI=; b=A3ChME/Xa42nuOuKyMpSOTj97OOeR0o57KeVB1BEMKMU0QI+k7/sCNDw3gysRHwt1o 27pmSzq4hTRu5Bd8v07x82xa5tb0mAnOwk6SJU22NYlQ/yE29IZKwLfCt0BOVuJEoCbz aWSvPFa9TnR8B10BfHiqQLxqGr+0UoZKjYa+dP20FNxws6g9WywYiwzf4YdLILGlbZ0A z+1qEKiQbTIjaTncpjkPLr4L8LnZihO4PG+I5lLEXi1xUjMU9piXU+x2UaAU5+2B+JV4 kmKr6Tw6Xbrf/BUolUUefqKcFfBcApXM1/7l/ZPHcVw/RSkCk0uEGtdxBILPyHsnNRn4 0nDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723853919; x=1724458719; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eE0tAujU+84Fl5Bml9LnGFFfJTwW8AbGe4iKDNDJLPI=; b=MmenzM7+opT+lMQ0Qm3fQraN5wbHk8KBxl/mlcdQLzY+hKNKN0uc/Jx5xFlAw2QJ63 Os7OkqURwIZ7YOwC5OwrHo4hjWCzpc5ZegEfXpi3zn39zURZ7jc2YCXEj5iHBNq3NXLG RAaN/DQUL/WnvM1qtJGaXRu8GRX9rT3dBJmpcdIBZMUZ/L/ACz0DuJ++CDqZ8EtO8hkC Gqv/2P6E28SUyU4TooR2fsA9rDt+O9UEm+r2vNfWmz72ZovOYd5lFLzN+nDFILEMoe83 vWdOhUcWOtwcPljrzFO+Ul6ynO8ouryM7J8dM32ZQsizZGiKFx5h/AGe7XmgegmPRx8Z enig== X-Gm-Message-State: AOJu0Yyy4NdtDQU6H2ZrBFOOxoWGFEWTsdV/h39Gn/1DplAcd0eb+f20 OSkjVjTeQP2x6t81uu+0yHjPg3RQS+ukVooxQHndZpJL8D/k0nja X-Google-Smtp-Source: AGHT+IH+rZczV1rucrOJegV86RCCzxDjSfxcW+VweSbEmrGCi5wWUt76859GZxn1s0ipCW3cR+tVCA== X-Received: by 2002:a05:6000:a81:b0:368:5bb4:169b with SMTP id ffacd0b85a97d-371943159b6mr2775762f8f.4.1723853917768; Fri, 16 Aug 2024 17:18:37 -0700 (PDT) Received: from [192.168.1.91] ([2001:818:e92f:6400:96b:aa92:afc0:2d3d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429d781047asm104842055e9.0.2024.08.16.17.18.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 17:18:37 -0700 (PDT) From: Pedro Falcato Date: Sat, 17 Aug 2024 01:18:28 +0100 Subject: [PATCH v3 1/7] mm: Move can_modify_vma to mm/vma.h MIME-Version: 1.0 Message-Id: <20240817-mseal-depessimize-v3-1-d8d2e037df30@gmail.com> References: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> In-Reply-To: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> To: Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Shuah Khan Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, jeffxu@chromium.org, oliver.sang@intel.com, torvalds@linux-foundation.org, Michael Ellerman , Kees Cook , Pedro Falcato X-Mailer: b4 0.14.1 X-Stat-Signature: otq54a6e7rzw1yh6puxymuwctsocnaya X-Rspam-User: X-Rspamd-Queue-Id: 38D6B8001B X-Rspamd-Server: rspam02 X-HE-Tag: 1723853919-905372 X-HE-Meta: U2FsdGVkX19LulBnKjOcTsqhx1VWG9hIRuWoQWaXqX9kcoiF+oNxQsNZeC9ZYAXYFKKh7QqnMFhFQNh+4Ef81qCa53I3yKw0BaxxCXHzcO4PsKtsEnv1RXYasB5yzsNczZp45ACplmGp3MBd76XCASRDDg6H5NSJZm9zJzbqxST0rb09rIlJu5/XZxba6QZc657IHeMnSn264k3EeO0cURRn7O4EMgBrMw+Cu04MMDoKF08X4qdnWEkY0GlVtk2lqnyQUjyacwba9O57B/M1ZE9H9LoCPndEvR4chJmC6XEJ3O1CNqg+qICMSb1S2PffMclDjdnjc1Xq/t29sEBOKINO3hMmXmmOEpGkp4HBN5bR3X0wF7ZxmfbbKf3YLOS3wtOPP/PYKV84ISBsB3J3F7sIs1eQuUGBpbxSh6YXfvw0cmLxsg2UHXKDRKYZYITM2zdYEDIV01FIKAqc8K8ufBulLiQVrsF48roUReRJ+g7B5oiNEyWSoDMTkayKzMrygywNPfg2eifB0MNFT75IJRdt4mw0Zc6zR7vykcS1EAMG7z0qO6Dq3rBrb/ZzQm+fqAFupCYdtRPUOrqtrjCyNOGwTv4XHgLHBNKQSDB0zCzgNiFWSnLWb9VYhc+5J59OMxckXW3M40marhw7WSAvid2XY312ibT4fxmdACf30RLYdRXfKUTBqzEWX5vn4JX3Lpglq0TfNcORPkCwwmc4go8fbsRVQoubCA1SN0lEfQZ8j+Br8jhPezY2WfMsYLeXZJ4xv0hT/0VEbauJSBAAU/vBOWnf4jYRywDckxPFtSSoCNLYmbqvXt0c21r2hjnYKKQDA/yzH7sNtLixXL0J++2b2NFf+Xu53KUWUjdvpaRWN2J0rZUH5Az/cRa2UtkXRtfcEUVmq3LBaTitA6pP+MDFAf/XtFPt2TkuE4t4wxn7l6l8Soq+9wprEuyovKMc2xW+j6v+WOMP2S4PdCh wUA65xAn d1b1QepZO65BUlSMdWhDNbPIOdxxx4as87WdUF2xSwODOBfAkAUEQ1Lhu+hHdH32LQOmAexFplJ2giUWP2or1tL27BlrJi+A0yM/mCktwA6+lXQf+AwPwnEKX2FirDNw7m9LoLZWALec9qbjLq9s0OZfRAM0VUpdW1YkbZpTBQFl5BSkEgrB6LaXxqoVjAvkgiPv39dSbOA/txCEQ0ji0mJ7yC0INC9T1pOJWc4/6ECTl4cllImDlzZt8DEDeoEG8GANkuBnTXVUooKhXFxY0WMWi71hL9h3Kqf91dJsTK8VPDSMYj1P4N+uB/jPt2jBgI+JkuMKDwxw/aNOIQZwE4S6HHAH+vlP49QMaNqaVTcZldhUwp0LUwEzyqnSNGK3gFP8ljjsjAq9h1dVIP+U0bpppY6noGB8xTZx9eP0pPjvB0S/t18EMATmVfAZn+65zg+43YIrQS0t4UCgv9mgb//lO6z/MTxk2FquCBytsu1dwyRU6n/MElupp7T06QrBhQ2iQXNgrUyG1q30= X-Bogosity: Ham, tests=bogofilter, spamicity=0.001109, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Move can_modify_vma to vma.h so it can be inlined properly (with the intent to remove can_modify_mm callsites). Signed-off-by: Pedro Falcato Reviewed-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes --- mm/mseal.c | 17 ----------------- mm/vma.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/mm/mseal.c b/mm/mseal.c index 15bba28acc00..2170e2139ca0 100644 --- a/mm/mseal.c +++ b/mm/mseal.c @@ -16,28 +16,11 @@ #include #include "internal.h" -static inline bool vma_is_sealed(struct vm_area_struct *vma) -{ - return (vma->vm_flags & VM_SEALED); -} - static inline void set_vma_sealed(struct vm_area_struct *vma) { vm_flags_set(vma, VM_SEALED); } -/* - * check if a vma is sealed for modification. - * return true, if modification is allowed. - */ -static bool can_modify_vma(struct vm_area_struct *vma) -{ - if (unlikely(vma_is_sealed(vma))) - return false; - - return true; -} - static bool is_madv_discard(int behavior) { switch (behavior) { diff --git a/mm/vma.h b/mm/vma.h index 6efdf1768a0a..e979015cc7fc 100644 --- a/mm/vma.h +++ b/mm/vma.h @@ -361,4 +361,32 @@ struct vm_area_struct *vma_iter_prev_range(struct vma_iterator *vmi) return mas_prev_range(&vmi->mas, 0); } +#ifdef CONFIG_64BIT + +static inline bool vma_is_sealed(struct vm_area_struct *vma) +{ + return (vma->vm_flags & VM_SEALED); +} + +/* + * check if a vma is sealed for modification. + * return true, if modification is allowed. + */ +static inline bool can_modify_vma(struct vm_area_struct *vma) +{ + if (unlikely(vma_is_sealed(vma))) + return false; + + return true; +} + +#else + +static inline bool can_modify_vma(struct vm_area_struct *vma) +{ + return true; +} + +#endif + #endif /* __MM_VMA_H */ From patchwork Sat Aug 17 00:18:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13766877 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 2CC2AC52D7D for ; Sat, 17 Aug 2024 00:18:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 409566B03CF; Fri, 16 Aug 2024 20:18:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3B9A46B03D0; Fri, 16 Aug 2024 20:18:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2348A6B03D2; Fri, 16 Aug 2024 20:18:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id F326F6B03CF for ; Fri, 16 Aug 2024 20:18:43 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A63401403E0 for ; Sat, 17 Aug 2024 00:18:43 +0000 (UTC) X-FDA: 82459826526.09.8025A3A Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) by imf06.hostedemail.com (Postfix) with ESMTP id C135D18000F for ; Sat, 17 Aug 2024 00:18:41 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=O5CIFoKj; spf=pass (imf06.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.208.176 as permitted sender) smtp.mailfrom=pedro.falcato@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=1723853848; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hQdfB2TJjcHTWcrkMiwvMjty3cwhCh0Z/hQjDmUKSz0=; b=o1RAd8y4647sqyS3C3ajY1kyTq+4UOZolcsAs/+bgaqkolCJhjNWqpm8SuQTXfANVGTejg sQioXYujP40A8swypu+kXy/Kg2Uw3sf+htqZOjTjqaWtHFa6Ad0fD51dHwpXTmhSCRueNe nzXEssb57Pkiskcg4uhyrovyW42spqY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723853848; a=rsa-sha256; cv=none; b=TmpdKH/FyQ8zxvcce4eSYHg89QPmIx3ymulBGD20gzMD3aajOL7uwP2h3QCq6IQaCY20mT 5gEZLjOjdustlQaImhzG89hexnqWrJzGiui1WRcriygmJkyPQx3JUfzQtzQsZYfrYciZQO Ia0Bkflwd6XJKFJQuZ9GkQkvCS8Phu4= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=O5CIFoKj; spf=pass (imf06.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.208.176 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-lj1-f176.google.com with SMTP id 38308e7fff4ca-2f15dd0b489so35015591fa.3 for ; Fri, 16 Aug 2024 17:18:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723853920; x=1724458720; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=hQdfB2TJjcHTWcrkMiwvMjty3cwhCh0Z/hQjDmUKSz0=; b=O5CIFoKjHctDAGZXHb3c9zhiC4A0t4p++AWWCEmVVhSQ8q3XdVM8slTZMJnEfRtwsN ddCidFR/C86etZkizeYoKesgw6fkGrDLToZZjt3mfSCNOoThnVfXlK0EBOvPCECcfLL9 hcJiruYq2cF9G176zVULflHJFxBb8Pvc5mVQEFdbFTerOccngyKOd9Q4b/qXF4lYYLF/ olOjJr9sgjvphTB4QOgT9wYnBhiNPbf0re0Racrqh1w3hkGhvNCFG3T5V0Vr+XVTkCeQ kurhRmp1rgWiCyR9dmOWtYdZ4cfTQmAoHQlfHC4weMXJQBlBFdF7VzMNJxpsuZKOpt1o lHfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723853920; x=1724458720; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hQdfB2TJjcHTWcrkMiwvMjty3cwhCh0Z/hQjDmUKSz0=; b=rutJcY+VTjk4AEjcWykd4L4g3BA7CM3GAU11SJM2D0s0VWlCHHMXZ+xf7jVPOj1gL9 itqkEtp81X2S1DktWfO7f5gviyXIjBsPNdnR4838J3/N9tZ8R3pYe5bnKVgDhl2RRZS6 XH0AiA41iATiq9MHW/adhxgLOQvGHZdFLATk+p3cCC9XDYJJnqyemcgLz1MZvVE3VNIi PZIzkvxv08TjUA7ywi5NBOnhTVnRGy+CGbVwviXVk0LzaA2WT6RRnCKS19n6LSOtnsSp T/KXB8lGN1jOO8jJ61TrFueL6UcudZFYGCEytbKi0Jq/RvX8n0JciIG7djsEVKH3zadT I8OQ== X-Gm-Message-State: AOJu0Yw9HlhHZgbsTPgAR2efRz4ha/16G4Ab5rJ0hpCq3TU0Lqp2qtfB JM4GOx1irujuk37lrz9wnTa3M67PBi8z/5FR79yuGopnKr/QbVPA X-Google-Smtp-Source: AGHT+IFZ9b78tVtcZPQGq9NYpF0/YwksiknLfMYODd0Q+Kd8EWcZCWbrFfO0fyGveiS9SrzB4Z6g/g== X-Received: by 2002:a2e:d01:0:b0:2f1:9248:c325 with SMTP id 38308e7fff4ca-2f3be5f8cecmr29055721fa.31.1723853919102; Fri, 16 Aug 2024 17:18:39 -0700 (PDT) Received: from [192.168.1.91] ([2001:818:e92f:6400:96b:aa92:afc0:2d3d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429d781047asm104842055e9.0.2024.08.16.17.18.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 17:18:38 -0700 (PDT) From: Pedro Falcato Date: Sat, 17 Aug 2024 01:18:29 +0100 Subject: [PATCH v3 2/7] mm/munmap: Replace can_modify_mm with can_modify_vma MIME-Version: 1.0 Message-Id: <20240817-mseal-depessimize-v3-2-d8d2e037df30@gmail.com> References: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> In-Reply-To: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> To: Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Shuah Khan Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, jeffxu@chromium.org, oliver.sang@intel.com, torvalds@linux-foundation.org, Michael Ellerman , Kees Cook , Pedro Falcato X-Mailer: b4 0.14.1 X-Rspamd-Queue-Id: C135D18000F X-Stat-Signature: 3bsauy931zf8pm1mmziyhtekeib3kuw9 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1723853921-501071 X-HE-Meta: U2FsdGVkX1/Sr3DfZbpaIKe5dWUrBwVgSITDqA28+xY6oq+VGWt6D1xsr1nLcsC9r+hlJXFC3rrbcfDEqbrDBS7qCGackI8DV8C/l+EnmfW594mbioYC/il5dlechg4voBXrRyMm4pBwEp3Bs0EKOOU/EK2u07TC732GFd4ujgTPxRYxawROvHwlykoEqPrhLwOpKvECs/oxoj4HcrYWWWvYPlqF0o506vUuIw2Xl7oEYP9IYsBMScvIY8mBr1okxFDrU5CGQrhnA9Xcub2DwzeGKSoJa8ypfR4wCYiXDaJx36x8h8Rntt8tFCBzuHGzKJT7Uf6L7B2/Yq7BvWhG4etuATf2ZxGZtVHOcQZ5833hXWadtuqiwDCpfML8oSAW8Y9udaApkFc3McmCYDJPy+VKs3XmiKO8FL1dcMefAa8z1tOPyGyykCc2DdD3tzeKmT7Oewd+tN49gxF/HpBR7fVCSSxvhh0BpKqynnvewneGoWhomnzwL4chnM31H4kZr1bFcSVbskilsSmuHgLnsqCF8ADh0wO3cCdtZJR5zhdags/FLdb9H1kOPbD2i/Dcc5g3HdAgJV40FngWz6XItlL5vSNz7QcFlKve3WVqsQf2GZ9TDoZRg15URYVM72EkHroJFnqOl7eeDU0BMQRpudmXnPX4ZDDf6D9t2btnPYBFodpcLuglNqolemYKSzF1VHNdgWtPK30FcK1ph3+tcwaQfCJIvvRAhYF/nRs7Cd+wKj44PMgjP4Q3OqYebh15egf7qaHb7h7CtwA7zdwXiQPcebnD1IYWHHqcNJprGAW81OXpq4EPmsYcnMeB+BfPVGonY/A7+XF6O/b2Yfejm2BDy95yLrNL4SslQiyaPVyV8U5lVvdU1IGzjAHV1dOr5Dq9AySlbos6NbaSR1lx+FyD0Srcsb8RRYgzkScfl6JkFk+PoA+gtk5Wmp+1C6uYxNBvN7BFu7GBodtbAz9 0YSoS8kB vd6UOspkHUYYMIkwSFmh6BkJeYxJPeAibwKggGK6agYBd+o83Am/6qiqT5ctxrad5xNDShfZnuQCB/2dqRijV7BGbmj8rSCufhkEQGxrO1tBOdHUcoC4jpx8Mw2sYwaCCHmQFl/S1KW6vYlXCDmwKBlBwIrzD9ace5sFzs3sfKdvGJSF8bO7FFOMpkDZpiibYraxVqFIAH/Ud1C5HMt0n/ZhnxsQuEl6F4Y+SSHS+ZWdc6scS9IPd/ofe5PSNAxwoesGMcMNlMP8MJrmjR2NKw+/EVD3NL1fHeEl0JlxD73DDJx8+ax40Z5bXIKHgWIFq3VFqdIeo4Gn+K3g6Sc8+/57/kliQLyNtviIvMlX9Q+377gB03Wg/yBuL5Zbn/L1eEXunogbmMFMcjWitaGUAlnrkSrDPYLTjXGaUPPLWbgmTSrcFRompYci4LdZlOe19JsviJv9ZSOAzItoBGUPytCq2TPJfR5IoQ5pZcZUn3aVR4Zq5wH3bK74RUNtL5s4To/8YcrUceCGARDM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000336, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: We were doing an extra mmap tree traversal just to check if the entire range is modifiable. This can be done when we iterate through the VMAs instead. Signed-off-by: Pedro Falcato Reviewed-by: Liam R. Howlett --- mm/mmap.c | 11 +---------- mm/vma.c | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 3af256bacef3..30ae4cb5cec9 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1740,16 +1740,7 @@ int do_vma_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, unsigned long start, unsigned long end, struct list_head *uf, bool unlock) { - struct mm_struct *mm = vma->vm_mm; - - /* - * Check if memory is sealed, prevent unmapping a sealed VMA. - * can_modify_mm assumes we have acquired the lock on MM. - */ - if (unlikely(!can_modify_mm(mm, start, end))) - return -EPERM; - - return do_vmi_align_munmap(vmi, vma, mm, start, end, uf, unlock); + return do_vmi_align_munmap(vmi, vma, vma->vm_mm, start, end, uf, unlock); } /* diff --git a/mm/vma.c b/mm/vma.c index 84965f2cd580..5850f7c0949b 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -712,6 +712,12 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, if (end < vma->vm_end && mm->map_count >= sysctl_max_map_count) goto map_count_exceeded; + /* Don't bother splitting the VMA if we can't unmap it anyway */ + if (!can_modify_vma(vma)) { + error = -EPERM; + goto start_split_failed; + } + error = __split_vma(vmi, vma, start, 1); if (error) goto start_split_failed; @@ -723,6 +729,11 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, */ next = vma; do { + if (!can_modify_vma(next)) { + error = -EPERM; + goto modify_vma_failed; + } + /* Does it split the end? */ if (next->vm_end > end) { error = __split_vma(vmi, next, end, 0); @@ -815,6 +826,7 @@ do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, __mt_destroy(&mt_detach); return 0; +modify_vma_failed: clear_tree_failed: userfaultfd_error: munmap_gather_failed: @@ -860,13 +872,6 @@ int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm, if (end == start) return -EINVAL; - /* - * Check if memory is sealed, prevent unmapping a sealed VMA. - * can_modify_mm assumes we have acquired the lock on MM. - */ - if (unlikely(!can_modify_mm(mm, start, end))) - return -EPERM; - /* Find the first overlapping VMA */ vma = vma_find(vmi, end); if (!vma) { From patchwork Sat Aug 17 00:18:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13766882 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 7208AC3DA4A for ; Sat, 17 Aug 2024 00:19:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E5CB26B0271; Fri, 16 Aug 2024 20:18:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D09F26B03D9; Fri, 16 Aug 2024 20:18:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B5AF96B03DA; Fri, 16 Aug 2024 20:18:55 -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 968266B0271 for ; Fri, 16 Aug 2024 20:18:55 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 369C1803E9 for ; Sat, 17 Aug 2024 00:18:55 +0000 (UTC) X-FDA: 82459827030.20.5EEDC6C Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) by imf14.hostedemail.com (Postfix) with ESMTP id 524FE100007 for ; Sat, 17 Aug 2024 00:18:53 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nqmZufR1; spf=pass (imf14.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.208.176 as permitted sender) smtp.mailfrom=pedro.falcato@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=1723853874; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=yXNmIj+NcTYxWHoqolJZlZHTThjq2Cli8Ss2fpPQn+g=; b=esgHvY4oggGzYKNqmuho6C4fI215Gw/1ban2beTzdTgE8Mnn5lr8DqfAxy0OwB2j3kLfu0 aiMf3mduSXOh5mJQ1j0gNnvCNEwUiKDJoxlqKemNaa55CMwb2tSMtXefZrxVY5hA9htrGr nKJe3oBnWiLNjF12+6fGirL5vLnltI8= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nqmZufR1; spf=pass (imf14.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.208.176 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723853874; a=rsa-sha256; cv=none; b=oNMOZ38ntInYKCu7CqwOqV8o616Y6ja0Aov8VFrGBbu0H8XISQrqrG8GAADCkmLSSH9MKb jA3dsj27ZMcy5EBJA9yRLCta4h6XPJk476uTJGLh0cWgjRn3T9kM9A5RRxmrnDoDIbsRKM WwF2zeHV5iXv1zd/wcodCVMXtZlZ11s= Received: by mail-lj1-f176.google.com with SMTP id 38308e7fff4ca-2f15e48f35bso26156011fa.0 for ; Fri, 16 Aug 2024 17:18:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723853932; x=1724458732; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=yXNmIj+NcTYxWHoqolJZlZHTThjq2Cli8Ss2fpPQn+g=; b=nqmZufR1ZmNS4GvBM8ESsgQEQEhF38RTJbWzgFBgJhXpZpElwQ+mFI20QHpTleY5zL 1e2a8X+2H9OwqKKXuppEXyr0HbdZuIOyVkLYnMP+rKDluqLemhkJmmw6qMBF8bQ1n8CF aFMV1abfmdTn/OwUl3oJMS3KwodQTOQbrSh+NghmTveTc2In2PnWEfOvZJDutKZFub+A 9O6t+fLVY3NdDcOTO2Re60M5oJT+kE6swvitkQEYwAM5b846QKH0U5P4qP0+P8r2daJD 9tBqMWa62oVox/mw5vSBlGDXKAcfPyBCEZVkRUCEKz5FqqhxUdhwhFqXgGTjeU+EheWf ThmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723853932; x=1724458732; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=yXNmIj+NcTYxWHoqolJZlZHTThjq2Cli8Ss2fpPQn+g=; b=tmDlb7tVJeGUzC2G0bkxz8IdSH2s0R9bTgAEsowVDn6XmqUGTimUUbCKLURqCvpr31 XpuXEltl2NsMtFAodvMSivX1Bc996cCc5VwUOFOgc1iQBqolaAzD6q/UeYXYskP5SrJr X/3PkeJnTPwtq3GkT3OZSGOzQeY8ISckA+xiEBk6SuNrRyhKgXjWoG3v+zl47xR5ZP+S Mi2MZdZLzmoHyhXTEycvhs+dAHGJz0rirSz18nDRhumSnWAXYziyUHBXE7Au5LfMirPM LnBxhfr/ESi3TNZTiVnna0wjt1j5JgsKPOv/22l5MiYy3/FLzEpowEzq1TPMzLxdF2yF YDqg== X-Gm-Message-State: AOJu0YxFbx8ZuXMig4+acDKaRkIicfMv43WSK3gGUycgweedFfSVNGHR Y3/CZpOIXJBs82jyul7gM2h5w38wo8FogcblWV43YDcBjpAKG6/Rftm3HAUO X-Google-Smtp-Source: AGHT+IFoYlT2c74A++INbVCxGR66uRuw0eMCD6t4X/oZJnDreC1ipLncpJI2gMf0RbWhFUUMPxAr9w== X-Received: by 2002:a5d:5a15:0:b0:360:7c4b:58c3 with SMTP id ffacd0b85a97d-371946a0b75mr5498311f8f.54.1723853920474; Fri, 16 Aug 2024 17:18:40 -0700 (PDT) Received: from [192.168.1.91] ([2001:818:e92f:6400:96b:aa92:afc0:2d3d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429d781047asm104842055e9.0.2024.08.16.17.18.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 17:18:40 -0700 (PDT) From: Pedro Falcato Date: Sat, 17 Aug 2024 01:18:30 +0100 Subject: [PATCH v3 3/7] mm/mprotect: Replace can_modify_mm with can_modify_vma MIME-Version: 1.0 Message-Id: <20240817-mseal-depessimize-v3-3-d8d2e037df30@gmail.com> References: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> In-Reply-To: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> To: Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Shuah Khan Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, jeffxu@chromium.org, oliver.sang@intel.com, torvalds@linux-foundation.org, Michael Ellerman , Kees Cook , Pedro Falcato X-Mailer: b4 0.14.1 X-Stat-Signature: 8woz18551nrdz1ps5fewt6wwmhkrnps5 X-Rspam-User: X-Rspamd-Queue-Id: 524FE100007 X-Rspamd-Server: rspam02 X-HE-Tag: 1723853933-147952 X-HE-Meta: U2FsdGVkX1+qE6NadW4Kc+LUcuw/WyAA5gV209dKwyh6C37r3hFgzWRTQF8RwRPwV/OfIakjodd1KPaHlv8brmS/eKuTaGy+qvrEMF8kjJPAF2fXxFGeQDWVjOwjLlWCeBdUfIwBzW1bzL3/jwZccqkHinHpuIPHWb+lrwHW9Z++oxHJVLQcom0kQQRnDhYaHlzrXFHkFM1uAS1euR8dxIsM+zq+NGTKjzcWAe2ExPbxyW7dWbBUsK8Ej2icpkCK5/Ut6ALxJ/H2AkYoZnuk4mXVIUhvVn8pnw5RASrXov/7rbvl6zXFAqUD30cbPPhVbD+fQUNv2cwDdQCI33q5MCdolxDTzbCYufnEV5h0DoK1olb+XjO4B79nNvZaFJ1FXizzT7knZki6AqIOO3X9Kl17rszNvqIiOuHPzAHQq3mMGG68TQts3nsfuXRlErOLvCnlUrNFteJif4312JvUwM1deR3ngeSpKe38rO6Br4PiNkg4RBZvHRl2UyBmtYYT9IrGkcyN4r9WJhzp6XaMXu1DbWLG/SNUGSMcuCShGlXZqiQgmN8Jf7IK0JIa6JNx/oTLcbxWh94uUc/8+xhrc3DAqiQyFuWWykqL7nxyzqlkr8XGxk97UzhUJN+enHxsRUuO7Lkw9o6lvY1I3inJhcl6jeGeXQoHKgWDENyjjcmpZfBUwHpv0wy1uPGS8l6BBEUyrj8HtwaggCt5ZsX18dEvnxwLHU32AAa1YdLho2aywBs8oAkic3YXdLhbZvklt6OHuW+e/S1MdVKG4xD0Bz2XSv4kPjSMgJPvjh9b1p9qF6rn8auwDIobnyzCi8JuY6ODUKNpwEEuDIcgyK0H1VEwbln+proxvMNsVxJF5SiF0EARDXWBFiLAQs609bppkSjvNnG+SJVteCtHqYo9GTyvwRE6xKbp/4a7tKLzcunnVvG7vuSU+raTSOziMQMB9vEfZK4mqv8DpYvPDZf VwJ6r6tE esauIRnmtPXrMc4v6Xv+RuddX53z9pF/SRgpe5v9UmBj4rHw9dZFRhiB4J2hvi08TvPZD30nPnwqdvUck2SHLaxAww7+k0scenrh16GyqFuTLxq691borJGytboIl1PRnIdsTHydztrPVNC+7Gs5LwQo5bP/3pIxomz5xuBuMrz/lriRSU17+RZLfyu7D4Ou9vPkWBCvjDza8TW+cfqyFB3kr5A5lykB552khbtGZ/4gK5DJBnYxWZt4WShS55DgPyUeS/zj2xyki565Z5WYeoStF8lowEXKKui3TnAJzXRI97/Q2UW4RnegYZJOgo03S8OBG2pSYs8dUOkj3rLJkE9j29SyYR68WhEtYKsUV2dJZVNFjEZRhAyp/cqR5jVudJqm+1OEVJpeeQd72sxfYdKiidTW7apTGKb89xMAD8+5kK+jhJf2bcGtNxrKps3eqZZtwnCIwp6PXLD2QpUn7O0zzeC7SyGtKMXa7ES9p63sK019q/qi001zmL4hCw3Bwn9aRF/vagh2hqoY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000059, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Avoid taking an extra trip down the mmap tree by checking the vmas directly. mprotect (per POSIX) tolerates partial failure. Signed-off-by: Pedro Falcato Reviewed-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes --- mm/mprotect.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/mm/mprotect.c b/mm/mprotect.c index 446f8e5f10d9..0c5d6d06107d 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -611,6 +611,9 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, unsigned long charged = 0; int error; + if (!can_modify_vma(vma)) + return -EPERM; + if (newflags == oldflags) { *pprev = vma; return 0; @@ -769,15 +772,6 @@ static int do_mprotect_pkey(unsigned long start, size_t len, } } - /* - * checking if memory is sealed. - * can_modify_mm assumes we have acquired the lock on MM. - */ - if (unlikely(!can_modify_mm(current->mm, start, end))) { - error = -EPERM; - goto out; - } - prev = vma_prev(&vmi); if (start > vma->vm_start) prev = vma; From patchwork Sat Aug 17 00:18:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13766878 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 CE6E2C3DA4A for ; Sat, 17 Aug 2024 00:18:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 801E96B0159; Fri, 16 Aug 2024 20:18:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B0C16B0161; Fri, 16 Aug 2024 20:18:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 602D56B0169; Fri, 16 Aug 2024 20:18:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3F3606B0159 for ; Fri, 16 Aug 2024 20:18:47 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E8DD7803EB for ; Sat, 17 Aug 2024 00:18:46 +0000 (UTC) X-FDA: 82459826652.25.1E4985F Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) by imf24.hostedemail.com (Postfix) with ESMTP id 16F2D180012 for ; Sat, 17 Aug 2024 00:18:44 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HR0FvjGE; spf=pass (imf24.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.44 as permitted sender) smtp.mailfrom=pedro.falcato@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=1723853911; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bAmgLDl1px7jqd76Q1p746SydraVXJ6Ud6jad/RgDJQ=; b=cdYvHIeBxzNhUIv1A2a/0R+biFx+Uy1/VLp/Zz8BE0cNqPW5mxCAyZt9GJPOZYHpB5pLDj zZ8EQG5OnwRk8JAfBLYrW6Wwm+TT4cBPEXpvXK4bcLM5wX6n1/gHr2HfxQm7VPP9YmXzot ayXxkMRsx0hRWXOmF7bNsHVmdg0Xbuw= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HR0FvjGE; spf=pass (imf24.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.44 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723853911; a=rsa-sha256; cv=none; b=WUPdCBatfny5zPi13lMvwGoGaVXIRgYrNyvXzP7fQd7MZnqjZcgINZHXNnlJCulSU+I4F3 5H31NAvp50oPmHjErIlrqoChnMxgD8v40la9cENPzB/4WucSYfLcIKX4Yuk97EwVOl5aas OE1APKloZK1zm3HOgmO4S6bW1rbfhDU= Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4281ca54fd3so18133355e9.2 for ; Fri, 16 Aug 2024 17:18:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723853924; x=1724458724; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=bAmgLDl1px7jqd76Q1p746SydraVXJ6Ud6jad/RgDJQ=; b=HR0FvjGEI2aHxF1IOQbQcdXo6MRd39tyejOQUrFjyvejtxbc4QmStvZuE8njghiVqz BnuA7ASQKVkz+yW8pnUqnDzUuTm1Su5+ndAXeRQI22jcDcJKG571f4XzG1YOuHaxdEXB J8y6Y2UHqhn2i0SkMvLB4jo5uOpoimanpgtG07Ow4W+IUUPdHsbUWt3YerKqWwbricPO RjxchTBnpksf+yK8bo7qNMd6UdJKckFPwhKHi8u7JvoRiD0P4xeQn6pS2d4KRV0rlL4e 2Yd7VcqGmMY1vuoXxEfRg8N+d3m4fow8g/Pc5NNL5oZ3ivAPlRiIcQFSNBQecrRoWAea AmwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723853924; x=1724458724; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bAmgLDl1px7jqd76Q1p746SydraVXJ6Ud6jad/RgDJQ=; b=KHaFtPEXhkyx9cUDd91/JkreqdOIMaPBDjAccVGgrEUe/UDhRTharOT2kPqW9gDrZU 0kgAJ+XoXhlmq4+c2twuxdOr3RxQqQT6z4NejuAAe43prwNbBhqqDsnG6ClmujQcXRde FwsEDRXp/gQPtU+DD98SoTy3iaIbcscqruBy8/kyaXCs9Bc56WDcgW1qtSlbJheX6zWZ xmCab8bfLiQT95sqqs5vcvG0UE3ttRc+dUtoZ3c+TDryD/uQerA/jaM6pArYhKiqVprd NIQwRBiPCHezCsmD1jiCIZhzbKXC5rpSXZAoRHwLo6zCbUg18igOC+ByKprPfLmqhAiM Um8w== X-Gm-Message-State: AOJu0YwN4xD4/y5VOwYGxgFgze3hLNQd5DIjZF5kIxtKmkAjUYyXHrRu 84//iLNhpWJ4HGUJQiWNTT+ZykTcupZT/246fkPzUG3vnKlI8iyy X-Google-Smtp-Source: AGHT+IGM0TLfyEre+jfwvYu3EzVkhOnPhcS/imjEho/f+Tms2EZbTyomcs1S5bb2scgoXXDqQlsfmg== X-Received: by 2002:a05:600c:4fc8:b0:426:593c:9361 with SMTP id 5b1f17b1804b1-429ed7d1f39mr29141995e9.26.1723853923095; Fri, 16 Aug 2024 17:18:43 -0700 (PDT) Received: from [192.168.1.91] ([2001:818:e92f:6400:96b:aa92:afc0:2d3d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429d781047asm104842055e9.0.2024.08.16.17.18.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 17:18:42 -0700 (PDT) From: Pedro Falcato Date: Sat, 17 Aug 2024 01:18:31 +0100 Subject: [PATCH v3 4/7] mm/mremap: Replace can_modify_mm with can_modify_vma MIME-Version: 1.0 Message-Id: <20240817-mseal-depessimize-v3-4-d8d2e037df30@gmail.com> References: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> In-Reply-To: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> To: Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Shuah Khan Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, jeffxu@chromium.org, oliver.sang@intel.com, torvalds@linux-foundation.org, Michael Ellerman , Kees Cook , Pedro Falcato X-Mailer: b4 0.14.1 X-Rspam-User: X-Stat-Signature: wg14d9j1eoy6816jch6obmezbrc61cem X-Rspamd-Queue-Id: 16F2D180012 X-Rspamd-Server: rspam11 X-HE-Tag: 1723853924-833835 X-HE-Meta: U2FsdGVkX1+WEQVaEd1HCadKWGN+KDYuYUkxIdpQM4p0AdF2tdBEwh9nF2aLuSfd8TpZCAVygUexyTJyI0MPiR3pCZeXMaM6HfcR1KOfIhjSoQSCYG1vY8opqv7PnRNsR7Z/IhGuFkQfpDq/dMXfivVTjtM+0+2IuFekFEdCjm6Xcr7VJ8Xa4OETIXvGrS61LtDijhz7aGi2YKp+vMi5e6gaThWlHppLnfJyE2OyrI+prCCEdpbj8xOtcMug63McKaLz1DjuEx5cgiwBuxa23glvLfQHRCK6OFOagbFiiJAjU3nnOwyvmzi671kWTkbTsIBpEUnim8uHRcaaJeBbB+QiXfRc0/lXRorpLTF7xxEe2UI04S74oQi3AliTO/XelTjrF6YgvKRvzVuoy7DwAXyjB5wQ23ye01jd6ic17G7+BGT8f6qpBHemTsX6wMXnLFNmZyueZhKzzmpDFB3MFKF0XzwdkAhUs8D4DmxEd2Ay+GPxtVUyWPdYF7jkE8gaOglEF+hfYJc4pouTV/UuWr91A3PJ3nkVJLm+rWSsoGFLnz2x0lPOQu/WbXo/vS4Q8wgvbW7VK5vktrh6p3NM0HO/SfFsW08EvDnehxzlV+ZSfnvdco5q9GgrsbNdmgUQMsMNns0FZARCeIGVGURsn3n5jvcsBYH5RBt2B87ATtp7F6B0+ho+qeQiEO5sJ6qNsNF5/YRBQ99Z0DU+M+SUBUW1aPH4TvdvQ8HemD8zNzatpMMl6pi587+YUIhnYlth/SuC5h+ttbM/nht8q0Lp4CArJCPIopCKATPr9jB6JqgajVIl0kcGZZgCOQTqCeZjyvy4eNAuvIDNfI6zDjLsjfOKE06LWQLSGcz5z05m6zWG33X3gVUuPR0A/FKUKgfCMJ7R78mmrcuNWG+zcHVJUC/xFAT64pRjBMZekc0ZvU+0C+D4wjDL7ZvlB9e06dbE4Dbb7cstb8TUnA7wdc5 0u72+Tqj zXZiux7Zs6xF4iha7ARVmORlYUHvf4CGmLMvNI/rVYXozol1kweQMB1vMqV1nxRNO6JadsrPbARg1Lg6RhQXBWoj7BNmGZ0zSxeoBPxNEmGgU4qSetLygM6HzxLI8B6GR6Ou+igwl1Amdqiz2oOtEyqNUQ57Ubtu6e58GO61uwnlOSkzQEefpIdaOEgOon49m+I1E2JaI51pKT9PUdmQKwAEV0LjkoaCpUSu3iBr2ECY49VfykSYatpKp0LDKCct54CKNUY/RtIwnzaFq3fcPRJL2D91N86AsOGrxz+IsdqYZBHqUDX4vhdnZ4R+ldwp/AxxeCUaZBShX/DQ+sG9ZW69uZ8xIyNywA0z1UowqOlK+6jt0KOtyvbQtDynf3LbaczecF8a9fm4wdXiLm0jSeydLNcARuJRa+1fOjX6jlBC5bVP1wb1GOh67+fqF2czMo5RexzwbskquOC5vxGerL+ZtKFex5Zdr4AzpxAdC6q97uh6YIHnGpbEfnYJc9MlMoimxAoXRfZ/znYA= 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: List-Subscribe: List-Unsubscribe: Delegate all can_modify checks to the proper places. Unmap checks are done in do_unmap (et al). The source VMA check is done purposefully before unmapping, to keep the original mseal semantics. Signed-off-by: Pedro Falcato Reviewed-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes --- mm/mremap.c | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/mm/mremap.c b/mm/mremap.c index e7ae140fc640..24712f8dbb6b 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -902,19 +902,6 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len, if ((mm->map_count + 2) >= sysctl_max_map_count - 3) return -ENOMEM; - /* - * In mremap_to(). - * Move a VMA to another location, check if src addr is sealed. - * - * Place can_modify_mm here because mremap_to() - * does its own checking for address range, and we only - * check the sealing after passing those checks. - * - * can_modify_mm assumes we have acquired the lock on MM. - */ - if (unlikely(!can_modify_mm(mm, addr, addr + old_len))) - return -EPERM; - if (flags & MREMAP_FIXED) { /* * In mremap_to(). @@ -1052,6 +1039,12 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, goto out; } + /* Don't allow remapping vmas when they have already been sealed */ + if (!can_modify_vma(vma)) { + ret = -EPERM; + goto out; + } + if (is_vm_hugetlb_page(vma)) { struct hstate *h __maybe_unused = hstate_vma(vma); @@ -1079,19 +1072,6 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len, goto out; } - /* - * Below is shrink/expand case (not mremap_to()) - * Check if src address is sealed, if so, reject. - * In other words, prevent shrinking or expanding a sealed VMA. - * - * Place can_modify_mm here so we can keep the logic related to - * shrink/expand together. - */ - if (unlikely(!can_modify_mm(mm, addr, addr + old_len))) { - ret = -EPERM; - goto out; - } - /* * Always allow a shrinking remap: that just unmaps * the unnecessary pages.. From patchwork Sat Aug 17 00:18:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13766879 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 A630FC52D7D for ; Sat, 17 Aug 2024 00:18:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8281D6B03D3; Fri, 16 Aug 2024 20:18:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D8006B03D4; Fri, 16 Aug 2024 20:18:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 651A86B03D5; Fri, 16 Aug 2024 20:18:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 461506B03D3 for ; Fri, 16 Aug 2024 20:18:49 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id F1BA61C1CBC for ; Sat, 17 Aug 2024 00:18:48 +0000 (UTC) X-FDA: 82459826736.10.1D4F371 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) by imf17.hostedemail.com (Postfix) with ESMTP id 0BEEC4001C for ; Sat, 17 Aug 2024 00:18:46 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KwbRux3U; spf=pass (imf17.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.221.42 as permitted sender) smtp.mailfrom=pedro.falcato@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=1723853913; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LrPcZ5dxXI2S7kFYpXooZU8/pyUJQvH2vRgq9oEXpMQ=; b=vH3cy1ywtYF0a9neQNSXiYDljsx9e14FukVuIZFdSqyeXEhzJ7tsXFHNb47eyN+6Y/C31j c573qxY7QxsJm1LbK90ZSVCuE/rlCaZPYGIaUC/wse09+C+/8QMMoWPonTBfr//MJpnugc kSavFR8oXVwJ5nvkZ4MtDKPQEkhmo6o= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KwbRux3U; spf=pass (imf17.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.221.42 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723853913; a=rsa-sha256; cv=none; b=NQC/cyhs7AU7azKk6dxqNzMTdFdMkDe/KY6rHyJ5up/YyagTU1N0bzi7HjcUlQOU8xAW2u E5j3NBQlawKXzBChIfLXVXD3ZoQcTBAo8b21DReaI0AgBJdYoTqD5ReN5hoJkYEbIXsnxh rfXMpYOxm/BpFSAGZOl8mgQaiHj3GLg= Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-37186c2278bso1402491f8f.1 for ; Fri, 16 Aug 2024 17:18:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723853925; x=1724458725; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=LrPcZ5dxXI2S7kFYpXooZU8/pyUJQvH2vRgq9oEXpMQ=; b=KwbRux3UZWxHF/wraj3B9zgit+O62cS+DiOf+3Tdg3nKYJ2dtiUkh44KjB6yDk0Wom YqljrilxHBTXA4QCGssLMKGPzCR/SBKs6qIsvKPsTZF/wsA1kEiZ2xZ5k3R0YfjsRrTV 9WMiAmA0T5zM0ipAbnf4wHlvI6m7YNPfd+aBpGAgKCgOFegBqOOFq6m0/CAItNKdCKQb jFQpTmWGSwEl8it61dg5q2TXQwwFvyrlsvW5zs5KcXAYfoFTNe2rdXlHIX/D3eyhS6aq Q81Fyp8D6gWZu504AfO+3lxfNVGjFztY7neucVH33aYA4PQ7twZGQNRmIrjDxFkVAkQk F0qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723853925; x=1724458725; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LrPcZ5dxXI2S7kFYpXooZU8/pyUJQvH2vRgq9oEXpMQ=; b=G815ACOQ04hNPRBDmYCwrXtF1bQnXNHSSjt3/8Ck/k9FfzkukRp64EmN5Ho4bjx8+p nO8T/LHY9+g6NM7k+a75FOqDpyh43Ctwvrya/ON1vRwvounLwYuT9rTXxUSjZMauKVZT ywGGN6kTwsvGv41TXt/Jy41h1YF1wsWKiH3NwZtIN7mLf6TBgFRYJOuASA0VMcb0PDLk riF6TS0MIW+79AOSUOB0JYva+ErqIKg2L8BiYunYihslyoagacMt6wv5QJy+/F2TpSU4 2p6e7TP7ErLXBxOLrwnEMSXuffbtYCGj0pfiD6Xg3m1LSyEaAhxkBdWA+Zqdf3zuJnVi oosA== X-Gm-Message-State: AOJu0Yyd+OmHfEyJ/ft7RvRvsO/6e2DIwcD7MvhOfWP4x/agQgXvl3Sr f1ilAvwg2lJqAFkQXCgdXahf7j0ep6zBue1QoeWCCMNUtHZXcz6Z X-Google-Smtp-Source: AGHT+IHob2iXeWVcLHrIvT5oHF+c90MvHS34UpZmw02JaWHR+F0oJhRdWeWAe0XuH/19P/AChePKAQ== X-Received: by 2002:a05:6000:2a8:b0:368:377a:e8bb with SMTP id ffacd0b85a97d-37186dda493mr7610866f8f.28.1723853924555; Fri, 16 Aug 2024 17:18:44 -0700 (PDT) Received: from [192.168.1.91] ([2001:818:e92f:6400:96b:aa92:afc0:2d3d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429d781047asm104842055e9.0.2024.08.16.17.18.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 17:18:44 -0700 (PDT) From: Pedro Falcato Date: Sat, 17 Aug 2024 01:18:32 +0100 Subject: [PATCH v3 5/7] mseal: Replace can_modify_mm_madv with a vma variant MIME-Version: 1.0 Message-Id: <20240817-mseal-depessimize-v3-5-d8d2e037df30@gmail.com> References: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> In-Reply-To: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> To: Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Shuah Khan Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, jeffxu@chromium.org, oliver.sang@intel.com, torvalds@linux-foundation.org, Michael Ellerman , Kees Cook , Pedro Falcato X-Mailer: b4 0.14.1 X-Rspam-User: X-Stat-Signature: wxxme75bywh1bjenazqb89ie11wpt8rd X-Rspamd-Queue-Id: 0BEEC4001C X-Rspamd-Server: rspam11 X-HE-Tag: 1723853926-775876 X-HE-Meta: U2FsdGVkX1+oN/e8ek+gCMFydjc5g5R/P6eD4WBbhLsBKfJi1tiDDcy6FA67Me/FeD8U95vkGHRSJFjbsqsA7vtIJV8QQwDBWcSkJtCbBIE/Nl/wLL3y5QlRLmhzCng4gvQjbWOPyr6tEJ919JyM4NkFAv1D8ZBiSciOQTHS3FGDJ3ywEv8hKj7mqctZ4gP0KEnL1wDdlMF6mGAv2Jsa8GH6Tp46wlApXN3qXMYQmTIKy6Vmyp+2IjMApo143tcAFn9nAd7OpOATc11relrTBHY97qTHHRfd2DHNroIPmRHsPIvVRBgf1rEQgI27nM8/s58GdBXIv8ySluoxPuzcTQxx6LTytHzyUdlvlLSlMAInEYd0iwYXTj6s9ZfTXBgmNia7I11eYbWOMBlNsids9claUWGBkSu/3AphDE/q1HOvuCmbSfLQP/9OlaYC8GO9HTCfY8H03gA+kxpK+X/6j9UX49t2R3u+uBPeGz/gWVBC0BgMyjdrfnG7QVBUkqMPuExyOL+lm8s/SxJOdtg/474+sBcpy3HCROU2Lv5SWYCJTeglZu+RbR5W5TUQohpRebNGujS+jK/tu0jdfY3qce+SoUAGYmjPuZMrQ/rZ0XgiydkenVLxpnpoI0T8z/xdPBcGLh+Ld68tGzIItNp0RGC8ru+lQtPDsJ9Db94RFcVLsjQzIL/nbt9wwM9rt1LVFoyzs5MxKlk7FaL29DqDvDbX9FaM1iZZJ1zXGFJc82wtG6UoW2s91Len86+NZrZipU0WwBqvqdZfg/OwvpTq15WY4VgVb93ywQ1Si9Wy90TCKMPY0F87w1ewVoDTmlV+x3CB9tuoa+IoiSmWpv6siBrkcQhztTfe+lE1YL+0/MJG/5Ws88r0z4asRUO7H5vCX93W5Gm7CZkRKg/xmPUSBFViczg8n8psMvpuWwhmCGEXYuZ7hO/q0Dggv0AIrW1N/DOZ51+XnGn3hdlibEV M6cUOxXZ OxzHx8XnSVxt4itlTIx0A2yBvvc/eoFUyhnraX83L20jIhN9vl+hPwxYUUuRtKbJapnUKPmm4TG3ZVOuUIh4eGpSjQ54h7S1qgW8l/R734avBQf1A5yzfRnCIUM0ncyFKDlhpKOrK3gJHapAX3PxzyGfcCYCIOZREjdZw9J2S5fBjHi71rQLzfYoqHgPqSc4E9aCnVgE09qaEInBCjmOBXKwG4MYug+5ydUaiefcK0MMPhBDrdVsoQSGqlst8U8p3phJnrs+4oRpJfRSu9WMXkPax5N79DEh/dTG3aHcQ9gE4YcO6dzicLT+1FgRsaWzG1VXZifto4vthBSEa/SxvlyRYfqhIN5bPGTzX6ZDShyEDKSn72NS4gXot9E3CIRLMwgm9ppLDiYfbLaEqAi4dWvnGK5kSbpLuGKezQCffP31ZZdISkZ7+yhIDdqj3bveQyQ+RuLGA7HBtNIEmvCtf5MCK4Muy7W1k/aLBrPe8Lv7TAgS7e2xJUxzxQRUGuT1rjHGm48x3UHKI3BA= 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: List-Subscribe: List-Unsubscribe: Replace can_modify_mm_madv() with a single vma variant, and associated checks in madvise. While we're at it, also invert the order of checks in: if (unlikely(is_ro_anon(vma) && !can_modify_vma(vma)) Checking if we can modify the vma itself (through vm_flags) is certainly cheaper than is_ro_anon() due to arch_vma_access_permitted() looking at e.g pkeys registers (with extra branches) in some architectures. This patch allows for partial madvise success when finding a sealed VMA, which historically has been allowed in Linux. Signed-off-by: Pedro Falcato Reviewed-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes --- mm/internal.h | 2 -- mm/madvise.c | 13 +++---------- mm/mseal.c | 17 ++++------------- mm/vma.h | 7 +++++++ 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index ca422aede342..1db320650539 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1363,8 +1363,6 @@ static inline int can_do_mseal(unsigned long flags) bool can_modify_mm(struct mm_struct *mm, unsigned long start, unsigned long end); -bool can_modify_mm_madv(struct mm_struct *mm, unsigned long start, - unsigned long end, int behavior); #else static inline int can_do_mseal(unsigned long flags) { diff --git a/mm/madvise.c b/mm/madvise.c index 89089d84f8df..4e64770be16c 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -1031,6 +1031,9 @@ static int madvise_vma_behavior(struct vm_area_struct *vma, struct anon_vma_name *anon_name; unsigned long new_flags = vma->vm_flags; + if (unlikely(!can_modify_vma_madv(vma, behavior))) + return -EPERM; + switch (behavior) { case MADV_REMOVE: return madvise_remove(vma, prev, start, end); @@ -1448,15 +1451,6 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh start = untagged_addr_remote(mm, start); end = start + len; - /* - * Check if the address range is sealed for do_madvise(). - * can_modify_mm_madv assumes we have acquired the lock on MM. - */ - if (unlikely(!can_modify_mm_madv(mm, start, end, behavior))) { - error = -EPERM; - goto out; - } - blk_start_plug(&plug); switch (behavior) { case MADV_POPULATE_READ: @@ -1470,7 +1464,6 @@ int do_madvise(struct mm_struct *mm, unsigned long start, size_t len_in, int beh } blk_finish_plug(&plug); -out: if (write) mmap_write_unlock(mm); else diff --git a/mm/mseal.c b/mm/mseal.c index 2170e2139ca0..fdd1666344fa 100644 --- a/mm/mseal.c +++ b/mm/mseal.c @@ -75,24 +75,15 @@ bool can_modify_mm(struct mm_struct *mm, unsigned long start, unsigned long end) } /* - * Check if the vmas of a memory range are allowed to be modified by madvise. - * the memory ranger can have a gap (unallocated memory). - * return true, if it is allowed. + * Check if a vma is allowed to be modified by madvise. */ -bool can_modify_mm_madv(struct mm_struct *mm, unsigned long start, unsigned long end, - int behavior) +bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior) { - struct vm_area_struct *vma; - - VMA_ITERATOR(vmi, mm, start); - if (!is_madv_discard(behavior)) return true; - /* going through each vma to check. */ - for_each_vma_range(vmi, vma, end) - if (unlikely(is_ro_anon(vma) && !can_modify_vma(vma))) - return false; + if (unlikely(!can_modify_vma(vma) && is_ro_anon(vma))) + return false; /* Allow by default. */ return true; diff --git a/mm/vma.h b/mm/vma.h index e979015cc7fc..da31d0f62157 100644 --- a/mm/vma.h +++ b/mm/vma.h @@ -380,6 +380,8 @@ static inline bool can_modify_vma(struct vm_area_struct *vma) return true; } +bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior); + #else static inline bool can_modify_vma(struct vm_area_struct *vma) @@ -387,6 +389,11 @@ static inline bool can_modify_vma(struct vm_area_struct *vma) return true; } +static inline bool can_modify_vma_madv(struct vm_area_struct *vma, int behavior) +{ + return true; +} + #endif #endif /* __MM_VMA_H */ From patchwork Sat Aug 17 00:18:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13766880 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 CF43DC52D7D for ; Sat, 17 Aug 2024 00:18:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 395A06B03D4; Fri, 16 Aug 2024 20:18:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 343E46B03D6; Fri, 16 Aug 2024 20:18:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 176716B03D7; Fri, 16 Aug 2024 20:18:51 -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 E4A696B03D4 for ; Fri, 16 Aug 2024 20:18:50 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 747581603C2 for ; Sat, 17 Aug 2024 00:18:50 +0000 (UTC) X-FDA: 82459826820.22.AFF3670 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) by imf20.hostedemail.com (Postfix) with ESMTP id 68D6C1C001D for ; Sat, 17 Aug 2024 00:18:48 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JQEGDNMl; spf=pass (imf20.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.44 as permitted sender) smtp.mailfrom=pedro.falcato@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=1723853869; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AVSLpFCNCxLphAVPpePgVTrDRTKaNng95YGqfZlpIJs=; b=E5aouc/qHkk11fLDzX6A+pRt8HGL523dznnM4ym1DsY7EDG2WpLcP9gRU5kts8EW+XoEci FGil/NqKV2qOVa7iKV8WwFkAcxD/0KPDBmgkokyMcDmxPbRyGKWb28DgHbNuu5KMH7klKV pEE+/Vve1sA+M4Li+H1sCPaCyWRaGr8= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JQEGDNMl; spf=pass (imf20.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.44 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723853869; a=rsa-sha256; cv=none; b=Uc4LAKIdIjEx7pnxpNB5T4K1k6Q3uXgtwW4PHf4zN7WTO8DyOECMHJ9WWovH0HxpIuqSET 1SF4r5hiVME7LClMyUgXcFCMcUP1TyQrlFnrpuslYqzGf3bXpNS971Uzem0KS5ec6VQ0DU +hPYxFH2YZK4yM1SveoaB+ZZyBDUuBk= Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-42816ca797fso18569085e9.2 for ; Fri, 16 Aug 2024 17:18:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723853927; x=1724458727; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=AVSLpFCNCxLphAVPpePgVTrDRTKaNng95YGqfZlpIJs=; b=JQEGDNMlRuWFJ/TU4AyOlQpl50uS4D6t0gdZ2TQlmjvgfGttdroQsWkTBk1oF/J78i 4vmdLolF5Amg1jW5QdugtNPhLhQWCO1d6OapHvXp4Z2GWj7yrilIYvuCOjTgEpQ1Eg0y VAPKcSMtw5dX+jxH1hFCYrnqOn0t+kfwttlD3DgexaKY3RnIhmW+oOOQgrrVtk7vvzdI HniqKyzjD3ocM2/ONGwHV63jeaIJR9C0Z7hffKyTIzl/oNzIpShZ99fhe9qfgyxcw4eg qxjap5euSzDTuSfcevsJCbsM6oYjOgMceelwwO+RJuRboKi26ZMS7hI4T/mKxlH3OV5k ZLVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723853927; x=1724458727; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AVSLpFCNCxLphAVPpePgVTrDRTKaNng95YGqfZlpIJs=; b=mPnWoI7dwCI+pSas+Su4gFbU4aqKjWxF4eHwsVXVpX+9S3wmWwyXVb3ELOJnPuxgVs 9AmpEXXUv88dCjP+zHLUifMJZBklNUUTXvLQL5wMptCXloaiEf/ZMuknGAhvGK26n4Xc rbtgye5aF6YDHYSJ9hmSwW799eKkTrIW9K7bm9WPZDGZwWDGrkaIXgNfjiLkZvPsAuVs ZEJk1e69kEcwwWXPEc3lBLspksrlMFlwLQOJzKp/hvGooffBgcG07xnvGsudjWsYaeuH SUNFclsGwvOJjBY0UYOFuKgHxmzvwUcyyhJSMZbCbZo2+0STO1AF6iV9voKELYUxoE7y i+Xg== X-Gm-Message-State: AOJu0YzWN9senmvR1vdySsYOewR8WbpTTmYM3bA6YfxYN/p6uH43NYOm mDa/MpMzzequ5XTj1/glelxkWAbMHShh+ixgik26guVt7BzG6Kg2 X-Google-Smtp-Source: AGHT+IEF/45IWEnY8seUwWDHiPQtCFESvI6bebHfacPqy451180xkuSrsBTLSGa9GQqBMGnh8DYNYw== X-Received: by 2002:a05:600c:35c5:b0:428:1a48:d5cf with SMTP id 5b1f17b1804b1-429ed7ba070mr33189155e9.9.1723853925945; Fri, 16 Aug 2024 17:18:45 -0700 (PDT) Received: from [192.168.1.91] ([2001:818:e92f:6400:96b:aa92:afc0:2d3d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429d781047asm104842055e9.0.2024.08.16.17.18.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 17:18:45 -0700 (PDT) From: Pedro Falcato Date: Sat, 17 Aug 2024 01:18:33 +0100 Subject: [PATCH v3 6/7] mm: Remove can_modify_mm() MIME-Version: 1.0 Message-Id: <20240817-mseal-depessimize-v3-6-d8d2e037df30@gmail.com> References: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> In-Reply-To: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> To: Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Shuah Khan Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, jeffxu@chromium.org, oliver.sang@intel.com, torvalds@linux-foundation.org, Michael Ellerman , Kees Cook , Pedro Falcato X-Mailer: b4 0.14.1 X-Stat-Signature: ut3ixgcn7pprspckr8563b71yaeerj8z X-Rspam-User: X-Rspamd-Queue-Id: 68D6C1C001D X-Rspamd-Server: rspam02 X-HE-Tag: 1723853928-925784 X-HE-Meta: U2FsdGVkX192+j8QRZageaTnDPSJM9Gb0xeoAkGqr/R9Atl8qJDnakt0wPfYwpTRp9kbHzh8TQmQZKHSP2Yo4OxIcdN90oAv8iTB7cfBukYzlBv2eNdQD/1EyjSu3V9T95Mr/CWW5viVNu2HNB1u1/g8hkctib3XHwpATpZOiXxiRpR/mOqVKw5NQ6xEgv8YcXRgtBjpZ0mp8wByFfrXYwqSR0rNCpEQgia5viuvGf4OzB/MaL8u5/c73rigqQpNN7nrwmEbxY/9Mkh1Lakuau/FVxkGNtFQcpMXNVJeRw08nIzX80liksHdq+dhhgpLRkiW5lAsNNt1Xic0uTxaZH/yAbqohp8ovYxyFYdgKA9PZAjERGSXBsjA+qsfqteXmctpysvQ1p8RqGPu+OIOJ4HVLavYAF4wiCYUfx/HLXTCKy5UJwVUnvNDs4iVlxwdGnq1wFzQBUHYCX/rdkBbeB1FrOYdkXKoDrUov/aERbSAlCYpHvnHaTaJ4UKYmCSRuyLieRpRab/r/yspOVnw9/5YwM9qQtuxAram7XIs0z9zXwmN2RQvXTyYDMqTsDXwe9MsiclXynlAFTaS0VpLRueV0JSRSzm+gPHAYir33J4TdznLgxIlsQrLqy0CLW6P9zV/8QOCq8JxQAFORAMomBrfyn+4SXlctHsPGe53o0Fw+Iphll8frwtGwZ61D2t1sPrFh057IjgHWjPdEwSBCcuovW/nyNLj42R2zGSvHFsVaEWuivTz8LB2jJfCC6N1CgQ5Zhftq0ec/BNptgZxIrCssdlwcKhI343XK+K39zRqwYPfS/Mao75eYHIP/Y/jRykfK5PvvBnHnUZ3JJNwPtQSyYzh9aJ0zeW5W8/DM6xqgU6TC3jdpOsUyv0O3sVuSygLqQVeMM3d/+2cVmcgpTFKmXsVGipNzOVhqL2udO9GrY2GpS5TBLJ4lhnpLaFQXc6en7IoB9i868MANwl t19jthb7 F0xI+UnXMrevjryhQ56EVo4VtSuwN+pJrDz/0zrEy1RSXV17ra37Rm6yZwh7h/ejhoIUuy1pGv/L2qESUkqJEboKqWxPEUwcqfPU5M+WN5yMUKqCkaWRUswSFkYYO8MjvhEvStBTUjxTBUnY58OGML2LiK2eDKnOqlE6scbOh3JaJwqpMv+oYTCnJwZHQLAcANG+MZyYdE3cnFHpmbQCc66u8b6iUL1+o0H7W1yZoNvRkmCtV2CU8F54o+uxc5gfj0CCmgNS0qb+yYAp4kye6mtWeGnKY8w+IDJ3Q6xuQWSnmgghW3icpKKeBzFmTULuQXTf41JHK2U72+aaQ/uH0UiDGKw8Eqz5CqjKN/ujLpPPUE47/Eu8++TEvjPfNIMevLuGl01unJzFCRH0Rl47iQtThpkd76kT6i/uM5NROhUgvoUByfOJgLEVbZBxiM+iBkCVGuVu99IMyo9HYAYhQFaRO6chXfxdo2jZk6NPSHg4GtNUzWmjbaea4JItujdK+RbJwjoywpbooOyM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000016, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: With no more users in the tree, we can finally remove can_modify_mm(). Signed-off-by: Pedro Falcato Reviewed-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes --- mm/internal.h | 14 -------------- mm/mseal.c | 21 --------------------- 2 files changed, 35 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 1db320650539..3b738b0ad893 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1361,25 +1361,11 @@ static inline int can_do_mseal(unsigned long flags) return 0; } -bool can_modify_mm(struct mm_struct *mm, unsigned long start, - unsigned long end); #else static inline int can_do_mseal(unsigned long flags) { return -EPERM; } - -static inline bool can_modify_mm(struct mm_struct *mm, unsigned long start, - unsigned long end) -{ - return true; -} - -static inline bool can_modify_mm_madv(struct mm_struct *mm, unsigned long start, - unsigned long end, int behavior) -{ - return true; -} #endif #ifdef CONFIG_SHRINKER_DEBUG diff --git a/mm/mseal.c b/mm/mseal.c index fdd1666344fa..28cd17d7aaf2 100644 --- a/mm/mseal.c +++ b/mm/mseal.c @@ -53,27 +53,6 @@ static bool is_ro_anon(struct vm_area_struct *vma) return false; } -/* - * Check if the vmas of a memory range are allowed to be modified. - * the memory ranger can have a gap (unallocated memory). - * return true, if it is allowed. - */ -bool can_modify_mm(struct mm_struct *mm, unsigned long start, unsigned long end) -{ - struct vm_area_struct *vma; - - VMA_ITERATOR(vmi, mm, start); - - /* going through each vma to check. */ - for_each_vma_range(vmi, vma, end) { - if (unlikely(!can_modify_vma(vma))) - return false; - } - - /* Allow by default. */ - return true; -} - /* * Check if a vma is allowed to be modified by madvise. */ From patchwork Sat Aug 17 00:18:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Falcato X-Patchwork-Id: 13766881 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 A57B7C52D7D for ; Sat, 17 Aug 2024 00:18:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3BAF66B03D6; Fri, 16 Aug 2024 20:18:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 375856B03D7; Fri, 16 Aug 2024 20:18:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E6CD6B03D8; Fri, 16 Aug 2024 20:18:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E3E2D6B03D6 for ; Fri, 16 Aug 2024 20:18:51 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A15F81A03FF for ; Sat, 17 Aug 2024 00:18:51 +0000 (UTC) X-FDA: 82459826862.07.2960254 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) by imf13.hostedemail.com (Postfix) with ESMTP id C923520003 for ; Sat, 17 Aug 2024 00:18:49 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iIGSQDCd; spf=pass (imf13.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.53 as permitted sender) smtp.mailfrom=pedro.falcato@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=1723853893; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3BNTUbl7+nCW9RPTsVIAgPqk6kKkrw4lo+pgAchkUc4=; b=ejXn6j6eMLotvfqtJv4uDf5agaArO4szd4cy99xIoWlIcxwE0/Y+Ql55I3WD+aJSmps+hL wnXAoJ/ZqrBexg17GF0l1VQDMPCo60xlrteB0Akpedw/AjK9n82K/h2IkcZ9URIuznYUgg 6Z0jfh2Y4Kx6zR7NpIRxZWnZN22jbuA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iIGSQDCd; spf=pass (imf13.hostedemail.com: domain of pedro.falcato@gmail.com designates 209.85.128.53 as permitted sender) smtp.mailfrom=pedro.falcato@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723853893; a=rsa-sha256; cv=none; b=jCvflqmDpEAhBBgoCfNuTYZzNsk26sfHR1Gr8dzek/nZKcHt7AMchA84H6DK1+ldwwY1VF FVGjizJ5rXEQNPwZYz4R9eNrGLH5kQEQU9P2Zu/jdB4kNPIetGulMhRN7CCkRa5iEwPUBz b3sO6e6Q1boRAIqrt6drbezI4htYVYo= Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4280bca3960so19732995e9.3 for ; Fri, 16 Aug 2024 17:18:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723853928; x=1724458728; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=3BNTUbl7+nCW9RPTsVIAgPqk6kKkrw4lo+pgAchkUc4=; b=iIGSQDCdkXkSfjEuKjLnp2MqBpMtf+fJlLc78vvyLLsglVTk/fhBdWlOSsOpb0M/fj VY2WuUckflpyP4fSqWqYO0xU9zCzWvAVdczrbjxLgT75mz7lhQTIQHhYxTytRR0s6uyt gCDB/Ad4aH9YpTy9EbXMjOzLFV8kgbi7Z+Dg4iYSkq0mJTj7klDAw8pceL/ZGBqleAFU stPH4qd7OhmqKj4yLywyWgU3MLx6+DPuOFW1EDfxQ7zHQruDbYqCmH0qwL9BXdf1eM5U cDD4EycKiB2m6cyFEkFPdI/QB7gKpDgFdMMgXIhNtOem8+kuSl/PdQSThe94Ky3uhxTD iWmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723853928; x=1724458728; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3BNTUbl7+nCW9RPTsVIAgPqk6kKkrw4lo+pgAchkUc4=; b=nfVifn3Zy62A1tX6tGMfMx3ysvmswZB8iH538V1yM4PvghHl8qyGkw5liB0h5c9L5G PQN2c++vC6OnlpoyYnsNFtdbjZZIyAwawhOaNKtpGEbB3JK5rBfnV12hHH/TMiL40JP4 2mR2VzT8TA6CIUnqwhR251b3Ur2SEkGN5pwCbQWTE/NXrgl3zJjj6yCrFalZnxlfQGUA kIpfj/B3F5BJu06QRP3p3aMsMQO9SWBkwNM+W+KkVW3SSltxIkWJSVmJj4F9AqHxrXNw 2S2EB68dtaEt6pZxtH+s0znK+XUhpP0pjD3f/DpGh2+uk4QD5ocyqbEjO1AFy1GnZeLW TcRA== X-Gm-Message-State: AOJu0Yx6CEMryBYsIcvP+uOD6loNneQwYfuccr5KgNIioTfhq12QUdaj C89fVtB0D28oSrxQnGrDwA3PsLKt97HO/j7VZ1buKFKhmq5RvCXb X-Google-Smtp-Source: AGHT+IFIhkDMqUt+pj5IYnY7Cri9JLM7/ke3nV3b+/GsdZDIlIl1wzdvrsc5f4Owwa72QzEPcfRwGQ== X-Received: by 2002:a05:600c:45ca:b0:426:5ddf:fd22 with SMTP id 5b1f17b1804b1-429ed78572bmr30664255e9.6.1723853927337; Fri, 16 Aug 2024 17:18:47 -0700 (PDT) Received: from [192.168.1.91] ([2001:818:e92f:6400:96b:aa92:afc0:2d3d]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-429d781047asm104842055e9.0.2024.08.16.17.18.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Aug 2024 17:18:46 -0700 (PDT) From: Pedro Falcato Date: Sat, 17 Aug 2024 01:18:34 +0100 Subject: [PATCH v3 7/7] selftests/mm: add more mseal traversal tests MIME-Version: 1.0 Message-Id: <20240817-mseal-depessimize-v3-7-d8d2e037df30@gmail.com> References: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> In-Reply-To: <20240817-mseal-depessimize-v3-0-d8d2e037df30@gmail.com> To: Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Shuah Khan Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, jeffxu@chromium.org, oliver.sang@intel.com, torvalds@linux-foundation.org, Michael Ellerman , Kees Cook , Pedro Falcato X-Mailer: b4 0.14.1 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: C923520003 X-Stat-Signature: gfj1kpw5megascqofa35gxob6ncxou6y X-HE-Tag: 1723853929-909501 X-HE-Meta: U2FsdGVkX1/UC3na4Puflsk/joc1k49PAuANOUcuTk45G7o0WPNLFKQ5eci38p+UBys/NrCiKRdEGaPLKkqF04yDmM5sQq1v0Y04/bvmEtRS2/tE2rQ0ZHEk2Cgb7scIrjvu9eEN3gdG4dgVvZL9ipzJ6CwyKcUuNbjKUCsqchrv3TpIybxUtWTVYzqN9rvZhL580v1QP+fI01mz6l/9b05455BhU7+67QbMb3uv3UDDxmQ9v0tkOP6l0N2vcZjv6hBLE+IMFieAn0HTg7TNfimIOMrOFyW861TyW8Vlq04xwUy3lQSFuDyO0r4VIUgKYsn1h2+tXdglZQsYu+TUq+QxxsOKWvcF26bGHQ4O3E2MBAKE16Wid8ejkr+5fpyghqZ62UAIu0tOkCY/qYoEkiSG23W92zq0MuuuveNUymIR9zXvbZa24UzFJNazCMY8+kpX0z1O++ckK6X4bIY9amF7aSD0smwaojia6ZzzB9J7nn/9gNdwJinU2dzWlS6TYKh1XvpJNMyScHcyi1i6cBwEXeO5Nr/G4CyLdMLD5Q9G3GIE8Sw0vZkJxSZ3lZhyqYxDnvG6U1low0tmuksaIv8dsvxf8oGCEVo4fdfcqG2UokG6/r6oWaKjvZ6YUSg/l3CetM2kuS8zQ7hRz0bYYQTSTZ8HaGlPsrL6niVQOwMrdzF+y6IL0ww30n4i06bjiOjgO6Co55BfIpsAOO5kLw7vsXwmIAlGBZAbhCYy+mfH89nAABsf9/xioBIaZUZJYDKRMSd4HDFj0b8dKv41Vajpdm2SPh/1vnm58gqeCrbRqWKd0fTN+4k5BzCKap6lboBKDPK8Em6gkD0VFtQCgJhF5fOvxPeIRf3dN+JqmuFYOA7zr4e8Of6xD3nMUQnnOFgtA9mM62QiNQqo04S7ByRvNlwQ+ShHYjUGZ3C1+3GwYFEp+F6sfeXbYU7rlzM8AlblkVj8EbqocLDZRyC Q+hsZXdO Uf6vhROG5PZFBMa+VCXlWJjVDAaUVZETlDtl0wsJ6K/j+0OFcVCT3CgZ7pNZxmtOy+wq2JBuA3GfJbrdnoXCu3ls5DtHOj8RV2co9aL/aNoZ7mZ3NkI1odYqdrKr+id7xKxEpG+lHFR8BF3kpA0nar2PaVF852fTfJKa364BJpxp/0RugVgcivjIYD3TfrRgSG0i5CWKQ/MoGjhj2zD36V6LzyNOr+G4rbmb9ni8I2RLWv9Bc7xIff3xiCa/1IDpMo13G4k2qBcUYGlsrTBie/hjXqb3kx/zWqLWqsNqrMJOwQmGkUqNtuAFefJ4/XVDKquJUbxCzCOxYEWczlsuKNwRXQG9ADZC1PzFesCDYEafpWB4PHx1/aLO4vePSAGrKXQ2zJBwBpEWQx5jBvIAubIWH8xmfxvoSqa1v2FWMsRs08UTQoVFRiyPUbj0cQDRWJX2iTotY/NAGaUbjt/rRJ58Y6o68sbnpX3E6 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000131, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add more mseal traversal tests across VMAs, where we could possibly screw up sealing checks. These test more across-vma traversal for mprotect, munmap and madvise. Particularly, we test for the case where a regular VMA is followed by a sealed VMA. Signed-off-by: Pedro Falcato Reviewed-by: Liam R. Howlett Reviewed-by: Lorenzo Stoakes Signed-off-by: Pedro Falcato --- tools/testing/selftests/mm/mseal_test.c | 111 +++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index 259bef4945e9..0d4d40fb0f88 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -766,6 +766,42 @@ static void test_seal_mprotect_partial_mprotect(bool seal) REPORT_TEST_PASS(); } +static void test_seal_mprotect_partial_mprotect_tail(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 2 * page_size; + int ret; + int prot; + + /* + * Check if a partial mseal (that results in two vmas) works correctly. + * It might mprotect the first, but it'll never touch the second (msealed) vma. + */ + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + if (seal) { + ret = sys_mseal(ptr + page_size, size); + FAIL_TEST_IF_FALSE(!ret); + } + + ret = sys_mprotect(ptr, size, PROT_EXEC); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + + if (seal) { + FAIL_TEST_IF_FALSE(get_vma_size(ptr + page_size, &prot) > 0); + FAIL_TEST_IF_FALSE(prot == 0x4); + } + + REPORT_TEST_PASS(); +} + + static void test_seal_mprotect_two_vma_with_gap(bool seal) { void *ptr; @@ -983,6 +1019,41 @@ static void test_seal_munmap_vma_with_gap(bool seal) REPORT_TEST_PASS(); } +static void test_seal_munmap_partial_across_vmas(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 2 * page_size; + int ret; + int prot; + + /* + * Check if a partial mseal (that results in two vmas) works correctly. + * It might unmap the first, but it'll never unmap the second (msealed) vma. + */ + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + if (seal) { + ret = sys_mseal(ptr + page_size, size); + FAIL_TEST_IF_FALSE(!ret); + } + + ret = sys_munmap(ptr, size); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + + if (seal) { + FAIL_TEST_IF_FALSE(get_vma_size(ptr + page_size, &prot) > 0); + FAIL_TEST_IF_FALSE(prot == 0x4); + } + + REPORT_TEST_PASS(); +} + static void test_munmap_start_freed(bool seal) { void *ptr; @@ -1735,6 +1806,37 @@ static void test_seal_discard_ro_anon(bool seal) REPORT_TEST_PASS(); } +static void test_seal_discard_across_vmas(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 2 * page_size; + int ret; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + if (seal) { + ret = seal_single_address(ptr + page_size, page_size); + FAIL_TEST_IF_FALSE(!ret); + } + + ret = sys_madvise(ptr, size, MADV_DONTNEED); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + + ret = sys_munmap(ptr, size); + if (seal) + FAIL_TEST_IF_FALSE(ret < 0); + else + FAIL_TEST_IF_FALSE(!ret); + + REPORT_TEST_PASS(); +} + + static void test_seal_madvise_nodiscard(bool seal) { void *ptr; @@ -1779,7 +1881,7 @@ int main(int argc, char **argv) if (!pkey_supported()) ksft_print_msg("PKEY not supported\n"); - ksft_set_plan(82); + ksft_set_plan(88); test_seal_addseal(); test_seal_unmapped_start(); @@ -1825,12 +1927,17 @@ int main(int argc, char **argv) test_seal_mprotect_split(false); test_seal_mprotect_split(true); + test_seal_mprotect_partial_mprotect_tail(false); + test_seal_mprotect_partial_mprotect_tail(true); + test_seal_munmap(false); test_seal_munmap(true); test_seal_munmap_two_vma(false); test_seal_munmap_two_vma(true); test_seal_munmap_vma_with_gap(false); test_seal_munmap_vma_with_gap(true); + test_seal_munmap_partial_across_vmas(false); + test_seal_munmap_partial_across_vmas(true); test_munmap_start_freed(false); test_munmap_start_freed(true); @@ -1862,6 +1969,8 @@ int main(int argc, char **argv) test_seal_madvise_nodiscard(true); test_seal_discard_ro_anon(false); test_seal_discard_ro_anon(true); + test_seal_discard_across_vmas(false); + test_seal_discard_across_vmas(true); test_seal_discard_ro_anon_on_rw(false); test_seal_discard_ro_anon_on_rw(true); test_seal_discard_ro_anon_on_shared(false);