Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp470443imi; Thu, 21 Jul 2022 05:01:13 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tNuxSPqxdMoDFL/F6+ycByW3O9s6RuIMEknyprDRZWxxRyjdmcBSEa/CAGV/QZRlbnBH9Y X-Received: by 2002:a17:902:ecd0:b0:16c:461d:8008 with SMTP id a16-20020a170902ecd000b0016c461d8008mr42881851plh.65.1658404872576; Thu, 21 Jul 2022 05:01:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658404872; cv=none; d=google.com; s=arc-20160816; b=ZVBsRBshANWQMZe0Lh/lPp2KhtH9bV9Q1VX+55eJ38TYsXRBhM1lJ51a1vfNM7i/9o c1/mr8Zk36SXAj63g5cbQxNuDHyWwL970K0cS+vFqC+BzQoLpmzL+lVZTCtbdI/i9ORH P4pbkGsguAYrE4FIbVc8Mo0xyPQuyKn3SS6daouSz4ubFkWoyCahR2blA4QpvNlfRL2R ToMlJRPQCaaZhb2FKR7bD0Fs1RGNFjKuVNo87rgagqb9sFbHSrQ/fHYKmY6d0NZOStqR e6otrBrsrKoQzsBEPT+SodXj/3I+5qTEJQD/Gtga0+i/gftWSQHqQrpNW3WLRhXD7Wf+ TNaQ== 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=CtSeHkcsOt/wsxKopTW7Mctc8/JLR9iTxdkdgSa2zlU=; b=UGqQgLmKddVK26zDi6e07lYi8UPZDwZLxtmj9TYvxN/peJZ2xVuCHVOZgae0oFg17h zfytaCYz7igZIbEZ09Irggbk2o4OiMDcSTkeDLgVB2bd8tX51LmYlRPI/OKqE5u1pkQN HNdFpycitvQqazMkVR+jEXhIltI59T7Cg/nOeEEIZDKm4mfyBM/B51KlSmN+patdNLTX i0rkarLgmKs3QTo3Qmj81/LUP181eOJI8gNSQij3PQ2PtAf3GnvAYdGjzJynJ+n5QhiI gi/6PfBrF3BNj6X3pdNsromaPZuNvLWpaHkgUff28de8GKWHSjyNyLMViJKl2Nw8rsgp kSwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=k0VzyS05; 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 t190-20020a6381c7000000b00419a2de79b6si1822152pgd.305.2022.07.21.05.00.54; Thu, 21 Jul 2022 05:01:12 -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=k0VzyS05; 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 S233385AbiGUL7W (ORCPT + 99 others); Thu, 21 Jul 2022 07:59:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232158AbiGUL7V (ORCPT ); Thu, 21 Jul 2022 07:59:21 -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 202963340B; Thu, 21 Jul 2022 04:59:20 -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 CBC97B82443; Thu, 21 Jul 2022 11:59:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 806D0C3411E; Thu, 21 Jul 2022 11:59:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1658404757; bh=ggHG6nD7013O5l8BLxi1J8dB0HMzHRPWFaAq8WtsKf4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=k0VzyS05s/qO/GLUCmqIQcQ620cwKH/qf1ycY0IbyfQgiucVn/Hz30Epfdg1TfTWC JfqKANjEkZt09LBApojiFDRMWD4s3kgce7EBKKZrErkyI0RPpZshbQwHdV1pNon8yF 9YIUeH15Qv2QGquouDjOCVT65dVFa9FIFYK8AsDZ5/GMSZy5nY5ZDuHSqZvt3NxVqt loMecXcx5znRYvXg7hCRoYYPHrmyq+Sjeq9oK+lDhq26hEa36aiIhjWrNx/HAwMyp9 bpnyJ8BaC5tCb1JcDmZDuGTFxR/Zu39/0bifRl/gRTUCa0Da5kMhXi8QyHsBrbPK4Z fE/Mkn4Lvvymw== Date: Thu, 21 Jul 2022 12:59:09 +0100 From: Lee Jones To: Jiri Olsa Cc: linux-kernel@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , bpf@vger.kernel.org Subject: Re: [PATCH 1/1] bpf: Drop unprotected find_vpid() in favour of find_get_pid() Message-ID: References: <20220721111430.416305-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.8 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 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, 21 Jul 2022, Jiri Olsa wrote: > On Thu, Jul 21, 2022 at 12:14:30PM +0100, 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. > > > > In an ideal world we would wrap the in-lined call to find_vpid() along > > with get_pid_task() in the suggested rcu_read_lock() and have done. > > However, looking at get_pid_task()'s internals, it already does that > > independently, so this would lead to deadlock. > > hm, we can have nested rcu_read_lock calls, right? I assumed not, but that might be an oversight on my part. Would that be your preference? > > Instead, we'll 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 > > --- > > 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); > > if (!task) > > return -ENOENT; > > -- Lee Jones [李琼斯]