From patchwork Tue Sep 10 14:41:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tao Chen X-Patchwork-Id: 13798648 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 D29CF196D9D; Tue, 10 Sep 2024 14:41:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725979282; cv=none; b=PSG4od7FANea0qQ+D5pcMSskkw5z1T2tOblJeUpuacJWYYCOtPODUzfyqg+PKTZJckuYK1QyUw8jLLA+WnGkWaR/0th8WI6XBdt4B4td6j57d+zNrEfzYp3LJqOTPTa7p0PWd9Mo4KBVduQmxVwllKzZtaKo2vPE0Zte1V08q1Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725979282; c=relaxed/simple; bh=PpD05954nnxBVZxuYesivDXuQr5KszMF/qiXMGW+GF8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sMHVuzmIebs/Aa8O67qh6vbIwQZBQEa4pxCdCt1YMp+G3bb/AmpCqLNceilWkpyA8fvNEH5NGiPM0sZPMHtyvflP6Liuenzv7NPWTclDZqjp4X8QY1Vw67csG8haFHrV7uUVk9jAsEpQ4d2oPUJCEZYK6VnFGkqzbx1y8m8ay84= 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=EZhnnAIg; arc=none smtp.client-ip=209.85.214.173 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="EZhnnAIg" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2054feabfc3so50136465ad.1; Tue, 10 Sep 2024 07:41:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725979280; x=1726584080; 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; bh=XAcIP6yyaAZJRPAhebVyDvt/NZWnopt98DEX8XwAkmk=; b=EZhnnAIgN3y/bFwp4vOVs7eDQA2iabFu+1F3wa41hwa6KckdDeCjIhCpqjRaUqU9X/ /TL+/hvSxLdbDK0pizViuiVpdKg/qBWK5iazwfxXm8C5Xjj6dTY/RvRhBfJOg3ycUwnO hUzL8dM0spSBUjx5s88kchYHgINsM20YGVWl+CIm5K1+pvypKo88c3m62lcPg12XYq70 3V27R6gtAUbCVcVoffkxgACQM5T/2Oxs3DHw7DSeAFZWcFl9LRC7+H+GHuu6oU5TYngv 5kQTjTAYiaCEOj7rK2ynW7hycTk2/R5md9iMpovv78Ez1drOF9/V1yG/xnG/fxUgu+5Y isuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725979280; x=1726584080; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XAcIP6yyaAZJRPAhebVyDvt/NZWnopt98DEX8XwAkmk=; b=XG+0PenK6fNjL/20W/bryzqzPBr5S24oieoIkGVYzzhG/Zo47ZuK3edXPxqB5pto+w PF9pISKSTMeIS8KZ71zhabnSNXNdrVqlq9fw6+9Am1c/6wLfxFxXq9CP2oTLFCkIJ9NR 05+6x8BZQGcz3MApcDxl+zHl5fsQjSUuxn94WRm/l8T3bEAsPI7uCE4QVoiD2pjzZrJo cV4UVSZ1tbufavDmYPSLP/pdH+ySlE2x7ym3DLPlVuzDONqoNxU+UmCb6PR17MPs51ta VruDYg4n2GDTfr+jHicdIIEbeLXVxbyk2mZHXqX+VP9jCHSboc7WWDCKlrDWYCW1xn+s DGpA== X-Forwarded-Encrypted: i=1; AJvYcCW0a+iEy9L83Cd+j9CzJCgeqt0BWNedMrFvbnTC5OoDnWlq1xE/W+mP8/uj7y53dLlupro=@vger.kernel.org, AJvYcCXQDD3Umna2PpRuD4FRjkWRdDCPk/svzC/WROT6vuUIuLUKAhdk+dC4/fDwg0MJhvhLUDOv1ba8vCT7hdSW@vger.kernel.org X-Gm-Message-State: AOJu0YwC0n3OFzDMGLTC/fhUHGnu1tVywd0mI3IRyTE6Pfau2opNljc9 bZ9bK6VObCp+f/2Ct8+dFqwIQcmQTCZE8TAjaSErXhTiffpcVjiy X-Google-Smtp-Source: AGHT+IHmvUBz7fk/UCIXIZ//0faoJOq2TX3I8JkxJznk+y/j/c6zzfsoyEBVe0TEHViH2yp9X/a0Sw== X-Received: by 2002:a17:902:c40c:b0:203:a046:c676 with SMTP id d9443c01a7336-2074c37971amr10873635ad.0.1725979279811; Tue, 10 Sep 2024 07:41:19 -0700 (PDT) Received: from localhost ([116.198.225.81]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20710e353ecsm49775845ad.116.2024.09.10.07.41.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Sep 2024 07:41:19 -0700 (PDT) From: Tao Chen To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Song Liu , Hou Tao , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev Cc: Tao Chen , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Jinke Han , Jiri Olsa Subject: [v3 PATCH bpf-next 1/2] bpf: Check percpu map value size first Date: Tue, 10 Sep 2024 22:41:10 +0800 Message-Id: <20240910144111.1464912-2-chen.dylane@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240910144111.1464912-1-chen.dylane@gmail.com> References: <20240910144111.1464912-1-chen.dylane@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Percpu map is often used, but the map value size limit often ignored, like issue: https://github.com/iovisor/bcc/issues/2519. Actually, percpu map value size is bound by PCPU_MIN_UNIT_SIZE, so we can check the value size whether it exceeds PCPU_MIN_UNIT_SIZE first, like percpu map of local_storage. Maybe the error message seems clearer compared with "cannot allocate memory". Signed-off-by: Tao Chen Signed-off-by: Jinke Han Acked-by: Jiri Olsa Acked-by: Andrii Nakryiko --- kernel/bpf/arraymap.c | 3 +++ kernel/bpf/hashtab.c | 3 +++ 2 files changed, 6 insertions(+) diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index a43e62e2a8bb..79660e3fca4c 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -73,6 +73,9 @@ int array_map_alloc_check(union bpf_attr *attr) /* avoid overflow on round_up(map->value_size) */ if (attr->value_size > INT_MAX) return -E2BIG; + /* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */ + if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE) + return -E2BIG; return 0; } diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 45c7195b65ba..b14b87463ee0 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -462,6 +462,9 @@ static int htab_map_alloc_check(union bpf_attr *attr) * kmalloc-able later in htab_map_update_elem() */ return -E2BIG; + /* percpu map value size is bound by PCPU_MIN_UNIT_SIZE */ + if (percpu && round_up(attr->value_size, 8) > PCPU_MIN_UNIT_SIZE) + return -E2BIG; return 0; } From patchwork Tue Sep 10 14:41:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tao Chen X-Patchwork-Id: 13798649 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.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 1D5411991A0; Tue, 10 Sep 2024 14:41:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725979285; cv=none; b=Mnu7DFqOB1Zm6R784jK9QlMcxil+ASgxXrp1eDgkhD7Qo5O74qEcth1pUMOk16FCsz9bFyI7ARfhDUrVLwiwJKt/3jJ/5BakZeqSzmZIkTjjBvNVa2bkqjArEshM+O9MrufQjmLFLvi0YXqcfIm5QvT1PW6hvbexkLkJGodGyU4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725979285; c=relaxed/simple; bh=2imfAZ0QXn5XVKjATXkFWcOVKNkAng8p1ZNwuhhJ5RQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=U93qMk2XMRSc+kI6Q4vazrwN4n8IslVGgCNw0LE4M5HPrufqD9c3rfwRV/7jfLw13TBmyawjQKvjGpQYwSbiz13UPC11VBsbn1zsZ3AhRRpGymPoGJabILhcpKSpDes3ekTpPIPih0O+AiQHAxcp1A82Oi1HsFcCVfWlH8WbNlw= 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=hRY11dDC; arc=none smtp.client-ip=209.85.210.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hRY11dDC" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-717934728adso4073206b3a.2; Tue, 10 Sep 2024 07:41:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725979283; x=1726584083; 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; bh=0P+s93nCaJEgoUjxEO3WaqLlG5SGgxS9Bl7qShFJrxc=; b=hRY11dDCZytttS1PmHFH+n8Yf6P/Nq9TAXjwB+tqytOfIFvoGh0Gd4nDrffyUoNKRV T187o6kUX7Tv5hepKL6hht0oN0HSaTAPLCSYSJJzpoqMkv0bpOf9cSlXhhphDHbenXPQ jiSWKiTKoAvMOx/CJOC1oF3+Tduuqh9RsFi3dLoc27T7neaOZSu/Hyi6AhLKkFxLEpy9 ZYKgnNLfPQqeDlWox4AZTY5dJYPWvt3Zh/u43mMlK325Qy0CkSjp93BvU1fmLCqGQCsN 9QoCOk/WHFlnDxRBOKDfyB7q2a7wISBZbqEtQHFGUCGLJgztH/PsfDEGrj2ICuEmkNU7 tM4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725979283; x=1726584083; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0P+s93nCaJEgoUjxEO3WaqLlG5SGgxS9Bl7qShFJrxc=; b=iQ+Q/cKW+NUbxkgb3kqlpNYSvbQjOLJ/AxRmPXtKqXa/fiNtc6+3XwgW5mP1vhAOrl 94tGitNB/IRr5cQB+toe/cxJbtFBVvBnJHCG1Lv1YkL1qIP+xNfgUPc/lYYuZmug5wZ/ ML4L2KIBRfon78lks5UbVt466LaHLRN4RGMkMdnI/Cb1WvqWjrtWyJO7uYNcC/92iSJW 7yWB53G4YZjXk4/G7fpOPoiJqfjw4aFaNUqezydBTv3gOdSHPfNvVQ9WpUhbTnKb3n/o T+lAIjjBtQg5pHAQIsYLIeBGM1dPmFPsDhoAh2bpzOkHGSsTeDeLc+XPHoXxIO1eTgzD nC0A== X-Forwarded-Encrypted: i=1; AJvYcCU5DrruMJi4ZeNjcP65KeXPaw85sGXlbQH1LOC9pu/wfIcPir3BrKXKAfXcsopHErN38O+Pj8m1ToTJJR3c@vger.kernel.org, AJvYcCVC3BkHwhdyo+Ym0Lnk5scSLwYDVVGLTVRb7XI7HYsHQCoe0ZHJNmMUdYq+ZKXB0I/e3Qs=@vger.kernel.org X-Gm-Message-State: AOJu0YxJdvzjITw4QIKJ55VG5wXJmQGCw5KnZZ/8e6n14Xhv0b4T8Mhj geSaeXSdFi+xLpVJuDLyF2nixuER7jAVDQGquT9NBK09/RYUyyWF X-Google-Smtp-Source: AGHT+IGz14YyT8T0TWHsj6wiz9mNiGZ3jnjO6YQU3sMSB41tAV5tMRU3SV/SAfqFfhX8RQNuwNCTFg== X-Received: by 2002:a05:6a00:1893:b0:718:e062:bd7e with SMTP id d2e1a72fcca58-718e404b746mr16792040b3a.24.1725979283082; Tue, 10 Sep 2024 07:41:23 -0700 (PDT) Received: from localhost ([116.198.225.81]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-719090c9b15sm1435842b3a.202.2024.09.10.07.41.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Sep 2024 07:41:22 -0700 (PDT) From: Tao Chen To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Song Liu , Hou Tao , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev Cc: Tao Chen , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Jinke Han Subject: [v3 PATCH bpf-next 2/2] bpf/selftests: Check errno when percpu map value size exceeds Date: Tue, 10 Sep 2024 22:41:11 +0800 Message-Id: <20240910144111.1464912-3-chen.dylane@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240910144111.1464912-1-chen.dylane@gmail.com> References: <20240910144111.1464912-1-chen.dylane@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net This test case checks the errno message when percpu map value size exceeds PCPU_MIN_UNIT_SIZE. root@debian:~# ./test_maps ... test_map_percpu_stats_hash_of_maps:PASS test_map_percpu_stats_map_value_size:PASS test_sk_storage_map:PASS Signed-off-by: Tao Chen Signed-off-by: Jinke Han --- .../selftests/bpf/map_tests/map_percpu_stats.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c b/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c index 2ea36408816b..1c7c04288eff 100644 --- a/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c +++ b/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c @@ -17,6 +17,7 @@ #define MAX_ENTRIES_HASH_OF_MAPS 64 #define N_THREADS 8 #define MAX_MAP_KEY_SIZE 4 +#define PCPU_MIN_UNIT_SIZE 32768 static void map_info(int map_fd, struct bpf_map_info *info) { @@ -456,6 +457,22 @@ static void map_percpu_stats_hash_of_maps(void) printf("test_%s:PASS\n", __func__); } +static void map_percpu_stats_map_value_size(void) +{ + int fd; + int value_sz = PCPU_MIN_UNIT_SIZE + 1; + struct bpf_map_create_opts opts = { .sz = sizeof(opts) }; + enum bpf_map_type map_types[] = { BPF_MAP_TYPE_PERCPU_ARRAY, + BPF_MAP_TYPE_PERCPU_HASH, + BPF_MAP_TYPE_LRU_PERCPU_HASH }; + for (int i = 0; i < ARRAY_SIZE(map_types); i++) { + fd = bpf_map_create(map_types[i], NULL, sizeof(__u32), value_sz, 1, &opts); + CHECK(fd < 0 && errno != E2BIG, "percpu map value size", + "error: %s\n", strerror(errno)); + } + printf("test_%s:PASS\n", __func__); +} + void test_map_percpu_stats(void) { map_percpu_stats_hash(); @@ -467,4 +484,5 @@ void test_map_percpu_stats(void) map_percpu_stats_percpu_lru_hash(); map_percpu_stats_percpu_lru_hash_no_common(); map_percpu_stats_hash_of_maps(); + map_percpu_stats_map_value_size(); }