From patchwork Fri Dec 1 09:46:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13475550 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 4792BC4167B for ; Fri, 1 Dec 2023 09:47:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9BD506B043D; Fri, 1 Dec 2023 04:47:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 96D2B6B043E; Fri, 1 Dec 2023 04:47:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 834696B0441; Fri, 1 Dec 2023 04:47:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 70F6E6B043D for ; Fri, 1 Dec 2023 04:47:10 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 45A7B14011D for ; Fri, 1 Dec 2023 09:47:10 +0000 (UTC) X-FDA: 81517771020.09.799E4C9 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf20.hostedemail.com (Postfix) with ESMTP id 961971C0020 for ; Fri, 1 Dec 2023 09:47:07 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="M/l7ve2z"; spf=pass (imf20.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=laoar.shao@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=1701424027; 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=MSQwZzhnajRqkX+xa7n/aRnDDu+LcQrNX6wmLbUqn44=; b=GDV9/8lJ/G24BNEZEZJ8mKtfc3cb4obU26T33xold7pmy6L00AYVkIsgmR+0e6l+fS0RWj leK/oi6NSd++1BeXDoKh0Vyt6qFAkWh4JhqrKMvvkKE+7yT8OC1i5YLtiAgOXKD/qAdT/v 5/U3sb4Ao6wxRLflJ6xLEHGpcVdPHx4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701424027; a=rsa-sha256; cv=none; b=IZiHdNfFXRHZObICGvVHRJRVNhH5wxRofGohpEG6CT/6FfLh8LCoGEnHIV6K1qCuRyYLv/ BhyzxnkslyVjFWl3FWvpwZ6XvK/NKdUjNxlgqfg8Hr4lvMfKNRzQ9LVI9UFlw/2PyyIz0N KmiitQxr+Rdl+NUqMgIp+XaEv1ElNU0= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="M/l7ve2z"; spf=pass (imf20.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1d04c097e34so2721315ad.0 for ; Fri, 01 Dec 2023 01:47:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701424026; x=1702028826; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=MSQwZzhnajRqkX+xa7n/aRnDDu+LcQrNX6wmLbUqn44=; b=M/l7ve2zYDIywgegaK91SG8fBnenZIIOzDtsypTbFH44VtGocMAf6cHux+UfHlD9/D rIMmFokmJj2PbAziajFwcwVy/KSXj0Ud6qK/FvvjBllRiXu4mB9V0pS+ZquD81/NetRp kBqwnqZsvu4/0cfakN8qfjksBJouB3cyk8pzKaO7M/dl7qRVcl66HHSo186vAlWzxwDr O7ejLKpyCwie+eQ5Wov6Vg3He1ZajyIq/evIH4rH+b8NyjCCoAsSHRhdF+T2hYhOxcuy hhi9iU0gXt+yY8eUbp9ZEl0+ZkQd2tsSi4dsL/x2af4EsgIUIGE2GqrJUTVsyiZvAtrh Y+2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701424026; x=1702028826; 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=MSQwZzhnajRqkX+xa7n/aRnDDu+LcQrNX6wmLbUqn44=; b=I75hSUq3KVP8pa4UaxcTLmNfkJJrNoP3nyglQeQ0/KrnUBUfgHt9lcFhIELafFMgJW gSQmTBmfjPJbkMds+O8l4fVEbUIZgUAs5eY547lUXJAgXbfzQLgH8KuvKw3qniefHVTo 42bu63rYO2Ehbim83Ob2+T8iLo/PlKYjtS7Z7zeRKnwlxQ8/BT5sbiK4IskzHwxXItgF agZ65/Udy+uZzjVNTuJl0FqXTnwN5a5iSa2luR9aal67wkS7PLZRbBClQKy/A6tXlicU RODuNcGAlg/nquUpBdkFFz5DG4bT1+zwu3QvylTaASKvFOcSMoZpkLo8glTT1cziXAEC 2PsQ== X-Gm-Message-State: AOJu0Yy6CEOx5tXh/WJJ61gsBb3Her9UGgxc/HBCxX0tjkvQBKOLhqsR h6LxX2WRy/34jsCIMPTlVa4= X-Google-Smtp-Source: AGHT+IGmotwUO4OKYMRU7spFszJBT86c3qy/herBn8J5gOFNgpuPDtjJOMaHjQCwebSs4WVxmj2u7w== X-Received: by 2002:a17:902:8485:b0:1cf:fb96:9692 with SMTP id c5-20020a170902848500b001cffb969692mr13632650plo.12.1701424026350; Fri, 01 Dec 2023 01:47:06 -0800 (PST) Received: from vultr.guest ([149.28.194.201]) by smtp.gmail.com with ESMTPSA id e6-20020a170902b78600b001bdd7579b5dsm2875534pls.240.2023.12.01.01.47.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 01:47:05 -0800 (PST) From: Yafang Shao To: akpm@linux-foundation.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, omosnace@redhat.com, mhocko@suse.com, ying.huang@intel.com Cc: linux-mm@kvack.org, linux-security-module@vger.kernel.org, bpf@vger.kernel.org, ligang.bdlg@bytedance.com, Yafang Shao Subject: [PATCH v3 0/7] mm, security, bpf: Fine-grained control over memory policy adjustments with lsm bpf Date: Fri, 1 Dec 2023 09:46:29 +0000 Message-Id: <20231201094636.19770-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Rspamd-Queue-Id: 961971C0020 X-Rspam-User: X-Stat-Signature: ctp34nqze1nrp1fuyj4wn8z8dsoktx1y X-Rspamd-Server: rspam03 X-HE-Tag: 1701424027-713798 X-HE-Meta: U2FsdGVkX182q21qhk93qtkEQ5KIzndwNFCrPrpEFiTuEasL9/ghcgbrOXkhYmJ+xIpTsgJVeBCzk404h2mshWtLW4Ic48J6R7oruwAS9dNjoGItoIPYuG1P5C/kC+xfbOnPWdAfZ6NEItgNp09msaF8KG2Iw6ugxoMFVsdmYYocicOGe2Lt7vtsk0XAq3i46y6GQh/iIcRFfYq4m75tafrhwcSgOQlOaL0t73Hot2Mrhirkqg1vpCDBdAwYXDvznY1EZzzri4ALRXcn2+vYDRTNc0jNDuu64q6X7YME7JtNf1YAo9CR4pY946GnCL1v/la2Ew3IFaVOl3eGhHfD1exFmkJ5hg74liQbfqub6lAl8o9O8r5fLzqsKj2bcjVq3RakhDEXFMLm/K9O8K505107h2Nxh+3bpwtnO5mRBsPzTelyjzYURo/R+PggLGc6LN3wE663IroBekCsYTPIb5unOWcGol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igWJPTqWWqZF/UEseafL9GwGfPuv86Ln6TofYlrEz6rnAkeJaHDHrX3wmqjfU3whX4zaY1lv3/LYNzZhJz9L9dE20EaIwjJQCL 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: Background ========== In our containerized environment, we've identified unexpected OOM events where the OOM-killer terminates tasks despite having ample free memory. This anomaly is traced back to tasks within a container using mbind(2) to bind memory to a specific NUMA node. When the allocated memory on this node is exhausted, the OOM-killer, prioritizing tasks based on oom_score, indiscriminately kills tasks. The Challenge ============= In a containerized environment, independent memory binding by a user can lead to unexpected system issues or disrupt tasks being run by other users on the same server. If a user genuinely requires memory binding, we will allocate dedicated servers to them by leveraging kubelet deployment. Currently, users possess the ability to autonomously bind their memory to specific nodes without explicit agreement or authorization from our end. It's imperative that we establish a method to prevent this behavior. Proposed Solution ================= - Capability Currently, any task can perform MPOL_BIND without specific capabilities. Enforcing CAP_SYS_RESOURCE or CAP_SYS_NICE could be an option, but this may have unintended consequences. Capabilities, being broad, might grant unnecessary privileges. We should explore alternatives to prevent unexpected side effects. - LSM Introduce LSM hooks for syscalls such as mbind(2) and set_mempolicy(2) to disable MPOL_BIND. This approach is more flexibility and allows for fine-grained control without unintended consequences. A sample LSM BPF program is included, demonstrating practical implementation in a production environment. - seccomp seccomp is relatively heavyweight, making it less suitable for enabling in our production environment: - Both kubelet and containers need adaptation to support it. - Dynamically altering security policies for individual containers without interrupting their operations isn't straightforward. Future Considerations ===================== In addition, there's room for enhancement in the OOM-killer for cases involving CONSTRAINT_MEMORY_POLICY. It would be more beneficial to prioritize selecting a victim that has allocated memory on the same NUMA node. My exploration on the lore led me to a proposal[0] related to this matter, although consensus seems elusive at this point. Nevertheless, delving into this specific topic is beyond the scope of the current patchset. [0]. https://lore.kernel.org/lkml/20220512044634.63586-1-ligang.bdlg@bytedance.com/ Changes: - RCC v2 -> v3: - Add MPOL_F_NUMA_BALANCING man-page (Ying) - Fix bpf selftests error reported by bot+bpf-ci - RFC v1 -> RFC v2: https://lwn.net/Articles/952339/ - Refine the commit log to avoid misleading - Use one common lsm hook instead and add comment for it - Add selinux implementation - Other improments in mempolicy - RFC v1: https://lwn.net/Articles/951188/ Yafang Shao (6): mm, doc: Add doc for MPOL_F_NUMA_BALANCING mm: mempolicy: Revise comment regarding mempolicy mode flags mm, security: Fix missed security_task_movememory() mm, security: Add lsm hook for memory policy adjustment security: selinux: Implement set_mempolicy hook selftests/bpf: Add selftests for set_mempolicy with a lsm prog .../admin-guide/mm/numa_memory_policy.rst | 27 +++++++ include/linux/lsm_hook_defs.h | 3 + include/linux/security.h | 9 +++ include/uapi/linux/mempolicy.h | 2 +- mm/mempolicy.c | 22 ++++- security/security.c | 13 +++ security/selinux/hooks.c | 8 ++ security/selinux/include/classmap.h | 2 +- .../selftests/bpf/prog_tests/set_mempolicy.c | 81 +++++++++++++++++++ .../selftests/bpf/progs/test_set_mempolicy.c | 28 +++++++ 10 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/set_mempolicy.c create mode 100644 tools/testing/selftests/bpf/progs/test_set_mempolicy.c NOT kernel: Yafang Shao (1): NOT kernel/man2/mbind.2: Add mode flag MPOL_F_NUMA_BALANCING