Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp5585259rwb; Mon, 8 Aug 2022 23:55:16 -0700 (PDT) X-Google-Smtp-Source: AA6agR4/qXBiU7vASmMYKFQ9cfLTrSntYfas/tRtS6JGlxC8zLp61f1XWT6OADuSaKT5+9DHZRZC X-Received: by 2002:a17:906:9bdd:b0:730:5b04:7298 with SMTP id de29-20020a1709069bdd00b007305b047298mr17150115ejc.180.1660028116546; Mon, 08 Aug 2022 23:55:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660028116; cv=none; d=google.com; s=arc-20160816; b=yXRegWMqQyUAulAyUeJBPHSTz+WawdEGq5VIB6WagFE0888Ef1MG2X4Lvey2YRKiiB f2FZPuYG8IYmyi3bp7/v8e8mRn5w93SYyYr4RLrgi+tG8k6kGZMtLel/NFwb48Pdaji8 qB1JMlaaMt96sTg1QmWRLKv44qkpubDIzyMRldZTIomjhnSncBfCTBrj5amN01/Vd2Id QUpoNI23VP6OJ/TqdvlAeLKCDIhW3x3nvsw/R25SLFRKFWRhs291a9sfFmnhbwVfXT3O tfDFSUezSGhkzYNXXRB4Ba0CoLoPPk/g2tRxM64ud1+HHFMIUIwb26eDjUy8cdOmFC3A zjQg== 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=RUGvoE2dDSi/pu1j+l0xGuITl03af2p5856YywlPCs0=; b=vGJR1EXmlhe5XXkPLtqkD1W9eoyGOLk/4FCSqK4dbWaY1XLJ9IYMNOq2qYxfDKdvQs rIvmUJ9Da9Z+hgiWuK0EFsi7rqzlabjg93T9uKxV4JISkaYPb7/LEqyg84mI5BzEBW01 +9Tv13o/ukYi9Qh27BWQGhqWTCBpyhMt1g8KXwLGmHnCL1F/TSZwBrC5C9pZGPQjrN+X 6ZmQYTy+bYkcECv+KD5mNIFH5wrwtfPUMKhHbxFsEjuaH5UoUq7BgJVI29HyaYXasSdZ 9Wr4Cvjf+dkclsDf0ku6TXQM+YhghPcObjJ/vDlekO6/gb7JB3sy7mQhGSsETrr6MqDn 9K5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iIr5JmbC; 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 v23-20020a50a457000000b0043ba52ffd16si7892961edb.453.2022.08.08.23.54.46; Mon, 08 Aug 2022 23:55:16 -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=iIr5JmbC; 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 S232353AbiHIGuN (ORCPT + 99 others); Tue, 9 Aug 2022 02:50:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229600AbiHIGuL (ORCPT ); Tue, 9 Aug 2022 02:50:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA3E61F2C4; Mon, 8 Aug 2022 23:50:10 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 425A261218; Tue, 9 Aug 2022 06:50:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 70ECEC433C1; Tue, 9 Aug 2022 06:50:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660027809; bh=ynJyKQ66METpJqPUjWfrzqF4JjlT26+ei9p1vjSEDTI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=iIr5JmbCbVbcJ6eGv8iS/D+qITb4OE5u3y5124+JdEU0NdfQMGYYx93fRc8U9dEw7 vKPsO0mHX7pWGitYMqbgeHi1IaWXkc1J4I4zpMUJYZg1T8IWq4QJbmiJwylmIY5RUY frxbvpxxZzliB8m1wrr7FffbnjhnfWlCrhOtQKgyN+rfWRYrF8HNJ6CH5eWbo0iLVd kfSJoS4jPAdy4GMv/0OXlUZQNF+Eedk1DOagWNqqtObYNqFUf+5KwyS1x7cd7jJmUC JrLHhd5GZDka1CbOUaMbgFDrjsVkN3ML+OWDyEXqwUDeAQIKSKGdP/H9wKc/ej/+wa N7WFOY/dS2+lw== Date: Tue, 9 Aug 2022 07:50:02 +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 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/ -- Lee Jones [李琼斯]