+int devpts_new_index(void)
+{
+ int index;
+ int ida_ret;
+
+retry:
+ if (!ida_pre_get(&allocated_ptys, GFP_KERNEL)) {
+ return -ENOMEM;
+ }
+
+ mutex_lock(&allocated_ptys_lock);
+ ida_ret = ida_get_new(&allocated_ptys, &index);
+ if (ida_ret < 0) {
+ mutex_unlock(&allocated_ptys_lock);
+ if (ida_ret == -EAGAIN)
+ goto retry;
+ return -EIO;
+ }
+
+ if (index >= pty_limit) {
+ ida_remove(&allocated_ptys, index);
+ mutex_unlock(&allocated_ptys_lock);
+ return -EIO;
+ }
+ mutex_unlock(&allocated_ptys_lock);
+ return index;
+}
+
+void devpts_kill_index(int idx)
+{
+ mutex_lock(&allocated_ptys_lock);
+ ida_remove(&allocated_ptys, idx);
+ mutex_unlock(&allocated_ptys_lock);
+}
+