From patchwork Fri Nov 15 00:46:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13875794 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 30C041DFFD for ; Fri, 15 Nov 2024 00:43:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731631402; cv=none; b=grQakW5kD2fPxpCT9V5G3EGiP2HnWu5IsDxr0g4cdJ8RpTI8wF7SOfNzPQg69CXLhaqWs1nwE66RB717K1nGMA3GUAr+3F8DgJ/7fZt1/9W+HWGuD6uMTBvCAqeHC8QtgZW4GwKD0vpQVNzbr4nI0Hq7HBmP83w8JgaLzQqz9y8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731631402; c=relaxed/simple; bh=G3wa07rZYgN6CVGveQn2J5nSIZh5i+vLZIp5qLRAfWo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qc/Ql9r4wzAsBMgMuq4IzXZDzfeVONPT7TrBqCOtsIeOz6zohqBJhZezKhbXb+bVu8Fk0l+xO4rmY+a0eyTLqdflEE4rHHg0HFJc25GsSSfQvggKLd/ykbx6DHMcAdMPSeQmgxzU4Ewb6WoWVbxLVIQStLV9UVORxTOyhqNxoT4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com; spf=pass smtp.mailfrom=isovalent.com; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b=MUxikb1S; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=isovalent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=isovalent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=isovalent.com header.i=@isovalent.com header.b="MUxikb1S" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-4315839a7c9so10881235e9.3 for ; Thu, 14 Nov 2024 16:43:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent.com; s=google; t=1731631398; x=1732236198; 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=uoG5JO9syP1TPphV80q7oHvJ3++/Z/0Nk/f2xqd8VJ8=; b=MUxikb1Sct1lLXAOLIaAtYhc3tmot/RVMq5FlvXdQr/9kCKL5IDD5Jb6lj//LJfEH+ W0IWtrBL0eSHo3us+gO3/WksRMaZSr6lQxCSagihdxmWcDBtoDxktwlEos7QAXMVeOxp wY/Y+QpzRCU4ERj2WumxVMEsGZOCQnns1IomEmiIWqb53dkhwpxgA/UThA38pxRJYeT0 mKLjaW1N0x+mCnHgVmMAt1f0tZk/XnvkYu/FOfMXdGnQRXQXdrQjFWoDvLMukr4PKgcQ w4/w1LjRX7tYCQ9I7CHs5dMGm+pjdS1G1BOagr1vbdtapMFFMev1NjcL0Sf7m+Fp//UD bh+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731631398; x=1732236198; 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=uoG5JO9syP1TPphV80q7oHvJ3++/Z/0Nk/f2xqd8VJ8=; b=sl0IHIMgU3FsQusINXqml+BFP6wC9zS2njao0ei3t1iSkVhN0J6G/zrWiGtzHKKAF/ CXPiMyOaKEOA0ci83vPjH/tiXyDKxOEnnYMKky8eC6LoIdF7aqmCBMp9Az0HfGCsRX/X 4c568LkXhWck9HGiwQr+CItnGwNB2Ooe3yjufiuvAeHkzpz6010t0u/QPWdzl+ZOgqKH cqQzASVWdPfymP3zieotumRJspODsIXppdnS2+0CKDMiGB0qJK2mDUkBPzm/FH9iU2jJ wYW7pPwhSiqFtsTJ49bfmP9BOY7jDDwjOmba83EiEaMzrRSdYPmBzCliakgGXH7AGvF/ UaGg== X-Gm-Message-State: AOJu0Yx2xnScOXuC5efqW6HFpGcV8hBqQNr5jJ3AnUxhIDREQhILgozO 1IgX/snway79WTW5AEFZS3qpAD/TIU3EEcc5ud6sXcaZEvnJVDsIl3eoBIzzhXr033hn4YE9px2 FKCM= X-Google-Smtp-Source: AGHT+IF6p/Yd0VBysfQgFmlV6dZiXEHkaH/Zq2elZQEHUUuEOYJd0iP3nYhYddUb0DHqt1m7hpwSYw== X-Received: by 2002:a05:600c:35d0:b0:432:dc5a:b305 with SMTP id 5b1f17b1804b1-432df72a2ccmr5747275e9.12.1731631397497; Thu, 14 Nov 2024 16:43:17 -0800 (PST) Received: from localhost.localdomain ([2a04:ee41:4:b2de:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432dab78783sm36781975e9.12.2024.11.14.16.43.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Nov 2024 16:43:17 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org Cc: Anton Protopopov Subject: [PATCH bpf-next 1/5] bpf: add a __btf_get_by_fd helper Date: Fri, 15 Nov 2024 00:46:03 +0000 Message-Id: <20241115004607.3144806-2-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241115004607.3144806-1-aspsk@isovalent.com> References: <20241115004607.3144806-1-aspsk@isovalent.com> 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 Add a new helper to get a pointer to a struct btf from a file descriptor which doesn't increase a refcount. Signed-off-by: Anton Protopopov --- include/linux/btf.h | 13 +++++++++++++ kernel/bpf/btf.c | 13 ++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index 4214e76c9168..050051a578a8 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -4,6 +4,7 @@ #ifndef _LINUX_BTF_H #define _LINUX_BTF_H 1 +#include #include #include #include @@ -143,6 +144,18 @@ void btf_get(struct btf *btf); void btf_put(struct btf *btf); const struct btf_header *btf_header(const struct btf *btf); int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_sz); + +static inline struct btf *__btf_get_by_fd(struct fd f) +{ + if (fd_empty(f)) + return ERR_PTR(-EBADF); + + if (unlikely(fd_file(f)->f_op != &btf_fops)) + return ERR_PTR(-EINVAL); + + return fd_file(f)->private_data; +} + struct btf *btf_get_by_fd(int fd); int btf_get_info_by_fd(const struct btf *btf, const union bpf_attr *attr, diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index e7a59e6462a9..ad5310fa1d3b 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -7743,17 +7743,12 @@ int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) struct btf *btf_get_by_fd(int fd) { - struct btf *btf; CLASS(fd, f)(fd); + struct btf *btf; - if (fd_empty(f)) - return ERR_PTR(-EBADF); - - if (fd_file(f)->f_op != &btf_fops) - return ERR_PTR(-EINVAL); - - btf = fd_file(f)->private_data; - refcount_inc(&btf->refcnt); + btf = __btf_get_by_fd(f); + if (!IS_ERR(btf)) + refcount_inc(&btf->refcnt); return btf; }