From 1c055e55a242a33e574e48be530e06770a210dcd Mon Sep 17 00:00:00 2001
From: hc <hc@nodka.com>
Date: Mon, 19 Feb 2024 03:26:26 +0000
Subject: [PATCH] add r8169 read mac form eeprom

---
 kernel/drivers/dma-buf/dma-heap.c |   59 ++++++++++++++++++++++++++---------------------------------
 1 files changed, 26 insertions(+), 33 deletions(-)

diff --git a/kernel/drivers/dma-buf/dma-heap.c b/kernel/drivers/dma-buf/dma-heap.c
index 87f0b06..004f960 100644
--- a/kernel/drivers/dma-buf/dma-heap.c
+++ b/kernel/drivers/dma-buf/dma-heap.c
@@ -11,6 +11,7 @@
 #include <linux/device.h>
 #include <linux/dma-buf.h>
 #include <linux/err.h>
+#include <linux/xarray.h>
 #include <linux/list.h>
 #include <linux/slab.h>
 #include <linux/nospec.h>
@@ -49,9 +50,7 @@
 static DEFINE_MUTEX(heap_list_lock);
 static dev_t dma_heap_devt;
 static struct class *dma_heap_class;
-
-static DEFINE_MUTEX(heap_devnode_lock);
-static DECLARE_BITMAP(heap_devnode_nums, NUM_HEAP_MINORS);
+static DEFINE_XARRAY_ALLOC(dma_heap_minors);
 
 struct dma_heap *dma_heap_find(const char *name)
 {
@@ -124,14 +123,11 @@
 {
 	struct dma_heap *heap;
 
-	mutex_lock(&heap_devnode_lock);
-	heap = container_of(inode->i_cdev, struct dma_heap, heap_cdev);
+	heap = xa_load(&dma_heap_minors, iminor(inode));
 	if (!heap) {
 		pr_err("dma_heap: minor %d unknown.\n", iminor(inode));
-		mutex_unlock(&heap_devnode_lock);
 		return -ENODEV;
 	}
-	mutex_unlock(&heap_devnode_lock);
 
 	/* instance data as context */
 	file->private_data = heap;
@@ -273,9 +269,7 @@
 
 	device_destroy(dma_heap_class, heap->heap_devt);
 	cdev_del(&heap->heap_cdev);
-	mutex_lock(&heap_devnode_lock);
-	clear_bit(minor, heap_devnode_nums);
-	mutex_unlock(&heap_devnode_lock);
+	xa_erase(&dma_heap_minors, minor);
 
 	kfree(heap);
 }
@@ -320,7 +314,7 @@
 
 struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
 {
-	struct dma_heap *heap, *err_ret;
+	struct dma_heap *heap, *h, *err_ret;
 	unsigned int minor;
 	int ret;
 
@@ -334,15 +328,6 @@
 		return ERR_PTR(-EINVAL);
 	}
 
-	/* check the name is unique */
-	heap = dma_heap_find(exp_info->name);
-	if (heap) {
-		pr_err("dma_heap: Already registered heap named %s\n",
-		       exp_info->name);
-		dma_heap_put(heap);
-		return ERR_PTR(-EINVAL);
-	}
-
 	heap = kzalloc(sizeof(*heap), GFP_KERNEL);
 	if (!heap)
 		return ERR_PTR(-ENOMEM);
@@ -352,18 +337,14 @@
 	heap->ops = exp_info->ops;
 	heap->priv = exp_info->priv;
 
-	/* Part 1: Find a free minor number */
-
-	mutex_lock(&heap_devnode_lock);
-	minor = find_next_zero_bit(heap_devnode_nums, NUM_HEAP_MINORS, 0);
-	if (minor == NUM_HEAP_MINORS) {
-		mutex_unlock(&heap_devnode_lock);
+	/* Find unused minor number */
+	ret = xa_alloc(&dma_heap_minors, &minor, heap,
+		       XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL);
+	if (ret < 0) {
 		pr_err("dma_heap: Unable to get minor number for heap\n");
-		err_ret = ERR_PTR(-ENFILE);
+		err_ret = ERR_PTR(ret);
 		goto err0;
 	}
-	set_bit(minor, heap_devnode_nums);
-	mutex_unlock(&heap_devnode_lock);
 
 	/* Create device */
 	heap->heap_devt = MKDEV(MAJOR(dma_heap_devt), minor);
@@ -390,19 +371,31 @@
 	/* Make sure it doesn't disappear on us */
 	heap->heap_dev = get_device(heap->heap_dev);
 
-	/* Add heap to the list */
 	mutex_lock(&heap_list_lock);
+	/* check the name is unique */
+	list_for_each_entry(h, &heap_list, list) {
+		if (!strcmp(h->name, exp_info->name)) {
+			mutex_unlock(&heap_list_lock);
+			pr_err("dma_heap: Already registered heap named %s\n",
+			       exp_info->name);
+			err_ret = ERR_PTR(-EINVAL);
+			put_device(heap->heap_dev);
+			goto err3;
+		}
+	}
+
+	/* Add heap to the list */
 	list_add(&heap->list, &heap_list);
 	mutex_unlock(&heap_list_lock);
 
 	return heap;
 
+err3:
+	device_destroy(dma_heap_class, heap->heap_devt);
 err2:
 	cdev_del(&heap->heap_cdev);
 err1:
-	mutex_lock(&heap_devnode_lock);
-	clear_bit(minor, heap_devnode_nums);
-	mutex_unlock(&heap_devnode_lock);
+	xa_erase(&dma_heap_minors, minor);
 err0:
 	kfree(heap);
 	return err_ret;

--
Gitblit v1.6.2