162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2016 Parav Pandit <pandit.parav@gmail.com>
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include "core_priv.h"
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci/**
962306a36Sopenharmony_ci * ib_device_register_rdmacg - register with rdma cgroup.
1062306a36Sopenharmony_ci * @device: device to register to participate in resource
1162306a36Sopenharmony_ci *          accounting by rdma cgroup.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * Register with the rdma cgroup. Should be called before
1462306a36Sopenharmony_ci * exposing rdma device to user space applications to avoid
1562306a36Sopenharmony_ci * resource accounting leak.
1662306a36Sopenharmony_ci */
1762306a36Sopenharmony_civoid ib_device_register_rdmacg(struct ib_device *device)
1862306a36Sopenharmony_ci{
1962306a36Sopenharmony_ci	device->cg_device.name = device->name;
2062306a36Sopenharmony_ci	rdmacg_register_device(&device->cg_device);
2162306a36Sopenharmony_ci}
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/**
2462306a36Sopenharmony_ci * ib_device_unregister_rdmacg - unregister with rdma cgroup.
2562306a36Sopenharmony_ci * @device: device to unregister.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * Unregister with the rdma cgroup. Should be called after
2862306a36Sopenharmony_ci * all the resources are deallocated, and after a stage when any
2962306a36Sopenharmony_ci * other resource allocation by user application cannot be done
3062306a36Sopenharmony_ci * for this device to avoid any leak in accounting.
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_civoid ib_device_unregister_rdmacg(struct ib_device *device)
3362306a36Sopenharmony_ci{
3462306a36Sopenharmony_ci	rdmacg_unregister_device(&device->cg_device);
3562306a36Sopenharmony_ci}
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ciint ib_rdmacg_try_charge(struct ib_rdmacg_object *cg_obj,
3862306a36Sopenharmony_ci			 struct ib_device *device,
3962306a36Sopenharmony_ci			 enum rdmacg_resource_type resource_index)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	return rdmacg_try_charge(&cg_obj->cg, &device->cg_device,
4262306a36Sopenharmony_ci				 resource_index);
4362306a36Sopenharmony_ci}
4462306a36Sopenharmony_ciEXPORT_SYMBOL(ib_rdmacg_try_charge);
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_civoid ib_rdmacg_uncharge(struct ib_rdmacg_object *cg_obj,
4762306a36Sopenharmony_ci			struct ib_device *device,
4862306a36Sopenharmony_ci			enum rdmacg_resource_type resource_index)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	rdmacg_uncharge(cg_obj->cg, &device->cg_device,
5162306a36Sopenharmony_ci			resource_index);
5262306a36Sopenharmony_ci}
5362306a36Sopenharmony_ciEXPORT_SYMBOL(ib_rdmacg_uncharge);
54