From nobody Wed Dec 31 15:00:58 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6396C4332F for ; Thu, 2 Nov 2023 15:33:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347632AbjKBPdJ (ORCPT ); Thu, 2 Nov 2023 11:33:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345328AbjKBPdA (ORCPT ); Thu, 2 Nov 2023 11:33:00 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0F8AFB for ; Thu, 2 Nov 2023 08:32:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-Id:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=ldnPYKUyHMdijInwVZoC1NuQUIqnzmW6aookZYRZh1U=; b=jahtwHNhX+yr0vKRzO1fhTymzt SLnwIxFZMgFhCVyxgCoHHeHHNal4O09ds8khll2mHqBKCDVh1dg2ozVPmfCLnj1HDCscJXKAOx2eX Ee3F4Kb5E8i3CTw3G4yB+MVmOgYao23eRwYHX4UqNBoo4R/keJ47nkef3A28xvpucksuTa99bXHb3 ZW43WHCx7msUMSWBXjVDb+1D1Hb+R6OMJ9U7Jo1IMe6/becw+Moi5BFHjwsuOCeCEbCAiVuL+MJoK iFR/PUCJJU0V312y8oU8NDuqXEHCEFTrLny4nBVxCbIIoheFbGQeGydTLEw4m7Viz9C3J0D2Xgiab tZ/njmDA==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1qyZgU-0005PD-UQ; Thu, 02 Nov 2023 15:32:39 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id 4397930098F; Thu, 2 Nov 2023 16:32:39 +0100 (CET) Message-Id: <20231102152018.069585432@infradead.org> User-Agent: quilt/0.65 Date: Thu, 02 Nov 2023 16:09:22 +0100 From: Peter Zijlstra To: mingo@kernel.org Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, irogers@google.com, adrian.hunter@intel.com Subject: [PATCH 03/13] perf: Simplify perf_fget_light() References: <20231102150919.719936610@infradead.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce fdnull and use it to simplify perf_fget_light() to either return a valid struct fd or not -- much like fdget() itself. Signed-off-by: Peter Zijlstra (Intel) --- include/linux/file.h | 7 ++++++- kernel/events/core.c | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) --- a/include/linux/file.h +++ b/include/linux/file.h @@ -59,6 +59,8 @@ static inline struct fd __to_fd(unsigned return (struct fd){(struct file *)(v & ~3),v & 3}; } =20 +#define fdnull __to_fd(0) + static inline struct fd fdget(unsigned int fd) { return __to_fd(__fdget(fd)); --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5802,18 +5802,17 @@ EXPORT_SYMBOL_GPL(perf_event_period); =20 static const struct file_operations perf_fops; =20 -static inline int perf_fget_light(int fd, struct fd *p) +static inline struct fd perf_fdget(int fd) { struct fd f =3D fdget(fd); if (!f.file) - return -EBADF; + return fdnull; =20 if (f.file->f_op !=3D &perf_fops) { fdput(f); - return -EBADF; + return fdnull; } - *p =3D f; - return 0; + return f; } =20 static int perf_event_set_output(struct perf_event *event, @@ -5864,10 +5863,9 @@ static long _perf_ioctl(struct perf_even int ret; if (arg !=3D -1) { struct perf_event *output_event; - struct fd output; - ret =3D perf_fget_light(arg, &output); - if (ret) - return ret; + struct fd output =3D perf_fdget(arg); + if (!output.file) + return -EBADF; output_event =3D output.file->private_data; ret =3D perf_event_set_output(event, output_event); fdput(output); @@ -12401,9 +12399,11 @@ SYSCALL_DEFINE5(perf_event_open, return event_fd; =20 if (group_fd !=3D -1) { - err =3D perf_fget_light(group_fd, &group); - if (err) + group =3D perf_fdget(group_fd); + if (!group.file) { + err =3D -EBADF; goto err_fd; + } group_leader =3D group.file->private_data; if (flags & PERF_FLAG_FD_OUTPUT) output_event =3D group_leader;