Commit 64ae7445 authored by Sven Eckelmann's avatar Sven Eckelmann Committed by Antonio Quartulli

batman-adv: Split batadv_iv_ogm_orig_del_if function

batadv_iv_ogm_orig_del_if handles two different buffers bcast_own and
bcast_own_sum which should be resized. The error handling two for
allocating these buffers causes the complexity of this function. This can
be avoided completely when the function is split into a main function
handling the locking, freeing and call of the subfunctions.

The subfunction can then independently handle the resize of the buffers.
This also allows to easily reuse the old buffer (which always is larger) in
case a smaller buffer could not be allocated without increasing the code
complexity.
Signed-off-by: default avatarSven Eckelmann <sven@narfation.org>
Signed-off-by: default avatarMarek Lindner <mareklindner@neomailbox.ch>
Signed-off-by: default avatarAntonio Quartulli <a@unstable.cc>
parent 86de37c1
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <linux/jiffies.h> #include <linux/jiffies.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/kref.h> #include <linux/kref.h>
#include <linux/lockdep.h>
#include <linux/netdevice.h> #include <linux/netdevice.h>
#include <linux/pkt_sched.h> #include <linux/pkt_sched.h>
#include <linux/printk.h> #include <linux/printk.h>
...@@ -175,71 +176,107 @@ static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node, ...@@ -175,71 +176,107 @@ static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
} }
/** /**
* batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own
* exclude the removed interface
* @orig_node: the orig_node that has to be changed * @orig_node: the orig_node that has to be changed
* @max_if_num: the current amount of interfaces * @max_if_num: the current amount of interfaces
* @del_if_num: the index of the interface being removed * @del_if_num: the index of the interface being removed
*
* Return: 0 on success, a negative error code otherwise.
*/ */
static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node, static void
int max_if_num, int del_if_num) batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
int max_if_num, int del_if_num)
{ {
int ret = -ENOMEM; size_t chunk_size;
size_t chunk_size, if_offset; size_t if_offset;
void *data_ptr = NULL; void *data_ptr;
spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
/* last interface was removed */ lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
if (max_if_num == 0)
goto free_bcast_own;
chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS; chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC); data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
if (!data_ptr) if (!data_ptr)
goto unlock; /* use old buffer when new one could not be allocated */
data_ptr = orig_node->bat_iv.bcast_own;
/* copy first part */ /* copy first part */
memcpy(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size); memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
/* copy second part */ /* copy second part */
if_offset = (del_if_num + 1) * chunk_size; if_offset = (del_if_num + 1) * chunk_size;
memcpy((char *)data_ptr + del_if_num * chunk_size, memmove((char *)data_ptr + del_if_num * chunk_size,
(uint8_t *)orig_node->bat_iv.bcast_own + if_offset, (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
(max_if_num - del_if_num) * chunk_size); (max_if_num - del_if_num) * chunk_size);
free_bcast_own: /* bcast_own was shrunk down in new buffer; free old one */
kfree(orig_node->bat_iv.bcast_own); if (orig_node->bat_iv.bcast_own != data_ptr) {
orig_node->bat_iv.bcast_own = data_ptr; kfree(orig_node->bat_iv.bcast_own);
orig_node->bat_iv.bcast_own = data_ptr;
}
}
/**
* batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum
* @orig_node: the orig_node that has to be changed
* @max_if_num: the current amount of interfaces
* @del_if_num: the index of the interface being removed
*/
static void
batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
int max_if_num, int del_if_num)
{
size_t if_offset;
void *data_ptr;
if (max_if_num == 0) lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
goto free_own_sum;
data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC); data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
if (!data_ptr) { if (!data_ptr)
kfree(orig_node->bat_iv.bcast_own); /* use old buffer when new one could not be allocated */
goto unlock; data_ptr = orig_node->bat_iv.bcast_own_sum;
}
memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum, memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
del_if_num * sizeof(u8)); del_if_num * sizeof(u8));
if_offset = (del_if_num + 1) * sizeof(u8); if_offset = (del_if_num + 1) * sizeof(u8);
memcpy((char *)data_ptr + del_if_num * sizeof(u8), memmove((char *)data_ptr + del_if_num * sizeof(u8),
orig_node->bat_iv.bcast_own_sum + if_offset, orig_node->bat_iv.bcast_own_sum + if_offset,
(max_if_num - del_if_num) * sizeof(u8)); (max_if_num - del_if_num) * sizeof(u8));
/* bcast_own_sum was shrunk down in new buffer; free old one */
if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
kfree(orig_node->bat_iv.bcast_own_sum);
orig_node->bat_iv.bcast_own_sum = data_ptr;
}
}
free_own_sum: /**
kfree(orig_node->bat_iv.bcast_own_sum); * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
orig_node->bat_iv.bcast_own_sum = data_ptr; * exclude the removed interface
* @orig_node: the orig_node that has to be changed
* @max_if_num: the current amount of interfaces
* @del_if_num: the index of the interface being removed
*
* Return: 0 on success, a negative error code otherwise.
*/
static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
int max_if_num, int del_if_num)
{
spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
if (max_if_num == 0) {
kfree(orig_node->bat_iv.bcast_own);
kfree(orig_node->bat_iv.bcast_own_sum);
orig_node->bat_iv.bcast_own = NULL;
orig_node->bat_iv.bcast_own_sum = NULL;
} else {
batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
del_if_num);
batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
del_if_num);
}
ret = 0;
unlock:
spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
return ret; return 0;
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment