Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp1135767lqt; Sat, 20 Apr 2024 02:10:36 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX2IbT8iFr1kWBJ+xBXOKDLI6dOv2syNAg6iHvqRJlTgZlINBoEqPVtcZaWi28nKceeEpBYtfJQT4EEeGUbyMpEyVVQ7g2PxpzHPGv5VQ== X-Google-Smtp-Source: AGHT+IE9tP+VpP8jRseufUyDltsda+BYZ+oSlZkJnaOMHrbKuVatlctZClenidtOs+iEav8VnVtY X-Received: by 2002:a05:6a20:6a03:b0:1a7:35b1:1894 with SMTP id p3-20020a056a206a0300b001a735b11894mr5541104pzk.32.1713604236628; Sat, 20 Apr 2024 02:10:36 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713604236; cv=pass; d=google.com; s=arc-20160816; b=CBShT6/2g0g02g/BVQ1HmVvcO3x7Tzbw72iRBj5lmDMmNCnhZeNKoYM21iTGBn5pmk D7wQmq1yhwOEVb1PvJPXzXXDGEmNn+9Yk3ng1np4wZxF6wEHZzkyDrwXG268dzPUK2o1 GqNPKsERGFSWoYFYbr2OSGd5vMwEmosFIMCJcZWXMCs4JFQ0rBMM4rPL42XeAYy/HZ3o 32iTd6pGpTaTBaRsirGzkkENtL7fJZWmG3bpE5VZ4RESu3yawpTUm2Dk9aWdT6HyY/XU grXNfBq0FKZE3keA0SZaW0aOSX9AwmEBx4xxTJKb/+XEfBnhGFdwVAbrDyAu3/7/mbNG P3lw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=llIyn517JuwCzqOpk3wkMIx/IPUlD/HFuJ7jiMryUiw=; fh=Cxwo/A+C89tjOzVq8R/o/DH+l+P6Lpt8VwbqwppZI0Y=; b=fJuyJsU3dL46d3wXbmiCvbhuUSvDAIF0EZ6uaSYlBzFe8NVju9PG/42q3MscirvABd lzj/G4Nqcv2w1dKgVzvK91W/R7S9BqLssz8GUjSruLHUz/DdTMz0CL8N3PNFIWLpM+bw Leb7GonBU+i5ojfVDy+BsK8jJgJrygg3sIpRmTF8Z0oEQFMaHK/tXUHL6QC7dE2hXQDM 9yqa/BpdaWtXZEiTeUmfDhHFv/3tlKP2X6xMrzbAxAhXQbRizN4E0zxfSut0ZpNugqHf BWW66R5VMmJnYHVH9mmBBHkxchN6hYQkwALUMaeynMYtQLoBK0k/LLB9ftHkJePNsTZX mUqA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=reOsLPrw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-152187-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152187-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 12-20020a17090a098c00b002a52c9f792asi4573057pjo.47.2024.04.20.02.10.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Apr 2024 02:10:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-152187-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=reOsLPrw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-152187-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152187-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 40DD1281FC1 for ; Sat, 20 Apr 2024 09:10:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7C4B817559; Sat, 20 Apr 2024 09:09:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="reOsLPrw" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9E5F529D11; Sat, 20 Apr 2024 09:09:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713604178; cv=none; b=XTLUNVlPRmCWueKeVFt9jK5fYCJTxnMbhwC0PduIj+/gAYyFq/jD7voIYHQCsA2sGmxGlHzdRfJgGQFrWWAVrUQ/oLNiMwUbdb/Ba1C6TqUNmzdQ5D5k3Qiwjplif0UJlVjGJa3stPiQAyC2YZLpXUUkc3x6z4hr6woD/Np/I4c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713604178; c=relaxed/simple; bh=WVSMaupzImXDv9nSr/gpfg2Ic+L7htiabArXRl83nQ4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Sf5jRbnqrUE3wl1XaZ2X7PFYp+ZZPiFZa9kY93T5XcSu69hUb8qR/T4GL/6wPY58QBWCxbmT5vORnNc1CmIHvqgdIVkqwVrUpp35pm+uVKltMZ90V2steNLjEPjjLo8cK/6c9hOQmYAzYSYdt1/JpUjuUYTMNfTrpRkGb7HuWfU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=reOsLPrw; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2BFB2C072AA; Sat, 20 Apr 2024 09:09:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713604178; bh=WVSMaupzImXDv9nSr/gpfg2Ic+L7htiabArXRl83nQ4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=reOsLPrwdTypiDC0hFkNseFueXT7IKEJGwbYHUEkKX9h5fekP0wn5ujCuuHekEm3r wphYOT2Tr91vKmG27Hi4KWIEbh9wo2PiXxufR4B4aFFKkmLMm3/flDACzlNVqVeRTM t85SjmgHWjP+6qZVhdxgvwmqT7HVnSuXEYAwgMb9raNsR3VHKGVhcXMXuMHg/sqnH9 lQFkrN1G3gN74L5suHRSb56ZOtTtMXj/9sVdepHiYQmPsnD3SzRBRBZkXkCODU8Eja 6ib8xdHj4wOSIq8Z2zFmhpcJloQ8jgEOsA5bfNIBqKtuACQXg0zzJ+usKGLdvK72a2 etgu2DQ9VKyDA== From: Benjamin Tissoires Date: Sat, 20 Apr 2024 11:09:04 +0200 Subject: [PATCH bpf-next v2 04/16] bpf: replace bpf_timer_cancel_and_free with a generic helper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240420-bpf_wq-v2-4-6c986a5a741f@kernel.org> References: <20240420-bpf_wq-v2-0-6c986a5a741f@kernel.org> In-Reply-To: <20240420-bpf_wq-v2-0-6c986a5a741f@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1713604159; l=3194; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=WVSMaupzImXDv9nSr/gpfg2Ic+L7htiabArXRl83nQ4=; b=fCly4uRTNDWWLUxSirVwwcOrEVwLdKRs0BHyBFIzyVFpAp1M3C+GEQ2ZbA9f4RKx03uXMHlEY ziKcGLv4NznB4iwqBMMJYRJnDNAzrb8wS0zWymgHmoya+ORTpDoKIaB X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= Same reason than most bpf_timer* functions, we need almost the same for workqueues. So extract the generic part out of it so bpf_wq_cancel_and_free can reuse it. Signed-off-by: Benjamin Tissoires --- no changes in v2 --- kernel/bpf/helpers.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index d0a645b09d3d..78847f444f79 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1413,36 +1413,44 @@ static const struct bpf_func_proto bpf_timer_cancel_proto = { .arg1_type = ARG_PTR_TO_TIMER, }; -/* This function is called by map_delete/update_elem for individual element and - * by ops->map_release_uref when the user space reference to a map reaches zero. - */ -void bpf_timer_cancel_and_free(void *val) +static struct bpf_async_cb *__bpf_async_cancel_and_free(struct bpf_async_kern *async) { - struct bpf_async_kern *timer = val; - struct bpf_hrtimer *t; + struct bpf_async_cb *cb; - /* Performance optimization: read timer->timer without lock first. */ - if (!READ_ONCE(timer->timer)) - return; + /* Performance optimization: read async->cb without lock first. */ + if (!READ_ONCE(async->cb)) + return NULL; - __bpf_spin_lock_irqsave(&timer->lock); + __bpf_spin_lock_irqsave(&async->lock); /* re-read it under lock */ - t = timer->timer; - if (!t) + cb = async->cb; + if (!cb) goto out; - drop_prog_refcnt(&t->cb); + drop_prog_refcnt(cb); /* The subsequent bpf_timer_start/cancel() helpers won't be able to use * this timer, since it won't be initialized. */ - WRITE_ONCE(timer->timer, NULL); + WRITE_ONCE(async->cb, NULL); out: - __bpf_spin_unlock_irqrestore(&timer->lock); + __bpf_spin_unlock_irqrestore(&async->lock); + return cb; +} + +/* This function is called by map_delete/update_elem for individual element and + * by ops->map_release_uref when the user space reference to a map reaches zero. + */ +void bpf_timer_cancel_and_free(void *val) +{ + struct bpf_hrtimer *t; + + t = (struct bpf_hrtimer *)__bpf_async_cancel_and_free(val); + if (!t) return; /* Cancel the timer and wait for callback to complete if it was running. * If hrtimer_cancel() can be safely called it's safe to call kfree(t) * right after for both preallocated and non-preallocated maps. - * The timer->timer = NULL was already done and no code path can + * The async->cb = NULL was already done and no code path can * see address 't' anymore. * * Check that bpf_map_delete/update_elem() wasn't called from timer @@ -1451,7 +1459,7 @@ void bpf_timer_cancel_and_free(void *val) * return -1). Though callback_fn is still running on this cpu it's * safe to do kfree(t) because bpf_timer_cb() read everything it needed * from 't'. The bpf subprog callback_fn won't be able to access 't', - * since timer->timer = NULL was already done. The timer will be + * since async->cb = NULL was already done. The timer will be * effectively cancelled because bpf_timer_cb() will return * HRTIMER_NORESTART. */ -- 2.44.0