From patchwork Mon Dec 2 12:38:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13890528 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 11A9C1D86C9; Mon, 2 Dec 2024 12:38:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733143108; cv=none; b=M13WYoXdXVGjKLw9bKUgr9Gmb7HlxrkCojlztLlXe0lUS74VNDiuOQ8jnj6OK5VdUSDDE7uDvgg7a0NIWIRC6cwibl0kJeqBcmeEZQP2i40vurEoGoYmHhiiq+I8RmZz8/uyGkmANsmny44CcwUU+Vlml6siVZgI1euPB7LjzXc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733143108; c=relaxed/simple; bh=g20qBYlXeNwQUsiewfivl+fCumQUvVsethy31GmWhWA=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition; b=KkiUloCySQ4a7d50dt5CnTPwGZMArWZy4WMBKQZRmKjviLV6t4cyk2n3u9fTfqeFKmcRfCyLRfcdiPiHsnQ65PmmdMkDT7dystbM+Xdn8i4K/ruoziLn2Tcxahan98wBkq6KEW7iwJ9hvN9dtOnjpiuHSbaJNOlE6bSTGQ0wsMI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VPhy1djw; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VPhy1djw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 91CC4C4CED1; Mon, 2 Dec 2024 12:38:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733143107; bh=g20qBYlXeNwQUsiewfivl+fCumQUvVsethy31GmWhWA=; h=Date:From:To:Cc:Subject:From; b=VPhy1djwN2JsmpJlUbIw4kNwHT+eZ5MP+e6rw9LdNFWUjIHazHMeOSoA4YwqjrqWE NIQyeiXbP6F1fCbGQB+Ami8wgMF3wgXnAc7UHIXElLTx0aDiklhX4aL+nWcIKTMl8+ IaRX5DquGC2CZQ21sTAwJySyr7LgkT0b8t9VayG6rA7PJF+I99Bqi744VbFYLznbiD pT4wOqgHmtB4ZC/S6bgVAuBklpsX+pzMV4/fuJn14gp4s6yWTdtmC6+Tl2V4ZCH/Fr 9V6LP89mxWn/1d+Rx8MVGVznjIdUx1Omku+fPVkkPjNVFwTmiT/mGjiFNJClWATZya 1+zDxN+5kwkFw== Date: Mon, 2 Dec 2024 13:38:22 +0100 From: Christian Brauner To: Alexei Starovoitov , Song Liu , Andrii Nakryiko , Martin KaFai Lau , KP Singh Cc: linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org Subject: bpf_task_storage improvement question Message-ID: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline Hey, I just had to take a quick look at kernel/bpf/bpf_task_storage.c and realized that you're doing: fd = *(int *)key; pid = pidfd_get_pid(fd, &f_flags); // something something task = pid_task(pid, PIDTYPE_PID); bpf_task_storage_lock(); // something something bpf_task_storage_unlock(); put_pid(pid); That reference count bump on struct pid seems unnecessary and I suspect your lookup routines are supposed to be fast. So why don't you just open-code this. Something like: It remains pinned by the pidfd anyway. diff --git a/kernel/bpf/bpf_task_storage.c b/kernel/bpf/bpf_task_storage.c index bf7fa15fdcc6..dc36a33c7b6d 100644 --- a/kernel/bpf/bpf_task_storage.c +++ b/kernel/bpf/bpf_task_storage.c @@ -92,10 +92,12 @@ static void *bpf_pid_task_storage_lookup_elem(struct bpf_map *map, void *key) struct task_struct *task; unsigned int f_flags; struct pid *pid; - int fd, err; - fd = *(int *)key; - pid = pidfd_get_pid(fd, &f_flags); + CLASS(fd, f)(*(int *)key); + if (fd_empty(f)) + return -EBADF; + + pid = pidfd_pid(f); if (IS_ERR(pid)) return ERR_CAST(pid); @@ -104,19 +106,13 @@ static void *bpf_pid_task_storage_lookup_elem(struct bpf_map *map, void *key) */ WARN_ON_ONCE(!rcu_read_lock_held()); task = pid_task(pid, PIDTYPE_PID); - if (!task) { - err = -ENOENT; - goto out; - } + if (!task) + return ERR_PTR(-ENOENT); bpf_task_storage_lock(); sdata = task_storage_lookup(task, map, true); bpf_task_storage_unlock(); - put_pid(pid); return sdata ? sdata->data : NULL; -out: - put_pid(pid); - return ERR_PTR(err); } which avoids the reference count bumps on @pid.