Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp1228488rdb; Fri, 16 Feb 2024 08:58:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWHOCmcF2JC1/bvrFh3wOZ/qg8NZFOmnJvY1kc+gAg8mJ4iHYwDFI/hZkXeVs770Adysux56j7eyTJdBO05OgEsUSP5TtWipsLGZSfytA== X-Google-Smtp-Source: AGHT+IEOkYQ2QG5CWedOXBaRm6agra1gsXNhjZkJTP5o+lrvlJEkB4ixlyy28kgnHHnSm3nJvPdp X-Received: by 2002:a05:6102:38ca:b0:46e:c3f1:bb76 with SMTP id k10-20020a05610238ca00b0046ec3f1bb76mr6459521vst.3.1708102730277; Fri, 16 Feb 2024 08:58:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708102730; cv=pass; d=google.com; s=arc-20160816; b=cEXNqe9xFTTULRmCbl3PwhXFwDPHzPESqdSLVif+j6OJkkfUPNWOBVRjcNY0NOjKuq aE2Auv7IL/AnRW6wK+mRxnuYpFu3xjM70QHv5JSUDgKRLsmytVAgBtTS96vzDGd7x+aK LhnLH66NOXgYt3UghF3l3KSzni3VEyrdpazVa8RSBcwqaGK2WqOp2cZ6GN8AQX+Z4Ct8 aDXDyQ9VWxJx8jXyiy5d14y7P3z/3TEYUUO4jFWSTDLwldsyrhFRqAaSJKo68ofF9unB juxFI1/8q1rx+r2DMAB1J7EAvAYgkaZQuVJGrWsJ8NFkTyParPJKK9js1UWgH9J0+X6l JRzQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=8H6TMdxbH3gjPcLATt0FrG9vNldFLghzgP5wmvBiIh8=; fh=bRNIBfXSgJvRShAK1CCyWUEoKUMLd1Xv48UNznWxNhQ=; b=qFeokiwMSKsEiPQDDHPj2BG7+hXqhAjUvonfp5016Z7NitgiytOyCtJIBUIITNgZ3r am20o99q+S0cWcu1K0C3THOBK4dKPJ0EdoiYk2X0VGQqpXkJAnnte+qT4qsfVyUu+U7J Ln0R/hsQv9B9Ak/LQJo40pp8vZjzPjdfy/UZJn4HnTAMbRimqmrNvl4zFaT8/oEOzaYF wQFdNJ4/GJDUO076Bzfu44WCC6J87JjCfiXxzeKDxa1KvP6nkpguy17lRuBzRIKG5ied AhAWiujFHrVWLmV/5a3qJQTAyVDOXvks8F0vhHXwbl0CsRDX5yM8cz1xbwOQ/uQ3GXRA Pa3Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=S7vq49DO; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-68999-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68999-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 a10-20020a05622a064a00b0042dce6c1246si241744qtb.318.2024.02.16.08.58.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 08:58:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-68999-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=@gmail.com header.s=20230601 header.b=S7vq49DO; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-68999-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68999-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 F3F941C23056 for ; Fri, 16 Feb 2024 16:58:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5A454130E3E; Fri, 16 Feb 2024 16:58:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S7vq49DO" Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 120D01E86B; Fri, 16 Feb 2024 16:58:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708102718; cv=none; b=PmciqTp8c2cE2Hm9GXy7L1kw1pNYZ1+TasQIJDeVwnsXjHzK6FdydXSP/Hyw0ZYRqyjA+Kb7aysBg9IIV37EcliFFpHQA4MP0emXOvF6TiaT43H1Q0u7mJvuUoY0D3pumzpaEJpRwbRZtw2PamTH6y06x0d63Xc8Sz/QeVuNaXA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708102718; c=relaxed/simple; bh=5+/OALfWeGla2oIoNvZlD1Lmsrn4u5yZITZBsot76aM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=qu+c8uCJECK/YzQ+Lszph31NoPCUXyyIO88Ti+S6mPdJR9eYVzW93Jy3IGnC6aGWpKcyObZ9pb6PtHyeDQG3RGL1T8pZzjWoEfTKDyg5Bn20EucRsZopRpJd3rKpCYSamPhdfqd6PDLmASSOGW81RX6hi5C81sBAcjsNEXZ7iFY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=S7vq49DO; arc=none smtp.client-ip=209.85.128.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-6080a19ea1cso1355687b3.1; Fri, 16 Feb 2024 08:58:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708102716; x=1708707516; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=8H6TMdxbH3gjPcLATt0FrG9vNldFLghzgP5wmvBiIh8=; b=S7vq49DOS9MmNmCsn09F0Nbb9mPTtadpeu0vFbwDsMZY+qhWKS5E4ZyXHuXB91jCuZ bKdSdZzqG65Q57GrFeODpbt6iyew31V4IQKIDaPvzdC0KqrNTRylXgKVLVIFliewHi1m aoYeySrHDzTqddhHbMYetqb8wc+MPNWQTL3WIGJa4tmY32LCAHIpdr7qkTo4cxvsZg7F 6RRN17jSdaQQ2haRTPtMz5uSsIYRkrcqlzuNe4cXVXi0aKB5weH0adMMc6l2ctOH48KO /f4HnSHmr21RrlyE3CSgUz1RpTQcrIv39KV9hAgfRF4UJaxuHgAla7mmqgIJU3LJZt1C KIXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708102716; x=1708707516; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8H6TMdxbH3gjPcLATt0FrG9vNldFLghzgP5wmvBiIh8=; b=tT0rnBFBp1amTQSDdudu5WGfCIO5Si3rP2tOAoAAIZvh5TrPB/hXImkY3lS9Cxfnta 0jB1hbvjed1NZWIWQCDEuc9qOLkr7+ww3lmhTAH6moQSF2NzOe7FEc8MlBSZQYwh9qrh 6fKgXM2wdcGRxLuJM8FiZK7vwpZA8koODUHlo9yTA9Vc2lgzZFitg39dWps2ehAgx2/r rGhlxn7/3rQUYO9UBalDHU1TZOM6sHbN9mEoHtpQ5nCJz0fDFVZtSFFNvL3kqejmpCvO 8sED0qcp96KCn1qTiU6UfX3DDSQFppo9ODUiw1SIdxIUP2nE73tg3C6xDNBynnpjz4J1 ISIw== X-Forwarded-Encrypted: i=1; AJvYcCULHm/mVBH7i+JAMwcfTp27/8f1hvjIXh3n2wvwo5/kuIwSoF7hAVP1AYwCEz6BMVxx7RE9AELvLl7hVdsFBdawy2oppnjTCpJPScD4pRwMJLLudl8EWcmDk7T/aqOgDnv80J85S5FTZflbC3zrQ/l6p4Mqnz8VfGuqQl+SfQ8rx+IdGVNymi8He/XJz1f2xiv6wjRgSL/tYNwhpVNoHMJECboujLs= X-Gm-Message-State: AOJu0YxeS4lVEfO2Jyktx+8qWfzkx95jXgak1HiincdxTakMoyOqbYip +sy/sgxvCYP4DjKa01VlPAi7Q+jh9CRQ6E1CKfHmGTYqs5zhYpTM X-Received: by 2002:a81:6cc1:0:b0:607:d048:bd23 with SMTP id h184-20020a816cc1000000b00607d048bd23mr5474345ywc.20.1708102715908; Fri, 16 Feb 2024 08:58:35 -0800 (PST) Received: from ?IPV6:2600:1700:6cf8:1240:6477:3a7d:9823:f253? ([2600:1700:6cf8:1240:6477:3a7d:9823:f253]) by smtp.gmail.com with ESMTPSA id n1-20020a819e41000000b00604d60609d9sm400781ywj.121.2024.02.16.08.58.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 16 Feb 2024 08:58:35 -0800 (PST) Message-ID: <51b1ae50-161f-435e-afe0-6d11f2cfbfc6@gmail.com> Date: Fri, 16 Feb 2024 08:58:33 -0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH RFC bpf-next v2 02/10] bpf/helpers: introduce sleepable timers Content-Language: en-US To: 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 , Benjamin Tissoires , Jonathan Corbet , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org References: <20240214-hid-bpf-sleepable-v2-0-5756b054724d@kernel.org> <20240214-hid-bpf-sleepable-v2-2-5756b054724d@kernel.org> From: Kui-Feng Lee In-Reply-To: <20240214-hid-bpf-sleepable-v2-2-5756b054724d@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2/14/24 09:18, Benjamin Tissoires wrote: > +static void bpf_timer_work_cb(struct work_struct *work) > +{ > + struct bpf_hrtimer *t = container_of(work, struct bpf_hrtimer, work); > + struct bpf_map *map = t->map; > + void *value = t->value; > + bpf_callback_t callback_fn; > + void *key; > + u32 idx; > + > + BTF_TYPE_EMIT(struct bpf_timer); > + > + rcu_read_lock(); > + callback_fn = rcu_dereference(t->sleepable_cb_fn); > + rcu_read_unlock(); > + if (!callback_fn) > + return; > + > + /* FIXME: do we need any locking? */ > + if (map->map_type == BPF_MAP_TYPE_ARRAY) { > + struct bpf_array *array = container_of(map, struct bpf_array, map); > + > + /* compute the key */ > + idx = ((char *)value - array->value) / array->elem_size; > + key = &idx; > + } else { /* hash or lru */ > + key = value - round_up(map->key_size, 8); > + } > + > + /* FIXME: this crashes the system with > + * BUG: kernel NULL pointer dereference, address: 000000000000000b > + */ > + /* callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0); */ > + /* The verifier checked that return value is zero. */ > +} > + > static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running); > > static enum hrtimer_restart bpf_timer_cb(struct hrtimer *hrtimer) > { > struct bpf_hrtimer *t = container_of(hrtimer, struct bpf_hrtimer, timer); > + bpf_callback_t callback_fn, sleepable_cb_fn; > struct bpf_map *map = t->map; > void *value = t->value; > - bpf_callback_t callback_fn; > void *key; > u32 idx; > > BTF_TYPE_EMIT(struct bpf_timer); > + sleepable_cb_fn = rcu_dereference_check(t->sleepable_cb_fn, > rcu_read_lock_bh_held()); > + if (sleepable_cb_fn) { > + schedule_work(&t->work); It seems nothing to stop the timer from being free here, right? You should have a way to make sure the timer & programs here is still alive when the work is running. For example, it can be flags to indicate the work is scheduled to prevent the timer from releasing, and indicate the timer should be free when returning from the callback. > + goto out; > + } > + > callback_fn = rcu_dereference_check(t->callback_fn, rcu_read_lock_bh_held()); > if (!callback_fn)