Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2565547rdb; Mon, 12 Feb 2024 08:47:32 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU6ADTEBP9yKJ1N3EGC9tLHyCFZeLZtRVTey2j7pZ4nZSTgrTAhPmYjMSN58Eq4cMApvIxWLledvuZEVUCNZhFgJpS1ExBVwfNvNAAgwA== X-Google-Smtp-Source: AGHT+IF/0FyrQxI6lGbRPDsByPo5wzwkxmYymnxwIjd5+va1Dyegi26832tYK+u8CIq20lYMaW8/ X-Received: by 2002:a05:6a20:6f91:b0:19c:7485:4f75 with SMTP id gv17-20020a056a206f9100b0019c74854f75mr12076626pzb.42.1707756452136; Mon, 12 Feb 2024 08:47:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707756452; cv=pass; d=google.com; s=arc-20160816; b=mfdGSAkpJm7+m4v4JLNPt2iwDzJcRDVZnSXg1YZ/dg6TuFJuBdAGBo65dmKuvi1xan amjiEZy5SYibF1e+Z+AJune3gso1+6/71v3VEmqmD+h0UoWPwodazp4MPJiajT/We/BI GEfstvL2LhSHnGGsKSL+WYgtkwO1X7NcfDguNHLVgo1/WGo6rJSt9+d9rfB7anlse1aW Gw9QSdOD74tmG5Y4dARajdMqARZPHJ0On0yozAi24vFgsqc6ji+5mw/+r/l/CIvoC0UR oHl5c3WFF6SS4x7/pXzw7XzMnuQ0slIKVyoqHK980UVQDWamfYaefr9/kzJjXmv+R2TA eGeA== 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=MY228vr+39uuu/xQY9iIz40rY8941W7snNOmZuW3dRk=; fh=U3e7LJlW+Y84B0I7zgs5WZX+2AuclBvj2ezZ2quyLbc=; b=bcX1Q1VoldFP0azpBI6gSHiql02Qfvgem6MFTQkBZqBGFPCVafuALgMfHR2vetjqKk ry5NA2t0wQzIIHi4c+0pe5/JAeMfhGyLlXvCLvm3k5SmdD/630EGgWnt620anFAXTKWO vaXN3C8sS8SH4/IcPr3XMM/Yp2ns6pF6DJDdpZtD5xUtPOmKkJBULyRf+mZuIFR7xq+N dENV8aOO5Fr/jF1r42ZmdL4zBbJFCAPf/kXWq+TaDa8kbgkPt57kx7nyppghdMmzqWxd yk2R/SPlc72/6hzIWjLlmdNaRGCTD2YEvEZ3tV1znoPkJuX+An+l0YTF1y2i+bicD5Dh yq0A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LY8u65Fl; 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-62004-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62004-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCWdjbSMi1Q9c0cWeqypIOkh9kSoLKllybA0pMnLogjHZqEeIR5vkJUs77LmHQTgJnuBypRTVy64gwNGhR7/B7kCwOfis0u4aAWpxjRqEA== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id jw12-20020a056a00928c00b006e069e2a982si5280549pfb.298.2024.02.12.08.47.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 08:47:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-62004-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=LY8u65Fl; 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-62004-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62004-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id C4D2628278B for ; Mon, 12 Feb 2024 16:47:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 723613D0CD; Mon, 12 Feb 2024 16:47:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="LY8u65Fl" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 D2CB91E89B for ; Mon, 12 Feb 2024 16:47:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707756441; cv=none; b=eKXn1HQ3gG9NHIzj2PeQO4wEGuPkZK20pQZf7jSLJnlSw5Tn2z2dM+76sCRJfQdi0dr8MV14ykWFVViTJipw/s00ahQn5k8nuIVxUSx3W+HSITp7uJdYu6XCRgvk6LeJIHBDWxMkBmImUaHF9RyRK3e6MvE8LRJZXQYMqG8LpKQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707756441; c=relaxed/simple; bh=MY228vr+39uuu/xQY9iIz40rY8941W7snNOmZuW3dRk=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=q1E6mhLpBzXuB7hYjdW8pTZH5+s698VNjWOI6O6DPmBTorNp9M1vIwgTZtIjtP/ujkAceaUpiBwDH6Rl3q/+7+u+Efpm8WOTjAmspi+pyD6TaRubp1cqtOeeGVsWVYrFknD3IZZvtwZxvM9QON9dPrlFVd0h7UxMCd/zxDE1cDI= 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=LY8u65Fl; arc=none smtp.client-ip=170.10.133.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=1707756438; 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=MY228vr+39uuu/xQY9iIz40rY8941W7snNOmZuW3dRk=; b=LY8u65FlYQSSxdoig1oL2iBWgNMmnY6UnBav6C3rL+gEFbmCyD0sahm0ydcn3p06dwS90R MfZIg0z8knLYV+qPhJdCG/bqP6SE0jOHXAV9j+oniWrrOJahIWnHEzwwOqX9EALAgpTXY/ nKIra0GLWtrEa/iEkyVIzmA78DAZWpw= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-684-_GxFuP09Ohi-K7vwvkEvVQ-1; Mon, 12 Feb 2024 11:47:16 -0500 X-MC-Unique: _GxFuP09Ohi-K7vwvkEvVQ-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a30f9374db7so440459766b.0 for ; Mon, 12 Feb 2024 08:47:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707756435; x=1708361235; 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=MY228vr+39uuu/xQY9iIz40rY8941W7snNOmZuW3dRk=; b=hutYud5TxbzBaMxD+vOj+MECVMmPsik1v63qhRzkMYLXiVpOAqDxeS8gNw6Y27r0D2 hOnFmnhN0Ky+qnWdhdeZRhYJhbJiu8cNJWHm8c/AYg8XnCnz3nEf5/zYWuRq4S2/iObo cVnc26T+lVLJYGHyljq8bgHRjU+Qcv8/uI9/kqS/tpWATYDCu3V4Q5Fm2roOmqbAdQuN BIXLvdBVAQ3Lz3TTNjs1tbmeKwOqhzzjf/vCVx7Uwklfdk1DFHzc2wsQNWli1BYpha0D ZuMDIfklNs1kePbxFZvwSC3jQusbo3QuctUm2q67eDIMwhWNskzL+NHJu2J7yxJ4mv1N KFig== X-Forwarded-Encrypted: i=1; AJvYcCWhIpchbTJu1WW5o8p9V3yGE7Ge35S/Jfp8MG1gcjphIKFiFTIT+Cczgi/dtDss/apm4e1/T6bv7iuuvdBh9XwOYDqs+a0HQG6a2JZL X-Gm-Message-State: AOJu0YzfpixiLbTgLQ1wo+k7LqxzldFGJC23mApTQiZaljnbF77EhXDF bPrPiu+klXdyhK9H33iKPYNTI2RW0/Nn2jkU7SgE0144cTSWjZp88bdvLg9B3FL7EYtqGJZN6Sm /MPaWispb7rbB/lI+7MkmkNyAY6ALaWw2XyZeIIZP/GhX0gnEx/mjYnCbSucB0S8Cwe1pRHKydi 5Y+HPDA6/A3rSyCrEzC+53KsCjJPwgLFrnsGdt X-Received: by 2002:a17:906:a45:b0:a35:3eb8:2f6e with SMTP id x5-20020a1709060a4500b00a353eb82f6emr27092ejf.33.1707756435667; Mon, 12 Feb 2024 08:47:15 -0800 (PST) X-Received: by 2002:a17:906:a45:b0:a35:3eb8:2f6e with SMTP id x5-20020a1709060a4500b00a353eb82f6emr27061ejf.33.1707756435301; Mon, 12 Feb 2024 08:47:15 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240209-hid-bpf-sleepable-v1-0-4cc895b5adbd@kernel.org> <87bk8pve2z.fsf@toke.dk> <875xyxva9u.fsf@toke.dk> In-Reply-To: <875xyxva9u.fsf@toke.dk> From: Benjamin Tissoires Date: Mon, 12 Feb 2024 17:47:03 +0100 Message-ID: Subject: Re: [PATCH RFC bpf-next 0/9] allow HID-BPF to do device IOs To: =?UTF-8?B?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vuc2Vu?= Cc: Benjamin Tissoires , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Jiri Kosina , Jonathan Corbet , Shuah Khan , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Feb 9, 2024 at 6:05=E2=80=AFPM Toke H=C3=B8iland-J=C3=B8rgensen wrote: > > Benjamin Tissoires writes: > > > On Fri, Feb 9, 2024 at 4:42=E2=80=AFPM Toke H=C3=B8iland-J=C3=B8rgensen= wrote: > >> > >> Benjamin Tissoires writes: > >> > >> > [Putting this as a RFC because I'm pretty sure I'm not doing the thi= ngs > >> > correctly at the BPF level.] > >> > [Also using bpf-next as the base tree as there will be conflicting > >> > changes otherwise] > >> > > >> > Ideally I'd like to have something similar to bpf_timers, but not > >> > in soft IRQ context. So I'm emulating this with a sleepable > >> > bpf_tail_call() (see "HID: bpf: allow to defer work in a delayed > >> > workqueue"). > >> > >> Why implement a new mechanism? Sounds like what you need is essentiall= y > >> the bpf_timer functionality, just running in a different context, righ= t? > > > > Heh, that's exactly why I put in a RFC :) > > > > So yes, the bpf_timer approach is cleaner, but I need it in a > > workqueue, as a hrtimer in a softIRQ would prevent me to kzalloc and > > wait for the device. > > Right, makes sense. > > >> So why not just add a flag to the timer setup that controls the callba= ck > >> context? I've been toying with something similar for restarting XDP TX > >> for my queueing patch series (though I'm not sure if this will actuall= y > >> end up being needed in the end): > >> > >> https://git.kernel.org/pub/scm/linux/kernel/git/toke/linux.git/commit/= ?h=3Dxdp-queueing-08&id=3D54bc201a358d1ac6ebfe900099315bbd0a76e862 > >> > > > > Oh, nice. Good idea. But would it be OK to have a "timer-like" where > > it actually defers the job in a workqueue instead of using an hrtimer? > > That's conceptually still a timer, though, isn't it? I.e., it's a > mechanism whereby you specify a callback and a delay, and bpf_timer > ensures that your callback is called after that delay. IMO it's totally > congruent with that API to be able to specify a different execution > context as part of the timer setup. Yep :) There is still a problem I wasn't able to fix over the week end and today. How can I tell the verifier that the callback is sleepable, when the tracing function that called the timer_start() function is not? (more on that below). > > As for how to implement it, I suspect the easiest may be something > similar to what the patch I linked above does: keep the hrtimer, and > just have a different (kernel) callback function when the timer fires > which does an immediate schedule_work() (without the _delayed) and then > runs the BPF callback in that workqueue. I.e., keep the delay handling > the way the existing bpf_timer implementation does it, and just add an > indirection to start the workqueue in the kernel dispatch code. Sounds good, especially given that's roughly how the delayed_timers are implemented. > > > I thought I would have to rewrite the entire bpf_timer approach > > without the softIRQ, but if I can just add a new flag, that will make > > things way simpler for me. > > IMO that would be fine. You may want to wait for the maintainers to > chime in before going down this route, though :) > > > This however raises another issue if I were to use the bpf_timers: now > > the HID-BPF kfuncs will not be available as they are only available to > > tracing prog types. And when I tried to call them from a bpf_timer (in > > softIRQ) they were not available. > > IIUC, the bpf_timer callback is just a function (subprog) from the > verifier PoV, so it is verified as whatever program type is creating the > timer. So in other words, as long as you setup the timer from inside a > tracing prog type, you should have access to all the same kfuncs, I > think? Yep, you are correct. But as mentioned above, I am now in trouble with the sleepable state: - I need to call timer_start() from a non sleepable tracing function (I'm in hard IRQ when dealing with a physical device) - but then, ideally, the callback function needs to be tagged as a sleepable one, so I can export my kfuncs which are doing kzalloc and device IO as such. However, I can not really teach the BPF verifier to do so: - it seems to check for the callback first when it is loaded, and there is no SEC() equivalent for static functions - libbpf doesn't have access to the callback as a prog as it has to be a static function, and thus isn't exported as a full-blown prog. - the verifier only checks for the callback when dealing with BPF_FUNC_timer_set_callback, which doesn't have a "flag" argument (though the validation of the callback has already been done while checking it first, so we are already too late to change the sleppable state of the callback) Right now, the only OK-ish version I have is declaring the kfunc as non-sleepable, but checking that we are in a different context than the IRQ of the initial event. This way, I am not crashing if this function is called from the initial IRQ, but will still crash if used outside of the hid context. This is not satisfactory, but I feel like it's going to be hard to teach the verifier that the callback function is sleepable in that case (maybe we could suffix the callback name, like we do for arguments, but this is not very clean either). Cheers, Benjamin