Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests: ublk: add utils.h

Signed-off-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20250713143415.2857561-18-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Ming Lei and committed by
Jens Axboe
e56828f4 c1dc9b0d

+72 -62
+2 -62
tools/testing/selftests/ublk/kublk.h
··· 29 29 #include "ublk_dep.h" 30 30 #include <linux/ublk_cmd.h> 31 31 32 - #define __maybe_unused __attribute__((unused)) 33 - #define MAX_BACK_FILES 4 34 - #ifndef min 35 - #define min(a, b) ((a) < (b) ? (a) : (b)) 36 - #endif 32 + #include "utils.h" 37 33 38 - #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) 34 + #define MAX_BACK_FILES 4 39 35 40 36 /****************** part 1: libublk ********************/ 41 37 ··· 47 51 #define UBLK_MAX_THREADS_SHIFT 5 48 52 #define UBLK_MAX_THREADS (1 << UBLK_MAX_THREADS_SHIFT) 49 53 #define UBLK_QUEUE_DEPTH 1024 50 - 51 - #define UBLK_DBG_DEV (1U << 0) 52 - #define UBLK_DBG_THREAD (1U << 1) 53 - #define UBLK_DBG_IO_CMD (1U << 2) 54 - #define UBLK_DBG_IO (1U << 3) 55 - #define UBLK_DBG_CTRL_CMD (1U << 4) 56 - #define UBLK_LOG (1U << 5) 57 54 58 55 struct ublk_dev; 59 56 struct ublk_queue; ··· 200 211 void *private_data; 201 212 }; 202 213 203 - #ifndef offsetof 204 - #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER) 205 - #endif 206 - 207 - #ifndef container_of 208 - #define container_of(ptr, type, member) ({ \ 209 - unsigned long __mptr = (unsigned long)(ptr); \ 210 - ((type *)(__mptr - offsetof(type, member))); }) 211 - #endif 212 - 213 - #define round_up(val, rnd) \ 214 - (((val) + ((rnd) - 1)) & ~((rnd) - 1)) 215 - 216 - 217 - extern unsigned int ublk_dbg_mask; 218 214 extern int ublk_queue_io_cmd(struct ublk_thread *t, struct ublk_io *io); 219 215 220 216 ··· 247 273 static inline unsigned short ublk_cmd_op_nr(unsigned int op) 248 274 { 249 275 return _IOC_NR(op); 250 - } 251 - 252 - static inline void ublk_err(const char *fmt, ...) 253 - { 254 - va_list ap; 255 - 256 - va_start(ap, fmt); 257 - vfprintf(stderr, fmt, ap); 258 - } 259 - 260 - static inline void ublk_log(const char *fmt, ...) 261 - { 262 - if (ublk_dbg_mask & UBLK_LOG) { 263 - va_list ap; 264 - 265 - va_start(ap, fmt); 266 - vfprintf(stdout, fmt, ap); 267 - } 268 - } 269 - 270 - static inline void ublk_dbg(int level, const char *fmt, ...) 271 - { 272 - if (level & ublk_dbg_mask) { 273 - va_list ap; 274 - 275 - va_start(ap, fmt); 276 - vfprintf(stdout, fmt, ap); 277 - } 278 276 } 279 277 280 278 static inline struct ublk_queue *ublk_io_to_queue(const struct ublk_io *io) ··· 404 458 void backing_file_tgt_deinit(struct ublk_dev *dev); 405 459 int backing_file_tgt_init(struct ublk_dev *dev); 406 460 407 - static inline unsigned int ilog2(unsigned int x) 408 - { 409 - if (x == 0) 410 - return 0; 411 - return (sizeof(x) * 8 - 1) - __builtin_clz(x); 412 - } 413 461 #endif
+70
tools/testing/selftests/ublk/utils.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef KUBLK_UTILS_H 3 + #define KUBLK_UTILS_H 4 + 5 + #define __maybe_unused __attribute__((unused)) 6 + 7 + #ifndef min 8 + #define min(a, b) ((a) < (b) ? (a) : (b)) 9 + #endif 10 + 11 + #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) 12 + 13 + #ifndef offsetof 14 + #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER) 15 + #endif 16 + 17 + #ifndef container_of 18 + #define container_of(ptr, type, member) ({ \ 19 + unsigned long __mptr = (unsigned long)(ptr); \ 20 + ((type *)(__mptr - offsetof(type, member))); }) 21 + #endif 22 + 23 + #define round_up(val, rnd) \ 24 + (((val) + ((rnd) - 1)) & ~((rnd) - 1)) 25 + 26 + static inline unsigned int ilog2(unsigned int x) 27 + { 28 + if (x == 0) 29 + return 0; 30 + return (sizeof(x) * 8 - 1) - __builtin_clz(x); 31 + } 32 + 33 + #define UBLK_DBG_DEV (1U << 0) 34 + #define UBLK_DBG_THREAD (1U << 1) 35 + #define UBLK_DBG_IO_CMD (1U << 2) 36 + #define UBLK_DBG_IO (1U << 3) 37 + #define UBLK_DBG_CTRL_CMD (1U << 4) 38 + #define UBLK_LOG (1U << 5) 39 + 40 + extern unsigned int ublk_dbg_mask; 41 + 42 + static inline void ublk_err(const char *fmt, ...) 43 + { 44 + va_list ap; 45 + 46 + va_start(ap, fmt); 47 + vfprintf(stderr, fmt, ap); 48 + } 49 + 50 + static inline void ublk_log(const char *fmt, ...) 51 + { 52 + if (ublk_dbg_mask & UBLK_LOG) { 53 + va_list ap; 54 + 55 + va_start(ap, fmt); 56 + vfprintf(stdout, fmt, ap); 57 + } 58 + } 59 + 60 + static inline void ublk_dbg(int level, const char *fmt, ...) 61 + { 62 + if (level & ublk_dbg_mask) { 63 + va_list ap; 64 + 65 + va_start(ap, fmt); 66 + vfprintf(stdout, fmt, ap); 67 + } 68 + } 69 + 70 + #endif