Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1494212lqe; Mon, 8 Apr 2024 10:20:54 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVnAxudR8N4bHiutduhKA33qM2pR2LYLn/T/tPjIqs/yQ9ht5wW8ZphUUO1qILR7aM5DmpwixdnuLd2PAw8o6zNVLVCHlq22Xy7iCeNHQ== X-Google-Smtp-Source: AGHT+IGrDxed/ZVZZC64vNDzSiglXAvOlCIdhZ/1mtCQhjUjL+PHJtm8mjLkFh2Q7TcRJhEDsjpF X-Received: by 2002:a25:a1a3:0:b0:de0:f230:2d75 with SMTP id a32-20020a25a1a3000000b00de0f2302d75mr3307717ybi.0.1712596853974; Mon, 08 Apr 2024 10:20:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712596853; cv=pass; d=google.com; s=arc-20160816; b=q+SgMYHSmuu5YAK3tasJ/yRVyqxoZuFEDtcwXFUaHX6KhK33o3m15kipfWQYzgGiaT q5Pt4X74hxTL5xDxAiGXSi2UOvERfb8SJXyzV8VEyxzerUs4UkWzB+F/UARrgibalBkL AindObIaYwjmw7G96SCiVfgLKfQk/zYLMamirZWrr3sRfz7OXoZE6r1Nl0zoAwuNh9MS LojRzOBHPWPIn0njFBXip0lCbuJGsw2fbiTZLb4rj00hXUZ5CJVlcXYYzPXYtv/lILek +Hhc77IOaOqgc70T9nvEsWoVzlSqThZIPyy8bmBLMST+la67JBfdKqmdg2M+VY6zLKgs DsSw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=50vujPDoJgDaPu8/wFGRO5CAScsruXRwyphlNNmfr0k=; fh=JE54oD8fbPrYEVioYD1GHN6U7cdpjZLUHzsC70Kbf3M=; b=nXas5CZ8cKbcfX0M/DCM8+VSAJyGT9e3cEPQWebOBi3JGkZmyVKdMz9xQIuJQ2wt/E gEF6KX+s8FY7Fp8pZF5S/r5fD3QGpw1vtFBj7tzEVCGwB2MrVfR9mRWBEBfe4U6GjAsx h4ltYntFkXMAqctxvd45ZlULOx2S1rMDDfIVrIPi6elkTrNHKOH3KjDS4fPjpM7r2rAS /ulELeXPy+SBiK/dhp2u27931p+NdUoumutOR5PDWvI8w6D+Gyt3zAgyAAz40gF2yyVT BZCKV5KP9Gk/WBSBjvbRutoHqs4qhvoMyOHAMuTXmWI8cUIj6tedTla4P42mwnx67WqP tbVg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Dm4OwNNh; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-135741-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135741-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id h4-20020a0ceec4000000b00696a4a137edsi9086417qvs.207.2024.04.08.10.20.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 10:20:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-135741-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Dm4OwNNh; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-135741-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135741-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id A31571C215F2 for ; Mon, 8 Apr 2024 17:20:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5608D143C4F; Mon, 8 Apr 2024 17:20:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Dm4OwNNh" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 D28C3142E80 for ; Mon, 8 Apr 2024 17:20:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712596843; cv=none; b=skQJC425MHGUOiuhp3Mrr1/dC5SA8UEL0uW8wxgSwrkQCP/c2srhq5/j8kWW6EMs0wMxkhtu4+HGFrN2RoE0ftZOr9tOd3aepIFsCMxLVXTW8E441Y/8je4aVmq6ulcSidkAASV4EqpcxUHE1uf5+LGXanZ0j5qnfrGZfFYF2ms= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712596843; c=relaxed/simple; bh=LDjaWAvebvLx3MhYgg+6m/Onrp7EZMzfq2XnS1PyCHM=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=i/YUAB492CZpa9Gdbrl/83WhwTH3CL3skvOYMwnzZKZFlArKKZz/uX7nuuU7ZDpLvAvhrJ/2M2g4lvOIQyNSPwFwwSaj+4fTuwRxMUe/LRvWSOgWD1kcJVLgF4MOtC7dLmPDh/onsEtQeiF4eD2IsC9A29rF/yXPeRgYPRjMAy4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Dm4OwNNh; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712596840; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=50vujPDoJgDaPu8/wFGRO5CAScsruXRwyphlNNmfr0k=; b=Dm4OwNNh9HatpJ3hBxmXHiEi5ilD424E4KOM4k7o0d1wlLp83Jk8JEicsBuoszgg1C7Gia ziMwkt/mBt1ua72ZL2AN/IMGP/BvCrtlZwPWY4mOZca/lF0NXcJ9o1HF47Ng7Po3axsudq Xq2dSaorpm2775atGzi01+pQHTGe3bk= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-633-RyhsbNvbPy-c0OaeBitPVg-1; Mon, 08 Apr 2024 13:20:39 -0400 X-MC-Unique: RyhsbNvbPy-c0OaeBitPVg-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a51aec8eb93so176860666b.0 for ; Mon, 08 Apr 2024 10:20:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712596838; x=1713201638; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=50vujPDoJgDaPu8/wFGRO5CAScsruXRwyphlNNmfr0k=; b=TTZHRj/QsH2uysoa9VPcFW8zngsCaEgQbCq+V9w72th74rnXU2VrEDV1zyOLb9MxKf 5rF8BT3YxXLd0Q6qXeiYNR1iqeWF5UmckhCVwNEQGJ7zjy9yIzJPD/k0ohD+VLrP9ttM ggK/HKtJz8JjQgfLJ/vBGBup4Yxr4YzaVdBX4h9Vq0Tp7yN+9I7gYjB4Or0AzsaIX0fv u2ueP3g9BwCfwAkeNpfttnTJU6Ze9VwoZUjCeOqz/EFUwyl25oOTKrQ/fUhJNtcZgZPQ H4S/6MwZhxaLAG0rFQYs/bFOXUk/IjtLDBm7oMs6U3+4u6W16US7VYUan9wGo9nV1+d+ uv+A== X-Forwarded-Encrypted: i=1; AJvYcCWjpGIZFqHwHLZSusHO1EG1USzx2y3hKGkzEjm2J9aCun4hF7y9BjiCbW1FFSiWrO5ErpcAx1dVEPF4rDBwNx8MMrLc+iqOB5cPS2gv X-Gm-Message-State: AOJu0YwRguIoD0YV7Bhp+bqjz5Mwuayvoqflt4XVEaEIRYax0uz/7+Sa eJItbY6Q7HdR/5i2buCVSihYj7bhgK/KKPNiXpMURfbLHw7ILL0M5wqSTE8HYAKj74oiEHutvsT VX2G4x2jeaYa+LYHkosJIZE2JM3Xc/fmAYDPd/DCmeBl5r8JYFHZ/r7+D/UM2sSzmQgnJ2TpCrw ZkDM6LvlV3KEkPeBIjxiTwjabxIJNtpfgq1IiC X-Received: by 2002:a17:907:6e86:b0:a51:cbd5:1d1a with SMTP id sh6-20020a1709076e8600b00a51cbd51d1amr3393909ejc.32.1712596838386; Mon, 08 Apr 2024 10:20:38 -0700 (PDT) X-Received: by 2002:a17:907:6e86:b0:a51:cbd5:1d1a with SMTP id sh6-20020a1709076e8600b00a51cbd51d1amr3393895ejc.32.1712596838037; Mon, 08 Apr 2024 10:20:38 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240408-hid-bpf-sleepable-v6-0-0499ddd91b94@kernel.org> <20240408-hid-bpf-sleepable-v6-1-0499ddd91b94@kernel.org> <65c249a6af45bfa5fe0f6c2331dcc1771a6f0b05.camel@gmail.com> In-Reply-To: <65c249a6af45bfa5fe0f6c2331dcc1771a6f0b05.camel@gmail.com> From: Benjamin Tissoires Date: Mon, 8 Apr 2024 19:20:26 +0200 Message-ID: Subject: Re: [PATCH RFC bpf-next v6 1/6] bpf/helpers: introduce sleepable bpf_timers To: Eduard Zingerman Cc: Benjamin Tissoires , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Apr 8, 2024 at 7:08=E2=80=AFPM Eduard Zingerman = wrote: > > On Mon, 2024-04-08 at 10:09 +0200, Benjamin Tissoires wrote: > > [...] > > > diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > > index 9234174ccb21..fd05d4358b31 100644 > > --- a/kernel/bpf/helpers.c > > +++ b/kernel/bpf/helpers.c > > @@ -1096,12 +1096,19 @@ const struct bpf_func_proto bpf_snprintf_proto = =3D { > > * freeing the timers when inner map is replaced or deleted by user sp= ace. > > */ > > struct bpf_hrtimer { > > - struct hrtimer timer; > > + union { > > + struct hrtimer timer; > > + struct work_struct work; > > + }; > > struct bpf_map *map; > > struct bpf_prog *prog; > > void __rcu *callback_fn; > > void *value; > > - struct rcu_head rcu; > > + union { > > + struct rcu_head rcu; > > + struct work_struct sync_work; > > Nit: > I find this name very confusing, the field is used to cancel timer > execution, is it a convention to call such things '...sync...'? > > > + }; > > + u64 flags; > > }; > > > > [...] > > > +static void bpf_timer_sync_work_cb(struct work_struct *work) > > +{ > > + struct bpf_hrtimer *t =3D container_of(work, struct bpf_hrtimer, = sync_work); > > + > > + cancel_work_sync(&t->work); > > + > > + kfree_rcu(t, rcu); > > Sorry, I might be wrong, but this looks suspicious. > The 'rcu' field of 'bpf_hrtimer' is defined as follows: > > struct bpf_hrtimer { > ... > union { > struct rcu_head rcu; > struct work_struct sync_work; > }; > ... > }; > > And for sleepable timers the 'sync_work' field is set as follows: > > BPF_CALL_3(bpf_timer_init, struct bpf_timer_kern *, timer, struct bpf_map= *, map, > u64, flags) > { > ... > INIT_WORK(&t->sync_work, bpf_timer_sync_work_cb); > ... > } > > So, it looks like 'kfree_rcu' would be called for a non-rcu pointer. That was my initial assumption too, but Alexei told me it was fine. And I think he is correct because kfree_rcu doesn't need the rcu_head to be initialized. So in the end, we initialize the memory as a work_struct, and when that work kicks in, we reuse that exact same memory as the rcu_head. This is fine because that work will never be reused. If I understand correctly, this is to save a few bytes as this is a critical struct used in programs with a high rate usage, and every byte counts. Cheers, Benjamin > > > +} > > + > >