Skip to content

Commit 981e45c

Browse files
committed
pyverbs: Store action refs to fix cleanup errors
Store strong references to action objects in DrRule and DrActionDestAttr to prevent Python garbage collection while C-level objects still reference them. Signed-off-by: Shachar Kagan <skagan@nvidia.com>
1 parent 343d7de commit 981e45c

4 files changed

Lines changed: 19 additions & 6 deletions

File tree

pyverbs/providers/mlx5/dr_action.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ cdef class DrActionPopVLan(DrAction):
4343

4444
cdef class DrActionDestAttr(PyverbsCM):
4545
cdef DrAction dest
46+
cdef DrAction reformat
4647
cdef dv.mlx5dv_dr_action_dest_attr *action_dest_attr
4748
cdef dv.mlx5dv_dr_action_dest_reformat *dest_reformat
4849

pyverbs/providers/mlx5/dr_action.pyx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,15 @@ cdef class DrActionDestAttr(PyverbsCM):
216216
super().__init__()
217217
self.dest_reformat = NULL
218218
self.action_dest_attr = NULL
219-
if action_type == mlx5dv_dr_action_dest_type.MLX5DV_DR_ACTION_DEST:
220-
self.action_dest_attr = <dv.mlx5dv_dr_action_dest_attr *> calloc(
219+
self.dest = dest
220+
self.reformat = None
221+
self.action_dest_attr = <dv.mlx5dv_dr_action_dest_attr *> calloc(
221222
1, sizeof(dv.mlx5dv_dr_action_dest_attr))
222-
if self.action_dest_attr == NULL:
223-
raise PyverbsRDMAErrno('Memory allocation for DrActionDestAttr failed.')
224-
self.action_dest_attr.type = action_type
223+
if self.action_dest_attr == NULL:
224+
raise PyverbsRDMAErrno('Memory allocation for DrActionDestAttr failed.')
225+
self.action_dest_attr.type = action_type
226+
if action_type == mlx5dv_dr_action_dest_type.MLX5DV_DR_ACTION_DEST:
225227
self.action_dest_attr.dest = dest.action
226-
self.dest = dest
227228
elif action_type == mlx5dv_dr_action_dest_type.MLX5DV_DR_ACTION_DEST_REFORMAT:
228229
self.dest_reformat = <dv.mlx5dv_dr_action_dest_reformat *> calloc(
229230
1, sizeof(dv.mlx5dv_dr_action_dest_reformat))
@@ -232,6 +233,7 @@ cdef class DrActionDestAttr(PyverbsCM):
232233
self.action_dest_attr.dest_reformat = self.dest_reformat
233234
self.action_dest_attr.dest_reformat.reformat = reformat.action
234235
self.action_dest_attr.dest_reformat.dest = dest.action
236+
self.reformat = reformat
235237
else:
236238
raise PyverbsError('Unsupported action type is provided.')
237239

@@ -248,6 +250,8 @@ cdef class DrActionDestAttr(PyverbsCM):
248250
if self.dest_reformat != NULL:
249251
free(self.dest_reformat)
250252
self.dest_reformat = NULL
253+
self.dest = None
254+
self.reformat = None
251255

252256

253257
cdef class DrActionDestArray(DrAction):
@@ -280,6 +284,11 @@ cdef class DrActionDestArray(DrAction):
280284
raise PyverbsRDMAErrno('DrActionDestArray creation failed.')
281285
free(ptr_list)
282286
domain.dr_actions.add(self)
287+
for action in dest_actions:
288+
temp_attr = <DrActionDestAttr>action
289+
temp_attr.dest.add_ref(self)
290+
if temp_attr.reformat:
291+
temp_attr.reformat.add_ref(self)
283292

284293
def __dealloc__(self):
285294
self.close()

pyverbs/providers/mlx5/dr_rule.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ from pyverbs.base cimport PyverbsCM
99
cdef class DrRule(PyverbsCM):
1010
cdef dv.mlx5dv_dr_rule *rule
1111
cdef object dr_matcher
12+
cdef object actions

pyverbs/providers/mlx5/dr_rule.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ cdef class DrRule(PyverbsCM):
3535
raise PyverbsRDMAErrno('DrRule creation failed.')
3636
for i in range(0, len(actions)):
3737
(<DrAction>actions[i]).add_ref(self)
38+
self.actions = actions
3839
matcher.add_ref(self)
3940
self.dr_matcher = matcher
4041

@@ -50,3 +51,4 @@ cdef class DrRule(PyverbsCM):
5051
raise PyverbsRDMAError('Failed to destroy DrRule.', rc)
5152
self.rule = NULL
5253
self.dr_matcher = None
54+
self.actions = None

0 commit comments

Comments
 (0)