From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj2-f13.google.com (mail-pj2-f13.google.com [74.125.227.141]) (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 76902DF59 for ; Wed, 23 Sep 2026 00:08:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.227.141 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790122108; cv=none; b=YK5ym3qdkMtm6aPSE+8vlzbRJNDRbt7cyzLfFccNqmUcErbFANfw88kW2viAChbKDCHZtX7tfZaC4IL/xGPTGv+6nWu/8Kww3Qnprzs/nJ7TqOk/86cYR3j+Klr4HZmkv9hyNWQ+m8Rz0O58JUXQQ2keQ0rX92qDDqPMpahomm4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790122108; c=relaxed/simple; bh=fsWaJap2Z4snPeSf3DEBaGqtx3ZtQ4arg0Z0hs6TbO8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jZmb3tP4vtfXH0zvciEAKBQt/n9vtuJyDmazx1PAVs+MNdL8OAaHeJUuYWGrT26rr9BbX8Wof5Q8Kcbl6zTaI5k1lDqUqDebgvLWagm3Ltf+JMQrm1UddrgQKPWM7/QcQhyl42aaksPlOm53+saPg3SFWFLDQ+JzM6QDP3VpzWs= 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=cq2yw6Nt; arc=none smtp.client-ip=74.125.227.141 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cq2yw6Nt" Received: by mail-pj2-f13.google.com with SMTP id d9443c01a7336-2d9004a1ac0so1568125ad.3 for ; Tue, 22 Sep 2026 17:08:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790122107; x=1790726907; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=LKmQCvI2wiAQJXN+O58M5/YjmdCMCrm9Tdz8ibDBbpU=; b=cq2yw6NtCjOj5tsW3Fow42xuVBtmfqLBQd+bB7gR7tl5wIUu1xfQNzdLixk0ytZiCf p+ai/8XVv9RwHk4DvpPoFiY9r0mT1cOXs760+Oywj/Nqn0mPuWG7jf1zJBMxicilaHX7 kJXC+0JULD8btF76QXkcc9PhsguLyguPLFBCT6pu9Qn8+0LIzXuUWNHxCb3AdNB3nW1/ StB0/hJM+nXtKrOTE09Gj3CiZdGCWSnvM9Wu4Iyswjw82gjweRM1e/IBiucWHqOQf4QT y3YErqU3T/ToEs8fYLLjj5SQisLyJKhADr9kfMwDCrV9uMPgYvDxJkAjR7L0DWqkrHMh E3ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790122107; x=1790726907; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=LKmQCvI2wiAQJXN+O58M5/YjmdCMCrm9Tdz8ibDBbpU=; b=St04HW9v7ZLVV71hgt6T7IgcOpUBcLzrXHLIbNdSjVKNmwlodHKDMP3fe0+mg2Q3sU 4fGjM0xZUXvogyTKtpqoOqTn/DwaeugI4IMvgrZ+nhVgwjhHhiMtwAoKce+luZu/hXWD +EE0YWRlPlMqqO7eDM9WV4iUT6f1GZZa+tmiJsjGypaT7cbaVv8gupM5jH67l47P5TG2 6trxOeJ6iuOo3mAcrN4dsJJpa90soHnR8Q374z2wd0uPQdzsdoYTRCcrPW3Xd1YVVfnm qFZh710wrOZ57GxZ4kF47J0t/HdL1czVDo2vWo9YuulOJSHgGGUnqbzqdGCiQW1xA4xh sRnA== X-Forwarded-Encrypted: i=1; AKwUvBy62Vq3N5r+p9EeI86Oa7rRHj/05VQYwMh0MQEZjzXIBQ1itnZepdPHRYFFcbimlVnhE1GgaiUUjwNBlYA=@vger.kernel.org X-Gm-Message-State: AFuF++kPNLvQLMi0dHONYmhnI+7SMItyzWAGEj8zGsLA/1p3o0rEGXa/ eBJNwECT4CjEyaQ3ZRork+UN+EcRT6tzN0ZxZwcoiLiHwSSx39mLH58= X-Gm-Gg: AYBFou0kDIeDgtI86piYhy/UOIYE3OhmSmCah6z0cRJFFnhKqXEFryEG4qJyy4nJhrU UUv5PbdRTP2A+bW+/UdWASsiktqg6Yjy06iNuIddgEiLuDAG8tnjV6obBvnRtImu0xuor9BBaL9 JrgXV0eSeyVDTzsiU+XqNyYjpNdx+3VfI03Yw57e767Ko+0mj2Frl3KAO33OYhkbjMQfN9C7PwU CYn8wrp8d6EAWEJpIs+SI6np5TcUqe1o5MfDuix/VWZwqjyo9M+Mk8APAt9/qlqBtKZ1BWGjG+U Py+hekz3HbqTNNEDqXYztTOK8Xu09PRlyYQMQ7pXLcMhFgd/2Qsw5vFeRi8B7vpS5/ajEd7cSu3 HyjCa9KG5gU8G6e8lVcbL8bTBzrcAWuPicGjhucUlmr99etx7PdXnGUw8WVPjNyxpMdcac5Ve2N SveosRpqNoidp8zRspu0pL1OKnKqt+ur+DD4gica7+5l3EZDy12SG/aQfbLLSYI/R6TQOASvhbt IZCcdGeFI87WocYJzdzdqpoveU= X-Received: by 2002:a17:902:e741:b0:2dd:c053:e0fa with SMTP id d9443c01a7336-2df69e017bdmr8199925ad.41.1790122106661; Tue, 22 Sep 2026 17:08:26 -0700 (PDT) Received: from ydg-Zenbook-14-UM3406GA ([2001:2d8:7f00:8c85:2466:805a:e198:cde8]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2df6a5c3a3dsm2021425ad.43.2026.09.22.17.08.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Sep 2026 17:08:25 -0700 (PDT) From: Donggeun Yoo To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev, leon.hwang@linux.dev, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, donggeunyoo.kernel@gmail.com Subject: [PATCH bpf v2 1/2] bpf: Zero-fill other CPUs when BPF_F_CPU creates a per-cpu hash element Date: Wed, 23 Sep 2026 09:08:00 +0900 Message-ID: <20260923000801.1764758-2-donggeunyoo.kernel@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260923000801.1764758-1-donggeunyoo.kernel@gmail.com> References: <20260923000801.1764758-1-donggeunyoo.kernel@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit pcpu_init_value() initializes the per-cpu area of a newly created [lru_]percpu_hash element. The area is recycled, so when the value comes from a BPF program (onallcpus == false) it writes the running CPU's slot and zeroes the rest. bpf_percpu_hash_update() passes onallcpus == true, which delegates to pcpu_copy_value(). Since BPF_F_CPU was added that writes only the CPU named in map_flags when the flag is set, so on the create path the other slots keep the recycled element's values: update(k1, 0xdeadc0de, BPF_F_ALL_CPUS) every CPU holds 0xdeadc0de delete(k1) element back on the freelist update(k2, 0xc0ffee, BPF_F_CPU | 0) creates, writes CPU 0 only lookup(k2) CPU 0 0xc0ffee, rest 0xdeadc0de Zero-fill the other CPUs on that arm too. Fixes: c6936161fd55 ("bpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu_hash and lru_percpu_hash maps") Signed-off-by: Donggeun Yoo --- The merged arm no longer calls bpf_obj_cancel_fields() on the named CPU. That call is inert here: it acts only on BPF_TIMER, BPF_WORKQUEUE and BPF_TASK_WORK, which map_check_btf() rejects for [lru_]percpu_hash. v2: name the BPF_F_CPU entry condition in the block comment. kernel/bpf/hashtab.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 4f495dcbf670c..f7a9c432d9b76 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -1054,14 +1054,16 @@ static void pcpu_init_value(struct bpf_htab *htab, void __percpu *pptr, /* When not setting the initial value on all cpus, zero-fill element * values for other cpus. Otherwise, bpf program has no way to ensure * known initial values for cpus other than current one - * (onallcpus=false always when coming from bpf prog). + * (onallcpus=false always when coming from bpf prog, + * map_flags & BPF_F_CPU when coming from syscall but setting + * only one cpu). */ - if (!onallcpus) { - int current_cpu = raw_smp_processor_id(); + if (!onallcpus || (map_flags & BPF_F_CPU)) { + int init_cpu = onallcpus ? map_flags >> 32 : raw_smp_processor_id(); int cpu; for_each_possible_cpu(cpu) { - if (cpu == current_cpu) + if (cpu == init_cpu) copy_map_value(&htab->map, per_cpu_ptr(pptr, cpu), value); else /* Since elem is preallocated, we cannot touch special fields */ zero_map_value(&htab->map, per_cpu_ptr(pptr, cpu)); -- 2.53.0