From patchwork Mon Dec 30 17:18:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Enache X-Patchwork-Id: 13923376 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-40130.protonmail.ch (mail-40130.protonmail.ch [185.70.40.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9A1C10F2 for ; Mon, 30 Dec 2024 17:18:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735579126; cv=none; b=LawhKc3tHevr468PmB//nWB+Egn6fPpk88LKzRbfQsFPhsXGjbqkfTnPiJ3POO02AMc5lEnfjo+DB9Q2Hs5dUGsDCPxzxd98kbgWxCyX+ur7he3RWmMxaeksjasO4r3wo1w/YwDw/0VCjpdknq8kWpPsW0qhvZBxshOmxkvYTVY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735579126; c=relaxed/simple; bh=gmVQpRLhBhD5cBRg5VxIsVcG+RBx1LjCTYMpAk7pJMw=; h=Date:To:From:Subject:Message-ID:MIME-Version:Content-Type; b=O+CWw+ZNZjjfkyvJsHFJqu6z9N7VzC+f4irzX+bTwPRtl+uZGC9HKN716Jn/6rbmmilSeMXtsDa8v8JxkVAqI3v1rTt/XAcF0i6sxQtsopHmturSAw8aL0qLzF3TuZRZRTbTYc/dwgR4P0vZ5mabT5qHoi6CTUaz6Sy0BFJ4BXE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=Dn3XCvXo; arc=none smtp.client-ip=185.70.40.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="Dn3XCvXo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1735579116; x=1735838316; bh=fXmGm0P51qgdBHCRQBDsTCun9g+/gEtVFdtEF3QHJKM=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector: List-Unsubscribe:List-Unsubscribe-Post; b=Dn3XCvXoaBhpCLeGVL5wSgF4XutcTeVWtZbms2WsGf9m0IO/q9UESmAJgiocBpde5 MOLVuwmW1Yn4xsKzTf9iuNzmgsREfN0LguFu8R74zdEs2TUZzfoMDggRoV1kxFpCRA uJ74IVZf8mJOf0J+scp670kO+xGpSXZ9bU2XHLo7OAZa8kN2If/weMiu/15UEsPTZO 6R2Fvg12/yjtfIJhTzdOO3xEb1xUu7Gf6p8psEzKEZtlgznxVMWzf1eFPZRhf2Kw4V qW+NWKZfnIZn7BnrzA05uyjBpfAsRgQs6R1Qp2FFWG1wYJNJdCwi3lNbiVXbBrKT5v vlUAbt8HWe3Vw== Date: Mon, 30 Dec 2024 17:18:31 +0000 To: "bpf@vger.kernel.org" From: Andrei Enache Subject: [PATCH v3 bpf-next] bpf: Use non-executable memfds for maps Message-ID: <2NK63_D3A4XK54XvOAywlNwXaq6bq2I2nc2nU9g-YVdEkYaPPKcbcQ3RI0yRDc65N2LmtEx1e2aWDKXS0BabHqkihS2gtXBcghhwM5TfDeE=@proton.me> Feedback-ID: 46877017:user:proton X-Pm-Message-ID: 89cbd2046b5b5a8a89a82095bbb140c099087b20 Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net This patch enables use of non-executable memfds for bpf maps. [1] As this is a recent kernel feature, the code checks at runtime to make sure it is available. --- Changes in v3: - Check return value before checking errno - Update newline style - Link to v2: https://lore.kernel.org/bpf/Z3LHcCgqY7kHs08S@krava/T/ [1] https://lwn.net/Articles/918106/ Signed-off-by: Andrei Enache --- tools/lib/bpf/libbpf.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) -- 2.47.1 diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 66173ddb5..3a30c094d 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1732,11 +1732,22 @@ static int sys_memfd_create(const char *name, unsigned flags) #define MFD_CLOEXEC 0x0001U #endif +#ifndef MFD_NOEXEC_SEAL +#define MFD_NOEXEC_SEAL 0x0008U +#endif + static int create_placeholder_fd(void) { int fd; + int memfd; + + memfd = sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC); + + /* MFD_NOEXEC_SEAL is missing from older kernels */ + if (memfd < 0 && errno == EINVAL) + memfd = sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC | MFD_NOEXEC_SEAL); - fd = ensure_good_fd(sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC)); + fd = ensure_good_fd(memfd); if (fd < 0) return -errno; return fd;