Skip to contentSkip to conversation

accel-config: Fixes for pedantic compiler warnings

PATCH Superseded Ramesh Thomas 2 participants 3 messages Aug 24, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel-config: Fixes for pedantic compiler warnings

Debian builds enable the -Wpedantic option that gives warnings for features that are supported by GCC but prohibited by other compilers. This project is tied to the GCC compiler and uses several GCC builti-ins. Provide fixes to warnings that are potential issues. Several benighn warnings are suppressed using GCC #pragma.

---
 accfg/idxd.h         | 42 ++++++++++++++++++++----------------------
 accfg/lib/libaccfg.c | 20 +++++++++-----------
 ccan/list/list.h     |  3 +++
 ccan/minmax/minmax.h |  2 ++
 test/accel_test.h    |  2 ++
 test/dsa.c           |  4 ++--
 test/dsa_test.c      |  1 +
 test/iaa_test.c      |  1 +
 util/log.h           | 30 +++++++++++++++---------------
 util/util.h          |  1 +
 util/wrapper.c       |  2 ++
 11 files changed, 58 insertions(+), 50 deletions(-)
accfg/idxd.h +20 -22
index 19d5435..f8b0c9a 100644
@@ -9,28 +9,26 @@
 #include <linux/ioctl.h>
 
 /* Driver command error status */
-enum idxd_scmd_stat {
-	IDXD_SCMD_DEV_ENABLED = 0x80000010,
-	IDXD_SCMD_DEV_NOT_ENABLED = 0x80000020,
-	IDXD_SCMD_WQ_ENABLED = 0x80000021,
-	IDXD_SCMD_DEV_DMA_ERR = 0x80020000,
-	IDXD_SCMD_WQ_NO_GRP = 0x80030000,
-	IDXD_SCMD_WQ_NO_NAME = 0x80040000,
-	IDXD_SCMD_WQ_NO_SVM = 0x80050000,
-	IDXD_SCMD_WQ_NO_THRESH = 0x80060000,
-	IDXD_SCMD_WQ_PORTAL_ERR = 0x80070000,
-	IDXD_SCMD_WQ_RES_ALLOC_ERR = 0x80080000,
-	IDXD_SCMD_PERCPU_ERR = 0x80090000,
-	IDXD_SCMD_DMA_CHAN_ERR = 0x800a0000,
-	IDXD_SCMD_CDEV_ERR = 0x800b0000,
-	IDXD_SCMD_WQ_NO_SWQ_SUPPORT = 0x800c0000,
-	IDXD_SCMD_WQ_NONE_CONFIGURED = 0x800d0000,
-	IDXD_SCMD_WQ_NO_SIZE = 0x800e0000,
-	IDXD_SCMD_WQ_NO_PRIV = 0x800f0000,
-	IDXD_SCMD_WQ_IRQ_ERR = 0x80100000,
-	IDXD_SCMD_WQ_NO_DRV_NAME = 0x80200000,
-	IDXD_SCMD_DEV_EVL_ERR = 0x80300000,
-};
+#define IDXD_SCMD_DEV_ENABLED		0x80000010
+#define IDXD_SCMD_DEV_NOT_ENABLED	0x80000020
+#define IDXD_SCMD_WQ_ENABLED		0x80000021
+#define IDXD_SCMD_DEV_DMA_ERR		0x80020000
+#define IDXD_SCMD_WQ_NO_GRP		0x80030000
+#define IDXD_SCMD_WQ_NO_NAME		0x80040000
+#define IDXD_SCMD_WQ_NO_SVM		0x80050000
+#define IDXD_SCMD_WQ_NO_THRESH		0x80060000
+#define IDXD_SCMD_WQ_PORTAL_ERR		0x80070000
+#define IDXD_SCMD_WQ_RES_ALLOC_ERR	0x80080000
+#define IDXD_SCMD_PERCPU_ERR		0x80090000
+#define IDXD_SCMD_DMA_CHAN_ERR		0x800a0000
+#define IDXD_SCMD_CDEV_ERR		0x800b0000
+#define IDXD_SCMD_WQ_NO_SWQ_SUPPORT	0x800c0000
+#define IDXD_SCMD_WQ_NONE_CONFIGURED	0x800d0000
+#define IDXD_SCMD_WQ_NO_SIZE		0x800e0000
+#define IDXD_SCMD_WQ_NO_PRIV		0x800f0000
+#define IDXD_SCMD_WQ_IRQ_ERR		0x80100000
+#define IDXD_SCMD_WQ_NO_DRV_NAME	0x80200000
+#define IDXD_SCMD_DEV_EVL_ERR		0x80300000
 
 #define IDXD_SCMD_SOFTERR_MASK	0x80000000
 #define IDXD_SCMD_SOFTERR_SHIFT	16
accfg/lib/libaccfg.c +9 -11
index 292ce45..82e524f 100644
@@ -66,10 +66,8 @@ static unsigned int accfg_device_compl_size[] = {
 	[ACCFG_DEVICE_IAX] = 64,
 };
 
-enum {
-	ACCFG_CMD_STATUS_MAX = 0x45,
-	ACCFG_CMD_STATUS_ERROR = 0x80010000,
-};
+#define ACCFG_CMD_STATUS_MAX	0x45
+#define ACCFG_CMD_STATUS_ERROR	0x80010000
 
 #define SCMD_STAT(x) (((x) & ~IDXD_SCMD_SOFTERR_MASK) >> \
 		IDXD_SCMD_SOFTERR_SHIFT)
@@ -1764,13 +1762,13 @@ ACCFG_EXPORT int accfg_group_get_##field( \
 	return group->field; \
 }
 
-accfg_group_get_field(group, read_buffers_reserved);
-accfg_group_get_field(group, read_buffers_allowed);
-accfg_group_get_field(group, use_read_buffer_limit);
-accfg_group_get_field(group, traffic_class_a);
-accfg_group_get_field(group, traffic_class_b);
-accfg_group_get_field(group, desc_progress_limit);
-accfg_group_get_field(group, batch_progress_limit);
+accfg_group_get_field(group, read_buffers_reserved)
+accfg_group_get_field(group, read_buffers_allowed)
+accfg_group_get_field(group, use_read_buffer_limit)
+accfg_group_get_field(group, traffic_class_a)
+accfg_group_get_field(group, traffic_class_b)
+accfg_group_get_field(group, desc_progress_limit)
+accfg_group_get_field(group, batch_progress_limit)
 
 static void wqs_init(struct accfg_device *device)
 {
ccan/list/list.h +3 -0
index 68e3dea..6080e0b 100644
@@ -8,6 +8,9 @@
 #include <ccan/container_of/container_of.h>
 #include <ccan/check_type/check_type.h>
 
+#pragma GCC diagnostic ignored "-Wpedantic"
+#pragma GCC diagnostic ignored "-Wformat"
+
 /**
  * struct list_node - an entry in a doubly-linked list
  * @next: next entry (self if empty)
ccan/minmax/minmax.h +2 -0
index 54f246c..c57b736 100644
@@ -4,6 +4,8 @@
 
 #include "config.h"
 
+#pragma GCC diagnostic ignored "-Wpedantic"
+
 #include <ccan/build_assert/build_assert.h>
 
 #if !HAVE_STATEMENT_EXPR || !HAVE_TYPEOF
test/accel_test.h +2 -0
index 3b41a78..6aabfde 100644
@@ -6,6 +6,8 @@
 #include <accfg/idxd.h>
 #include "accfg_test.h"
 
+#pragma GCC diagnostic ignored "-Wpedantic"
+
 #define SHARED 1
 #define DEDICATED 0
 
test/dsa.c +2 -2
index 54000a8..5638147 100644
@@ -223,7 +223,7 @@ int init_crcgen(struct task *tsk, int tflags, int opcode, unsigned long xfer_siz
 	memset_pattern(tsk->src1, tsk->pattern, xfer_size);
 	tsk->crc_seed = 0x12345678;
 	if (tsk->test_flags & (unsigned int)(READ_CRC_SEED)) {
-		tsk->crc_seed_addr = aligned_alloc(ADDR_ALIGNMENT, sizeof(tsk->crc_seed));
+		tsk->crc_seed_addr = aligned_alloc(ADDR_ALIGNMENT, sizeof(*tsk->crc_seed_addr));
 		*tsk->crc_seed_addr = tsk->crc_seed;
 		tsk->crc_seed = 0x0;
 	}
@@ -253,7 +253,7 @@ int init_copy_crc(struct task *tsk, int tflags, int opcode, unsigned long xfer_s
 
 	tsk->crc_seed = 0x12345678;
 	if (tsk->test_flags & (unsigned int)(READ_CRC_SEED)) {
-		tsk->crc_seed_addr = aligned_alloc(ADDR_ALIGNMENT, sizeof(tsk->crc_seed));
+		tsk->crc_seed_addr = aligned_alloc(ADDR_ALIGNMENT, sizeof(*tsk->crc_seed_addr));
 		*tsk->crc_seed_addr = tsk->crc_seed;
 		tsk->crc_seed = 0x0;
 	}
test/dsa_test.c +1 -0
index a4aad29..9f9d99b 100644
@@ -13,6 +13,7 @@
 #include "dsa.h"
 
 #define DSA_TEST_SIZE 20000
+#pragma GCC diagnostic ignored "-Wformat"
 
 static void usage(void)
 {
test/iaa_test.c +1 -0
index a1f529d..de9b860 100644
@@ -9,6 +9,7 @@
 #include "iaa.h"
 
 #define IAA_TEST_SIZE 20000
+#pragma GCC diagnostic ignored "-Wformat"
 
 static void usage(void)
 {
util/log.h +15 -15
index dc39e9b..2734c94 100644
@@ -25,32 +25,32 @@ void log_init(struct log_ctx *ctx, const char *owner, const char *log_env);
 static inline void __attribute__((always_inline, format(printf, 2, 3)))
 	log_null(struct log_ctx *ctx, const char *format, ...) {}
 
-#define log_cond(ctx, prio, arg...) \
+#define log_cond(ctx, prio, ...) \
 do { \
 	if ((ctx)->log_priority >= prio) \
-		do_log(ctx, prio, __FILE__, __LINE__, __FUNCTION__, ## arg); \
+		do_log(ctx, prio, __FILE__, __LINE__, __func__, __VA_ARGS__); \
 } while (0)
 
 #ifdef ENABLE_LOGGING
 #  ifdef ENABLE_DEBUG
-#    define log_dbg(ctx, arg...) log_cond(ctx, LOG_DEBUG, ## arg)
+#    define log_dbg(ctx, ...) log_cond(ctx, LOG_DEBUG, __VA_ARGS__)
 #  else
-#    define log_dbg(ctx, arg...) log_null(ctx, ## arg)
+#    define log_dbg(ctx, ...) log_null(ctx, __VA_ARGS__)
 #  endif
-#  define log_info(ctx, arg...) log_cond(ctx, LOG_INFO, ## arg)
-#  define log_err(ctx, arg...) log_cond(ctx, LOG_ERR, ## arg)
-#  define log_notice(ctx, arg...) log_cond(ctx, LOG_NOTICE, ## arg)
+#  define log_info(ctx, ...) log_cond(ctx, LOG_INFO, __VA_ARGS__)
+#  define log_err(ctx, ...) log_cond(ctx, LOG_ERR,  __VA_ARGS__)
+#  define log_notice(ctx, ...) log_cond(ctx, LOG_NOTICE, __VA_ARGS__)
 #else
-#  define log_dbg(ctx, arg...) log_null(ctx, ## arg)
-#  define log_info(ctx, arg...) log_null(ctx, ## arg)
-#  define log_err(ctx, arg...) log_null(ctx, ## arg)
-#  define log_notice(ctx, arg...) log_null(ctx, ## arg)
+#  define log_dbg(ctx, ...) log_null(ctx, __VA_ARGS__)
+#  define log_info(ctx, ...) log_null(ctx, __VA_ARGS__)
+#  define log_err(ctx, ...) log_null(ctx, __VA_ARGS__)
+#  define log_notice(ctx, ...) log_null(ctx, __VA_ARGS__)
 #endif
 
-#define dbg(x, arg...) log_dbg(&(x)->ctx, ## arg)
-#define info(x, arg...) log_info(&(x)->ctx, ## arg)
-#define err(x, arg...) log_err(&(x)->ctx, ## arg)
-#define notice(x, arg...) log_notice(&(x)->ctx, ## arg)
+#define dbg(x, ...) log_dbg(&(x)->ctx, __VA_ARGS__)
+#define info(x, ...) log_info(&(x)->ctx, __VA_ARGS__)
+#define err(x, ...) log_err(&(x)->ctx, __VA_ARGS__)
+#define notice(x, ...) log_notice(&(x)->ctx, __VA_ARGS__)
 
 #ifndef HAVE_SECURE_GETENV
 #  ifdef HAVE___SECURE_GETENV
util/util.h +1 -0
index 001707e..2573ee0 100644
@@ -21,6 +21,7 @@
 #include <string.h>
 
 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
+#pragma GCC diagnostic ignored "-Wpedantic"
 
 #ifdef __GNUC__
 #define NORETURN __attribute__((__noreturn__))
util/wrapper.c +2 -0
index b0de7bc..42f3366 100644
@@ -33,6 +33,8 @@ char *xstrdup(const char *str)
 	return ret;
 }
 
+#pragma GCC diagnostic ignored "-Wuse-after-free"
+
 void *xrealloc(void *ptr, size_t size)
 {
 	void *ret = realloc(ptr, size);
Signature · 1 line
2.34.1
#1 lore raw

The xrealloc warning is a genuine issue where realloc fails, take the example:

	ret = realloc(ptr, size);

if ret is NULL then the reallocation failed it is important to realize ptr is still valid and you end up with a leak on ptr.

The function also handles the zero size realloc with a realloc attempt and then another attempt with a size 1.

I suspect the function should be more like:

void *xrealloc(void *ptr, size_t size)

{
	void *ret;

	if (!size)
		size = 1;
	ret = realloc(ptr, size);
	if (!ret) {
		free(ptr);
		die("Out of memory, realloc failed");	
	}
	return ret;
}

The free() is academic since die() terminates the code, but at least it removes any doubt from static analysis tools that ptr is not being leaked

-----Original Message-----
From: Thomas, Ramesh <[email protected]>
Sent: Thursday, August 24, 2023 3:33 AM
To: [email protected]
Cc: King, Colin <[email protected]>; Thomas, Ramesh <[email protected]>
Subject: [PATCH] accel-config: Fixes for pedantic compiler warnings

Debian builds enable the -Wpedantic option that gives warnings for features that are supported by GCC but prohibited by other compilers. This project is tied to the GCC compiler and uses several GCC builti-ins. Provide fixes to warnings that are potential issues. Several benighn warnings are suppressed using GCC #pragma.

--- code cut out 8< ----

+#pragma GCC diagnostic ignored "-Wuse-after-free" +
void *xrealloc(void *ptr, size_t size)
{
void *ret = realloc(ptr, size);
--
2.34.1

#2 lore raw
Quoted 7 lines · Colin

The xrealloc warning is a genuine issue where realloc fails, take the example:

	ret = realloc(ptr, size);

if ret is NULL then the reallocation failed it is important to realize ptr is still valid and you end up with a leak on ptr.

The function also handles the zero size realloc with a realloc attempt and then another attempt with a size 1.

The size = 1 is a meaningless hack inherited from the original version of this function taken from git tool source, which has changed in the latest version. The new version frees the ptr if size == 0.

Based on usage, this code should simply call die() on failure. See below for more explanation on usage.

Following code can be modified as below

Quoted 9 lines

I suspect the function should be more like:

void *xrealloc(void *ptr, size_t size)

{
	void *ret;

	if (!size)
		size = 1;

Above check is not necessary

Quoted 3 lines
	ret = realloc(ptr, size);
	if (!ret) {
		free(ptr);

I think free is not necessary, but no harm in leaving it. Static analysis tools find the NORETURN in the die() path and don't complain.

Quoted 6 lines
		die("Out of memory, realloc failed");	
	}
	return ret;
}

The free() is academic since die() terminates the code, but at least it removes any doubt from static analysis tools that ptr is not being leaked

The callers don't handle a NULL pointer return or a freed pointer returned by malloc(0) (if size==0 was passed to free the buffer), so nothing other than die() matters. Also don't see anywhere it is getting called with size==0.

Thanks,
Ramesh