@@ -77,45 +77,78 @@ int rocm_memory_init(struct memory_ctx *ctx) {
7777
7878#ifdef HAVE_ROCM_DMABUF
7979 if (rocm_ctx -> use_dmabuf ) {
80- int dmabuf_supported = 0 ;
80+ int dmabuf_supported = 0 ;
8181 const char kernel_opt1 [] = "CONFIG_DMABUF_MOVE_NOTIFY=y" ;
8282 const char kernel_opt2 [] = "CONFIG_PCI_P2PDMA=y" ;
8383 int found_opt1 = 0 ;
8484 int found_opt2 = 0 ;
85- FILE * fp ;
85+
86+ const char * config_path_fmts [] = {
87+ "/boot/config-%s" ,
88+ "/usr/src/linux-%s/.config" ,
89+ "/usr/src/linux/.config" ,
90+ "/usr/lib/modules/%s/config" ,
91+ "/usr/lib/ostree-boot/config-%s" ,
92+ "/usr/lib/kernel/config-%s" ,
93+ "/usr/src/linux-headers-%s/.config" ,
94+ "/lib/modules/%s/build/.config" ,
95+ };
96+ size_t num_paths = sizeof (config_path_fmts ) /
97+ sizeof (config_path_fmts [0 ]);
8698 struct utsname utsname ;
87- char kernel_conf_file [128 ];
99+ char kernel_conf_file [256 ];
88100 char buf [256 ];
101+ FILE * fp = NULL ;
102+ size_t i ;
89103
90104 if (uname (& utsname ) == -1 ) {
91- printf ("could not get kernel name" );
105+ printf ("could not get kernel name\n " );
92106 return FAILURE ;
93107 }
94108
95- snprintf ( kernel_conf_file , sizeof ( kernel_conf_file ),
96- "/boot/config-%s" , utsname . release );
97- fp = fopen ( kernel_conf_file , "r" );
98- if ( fp == NULL ) {
99- printf ( "could not open kernel conf file %s error: %m" ,
100- kernel_conf_file ) ;
101- return FAILURE ;
109+ for ( i = 0 ; i < num_paths ; i ++ ) {
110+ snprintf ( kernel_conf_file , sizeof ( kernel_conf_file ),
111+ config_path_fmts [ i ], utsname . release );
112+ fp = fopen ( kernel_conf_file , "r" );
113+ if ( fp != NULL ) {
114+ break ;
115+ }
102116 }
103117
104- while (fgets (buf , sizeof (buf ), fp ) != NULL ) {
105- if (strstr (buf , kernel_opt1 ) != NULL ) {
106- found_opt1 = 1 ;
107- }
108- if (strstr (buf , kernel_opt2 ) != NULL ) {
109- found_opt2 = 1 ;
110- }
111- if (found_opt1 && found_opt2 ) {
112- dmabuf_supported = 1 ;
113- break ;
118+ if (fp != NULL ) {
119+ while (fgets (buf , sizeof (buf ), fp ) != NULL ) {
120+ if (strstr (buf , kernel_opt1 ) != NULL ) {
121+ found_opt1 = 1 ;
122+ }
123+ if (strstr (buf , kernel_opt2 ) != NULL ) {
124+ found_opt2 = 1 ;
125+ }
126+ if (found_opt1 && found_opt2 ) {
127+ dmabuf_supported = 1 ;
128+ break ;
129+ }
130+ }
131+ fclose (fp );
132+ }
133+
134+ // Fallback, works inside Docker containers where /boot/config-* is unavailable.
135+ if (!dmabuf_supported ) {
136+ fp = fopen ("/proc/kallsyms" , "r" );
137+ if (fp != NULL ) {
138+ int o1 = 0 , o2 = 0 ;
139+ while (fgets (buf , sizeof (buf ), fp ) != NULL ) {
140+ if (!o1 && strstr (buf , " dma_buf_move_notify\n" )) o1 = 1 ;
141+ if (!o2 && strstr (buf , " pci_p2pdma" )) o2 = 1 ;
142+ if (o1 && o2 ) break ;
143+ }
144+ fclose (fp );
145+ dmabuf_supported = (o1 && o2 );
114146 }
115147 }
116- fclose (fp );
117148
118149 if (dmabuf_supported == 0 ) {
150+ printf ("could not verify dmabuf support from kernel "
151+ "config \n" );
119152 return FAILURE ;
120153 }
121154 }
0 commit comments