mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	libbpf: Ensure print callback usage is thread-safe
This patch prevents races on the print function pointer, allowing the libbpf_set_print() function to become thread-safe. Signed-off-by: JP Kobryn <inwardvessel@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20230325010845.46000-1-inwardvessel@gmail.com
This commit is contained in:
		
							parent
							
								
									5f5a7d8d8b
								
							
						
					
					
						commit
						f1cb927cdb
					
				
					 2 changed files with 8 additions and 3 deletions
				
			
		| 
						 | 
					@ -216,9 +216,10 @@ static libbpf_print_fn_t __libbpf_pr = __base_pr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn)
 | 
					libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	libbpf_print_fn_t old_print_fn = __libbpf_pr;
 | 
						libbpf_print_fn_t old_print_fn;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						old_print_fn = __atomic_exchange_n(&__libbpf_pr, fn, __ATOMIC_RELAXED);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	__libbpf_pr = fn;
 | 
					 | 
				
			||||||
	return old_print_fn;
 | 
						return old_print_fn;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -227,8 +228,10 @@ void libbpf_print(enum libbpf_print_level level, const char *format, ...)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	va_list args;
 | 
						va_list args;
 | 
				
			||||||
	int old_errno;
 | 
						int old_errno;
 | 
				
			||||||
 | 
						libbpf_print_fn_t print_fn;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!__libbpf_pr)
 | 
						print_fn = __atomic_load_n(&__libbpf_pr, __ATOMIC_RELAXED);
 | 
				
			||||||
 | 
						if (!print_fn)
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	old_errno = errno;
 | 
						old_errno = errno;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -101,6 +101,8 @@ typedef int (*libbpf_print_fn_t)(enum libbpf_print_level level,
 | 
				
			||||||
 * be used for libbpf warnings and informational messages.
 | 
					 * be used for libbpf warnings and informational messages.
 | 
				
			||||||
 * @param fn The log print function. If NULL, libbpf won't print anything.
 | 
					 * @param fn The log print function. If NULL, libbpf won't print anything.
 | 
				
			||||||
 * @return Pointer to old print function.
 | 
					 * @return Pointer to old print function.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This function is thread-safe.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
 | 
					LIBBPF_API libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue