From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B8B92BCF7F for ; Mon, 20 Oct 2025 03:17:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760930270; cv=none; b=OmCvhVRQNUWRn6usLBUGS1PoQEh/uBsDYhTOEcz8IL3veB51kVl2kUKkmZZhVsCqKn2gdkZievyU44cUqj7zk05t4aS/nRv42+7QMhNLDFJlFDZqqp5RTmPgOg/TtpS6KQI2CkayRWXcV4zze4ZzvgScNBn3vq99tDse+c4CGVk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760930270; c=relaxed/simple; bh=AZhf0bN3dMzYRxemSSdBnaN8exURAvP50px9ndo8GmA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=U3gEw1zJc+r6wLTaHbvRBmOSR3JFw/E0cpV3DecOHvKKyu0XRiWme5rBi4RPi/dD/hDatQxilgV23PkwD+YtdRb3iVPfBtr9yNT6ja/CoiQqZeBjTdCQrN6JUAo/a0T+961XKqnLTFi8VzsFqJELwshwUa9wRziPNgIzxYJUfz4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UlnxuMI3; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UlnxuMI3" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-29226bc4bafso22147425ad.0 for ; Sun, 19 Oct 2025 20:17:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760930266; x=1761535066; darn=vger.kernel.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=V4o89aqQquJfXg8DHNFFwhPZpHBrXkamiWZUkDvNw/U=; b=UlnxuMI3d3iZ0GlV6bQ/ncwcLi9A9eCBlo8bLl8h2i1Aur53Zj3Klknj7M0BLdvGWW rjY7cblE53XBIdVqIoDPUaaokLmwlsIlzO/jGBfay1lCz41Yk/x7VhNJPDzEW1u9iFvZ Cl9rpWXyqrBFtFTA8+dL1goIqqVynqpIHKnuY0al2SnnvIKRPP0rVg9JMyBxneUDAu7I Z2rOj2xV+5gl181vIu46EIg3yocldwnV/yl+d4X9oaFbrQYAuLm5+hbjH//BC/ain4cO YFJz40zsb+pgrfnZcjb2X3/apPb4lN0tJejh6U4zlQ56HqRW8VzGPLPagkk9c3UzOzOy zbTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760930266; x=1761535066; 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=V4o89aqQquJfXg8DHNFFwhPZpHBrXkamiWZUkDvNw/U=; b=I19Mp+qo3Cvd/isEG7fri9oHMifPX8uhumROmUENkYvXk7XxD2wVfKp4XfKL3jATsR YMNW/d17ER2QrEcZ0jBixIy/UfSvwrzhZ4NbgmnGZAo+M9GakotcIpVjeqRFmmeLCbGd ldsjucBZNeXovIbNOzwTqR1JYZW9bpp68n8lkLNxLVoWp+io17jM/WOz+UxzDne3pLNU ZCuaQ4x9XNYlVrHR7NFIYsuBnm/rf4fQoxRFH0eJr7zMqzmnzPC4Bvl4TFpmrBqL+Tpf OVX25Ke3cDVJbzf80nvdx02v6tlxV54MVrsrkSGhQmiUQyN0jT8SX1E9Fi5rABtq5mSZ bwDQ== X-Gm-Message-State: AOJu0YyhX9F9JyGsi4TH07JNJED9DH2Om8yFJ1lW7BL6YCzhE5pXWajw uH5hYSSf1rRQZB4UY/PZSMDUY2Hw4cH1wmRGjW3AJeNlzhIpNpFcYY7/ X-Gm-Gg: ASbGncun8oTejzJHQu+d1WCDQe45e4XmBXOkki2Cfm32BYjdlZBE2t7rFYi7iEavGX2 e10w+iwaIBcRH+rHyS8csDy3W/T0xIL7SqIQcDu7Y/NLWzKioAeMb8WPZW4GIP06LbNtBrjBWwC 4va13gYK/6xXm54WgtNrPvUplN8crvMoF27m6lyDrkIZCRhloK5QP56ZPW03UBG28G6pQbQMbQ3 72in79ArRF9NH2KBlSKYwU8GMsU5tjlpsJtlEyAzaPaApMlNiIRiSe8Fj6gp245r439WVl4y23s EWUXDqnU6VExsuAW4BS5PrpUSEGrLS8TQ+e20R0FoeqsDzlSEX3526XLp7fa+cCsSuOOzE8iGhp q4d/hyBxY84Eh08VMEPKxYrx2tjpiWkEbIKHh6TfN4BTLmFMvM5R9cbAOxoyQuBvs5I1dytetEf RO9cmlfNfhzRSjbmWlXdJr6cTK/1imCokNEciRhlgYvQQiVw== X-Google-Smtp-Source: AGHT+IGalCyWkc7C8LmPCGO9urIs1Pwdm8eNflQX/0Ho3b+S9hSSF1w5msjDxjkD1RQDifOCduychw== X-Received: by 2002:a17:902:d50f:b0:27d:69cc:990 with SMTP id d9443c01a7336-290cb27e0bfmr152822005ad.49.1760930266552; Sun, 19 Oct 2025 20:17:46 -0700 (PDT) Received: from localhost.localdomain ([2409:891f:1da1:a41d:3815:5989:6e28:9b6d]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-292471fddfesm66373435ad.88.2025.10.19.20.17.37 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 19 Oct 2025 20:17:46 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, david@redhat.com, ziy@nvidia.com, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, hannes@cmpxchg.org, usamaarif642@gmail.com, gutierrez.asier@huawei-partners.com, willy@infradead.org, ameryhung@gmail.com, rientjes@google.com, corbet@lwn.net, 21cnbao@gmail.com, shakeel.butt@linux.dev, tj@kernel.org, lance.yang@linux.dev, rdunlap@infradead.org Cc: bpf@vger.kernel.org, linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Yafang Shao Subject: [PATCH v11 mm-new 09/10] selftests/bpf: add test case to update THP policy Date: Mon, 20 Oct 2025 11:16:54 +0800 Message-Id: <20251020031655.1093-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20251020031655.1093-1-laoar.shao@gmail.com> References: <20251020031655.1093-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This test case exercises the BPF THP update mechanism by modifying an existing policy. The behavior confirms that: - EBUSY error occurs when attempting to install a BPF program on a process that already has an active BPF program - Updates to currently running programs are successfully processed - Local prog can't be updated by a global prog - Global prog can't be updated by a local prog - Global prog can be attached even if there's a local prog - Local prog can't be attached if there's a global prog Signed-off-by: Yafang Shao --- .../selftests/bpf/prog_tests/thp_adjust.c | 79 +++++++++++++++++++ .../selftests/bpf/progs/test_thp_adjust.c | 29 +++++++ 2 files changed, 108 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/thp_adjust.c b/tools/testing/selftests/bpf/prog_tests/thp_adjust.c index 2b23e2d08092..0d570cee9006 100644 --- a/tools/testing/selftests/bpf/prog_tests/thp_adjust.c +++ b/tools/testing/selftests/bpf/prog_tests/thp_adjust.c @@ -194,6 +194,79 @@ static void subtest_thp_eligible(void) bpf_link__destroy(ops_link); } +static void subtest_thp_policy_update(void) +{ + struct bpf_link *old_link, *new_link; + int elighble, err, pid; + char *ptr; + + pid = getpid(); + ptr = thp_alloc(); + + old_link = bpf_map__attach_struct_ops(skel->maps.thp_eligible_ops); + if (!ASSERT_OK_PTR(old_link, "attach_old_link")) + goto free; + + elighble = get_thp_eligible(pid, (unsigned long)ptr); + ASSERT_EQ(elighble, 0, "THPeligible"); + + /* Attach multi BPF-THP to a single process is rejected. */ + new_link = bpf_map__attach_struct_ops(skel->maps.thp_eligible_ops2); + if (!ASSERT_NULL(new_link, "attach_new_link")) + goto destory_old; + ASSERT_EQ(errno, EBUSY, "attach_new_link"); + + elighble = get_thp_eligible(pid, (unsigned long)ptr); + ASSERT_EQ(elighble, 0, "THPeligible"); + + err = bpf_link__update_map(old_link, skel->maps.thp_eligible_ops2); + ASSERT_EQ(err, 0, "update_old_link"); + + elighble = get_thp_eligible(pid, (unsigned long)ptr); + ASSERT_EQ(elighble, 1, "THPeligible"); + + /* Per process prog can't be update by a global prog */ + err = bpf_link__update_map(old_link, skel->maps.swap_ops); + ASSERT_EQ(err, -EINVAL, "update_old_link"); + +destory_old: + bpf_link__destroy(old_link); +free: + thp_free(ptr); +} + +static void subtest_thp_global_policy(void) +{ + struct bpf_link *local_link, *global_link; + int err; + + local_link = bpf_map__attach_struct_ops(skel->maps.thp_eligible_ops); + if (!ASSERT_OK_PTR(local_link, "attach_local_link")) + return; + + /* global prog can be attached even if there is a local prog */ + global_link = bpf_map__attach_struct_ops(skel->maps.swap_ops); + if (!ASSERT_OK_PTR(global_link, "attach_global_link")) { + bpf_link__destroy(local_link); + return; + } + + bpf_link__destroy(local_link); + + /* local prog can't be attaached if there is a global prog */ + local_link = bpf_map__attach_struct_ops(skel->maps.thp_eligible_ops); + if (!ASSERT_NULL(local_link, "attach_new_link")) + goto destory_global; + ASSERT_EQ(errno, EBUSY, "attach_new_link"); + + /* global prog can't be updated by a local prog */ + err = bpf_link__update_map(global_link, skel->maps.thp_eligible_ops); + ASSERT_EQ(err, -EINVAL, "update_old_link"); + +destory_global: + bpf_link__destroy(global_link); +} + static int thp_adjust_setup(void) { int err = -1, pmd_order; @@ -214,6 +287,8 @@ static int thp_adjust_setup(void) skel->bss->pmd_order = pmd_order; skel->struct_ops.thp_eligible_ops->pid = getpid(); + skel->struct_ops.thp_eligible_ops2->pid = getpid(); + /* swap_ops is a global prog since its pid is not set. */ err = test_thp_adjust__load(skel); if (!ASSERT_OK(err, "load")) @@ -240,6 +315,10 @@ void test_thp_adjust(void) if (test__start_subtest("thp_eligible")) subtest_thp_eligible(); + if (test__start_subtest("policy_update")) + subtest_thp_policy_update(); + if (test__start_subtest("global_policy")) + subtest_thp_global_policy(); thp_adjust_destroy(); } diff --git a/tools/testing/selftests/bpf/progs/test_thp_adjust.c b/tools/testing/selftests/bpf/progs/test_thp_adjust.c index b180a7f9b923..44648326819a 100644 --- a/tools/testing/selftests/bpf/progs/test_thp_adjust.c +++ b/tools/testing/selftests/bpf/progs/test_thp_adjust.c @@ -22,3 +22,32 @@ SEC(".struct_ops.link") struct bpf_thp_ops thp_eligible_ops = { .thp_get_order = (void *)thp_not_eligible, }; + +SEC("struct_ops/thp_get_order") +int BPF_PROG(thp_eligible, struct vm_area_struct *vma, enum tva_type type, + unsigned long orders) +{ + /* THPeligible in /proc/pid/smaps is 1 */ + if (type == TVA_SMAPS) + return pmd_order; + return pmd_order; +} + +SEC(".struct_ops.link") +struct bpf_thp_ops thp_eligible_ops2 = { + .thp_get_order = (void *)thp_eligible, +}; + +SEC("struct_ops/thp_get_order") +int BPF_PROG(alloc_not_in_swap, struct vm_area_struct *vma, enum tva_type type, + unsigned long orders) +{ + if (type == TVA_SWAP_PAGEFAULT) + return 0; + return -1; +} + +SEC(".struct_ops.link") +struct bpf_thp_ops swap_ops = { + .thp_get_order = (void *)alloc_not_in_swap, +}; -- 2.47.3