@@ -127,15 +127,23 @@ static void xen_update_blkif_status(struct xen_blkif *blkif)
127127 return ;
128128}
129129
130- static int xen_blkif_alloc_rings (struct xen_blkif * blkif )
130+ static int xen_blkif_alloc_rings (struct xen_blkif * blkif ,
131+ unsigned int nr_rings )
131132{
132133 unsigned int r ;
134+ struct xen_blkif_ring * rings ;
133135
134- blkif -> rings = kcalloc (blkif -> nr_rings , sizeof (struct xen_blkif_ring ),
135- GFP_KERNEL );
136- if (!blkif -> rings )
136+ rings = kcalloc (nr_rings , sizeof (struct xen_blkif_ring ), GFP_KERNEL );
137+ if (!rings )
137138 return - ENOMEM ;
138139
140+ write_lock (& blkif -> rings_lock );
141+
142+ blkif -> rings = rings ;
143+ blkif -> nr_rings = nr_rings ;
144+
145+ write_unlock (& blkif -> rings_lock );
146+
139147 for (r = 0 ; r < blkif -> nr_rings ; r ++ ) {
140148 struct xen_blkif_ring * ring = & blkif -> rings [r ];
141149
@@ -175,6 +183,7 @@ static struct xen_blkif *xen_blkif_alloc(domid_t domid)
175183 blkif -> domid = domid ;
176184 atomic_set (& blkif -> refcnt , 1 );
177185 init_completion (& blkif -> drain_complete );
186+ rwlock_init (& blkif -> rings_lock );
178187
179188 /*
180189 * Because freeing back to the cache may be deferred, it is not
@@ -269,6 +278,7 @@ static int xen_blkif_disconnect(struct xen_blkif *blkif)
269278 struct pending_req * req , * n ;
270279 unsigned int j , r ;
271280 bool busy = false;
281+ struct xen_blkif_ring * rings ;
272282
273283 for (r = 0 ; r < blkif -> nr_rings ; r ++ ) {
274284 struct xen_blkif_ring * ring = & blkif -> rings [r ];
@@ -335,10 +345,16 @@ static int xen_blkif_disconnect(struct xen_blkif *blkif)
335345 * blkif->rings was allocated in connect_ring, so we should free it in
336346 * here.
337347 */
338- kfree (blkif -> rings );
348+ write_lock (& blkif -> rings_lock );
349+
350+ rings = blkif -> rings ;
339351 blkif -> rings = NULL ;
340352 blkif -> nr_rings = 0 ;
341353
354+ write_unlock (& blkif -> rings_lock );
355+
356+ kfree (rings );
357+
342358 return 0 ;
343359}
344360
@@ -386,6 +402,7 @@ void xen_blkif_interface_fini(void)
386402 unsigned int i; \
387403 unsigned long long result = 0; \
388404 \
405+ read_lock(&blkif->rings_lock); \
389406 if (!blkif->rings) \
390407 goto out; \
391408 \
@@ -396,6 +413,7 @@ void xen_blkif_interface_fini(void)
396413 } \
397414 \
398415out: \
416+ read_unlock(&blkif->rings_lock); \
399417 return sprintf(buf, format, result); \
400418 } \
401419 static DEVICE_ATTR(name, 0444, show_##name, NULL)
@@ -1104,8 +1122,7 @@ static int connect_ring(struct backend_info *be)
11041122 requested_num_queues , xenblk_max_queues );
11051123 return - ENOSYS ;
11061124 }
1107- blkif -> nr_rings = requested_num_queues ;
1108- if (xen_blkif_alloc_rings (blkif ))
1125+ if (xen_blkif_alloc_rings (blkif , requested_num_queues ))
11091126 return - ENOMEM ;
11101127
11111128 pr_info ("%s: using %d queues, protocol %d (%s) %s\n" , dev -> nodename ,
0 commit comments