Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/drm_vma_manager: Add drm_vma_node_allow_once()

Currently there is no easy way for a drm driver to safely check and allow
drm_vma_offset_node for a drm file just once. Allow drm drivers to call
non-refcounted version of drm_vma_node_allow() so that a driver doesn't
need to keep track of each drm_vma_node_allow() to call subsequent
drm_vma_node_revoke() to prevent memory leak.

Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: David Airlie <airlied@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>
Cc: Andi Shyti <andi.shyti@linux.intel.com>

Suggested-by: Chris Wilson <chris.p.wilson@intel.com>
Signed-off-by: Nirmoy Das <nirmoy.das@intel.com>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Reviewed-by: Andi Shyti <andi.shyti@linux.intel.com>
Link: https://lore.kernel.org/r/20230117175236.22317-1-nirmoy.das@intel.com
Signed-off-by: Maxime Ripard <maxime@cerno.tech>

authored by

Nirmoy Das and committed by
Maxime Ripard
899d3a3c 2293a73a

+55 -22
+54 -22
drivers/gpu/drm/drm_vma_manager.c
··· 240 240 } 241 241 EXPORT_SYMBOL(drm_vma_offset_remove); 242 242 243 - /** 244 - * drm_vma_node_allow - Add open-file to list of allowed users 245 - * @node: Node to modify 246 - * @tag: Tag of file to remove 247 - * 248 - * Add @tag to the list of allowed open-files for this node. If @tag is 249 - * already on this list, the ref-count is incremented. 250 - * 251 - * The list of allowed-users is preserved across drm_vma_offset_add() and 252 - * drm_vma_offset_remove() calls. You may even call it if the node is currently 253 - * not added to any offset-manager. 254 - * 255 - * You must remove all open-files the same number of times as you added them 256 - * before destroying the node. Otherwise, you will leak memory. 257 - * 258 - * This is locked against concurrent access internally. 259 - * 260 - * RETURNS: 261 - * 0 on success, negative error code on internal failure (out-of-mem) 262 - */ 263 - int drm_vma_node_allow(struct drm_vma_offset_node *node, struct drm_file *tag) 243 + static int vma_node_allow(struct drm_vma_offset_node *node, 244 + struct drm_file *tag, bool ref_counted) 264 245 { 265 246 struct rb_node **iter; 266 247 struct rb_node *parent = NULL; ··· 263 282 entry = rb_entry(*iter, struct drm_vma_offset_file, vm_rb); 264 283 265 284 if (tag == entry->vm_tag) { 266 - entry->vm_count++; 285 + if (ref_counted) 286 + entry->vm_count++; 267 287 goto unlock; 268 288 } else if (tag > entry->vm_tag) { 269 289 iter = &(*iter)->rb_right; ··· 289 307 kfree(new); 290 308 return ret; 291 309 } 310 + 311 + /** 312 + * drm_vma_node_allow - Add open-file to list of allowed users 313 + * @node: Node to modify 314 + * @tag: Tag of file to remove 315 + * 316 + * Add @tag to the list of allowed open-files for this node. If @tag is 317 + * already on this list, the ref-count is incremented. 318 + * 319 + * The list of allowed-users is preserved across drm_vma_offset_add() and 320 + * drm_vma_offset_remove() calls. You may even call it if the node is currently 321 + * not added to any offset-manager. 322 + * 323 + * You must remove all open-files the same number of times as you added them 324 + * before destroying the node. Otherwise, you will leak memory. 325 + * 326 + * This is locked against concurrent access internally. 327 + * 328 + * RETURNS: 329 + * 0 on success, negative error code on internal failure (out-of-mem) 330 + */ 331 + int drm_vma_node_allow(struct drm_vma_offset_node *node, struct drm_file *tag) 332 + { 333 + return vma_node_allow(node, tag, true); 334 + } 292 335 EXPORT_SYMBOL(drm_vma_node_allow); 336 + 337 + /** 338 + * drm_vma_node_allow_once - Add open-file to list of allowed users 339 + * @node: Node to modify 340 + * @tag: Tag of file to remove 341 + * 342 + * Add @tag to the list of allowed open-files for this node. 343 + * 344 + * The list of allowed-users is preserved across drm_vma_offset_add() and 345 + * drm_vma_offset_remove() calls. You may even call it if the node is currently 346 + * not added to any offset-manager. 347 + * 348 + * This is not ref-counted unlike drm_vma_node_allow() hence drm_vma_node_revoke() 349 + * should only be called once after this. 350 + * 351 + * This is locked against concurrent access internally. 352 + * 353 + * RETURNS: 354 + * 0 on success, negative error code on internal failure (out-of-mem) 355 + */ 356 + int drm_vma_node_allow_once(struct drm_vma_offset_node *node, struct drm_file *tag) 357 + { 358 + return vma_node_allow(node, tag, false); 359 + } 360 + EXPORT_SYMBOL(drm_vma_node_allow_once); 293 361 294 362 /** 295 363 * drm_vma_node_revoke - Remove open-file from list of allowed users
+1
include/drm/drm_vma_manager.h
··· 74 74 struct drm_vma_offset_node *node); 75 75 76 76 int drm_vma_node_allow(struct drm_vma_offset_node *node, struct drm_file *tag); 77 + int drm_vma_node_allow_once(struct drm_vma_offset_node *node, struct drm_file *tag); 77 78 void drm_vma_node_revoke(struct drm_vma_offset_node *node, 78 79 struct drm_file *tag); 79 80 bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,