Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp6033318rwb; Tue, 9 Aug 2022 08:05:56 -0700 (PDT) X-Google-Smtp-Source: AA6agR4iDxZeyFCBWfusAqhW3HlIjkbKB/1BpC8By34/F0eRTNo2ohdLWcvBMZ4lpYcbOMEN7fi1 X-Received: by 2002:a05:6402:4303:b0:43d:94f5:8081 with SMTP id m3-20020a056402430300b0043d94f58081mr22000021edc.288.1660057556318; Tue, 09 Aug 2022 08:05:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660057556; cv=none; d=google.com; s=arc-20160816; b=Wv8SngD+/NApVZAiqnVzTMIqFKESwYUAfh/BQjkN857e4lVHbgiTldyi+aDc37YMV7 6XLN+aHQ8TRb3eoc8yoAQSxFLvWlRNjcQ15TmrVokSXxizzikZ43/wdJ2QiY0YvQagC4 mVzdigQJyQYA+jPTzf/tEySEIo6sVhrLI8R2+fhQpGr+rPEt5Gccf0PmVAvz2koNKG1Z NMlQlcWxa37/i+1bNRX1cvr3+eaoIqIHs/3KmTxseP91+Jwwz4XVlDG1oUiGlUAEzVUK 5hsJ4zzwRmxIbccr3EL07lGx5iFN5LDrr7lkCgBkqG2IEcm/5iFCZa4vpAIN78et9mBK Sf8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=FZWzu7y9cZ515G8p3adab/2sdQH4tG+7zriZfn5G1uM=; b=uk7Izt8mJ/kaU+0KwT0nNwSYJhXiY3WoLAXX92Z2WfGldXvoyDxduY/hCzvsG0sacK HfzwMK6riVNN/hUJS43jdcQ1U6rwJ+LohijxrZVTzOkjQ7+tU0LwcdybVG2NxOB45ul9 HKrGr42eadagT0/DZOMlqv3BOdDCObIxyOjNf8Mf4SjuXiWBedbhBpTR4l4tMoq7fZCl PATyr82376JLZFyQvOdoqT2vEGpGgQy/+h+nIJu74I8baNQoEHh68TWEUJAX5VWRyrSk UVa5mCFUX5ZoQthaSs/+e7Pr1Swd9MjiRRHC2W4wpeNv7euuNL54Mmq4ajWjofmpVOQj TQQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=JqvgdF3P; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ci19-20020a170906c35300b007314bba0e9bsi1918999ejb.353.2022.08.09.08.05.30; Tue, 09 Aug 2022 08:05:56 -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=@gmail.com header.s=20210112 header.b=JqvgdF3P; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243576AbiHIOjD (ORCPT + 99 others); Tue, 9 Aug 2022 10:39:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230178AbiHIOjA (ORCPT ); Tue, 9 Aug 2022 10:39:00 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0EB419C04; Tue, 9 Aug 2022 07:38:59 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id uj29so22704084ejc.0; Tue, 09 Aug 2022 07:38:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=FZWzu7y9cZ515G8p3adab/2sdQH4tG+7zriZfn5G1uM=; b=JqvgdF3PLdiGIlyld/3xKQfIV0pyoOxPGZ47eRx/jY2qe0pDbO9E0mRlpjDQvtO5qb ldoUevusjaC85SdjMTKL//GcZsPv2QN7HdKN+k1Rx4egd6YO9AWza7ufYt/Zf9d0NDXJ Uzz7JU2GmVyVJJVQGDkAKzkU28y2B/Ldb8xRhVTHQlohSjHZyoYezjFhPGdq4nKEFkhs gpD6T6KaEpTE3bAik8AWRF3l8UcSUXZ55rSMHNLjH5IjkjYup5G7DJRlw/kN2WAhWRgM vyvrdHd3DfRuID6DIGqadtMt62wATkGbXuxQ8E+RoFlyjjf25clBqeZtZmGaVRWYpdGK RBPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=FZWzu7y9cZ515G8p3adab/2sdQH4tG+7zriZfn5G1uM=; b=EClFY7/YT4FMG8xU+hrDV6buc4thFXW5uzakiuYUtScmImeP41i70O0RaGAxxi6Akf axqQPalfl+xOT3MjF3TvNxiELzoNFpIzd7tp2UnuHVam4FQRoJt0PgQMHMkSstoDWMbf t8rWaPE1zOhbBZF+gunGXZ6SLr6K//RggV0G9pgJLnC7DP18Mz81FKm37Zu9ZXkH6JA1 LmW1V85S+5r1cRTbfwQLitEP5J985La+3duBCsX1Kd1ZaB6/b/YC+sevbWHH0umzTd7D HKiGwt5M5tXtxjokXmCIaakbSTQyNf7nuw8oLN01OQD/Ez3aOGL6yOsPbhnWoTQ5VOqS ofLg== X-Gm-Message-State: ACgBeo2fUhqiwLgCa2Wf/8VOCDTX0K6W5xP6+1RP736UF2KRiVxfjTXp 8KcADS61/hVRXrxn1ZtQpLWGADTpQWKB5EKN7A8= X-Received: by 2002:a17:907:e8d:b0:730:a4e8:27ed with SMTP id ho13-20020a1709070e8d00b00730a4e827edmr16528349ejc.58.1660055938238; Tue, 09 Aug 2022 07:38:58 -0700 (PDT) MIME-Version: 1.0 References: <20220803134821.425334-1-lee@kernel.org> In-Reply-To: From: Alexei Starovoitov Date: Tue, 9 Aug 2022 07:38:46 -0700 Message-ID: Subject: Re: [PATCH v2 1/1] bpf: Drop unprotected find_vpid() in favour of find_get_pid() To: Lee Jones 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,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 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.