From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-170.mta1.migadu.com (out-170.mta1.migadu.com [95.215.58.170]) (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 46EB429A31C for ; Thu, 21 Aug 2025 16:08:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755792524; cv=none; b=ILU5wmPjuAAselEepscwBnUgL/hkTNyEVzcYtYwXNO/3tCHdYcWYawqBx90vIYX88bXetbHNWsMy2oJq4MDZIGmZuAbqiDbl8ZuIW+Y2cmJa9MYiX70iqZ3dt8VUy6FeaU5CuvK0qElmir9gyYjS7BBGLcP4T9jQmoakI9c7YEM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755792524; c=relaxed/simple; bh=AFJSzdxEFLzqDYuRsq2wGX4FfmbV4Tt3zzRs8ZuKzDI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FWb9JD4PjUs4wv5UChF+jlUcF2hoUG64WAoIwbcur/Zd3DTF4e/Lur0Svkc5bKZAPgxpmBGMrxm1o1grNGEQJ3GfamYRw2Nwe1pQbF+KaHdSV36dyLpyq3PqgEpElstPVq7wl1bMQ8yJRF3k4cs+yZPXTCuVz83xROYWGfsysj0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=UxvNCaQl; arc=none smtp.client-ip=95.215.58.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="UxvNCaQl" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1755792520; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ImdS0NHbqFUXfk7q1+h9wM8VLcIuoO3MpRwTi23WeOA=; b=UxvNCaQlIAUMDh4KWUZZ+PlWVHR345lQmcgZf7bitq4CLJo1NXQG+PFWf9tNPu4ykzumpH MxrqAAfDJCBuDrMCbwUpvSWm/sXXl+jtJrqLnazHlJtSYI12FLtqElIOilnF7s85fQnmw8 rNY8L1p6nQ4A+pTRvhaxIxxPbbiJ32k= From: Leon Hwang To: bpf@vger.kernel.org Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net, olsajiri@gmail.com, yonghong.song@linux.dev, song@kernel.org, eddyz87@gmail.com, dxu@dxuuu.xyz, deso@posteo.net, leon.hwang@linux.dev, kernel-patches-bot@fb.com Subject: [PATCH bpf-next v3 1/6] bpf: Introduce internal check_map_flags helper function Date: Fri, 22 Aug 2025 00:08:12 +0800 Message-ID: <20250821160817.70285-2-leon.hwang@linux.dev> In-Reply-To: <20250821160817.70285-1-leon.hwang@linux.dev> References: <20250821160817.70285-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT It is to unify map flags checking for lookup, update, lookup_batch and update_batch. Therefore, it will be convenient to check BPF_F_CPU flag in this helper function for them in next patch. Signed-off-by: Leon Hwang --- kernel/bpf/syscall.c | 45 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 0fbfa8532c392..19f7f5de5e7dc 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1654,6 +1654,17 @@ static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size) return NULL; } +static int check_map_flags(struct bpf_map *map, u64 flags, bool check_flag) +{ + if (check_flag && (flags & ~BPF_F_LOCK)) + return -EINVAL; + + if ((flags & BPF_F_LOCK) && !btf_record_has_field(map->record, BPF_SPIN_LOCK)) + return -EINVAL; + + return 0; +} + /* last field in 'union bpf_attr' used by this command */ #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags @@ -1669,9 +1680,6 @@ static int map_lookup_elem(union bpf_attr *attr) if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM)) return -EINVAL; - if (attr->flags & ~BPF_F_LOCK) - return -EINVAL; - CLASS(fd, f)(attr->map_fd); map = __bpf_map_get(f); if (IS_ERR(map)) @@ -1679,9 +1687,9 @@ static int map_lookup_elem(union bpf_attr *attr) if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) return -EPERM; - if ((attr->flags & BPF_F_LOCK) && - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) - return -EINVAL; + err = check_map_flags(map, attr->flags, true); + if (err) + return err; key = __bpf_copy_key(ukey, map->key_size); if (IS_ERR(key)) @@ -1744,11 +1752,9 @@ static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr) goto err_put; } - if ((attr->flags & BPF_F_LOCK) && - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { - err = -EINVAL; + err = check_map_flags(map, attr->flags, false); + if (err) goto err_put; - } key = ___bpf_copy_key(ukey, map->key_size); if (IS_ERR(key)) { @@ -1952,13 +1958,9 @@ int generic_map_update_batch(struct bpf_map *map, struct file *map_file, void *key, *value; int err = 0; - if (attr->batch.elem_flags & ~BPF_F_LOCK) - return -EINVAL; - - if ((attr->batch.elem_flags & BPF_F_LOCK) && - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) { - return -EINVAL; - } + err = check_map_flags(map, attr->batch.elem_flags, true); + if (err) + return err; value_size = bpf_map_value_size(map); @@ -2015,12 +2017,9 @@ int generic_map_lookup_batch(struct bpf_map *map, u32 value_size, cp, max_count; int err; - if (attr->batch.elem_flags & ~BPF_F_LOCK) - return -EINVAL; - - if ((attr->batch.elem_flags & BPF_F_LOCK) && - !btf_record_has_field(map->record, BPF_SPIN_LOCK)) - return -EINVAL; + err = check_map_flags(map, attr->batch.elem_flags, true); + if (err) + return err; value_size = bpf_map_value_size(map); -- 2.50.1