From patchwork Fri Dec 1 09:46:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13475555 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 43FBCC10F04 for ; Fri, 1 Dec 2023 09:47:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E30926B044D; Fri, 1 Dec 2023 04:47:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DB4E06B0444; Fri, 1 Dec 2023 04:47:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB88B6B0450; Fri, 1 Dec 2023 04:47:16 -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 A21876B0444 for ; Fri, 1 Dec 2023 04:47:16 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 798061A0163 for ; Fri, 1 Dec 2023 09:47:16 +0000 (UTC) X-FDA: 81517771272.19.D43C8C0 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf22.hostedemail.com (Postfix) with ESMTP id AD43CC001B for ; Fri, 1 Dec 2023 09:47:14 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HEM0MEtb; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701424034; 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:in-reply-to:references:references:dkim-signature; bh=Bc/JnQzKFnW7sQYk0Nc9Zpaw4UcMe6UJECHeJqeWNU0=; b=G+kE13vX7Aah6rB5ckCh9bBe3vMKtHTt2wyqyuOzmBT0dpKRqDri4Nt/85+qlsz4IbV6HV YzfMMbwjFqywPVT8dVHOZay8bWRrqWQFFaca4iEiq7we4dCOYD/0JxhyrtCf6UYiiBr0KC dCA1/JB7HU/vWIHnrIH2fjsxs8SIGk8= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HEM0MEtb; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701424034; a=rsa-sha256; cv=none; b=yLtxkiXZ8fDwYfjTPdZ7qdxjvElUxTAaKUusRYoTm+hv7WdEt4BMt2pd51bXAfGcOKBKh3 hFASklxE4iSI1006FL04UWChPh7nJIIbbQPrW/jfbWDUZq8xy0jP0RdM185vyPivXc4qVX wXpdm6hyrUeTT5TN0UjISSwVbUkYIQc= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1d045097b4cso2938365ad.0 for ; Fri, 01 Dec 2023 01:47:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701424033; x=1702028833; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Bc/JnQzKFnW7sQYk0Nc9Zpaw4UcMe6UJECHeJqeWNU0=; b=HEM0MEtb/Din4rDAMmSyMgAU/lKV5gVQu4LEsliZFOomhlX7mbuX/JP1Hva8SJfP/U Wt98MQ73GGj6qHG1rZsb9AdNocIiit64oQ50N4s0kzQC9A64quhKHpiV6S5GNLtdEYbE knRQRvnI2k9xIjHbqkJcr0IOT7kqqiJfMLIrw2H3t8C1xQg7Xqzse39z9lt83PKDH4G1 smlPqgW255jZvI2G5N8LNoKtxb+we9EUx9v2JhuW2nzT73zd+88whg2XlCyWrLVTG0ua y1wRJ4yoI4CxfooXxwPnuYf9S+0p5ZhTokmWoEhrwIdMj19sC9NyrjRLZDCYTEnCjVuJ WaMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701424033; x=1702028833; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Bc/JnQzKFnW7sQYk0Nc9Zpaw4UcMe6UJECHeJqeWNU0=; b=cmtmdZF2oAEmEvlw5zpNZSAOFRNRUBpjeN6j08n+x245YZR6pVtvfPC2B4Eg8FlQ55 nuepYsoqTvT0JyhrCUpuQlGwGg8WmgTTuOgmh2E2//JLaifGGQgthPILf5aC5n5wC05d XCeSP5y85uAXh2UWC7ZVGjf/ikAuMfBWRKtRIGzPZuCywV6wUB2qm+p9zivTJncU1nfE 88pkYcrI56f7rdnooSypDT/y73dG6oQqbCifsJ6QoLHApGe6SsHwI/zqDH4+cRQwJMDO 0xNPzfeWIm5aC6UwWGRNQ8MyqReiYcnGIcQuQN0S2IIfbhLqe6/WUECsa5oOoHrd5WfN 0uhg== X-Gm-Message-State: AOJu0YyJ/o6HNcBJCBIrQWJN0o/qh3PP59uhuJZiRiAzhBIa1NKklk8J 9szQr2Nd7zTPcCAWL6Ozvp4yWau8sadGa+Uc X-Google-Smtp-Source: AGHT+IHzDiK6o6MOlN3a1X1HHIiCln1ILc6SVwnXbjY0NMbXK8wHeCruOVe9g11XZtiAGRL7ZsnvhA== X-Received: by 2002:a17:902:e88b:b0:1cf:d404:5e7c with SMTP id w11-20020a170902e88b00b001cfd4045e7cmr20071237plg.42.1701424033643; Fri, 01 Dec 2023 01:47:13 -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.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 01:47:13 -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 6/7] selftests/bpf: Add selftests for set_mempolicy with a lsm prog Date: Fri, 1 Dec 2023 09:46:35 +0000 Message-Id: <20231201094636.19770-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231201094636.19770-1-laoar.shao@gmail.com> References: <20231201094636.19770-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: AD43CC001B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: sxheoih6sg59nsiwc4yeia1ezry9kgew X-HE-Tag: 1701424034-50915 X-HE-Meta: U2FsdGVkX1+/bLR1dxoMG26G1+dmJPx02+PHPqQxbkXGWjmPCVd3VozBgBnOCtgSp96sKKEaL57WymteS3Kt2x5zSJ1FxtuHtU4NvxoePApdBkdhjIrZG2Uoeokq649hdjniA0xQNc48hJRyABzucAT2vofSEQzkCbEqskeMmOm6kXJm3PnfTV1WzWti1f1m5S8z32Uu2Pukra/+iELbRwnLddxHTbTizqK6rjmPWNlqMGXn/NrJb13fVQNB4pjnwSv3H26Ib7UmDFd1/v/VjYNiEll9fw0ZXsmwRfcIMlIz/QKJyqbVXMrgZYqGlqUXBiroWsHdLD1Lp+CeSpfh+WPnRwDfkw53/Oj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heMT6xYOudUUEAeDBz/W3ydA9E4pKyqEjNNEfYHr85LrnoOlHWyPU1SAnZgLTgip5BmReA/cFkuOMDIsbSZHoRp7pl64BGvxiBSwKUbTuBZGNh76uiU0UxIgQNiwu6Ex6F0yH1r/9MATwAU5uMrOagLR+bDVG3JMlFAppbSsi+2RZFe6yY86r43ktoMKjio 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: The result as follows, #261/1 set_mempolicy/MPOL_BIND_with_lsm:OK #261/2 set_mempolicy/MPOL_DEFAULT_with_lsm:OK #261/3 set_mempolicy/MPOL_BIND_without_lsm:OK #261/4 set_mempolicy/MPOL_DEFAULT_without_lsm:OK #261 set_mempolicy:OK Summary: 1/4 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yafang Shao --- .../selftests/bpf/prog_tests/set_mempolicy.c | 81 +++++++++++++++++++ .../selftests/bpf/progs/test_set_mempolicy.c | 28 +++++++ 2 files changed, 109 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/set_mempolicy.c create mode 100644 tools/testing/selftests/bpf/progs/test_set_mempolicy.c diff --git a/tools/testing/selftests/bpf/prog_tests/set_mempolicy.c b/tools/testing/selftests/bpf/prog_tests/set_mempolicy.c new file mode 100644 index 000000000000..6d115ecedb10 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/set_mempolicy.c @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2023 Yafang Shao */ + +#include +#include +#include +#include +#include +#include "test_set_mempolicy.skel.h" + +#define SIZE 4096 + +static void mempolicy_bind(bool success) +{ + unsigned long mask = 1; + char *addr; + int err; + + addr = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (!ASSERT_OK_PTR(addr, "mmap")) + return; + + /* -lnuma is required by mbind(2), so use __NR_mbind to avoid the dependency. */ + err = syscall(__NR_mbind, addr, SIZE, MPOL_BIND, &mask, sizeof(mask), 0); + if (success) + ASSERT_OK(err, "mbind_success"); + else + ASSERT_ERR(err, "mbind_fail"); + + munmap(addr, SIZE); +} + +static void mempolicy_default(void) +{ + char *addr; + int err; + + addr = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (!ASSERT_OK_PTR(addr, "mmap")) + return; + + err = syscall(__NR_mbind, addr, SIZE, MPOL_DEFAULT, NULL, 0, 0); + ASSERT_OK(err, "mbind_success"); + + munmap(addr, SIZE); +} + +void test_set_mempolicy(void) +{ + struct test_set_mempolicy *skel; + int err; + + skel = test_set_mempolicy__open(); + if (!ASSERT_OK_PTR(skel, "open")) + return; + + skel->bss->target_pid = getpid(); + + err = test_set_mempolicy__load(skel); + if (!ASSERT_OK(err, "load")) + goto destroy; + + /* Attach LSM prog first */ + err = test_set_mempolicy__attach(skel); + if (!ASSERT_OK(err, "attach")) + goto destroy; + + /* syscall to adjust memory policy */ + if (test__start_subtest("MPOL_BIND_with_lsm")) + mempolicy_bind(false); + if (test__start_subtest("MPOL_DEFAULT_with_lsm")) + mempolicy_default(); + +destroy: + test_set_mempolicy__destroy(skel); + + if (test__start_subtest("MPOL_BIND_without_lsm")) + mempolicy_bind(true); + if (test__start_subtest("MPOL_DEFAULT_without_lsm")) + mempolicy_default(); +} diff --git a/tools/testing/selftests/bpf/progs/test_set_mempolicy.c b/tools/testing/selftests/bpf/progs/test_set_mempolicy.c new file mode 100644 index 000000000000..b5356d5fcb8b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_set_mempolicy.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2023 Yafang Shao */ + +#include "vmlinux.h" +#include +#include + +int target_pid; + +static int mem_policy_adjustment(u64 mode) +{ + struct task_struct *task = bpf_get_current_task_btf(); + + if (task->pid != target_pid) + return 0; + + if (mode != MPOL_BIND) + return 0; + return -1; +} + +SEC("lsm/set_mempolicy") +int BPF_PROG(setmempolicy, u64 mode, u16 mode_flags, nodemask_t *nmask, u32 flags) +{ + return mem_policy_adjustment(mode); +} + +char _license[] SEC("license") = "GPL";