Skip to content

Commit 0972c7d

Browse files
jgunthorpeawilliam
authored andcommitted
vfio/ccw: Use functions for alloc/free of the vfio_ccw_private
Makes the code easier to understand what is memory lifecycle and what is other stuff. Reviewed-by: Eric Farman <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent d0a9329 commit 0972c7d

File tree

1 file changed

+66
-47
lines changed

1 file changed

+66
-47
lines changed

drivers/s390/cio/vfio_ccw_drv.c

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -137,77 +137,107 @@ static void vfio_ccw_sch_irq(struct subchannel *sch)
137137
vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_INTERRUPT);
138138
}
139139

140-
static void vfio_ccw_free_regions(struct vfio_ccw_private *private)
140+
static struct vfio_ccw_private *vfio_ccw_alloc_private(struct subchannel *sch)
141141
{
142-
if (private->crw_region)
143-
kmem_cache_free(vfio_ccw_crw_region, private->crw_region);
144-
if (private->schib_region)
145-
kmem_cache_free(vfio_ccw_schib_region, private->schib_region);
146-
if (private->cmd_region)
147-
kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region);
148-
if (private->io_region)
149-
kmem_cache_free(vfio_ccw_io_region, private->io_region);
150-
}
151-
152-
static int vfio_ccw_sch_probe(struct subchannel *sch)
153-
{
154-
struct pmcw *pmcw = &sch->schib.pmcw;
155142
struct vfio_ccw_private *private;
156-
int ret = -ENOMEM;
157-
158-
if (pmcw->qf) {
159-
dev_warn(&sch->dev, "vfio: ccw: does not support QDIO: %s\n",
160-
dev_name(&sch->dev));
161-
return -ENODEV;
162-
}
163143

164144
private = kzalloc(sizeof(*private), GFP_KERNEL);
165145
if (!private)
166-
return -ENOMEM;
146+
return ERR_PTR(-ENOMEM);
147+
148+
private->sch = sch;
149+
mutex_init(&private->io_mutex);
150+
private->state = VFIO_CCW_STATE_NOT_OPER;
151+
INIT_LIST_HEAD(&private->crw);
152+
INIT_WORK(&private->io_work, vfio_ccw_sch_io_todo);
153+
INIT_WORK(&private->crw_work, vfio_ccw_crw_todo);
154+
atomic_set(&private->avail, 1);
167155

168156
private->cp.guest_cp = kcalloc(CCWCHAIN_LEN_MAX, sizeof(struct ccw1),
169157
GFP_KERNEL);
170158
if (!private->cp.guest_cp)
171-
goto out_free;
159+
goto out_free_private;
172160

173161
private->io_region = kmem_cache_zalloc(vfio_ccw_io_region,
174162
GFP_KERNEL | GFP_DMA);
175163
if (!private->io_region)
176-
goto out_free;
164+
goto out_free_cp;
177165

178166
private->cmd_region = kmem_cache_zalloc(vfio_ccw_cmd_region,
179167
GFP_KERNEL | GFP_DMA);
180168
if (!private->cmd_region)
181-
goto out_free;
169+
goto out_free_io;
182170

183171
private->schib_region = kmem_cache_zalloc(vfio_ccw_schib_region,
184172
GFP_KERNEL | GFP_DMA);
185173

186174
if (!private->schib_region)
187-
goto out_free;
175+
goto out_free_cmd;
188176

189177
private->crw_region = kmem_cache_zalloc(vfio_ccw_crw_region,
190178
GFP_KERNEL | GFP_DMA);
191179

192180
if (!private->crw_region)
193-
goto out_free;
181+
goto out_free_schib;
182+
return private;
183+
184+
out_free_schib:
185+
kmem_cache_free(vfio_ccw_schib_region, private->schib_region);
186+
out_free_cmd:
187+
kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region);
188+
out_free_io:
189+
kmem_cache_free(vfio_ccw_io_region, private->io_region);
190+
out_free_cp:
191+
kfree(private->cp.guest_cp);
192+
out_free_private:
193+
mutex_destroy(&private->io_mutex);
194+
kfree(private);
195+
return ERR_PTR(-ENOMEM);
196+
}
197+
198+
static void vfio_ccw_free_private(struct vfio_ccw_private *private)
199+
{
200+
struct vfio_ccw_crw *crw, *temp;
201+
202+
list_for_each_entry_safe(crw, temp, &private->crw, next) {
203+
list_del(&crw->next);
204+
kfree(crw);
205+
}
206+
207+
kmem_cache_free(vfio_ccw_crw_region, private->crw_region);
208+
kmem_cache_free(vfio_ccw_schib_region, private->schib_region);
209+
kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region);
210+
kmem_cache_free(vfio_ccw_io_region, private->io_region);
211+
kfree(private->cp.guest_cp);
212+
mutex_destroy(&private->io_mutex);
213+
kfree(private);
214+
}
215+
216+
static int vfio_ccw_sch_probe(struct subchannel *sch)
217+
{
218+
struct pmcw *pmcw = &sch->schib.pmcw;
219+
struct vfio_ccw_private *private;
220+
int ret = -ENOMEM;
221+
222+
if (pmcw->qf) {
223+
dev_warn(&sch->dev, "vfio: ccw: does not support QDIO: %s\n",
224+
dev_name(&sch->dev));
225+
return -ENODEV;
226+
}
227+
228+
private = vfio_ccw_alloc_private(sch);
229+
if (IS_ERR(private))
230+
return PTR_ERR(private);
194231

195-
private->sch = sch;
196232
dev_set_drvdata(&sch->dev, private);
197-
mutex_init(&private->io_mutex);
198233

199234
spin_lock_irq(sch->lock);
200-
private->state = VFIO_CCW_STATE_NOT_OPER;
201235
sch->isc = VFIO_CCW_ISC;
202236
ret = cio_enable_subchannel(sch, (u32)(unsigned long)sch);
203237
spin_unlock_irq(sch->lock);
204238
if (ret)
205239
goto out_free;
206240

207-
INIT_LIST_HEAD(&private->crw);
208-
INIT_WORK(&private->io_work, vfio_ccw_sch_io_todo);
209-
INIT_WORK(&private->crw_work, vfio_ccw_crw_todo);
210-
atomic_set(&private->avail, 1);
211241
private->state = VFIO_CCW_STATE_STANDBY;
212242

213243
ret = vfio_ccw_mdev_reg(sch);
@@ -228,31 +258,20 @@ static int vfio_ccw_sch_probe(struct subchannel *sch)
228258
cio_disable_subchannel(sch);
229259
out_free:
230260
dev_set_drvdata(&sch->dev, NULL);
231-
vfio_ccw_free_regions(private);
232-
kfree(private->cp.guest_cp);
233-
kfree(private);
261+
vfio_ccw_free_private(private);
234262
return ret;
235263
}
236264

237265
static void vfio_ccw_sch_remove(struct subchannel *sch)
238266
{
239267
struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
240-
struct vfio_ccw_crw *crw, *temp;
241268

242269
vfio_ccw_sch_quiesce(sch);
243-
244-
list_for_each_entry_safe(crw, temp, &private->crw, next) {
245-
list_del(&crw->next);
246-
kfree(crw);
247-
}
248-
249270
vfio_ccw_mdev_unreg(sch);
250271

251272
dev_set_drvdata(&sch->dev, NULL);
252273

253-
vfio_ccw_free_regions(private);
254-
kfree(private->cp.guest_cp);
255-
kfree(private);
274+
vfio_ccw_free_private(private);
256275

257276
VFIO_CCW_MSG_EVENT(4, "unbound from subchannel %x.%x.%04x\n",
258277
sch->schid.cssid, sch->schid.ssid,

0 commit comments

Comments
 (0)