Received: by 2002:a05:6359:322:b0:b3:69d0:12d8 with SMTP id ef34csp16528rwb; Wed, 10 Aug 2022 04:12:50 -0700 (PDT) X-Google-Smtp-Source: AA6agR6a1ft6Mg9PIFDRGYOhVKz1rSqN9Ao3X+y0q74fasIeM83eC6Nodb6Zr1lvu7xKKPO6XNhl X-Received: by 2002:a17:906:4fd3:b0:733:8be:24f8 with SMTP id i19-20020a1709064fd300b0073308be24f8mr1951640ejw.767.1660129970218; Wed, 10 Aug 2022 04:12:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660129970; cv=none; d=google.com; s=arc-20160816; b=DJ9AUyvE5pDHuBoBpUhBhy/TLBq7+7boupYPz8Ux5ea8kIwNhVO4fJERyRdzwkhlpS IUzXy/gd+hFX8mnK/hzFq8uIrsGTwT2txueXEVuJL042tC/KWszrb4koukRVID2ySeSs aN2rPgPjfTemvKbahcrngFK7y5aOGZd93KAjirfRmu9ilpTIZRzS0H7xYRCdI5yyszQm QQnBZeXMWyL9x/mDv6T41kgdaJCxaexqJ3Wgy0wpBS5E+g+KkT6HDucAq9o/HidMiMF+ TKF509XeefRuDOv6RDHX0BtKZZLOSgxKqRPfDlM5KMTDccJnkfbTFKcy6Cke015+6i1o 6hfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=cBxNdT4DjYKL9gXBCt/6HziiN38F+7LFTM79JFZ5HKs=; b=mhGXBN6ECqJmmncdzKs8BMk96d5pQ06TDuy3Fi/Ze2XzawQOnPkkQ56tdm7F5AuPkS 7Rq+LD3nUQo+ndj4wg9kpw8FYDW8HywuhCrPtMLII30JMSaBQ+omPn53LhHJzr/fwRjs 0IbJy9oz9esHN6kwDIWZW1YFsOyNlqe6rjXu75KqjSPmVKclX+CWAZGZKBc0RbOPZ8tO MLGzA16ylmM4FyvP27wp4Ly5+FkWnj8l7JB97DLTcnc/ttiniNDeSMe8p0KoJoLZ456W ea6X2Ij0TC1Bo48vN0KcWbmCl7lV5RRFu6ttSWwOn6VNpfL2m/QP5/7WQb8nk43YDB+P Csgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lmOdiUHs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bq6-20020a056402214600b0043ac5ba36easi9911756edb.419.2022.08.10.04.12.24; Wed, 10 Aug 2022 04:12:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lmOdiUHs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231756AbiHJLDp (ORCPT + 99 others); Wed, 10 Aug 2022 07:03:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229763AbiHJLDn (ORCPT ); Wed, 10 Aug 2022 07:03:43 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41F606C129; Wed, 10 Aug 2022 04:03:42 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id F07AFB818E4; Wed, 10 Aug 2022 11:03:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB177C433D6; Wed, 10 Aug 2022 11:03:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660129419; bh=gh8+q+3SCgVT0ty48TCgdbOn9Aa4aYJYb6QYSM/vEZY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=lmOdiUHsBj7m5zUaHK8f0Hgghk3YeOujaPK0T3iTaOTio2UU4C9M4Y3pvcdwYf6Yq /t6R2iHFSM3BTNd6rRhxlwYI/tIyt/sAKeftu+9XwndUf0FB5QC7r6eMuuaKRTCI4w IVjxq9g6nWIjwivit0lr2f1xSAxDoB2KrNrEwGTFOFOuxXExjLzR7o0kEcfqlc0811 FtuoeWUg/yWNMOhNC8cs0aA69a9C+ymhGRafeZGwjl0VG/Oe9XdNiejky08n3yXI2C i1zq8GJXkr4P7AIMi2zp+rECdATDELvU7CFmPE39WyeetZLcP8HnvAK+rqAs5GMuCD 6xTqNA5HlSXdQ== Date: Wed, 10 Aug 2022 12:03:33 +0100 From: Lee Jones To: Alexei Starovoitov Cc: LKML , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf Subject: Re: [PATCH v2 1/1] bpf: Drop unprotected find_vpid() in favour of find_get_pid() Message-ID: References: <20220803134821.425334-1-lee@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 09 Aug 2022, Alexei Starovoitov wrote: > On Mon, Aug 8, 2022 at 11:50 PM Lee Jones wrote: > > > > On Thu, 04 Aug 2022, Alexei Starovoitov wrote: > > > > > On Wed, Aug 3, 2022 at 6:48 AM Lee Jones wrote: > > > > > > > > The documentation for find_pid() clearly states: > > > > > > > > "Must be called with the tasklist_lock or rcu_read_lock() held." > > > > > > > > Presently we do neither. > > > > > > > > Let's use find_get_pid() which searches for the vpid, then takes a > > > > reference to it preventing early free, all within the safety of > > > > rcu_read_lock(). Once we have our reference we can safely make use of > > > > it up until the point it is put. > > > > > > > > Cc: Alexei Starovoitov > > > > Cc: Daniel Borkmann > > > > Cc: John Fastabend > > > > Cc: Andrii Nakryiko > > > > Cc: Martin KaFai Lau > > > > Cc: Song Liu > > > > Cc: Yonghong Song > > > > Cc: KP Singh > > > > Cc: Stanislav Fomichev > > > > Cc: Hao Luo > > > > Cc: Jiri Olsa > > > > Cc: bpf@vger.kernel.org > > > > Fixes: 41bdc4b40ed6f ("bpf: introduce bpf subcommand BPF_TASK_FD_QUERY") > > > > Signed-off-by: Lee Jones > > > > --- > > > > > > > > v1 => v2: > > > > * Commit log update - no code differences > > > > > > > > kernel/bpf/syscall.c | 5 ++++- > > > > 1 file changed, 4 insertions(+), 1 deletion(-) > > > > > > > > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c > > > > index 83c7136c5788d..c20cff30581c4 100644 > > > > --- a/kernel/bpf/syscall.c > > > > +++ b/kernel/bpf/syscall.c > > > > @@ -4385,6 +4385,7 @@ static int bpf_task_fd_query(const union bpf_attr *attr, > > > > const struct perf_event *event; > > > > struct task_struct *task; > > > > struct file *file; > > > > + struct pid *ppid; > > > > int err; > > > > > > > > if (CHECK_ATTR(BPF_TASK_FD_QUERY)) > > > > @@ -4396,7 +4397,9 @@ static int bpf_task_fd_query(const union bpf_attr *attr, > > > > if (attr->task_fd_query.flags != 0) > > > > return -EINVAL; > > > > > > > > - task = get_pid_task(find_vpid(pid), PIDTYPE_PID); > > > > + ppid = find_get_pid(pid); > > > > + task = get_pid_task(ppid, PIDTYPE_PID); > > > > + put_pid(ppid); > > > > > > rcu_read_lock/unlock around this line > > > would be a cheaper and faster alternative than pid's > > > refcount inc/dec. > > > > This was already discussed here: > > > > https://lore.kernel.org/all/YtsFT1yFtb7UW2Xu@krava/ > > Since several people thought about rcu_read_lock instead of your > approach it means that it's preferred. > Sooner or later somebody will send a patch to optimize > refcnt into rcu_read_lock. > So let's avoid the churn and do it now. I'm not wed to either approach. Please discuss it with Yonghong and Jiri and I'll do whatever is agreed upon. -- Lee Jones [李琼斯]