From patchwork Mon Oct 28 19:01:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 147559 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 77CF145C00; Mon, 28 Oct 2024 20:03:18 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id A70F3427D9; Mon, 28 Oct 2024 20:03:14 +0100 (CET) Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by mails.dpdk.org (Postfix) with ESMTP id A2D46427A1 for ; Mon, 28 Oct 2024 20:03:12 +0100 (CET) Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-71e4e481692so3790488b3a.1 for ; Mon, 28 Oct 2024 12:03:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1730142192; x=1730746992; darn=dpdk.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=fcT6h0Cmu1o16DMiPUCTo8HZFP/Fzfoj4kP6JzB6Bew=; b=XhYwtlT/546OJoL0ZhRaiCjc/w3+mSygMXPYNMxleFf+MpztjbB4poN3PJOrLwU45i N1tIRqnuUmdHb7NgeBaKprvybTlYHHCJjuHFeII7eyGVT6JSVkKdliiTYratjoko5kWX w24d3tOAePECamp3NzezzANmS1HrTSPVIOmipIdc7XXytI5vOGKeIhibBzNYTtdye0e4 JGEOSlbkv78FQngA3oPnX3El/w3IP3QNOGVUKqMzGvhuXZAoInyy/MS6opVXedYPTQxG dIzRFd3TJcJHFb6SkI2bvC5pbpdlW40wENAX12sA6MaNLJ4SDkAk1BhKapzmd//LuPbA jq0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730142192; x=1730746992; 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=fcT6h0Cmu1o16DMiPUCTo8HZFP/Fzfoj4kP6JzB6Bew=; b=FZGSXRB5s0dYnRJMKnmpcJq15QVLyfik4PNPykLIP9DxttRwlfqTni8lKCWjc4K/vE i1205R+kUepKl57+grVVUJk7Obce1RftK9A5uWdtx/GyTsVw71qva/NJ6WXoyfFaFDen 4yVEawseR2MHRqLck9f1jUlDFUS3z7TW9fiAD/QzPCa8DvSnXx3gQq/agkuTuPGpOcb9 BvGVbolOXuUr4L5USSZbY7U9fF1L4AOle32WUy3RMSlQQjdkECRaV2UMXrs4REVbC9xd uXOVmJLnH0kIdHiuC53jDawIGbA8xpmW8rqnCro6nAxmaR94nEF+DWz+EnZq7GWcqMJW 9Zyw== X-Gm-Message-State: AOJu0YyK2lxduoL/zZdGTMpm0NU8mN0/RfPOOjV+JxElwbzL+bg31QGc NUnamK0RgbVuAbUZcJXHyEWq4qTK2rRs8V34z7xykm+kW3K1dH2jTucLH63yGIjySE3tErqwAk9 x X-Google-Smtp-Source: AGHT+IEZVMhIjyKhvib3SdANNAK/8V1VIHugoEA/NsPurNHxPB8bdb90sT5LqOegVWbCbc5qiOyO5Q== X-Received: by 2002:a05:6a00:194b:b0:71e:fb4:6c98 with SMTP id d2e1a72fcca58-7206308e56dmr13031674b3a.23.1730142191836; Mon, 28 Oct 2024 12:03:11 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057939b82sm6152565b3a.81.2024.10.28.12.03.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 12:03:11 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Elena Agostini Subject: [PATCH v2 1/2] test-gpudev: avoid use-after-free and free-non-heap warnings Date: Mon, 28 Oct 2024 12:01:02 -0700 Message-ID: <20241028190301.264378-2-stephen@networkplumber.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241028190301.264378-1-stephen@networkplumber.org> References: <20241017225844.235401-1-stephen@networkplumber.org> <20241028190301.264378-1-stephen@networkplumber.org> MIME-Version: 1.0 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Remove the test intentionally frees a bad pointer in GPU memory, and GCC sees that at compile time now. Fix use after free in printf. Signed-off-by: Stephen Hemminger --- app/test-gpudev/main.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/app/test-gpudev/main.c b/app/test-gpudev/main.c index f065e6cd81..30fe573bf9 100644 --- a/app/test-gpudev/main.c +++ b/app/test-gpudev/main.c @@ -96,29 +96,19 @@ alloc_gpu_memory(uint16_t gpu_id) goto error; } - ret = rte_gpu_mem_free(gpu_id, (uint8_t *)(ptr_1)+0x700); - if (ret < 0) { - printf("GPU memory 0x%p NOT freed: GPU driver didn't find this memory address internally.\n", - (uint8_t *)(ptr_1)+0x700); - } else { - fprintf(stderr, "ERROR: rte_gpu_mem_free freed GPU memory 0x%p\n", - (uint8_t *)(ptr_1)+0x700); - goto error; - } - + printf("GPU memory 0x%p free\n", ptr_2); ret = rte_gpu_mem_free(gpu_id, ptr_2); if (ret < 0) { fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret); goto error; } - printf("GPU memory 0x%p freed\n", ptr_2); + printf("GPU memory 0x%p free\n", ptr_1); ret = rte_gpu_mem_free(gpu_id, ptr_1); if (ret < 0) { fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret); goto error; } - printf("GPU memory 0x%p freed\n", ptr_1); printf("\n=======> TEST: PASSED\n"); return 0; @@ -228,12 +218,12 @@ gpu_mem_cpu_map(uint16_t gpu_id) } printf("GPU memory CPU unmapped, 0x%p not valid anymore\n", ptr_cpu); + printf("GPU memory 0x%p freeing\n", ptr_gpu); ret = rte_gpu_mem_free(gpu_id, ptr_gpu); if (ret < 0) { fprintf(stderr, "rte_gpu_mem_free returned error %d\n", ret); goto error; } - printf("GPU memory 0x%p freed\n", ptr_gpu); printf("\n=======> TEST: PASSED\n"); return 0; From patchwork Mon Oct 28 19:01:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 147560 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id E48DF45C00; Mon, 28 Oct 2024 20:03:24 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D3A7E427DE; Mon, 28 Oct 2024 20:03:24 +0100 (CET) Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by mails.dpdk.org (Postfix) with ESMTP id 02202427DB for ; Mon, 28 Oct 2024 20:03:14 +0100 (CET) Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-7eab7622b61so3216924a12.1 for ; Mon, 28 Oct 2024 12:03:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1730142194; x=1730746994; darn=dpdk.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=/4h4uUqnihK/jxcF8R0proGKOTyctRUvYT6JUVYNlHE=; b=I4FCsAOsPWTbcVCP0DGclOrrdlf3HkbMY0Gw4LAaFrv0Lt0+/OJB+bE8Jd5vFjw+EA d5aLHM0mbep5yMG1IOza1Uws3Muu30f8QU9WySnU8lCm8c/eMJYJyPzgQZIH95h+tFfF kL+rp5UeKhukWS+zn9RIiJ6zKe/r57UWI5xTGWdMZxJDIMrGzIlNfRwkGEeHmTbTNSgE rVK7VQ6fS8DOI9fWa2toAgk9sgaAKH5Baq+ioCeut6v5iNSz30erIVxLUML/V2hg8/Oq Oi2XcINxPi/tX32KOMP0/S7KA4jjCea/Ku10sMIl4BoQrJ2UIgrRjoi9kR+ImkLeXQ8o 0PXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730142194; x=1730746994; 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=/4h4uUqnihK/jxcF8R0proGKOTyctRUvYT6JUVYNlHE=; b=qdMlqpIdfpJ78dmkOdM/H8Cy8QFa1WftfXXltfyoYcZI0ALsMxA15sinoc6S5cUbRK LFfpZSvvjrm7NrodtHuRIFkedqmuJ4np4FuBn/KawPVyZfez6QS2Cytfbn84Kf4Kfr1j EAnt7c6BEbSUJQ/AeMPxN4eqcCWeq1PX7BtCYOw0QyoQujuk0Cmo1DkQMM03rTGH5E6T YGTeFVwU3Ckp8dw8vE/F34i0eaKDjNvmcGH+Xp+pnr4UqVKpOrfWCHyxmqBxDOaYcLET AtG5nMwvz04VMoAevnv0liIMAL7Zo089Q5LiTP/aEGipFZWucGVjkhCGdHxAfIVYbVk8 1FeA== X-Gm-Message-State: AOJu0YwLH/DwEa5UE4uUxWXO3o2Z3R9afqLTio21rlmRi+PipFRyNPZi Sans8TIt4xgqgWmnIfmCtMPPzrE9+eLG3zZhUpkZePNDnTLwSG3qWOQsgPKKFa2FES0c+kNw0dK 2 X-Google-Smtp-Source: AGHT+IHLY+xgTWvJ0IIp13TyY3oT+tig46l9u1Rwy+EIpTBa95buaZtc6pppIJnk6Nkl3oquIti6AA== X-Received: by 2002:a05:6a20:c6ce:b0:1d8:a3ab:7228 with SMTP id adf61e73a8af0-1d9a81c3febmr11856691637.0.1730142192564; Mon, 28 Oct 2024 12:03:12 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057939b82sm6152565b3a.81.2024.10.28.12.03.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2024 12:03:12 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Elena Agostini Subject: [PATCH v2 2/2] gpudev: add malloc annotations to rte_gpu_mem_alloc Date: Mon, 28 Oct 2024 12:01:03 -0700 Message-ID: <20241028190301.264378-3-stephen@networkplumber.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241028190301.264378-1-stephen@networkplumber.org> References: <20241017225844.235401-1-stephen@networkplumber.org> <20241028190301.264378-1-stephen@networkplumber.org> MIME-Version: 1.0 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Add function attributes that allow detecting use after free, and calling free on bad pointer at compile time. Signed-off-by: Stephen Hemminger --- lib/gpudev/rte_gpudev.h | 46 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/gpudev/rte_gpudev.h b/lib/gpudev/rte_gpudev.h index 0a94a6abc4..7a35cf85d1 100644 --- a/lib/gpudev/rte_gpudev.h +++ b/lib/gpudev/rte_gpudev.h @@ -357,6 +357,28 @@ int rte_gpu_callback_unregister(int16_t dev_id, enum rte_gpu_event event, __rte_experimental int rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info); + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Deallocate a chunk of memory allocated with rte_gpu_mem_alloc(). + * + * @param dev_id + * Reference device ID. + * @param ptr + * Pointer to the memory area to be deallocated. + * NULL is a no-op accepted value. + * + * @return + * 0 on success, -rte_errno otherwise: + * - ENODEV if invalid dev_id + * - ENOTSUP if operation not supported by the driver + * - EPERM if driver error + */ +__rte_experimental +int rte_gpu_mem_free(int16_t dev_id, void *ptr); + /** * @warning * @b EXPERIMENTAL: this API may change without prior notice. @@ -385,28 +407,8 @@ int rte_gpu_info_get(int16_t dev_id, struct rte_gpu_info *info); */ __rte_experimental void *rte_gpu_mem_alloc(int16_t dev_id, size_t size, unsigned int align) -__rte_alloc_size(2); - -/** - * @warning - * @b EXPERIMENTAL: this API may change without prior notice. - * - * Deallocate a chunk of memory allocated with rte_gpu_mem_alloc(). - * - * @param dev_id - * Reference device ID. - * @param ptr - * Pointer to the memory area to be deallocated. - * NULL is a no-op accepted value. - * - * @return - * 0 on success, -rte_errno otherwise: - * - ENODEV if invalid dev_id - * - ENOTSUP if operation not supported by the driver - * - EPERM if driver error - */ -__rte_experimental -int rte_gpu_mem_free(int16_t dev_id, void *ptr); + __rte_alloc_size(2) __rte_alloc_align(3) + __rte_malloc __rte_dealloc(rte_gpu_mem_free, 2); /** * @warning