Re: [PATCH v6 2/2] tcmu: Add global data block pool support

2017-05-01 Thread Nicholas A. Bellinger
On Mon, 2017-05-01 at 13:40 -0500, Mike Christie wrote:
> On 04/30/2017 06:29 AM, Xiubo Li wrote:
> > [...]



> >> To avoid starvation, I think you want a second list/fifo that holds the
> >> watiers. In tcmu_get_empty_block if the list is not empty, record how
> >> many pages we needed and then add the device to the list and wait in
> >> tcmu_queue_cmd_ring.
> >>
> >> Above if we freed enough pages for the device at head then wake up the
> >> device.
> >>
> >> I think you also need a wake_up call in the completion path in case the
> >> initial call could not free enough pages. It could probably check if the
> >> completion was going to free enough pages for a waiter and then call
> >> wake.
> >>
> > Yes, I meant to introduce this later after this series to not let the
> > patches too
> > complex to review.
> > 
> > If you agree I will do this later, or in V7 series ?
> 
> 
> Yeah, I am ok with adding it after the initial patches go in.

Btw, adding your Acked-by for the initial merge of these.

If that's a problem, please make some noise.  ;)



Re: [PATCH v6 2/2] tcmu: Add global data block pool support

2017-05-01 Thread Mike Christie
On 04/30/2017 06:29 AM, Xiubo Li wrote:
> [...]
>>> +static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev,
>>> uint32_t dbi)
>>> +{
>>> +struct page *page;
>>> +int ret;
>>> +
>>> +mutex_lock(>cmdr_lock);
>>> +page = tcmu_get_block_page(udev, dbi);
>>> +if (likely(page)) {
>>> +mutex_unlock(>cmdr_lock);
>>> +return page;
>>> +}
>>> +
>>> +/*
>>> + * Normally it shouldn't be here:
>>> + * Only when the userspace has touched the blocks which
>>> + * are out of the tcmu_cmd's data iov[], and will return
>>> + * one zeroed page.
>>
>> Is it a userspace bug when this happens? Do you know when it is
>> occcuring?
> Since the UIO will map the whole ring buffer to the user space at the
> beginning, and the userspace is allowed and legal to access any block
> within the limits of the mapped ring area.
> 
> But actually when this happens, it normally will be one bug of the
> userspace. Without this checking the kernel will output many page fault
> dump traces.
> 
> Maybe here outputing some warning message is a good idea, and will be
> easy to debug for userspace.

Yeah.

> 
> 
> [...]
>>> @@ -1388,6 +1509,81 @@ static ssize_t tcmu_cmd_time_out_store(struct
>>> config_item *item, const char *pag
>>>   .tb_dev_attrib_attrs= NULL,
>>>   };
>>>   +static int unmap_thread_fn(void *data)
>>> +{
>>> +struct tcmu_dev *udev;
>>> +loff_t off;
>>> +uint32_t start, end, block;
>>> +struct page *page;
>>> +int i;
>>> +
>>> +while (1) {
>>> +DEFINE_WAIT(__wait);
>>> +
>>> +prepare_to_wait(_wait, &__wait, TASK_INTERRUPTIBLE);
>>> +schedule();
>>> +finish_wait(_wait, &__wait);
>>> +
>>> +mutex_lock(_udev_mutex);
>>> +list_for_each_entry(udev, _udev, node) {
>>> +mutex_lock(>cmdr_lock);
>>> +
>>> +/* Try to complete the finished commands first */
>>> +tcmu_handle_completions(udev);
>>> +
>>> +/* Skip the udevs waiting the global pool or in idle */
>>> +if (udev->waiting_global || !udev->dbi_thresh) {
>>> +mutex_unlock(>cmdr_lock);
>>> +continue;
>>> +}
>>> +
>>> +end = udev->dbi_max + 1;
>>> +block = find_last_bit(udev->data_bitmap, end);
>>> +if (block == udev->dbi_max) {
>>> +/*
>>> + * The last bit is dbi_max, so there is
>>> + * no need to shrink any blocks.
>>> + */
>>> +mutex_unlock(>cmdr_lock);
>>> +continue;
>>> +} else if (block == end) {
>>> +/* The current udev will goto idle state */
>>> +udev->dbi_thresh = start = 0;
>>> +udev->dbi_max = 0;
>>> +} else {
>>> +udev->dbi_thresh = start = block + 1;
>>> +udev->dbi_max = block;
>>> +}
>>> +
>>> +/* Here will truncate the data area from off */
>>> +off = udev->data_off + start * DATA_BLOCK_SIZE;
>>> +unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
>>> +
>>> +/* Release the block pages */
>>> +for (i = start; i < end; i++) {
>>> +page = radix_tree_delete(>data_blocks, i);
>>> +if (page) {
>>> +__free_page(page);
>>> +atomic_dec(_db_count);
>>> +}
>>> +}
>>> +mutex_unlock(>cmdr_lock);
>>> +}
>>> +
>>> +/*
>>> + * Try to wake up the udevs who are waiting
>>> + * for the global data pool.
>>> + */
>>> +list_for_each_entry(udev, _udev, node) {
>>> +if (udev->waiting_global)
>>> +wake_up(>wait_cmdr);
>>> +}
>>
>> To avoid starvation, I think you want a second list/fifo that holds the
>> watiers. In tcmu_get_empty_block if the list is not empty, record how
>> many pages we needed and then add the device to the list and wait in
>> tcmu_queue_cmd_ring.
>>
>> Above if we freed enough pages for the device at head then wake up the
>> device.
>>
>> I think you also need a wake_up call in the completion path in case the
>> initial call could not free enough pages. It could probably check if the
>> completion was going to free enough pages for a waiter and then call
>> wake.
>>
> Yes, I meant to introduce this later after this series to not let the
> patches too
> complex to review.
> 
> If you agree I will do this later, or in V7 series ?


Yeah, I am ok with adding it after the initial patches go in.


Re: [PATCH v6 2/2] tcmu: Add global data block pool support

2017-04-30 Thread Xiubo Li

[...]


+
+static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
+ struct tcmu_cmd *tcmu_cmd,
+ uint32_t blocks_needed)


Can drop blocks_needed.


Will fix it.

[...]
  
-static void *tcmu_get_block_addr(struct tcmu_dev *udev, uint32_t dbi)

+static inline struct page *
+tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
  {
return radix_tree_lookup(>data_blocks, dbi);
  }
@@ -365,17 +417,20 @@ static int alloc_and_scatter_data_area(struct tcmu_dev 
*udev,


We don't actually alloc the area here any more. Could probably rename
it to be similar to gather_data_area.


Yes, will rename to scatter_data_area.

[...]



@@ -616,6 +711,7 @@ static bool is_ring_space_avail(struct tcmu_dev *udev, 
size_t cmd_size, size_t d
, _cnt, copy_to_data_area);
if (ret) {
pr_err("tcmu: alloc and scatter data failed\n");
+   mutex_unlock(>cmdr_lock);


I think here and in the error case below, you need to do a
tcmu_cmd_free_data.

Yes, good catch, and the tcmu_cmd_free_data is needed here to free the 
bits in udev->data_bitmap.

And the unlock will be added in the first patch.

[...]

+static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t 
dbi)
+{
+   struct page *page;
+   int ret;
+
+   mutex_lock(>cmdr_lock);
+   page = tcmu_get_block_page(udev, dbi);
+   if (likely(page)) {
+   mutex_unlock(>cmdr_lock);
+   return page;
+   }
+
+   /*
+* Normally it shouldn't be here:
+* Only when the userspace has touched the blocks which
+* are out of the tcmu_cmd's data iov[], and will return
+* one zeroed page.


Is it a userspace bug when this happens? Do you know when it is occcuring?
Since the UIO will map the whole ring buffer to the user space at the 
beginning, and the userspace is allowed and legal to access any block 
within the limits of the mapped ring area.


But actually when this happens, it normally will be one bug of the 
userspace. Without this checking the kernel will output many page fault 
dump traces.


Maybe here outputing some warning message is a good idea, and will be 
easy to debug for userspace.



[...]

@@ -1388,6 +1509,81 @@ static ssize_t tcmu_cmd_time_out_store(struct 
config_item *item, const char *pag
.tb_dev_attrib_attrs= NULL,
  };
  
+static int unmap_thread_fn(void *data)

+{
+   struct tcmu_dev *udev;
+   loff_t off;
+   uint32_t start, end, block;
+   struct page *page;
+   int i;
+
+   while (1) {
+   DEFINE_WAIT(__wait);
+
+   prepare_to_wait(_wait, &__wait, TASK_INTERRUPTIBLE);
+   schedule();
+   finish_wait(_wait, &__wait);
+
+   mutex_lock(_udev_mutex);
+   list_for_each_entry(udev, _udev, node) {
+   mutex_lock(>cmdr_lock);
+
+   /* Try to complete the finished commands first */
+   tcmu_handle_completions(udev);
+
+   /* Skip the udevs waiting the global pool or in idle */
+   if (udev->waiting_global || !udev->dbi_thresh) {
+   mutex_unlock(>cmdr_lock);
+   continue;
+   }
+
+   end = udev->dbi_max + 1;
+   block = find_last_bit(udev->data_bitmap, end);
+   if (block == udev->dbi_max) {
+   /*
+* The last bit is dbi_max, so there is
+* no need to shrink any blocks.
+*/
+   mutex_unlock(>cmdr_lock);
+   continue;
+   } else if (block == end) {
+   /* The current udev will goto idle state */
+   udev->dbi_thresh = start = 0;
+   udev->dbi_max = 0;
+   } else {
+   udev->dbi_thresh = start = block + 1;
+   udev->dbi_max = block;
+   }
+
+   /* Here will truncate the data area from off */
+   off = udev->data_off + start * DATA_BLOCK_SIZE;
+   unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
+
+   /* Release the block pages */
+   for (i = start; i < end; i++) {
+   page = radix_tree_delete(>data_blocks, i);
+   if (page) {
+   __free_page(page);
+   atomic_dec(_db_count);
+   }
+   }
+   mutex_unlock(>cmdr_lock);
+   }
+
+  

Re: [PATCH v6 2/2] tcmu: Add global data block pool support

2017-04-30 Thread Mike Christie
On 04/26/2017 01:25 AM, lixi...@cmss.chinamobile.com wrote:
> From: Xiubo Li 
> 
> For each target there will be one ring, when the target number
> grows larger and larger, it could eventually runs out of the
> system memories.
> 
> In this patch for each target ring, currently for the cmd area
> the size will be fixed to 8MB and for the data area the size
> will grow from 0 to max 256K * PAGE_SIZE(1G for 4K page size).
> 
> For all the targets' data areas, they will get empty blocks
> from the "global data block pool", which has limited to 512K *
> PAGE_SIZE(2G for 4K page size) for now.
> 
> When the "global data block pool" has been used up, then any
> target could wake up the unmap thread routine to shrink other
> targets' data area memories. And the unmap thread routine will
> always try to truncate the ring vma from the last using block
> offset.
> 
> When user space has touched the data blocks out of tcmu_cmd
> iov[], the tcmu_page_fault() will try to return one zeroed blocks.
> 
> Here we move the timeout's tcmu_handle_completions() into unmap
> thread routine, that's to say when the timeout fired, it will
> only do the tcmu_check_expired_cmd() and then wake up the unmap
> thread to do the completions() and then try to shrink its idle
> memories. Then the cmdr_lock could be a mutex and could simplify
> this patch because the unmap_mapping_range() or zap_* may go to
> sleep.
> 
> Signed-off-by: Xiubo Li 
> Signed-off-by: Jianfei Hu 
> ---
>  drivers/target/target_core_user.c | 446 
> --
>  1 file changed, 327 insertions(+), 119 deletions(-)
> 
> diff --git a/drivers/target/target_core_user.c 
> b/drivers/target/target_core_user.c
> index 8491752..46f5a1c 100644
> --- a/drivers/target/target_core_user.c
> +++ b/drivers/target/target_core_user.c
> @@ -31,6 +31,8 @@
>  #include 
>  #include 
>  #include 
> +#include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -67,17 +69,24 @@
>  
>  #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
>  
> -/* For cmd area, the size is fixed 2M */
> -#define CMDR_SIZE (2 * 1024 * 1024)
> +/* For cmd area, the size is fixed 8MB */
> +#define CMDR_SIZE (8 * 1024 * 1024)
>  
> -/* For data area, the size is fixed 32M */
> -#define DATA_BLOCK_BITS (8 * 1024)
> -#define DATA_BLOCK_SIZE 4096
> +/*
> + * For data area, the block size is PAGE_SIZE and
> + * the total size is 256K * PAGE_SIZE.
> + */
> +#define DATA_BLOCK_SIZE PAGE_SIZE
> +#define DATA_BLOCK_BITS (256 * 1024)
>  #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE)
> +#define DATA_BLOCK_INIT_BITS 128
>  
> -/* The ring buffer size is 34M */
> +/* The total size of the ring is 8M + 256K * PAGE_SIZE */
>  #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
>  
> +/* Default maximum of the global data blocks(512K * PAGE_SIZE) */
> +#define TCMU_GLOBAL_MAX_BLOCKS (512 * 1024)
> +
>  static struct device *tcmu_root_device;
>  
>  struct tcmu_hba {
> @@ -87,6 +96,8 @@ struct tcmu_hba {
>  #define TCMU_CONFIG_LEN 256
>  
>  struct tcmu_dev {
> + struct list_head node;
> +
>   struct se_device se_dev;
>  
>   char *name;
> @@ -98,6 +109,8 @@ struct tcmu_dev {
>  
>   struct uio_info uio_info;
>  
> + struct inode *inode;
> +
>   struct tcmu_mailbox *mb_addr;
>   size_t dev_size;
>   u32 cmdr_size;
> @@ -108,10 +121,11 @@ struct tcmu_dev {
>   size_t data_size;
>  
>   wait_queue_head_t wait_cmdr;
> - /* TODO should this be a mutex? */
> - spinlock_t cmdr_lock;
> + struct mutex cmdr_lock;
>  
> + bool waiting_global;
>   uint32_t dbi_max;
> + uint32_t dbi_thresh;
>   DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS);
>   struct radix_tree_root data_blocks;
>  
> @@ -146,6 +160,13 @@ struct tcmu_cmd {
>   unsigned long flags;
>  };
>  
> +static struct task_struct *unmap_thread;
> +static wait_queue_head_t unmap_wait;
> +static DEFINE_MUTEX(root_udev_mutex);
> +static LIST_HEAD(root_udev);
> +
> +static atomic_t global_db_count = ATOMIC_INIT(0);
> +
>  static struct kmem_cache *tcmu_cmd_cache;
>  
>  /* multicast group */
> @@ -174,48 +195,79 @@ enum tcmu_multicast_groups {
>  #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
>  #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
>  
> -static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd)
> +static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
>  {
>   struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
>   uint32_t i;
>  
> - for (i = 0; i < tcmu_cmd->dbi_cnt; i++)
> + for (i = 0; i < len; i++)
>   clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
>  }
>  
> -static int tcmu_get_empty_block(struct tcmu_dev *udev, void **addr)
> +static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
> + struct tcmu_cmd *tcmu_cmd)
>  {
> - void *p;
> - uint32_t 

Re: [PATCH v6 2/2] tcmu: Add global data block pool support

2017-04-26 Thread kbuild test robot
Hi Xiubo,

[auto build test WARNING on next-20170424]
[also build test WARNING on v4.11-rc8]
[cannot apply to v4.9-rc8 v4.9-rc7 v4.9-rc6]
[if your patch is applied to the wrong git tree, please drop us a note to help 
improve the system]

url:
https://github.com/0day-ci/linux/commits/lixiubo-cmss-chinamobile-com/tcmu-Dynamic-growing-data-area-support/20170426-162910
config: xtensa-allmodconfig (attached as .config)
compiler: xtensa-linux-gcc (GCC) 4.9.0
reproduce:
wget 
https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O 
~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=xtensa 

All warnings (new ones prefixed by >>):

   In file included from include/linux/printk.h:329:0,
from include/linux/kernel.h:13,
from include/linux/list.h:8,
from include/linux/preempt.h:10,
from include/linux/spinlock.h:50,
from drivers//target/target_core_user.c:21:
   drivers//target/target_core_user.c: In function 'is_ring_space_avail':
>> include/linux/dynamic_debug.h:74:16: warning: format '%zu' expects argument 
>> of type 'size_t', but argument 3 has type 'long unsigned int' [-Wformat=]
 static struct _ddebug  __aligned(8)   \
   ^
   include/linux/dynamic_debug.h:110:2: note: in expansion of macro 
'DEFINE_DYNAMIC_DEBUG_METADATA_KEY'
 DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, 0, 0)
 ^
   include/linux/dynamic_debug.h:124:2: note: in expansion of macro 
'DEFINE_DYNAMIC_DEBUG_METADATA'
 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);  \
 ^
   include/linux/printk.h:333:2: note: in expansion of macro 'dynamic_pr_debug'
 dynamic_pr_debug(fmt, ##__VA_ARGS__)
 ^
   drivers//target/target_core_user.c:575:4: note: in expansion of macro 
'pr_debug'
   pr_debug("no data space: only %zu available, but ask for %zu\n",
   ^
--
   In file included from include/linux/printk.h:329:0,
from include/linux/kernel.h:13,
from include/linux/list.h:8,
from include/linux/preempt.h:10,
from include/linux/spinlock.h:50,
from drivers/target/target_core_user.c:21:
   drivers/target/target_core_user.c: In function 'is_ring_space_avail':
>> include/linux/dynamic_debug.h:74:16: warning: format '%zu' expects argument 
>> of type 'size_t', but argument 3 has type 'long unsigned int' [-Wformat=]
 static struct _ddebug  __aligned(8)   \
   ^
   include/linux/dynamic_debug.h:110:2: note: in expansion of macro 
'DEFINE_DYNAMIC_DEBUG_METADATA_KEY'
 DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, 0, 0)
 ^
   include/linux/dynamic_debug.h:124:2: note: in expansion of macro 
'DEFINE_DYNAMIC_DEBUG_METADATA'
 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);  \
 ^
   include/linux/printk.h:333:2: note: in expansion of macro 'dynamic_pr_debug'
 dynamic_pr_debug(fmt, ##__VA_ARGS__)
 ^
   drivers/target/target_core_user.c:575:4: note: in expansion of macro 
'pr_debug'
   pr_debug("no data space: only %zu available, but ask for %zu\n",
   ^

vim +74 include/linux/dynamic_debug.h

b48420c1 Jim Cromie  2012-04-27  58 const 
char *modname);
b48420c1 Jim Cromie  2012-04-27  59  
cbc46635 Joe Perches 2011-08-11  60  struct device;
cbc46635 Joe Perches 2011-08-11  61  
b9075fa9 Joe Perches 2011-10-31  62  extern __printf(3, 4)
906d2015 Joe Perches 2014-09-24  63  void __dynamic_dev_dbg(struct _ddebug 
*descriptor, const struct device *dev,
b9075fa9 Joe Perches 2011-10-31  64const char *fmt, ...);
cbc46635 Joe Perches 2011-08-11  65  
ffa10cb4 Jason Baron 2011-08-11  66  struct net_device;
ffa10cb4 Jason Baron 2011-08-11  67  
b9075fa9 Joe Perches 2011-10-31  68  extern __printf(3, 4)
906d2015 Joe Perches 2014-09-24  69  void __dynamic_netdev_dbg(struct _ddebug 
*descriptor,
ffa10cb4 Jason Baron 2011-08-11  70   const struct 
net_device *dev,
b9075fa9 Joe Perches 2011-10-31  71   const char *fmt, ...);
ffa10cb4 Jason Baron 2011-08-11  72  
9049fc74 Jason Baron 2016-08-03  73  #define 
DEFINE_DYNAMIC_DEBUG_METADATA_KEY(name, fmt, key, init)\
c0d2af63 Joe Perches 2012-10-18 @74 static struct _ddebug  __aligned(8) 
\
07613b0b Jason Baron 2011-10-04  75 __attribute__((section("__verbose"))) 
name = {  \
07613b0b Jason Baron 2011-10-04  76 .modname = KBUILD_MODNAME,  
\
07613b0b Jason Baron 2011-10-04  77 .function = __func__,   
\
07613b0b Jason Baron 2011-10-04  78 .filename = __FILE__,   
\
07613b0b Jason Baron 2011-10-04  79 .format = (fmt),
\
07613b0b Jason Baron 2011-10-04  80  

[PATCH v6 2/2] tcmu: Add global data block pool support

2017-04-26 Thread lixiubo
From: Xiubo Li 

For each target there will be one ring, when the target number
grows larger and larger, it could eventually runs out of the
system memories.

In this patch for each target ring, currently for the cmd area
the size will be fixed to 8MB and for the data area the size
will grow from 0 to max 256K * PAGE_SIZE(1G for 4K page size).

For all the targets' data areas, they will get empty blocks
from the "global data block pool", which has limited to 512K *
PAGE_SIZE(2G for 4K page size) for now.

When the "global data block pool" has been used up, then any
target could wake up the unmap thread routine to shrink other
targets' data area memories. And the unmap thread routine will
always try to truncate the ring vma from the last using block
offset.

When user space has touched the data blocks out of tcmu_cmd
iov[], the tcmu_page_fault() will try to return one zeroed blocks.

Here we move the timeout's tcmu_handle_completions() into unmap
thread routine, that's to say when the timeout fired, it will
only do the tcmu_check_expired_cmd() and then wake up the unmap
thread to do the completions() and then try to shrink its idle
memories. Then the cmdr_lock could be a mutex and could simplify
this patch because the unmap_mapping_range() or zap_* may go to
sleep.

Signed-off-by: Xiubo Li 
Signed-off-by: Jianfei Hu 
---
 drivers/target/target_core_user.c | 446 --
 1 file changed, 327 insertions(+), 119 deletions(-)

diff --git a/drivers/target/target_core_user.c 
b/drivers/target/target_core_user.c
index 8491752..46f5a1c 100644
--- a/drivers/target/target_core_user.c
+++ b/drivers/target/target_core_user.c
@@ -31,6 +31,8 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 #include 
 #include 
 #include 
@@ -67,17 +69,24 @@
 
 #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
 
-/* For cmd area, the size is fixed 2M */
-#define CMDR_SIZE (2 * 1024 * 1024)
+/* For cmd area, the size is fixed 8MB */
+#define CMDR_SIZE (8 * 1024 * 1024)
 
-/* For data area, the size is fixed 32M */
-#define DATA_BLOCK_BITS (8 * 1024)
-#define DATA_BLOCK_SIZE 4096
+/*
+ * For data area, the block size is PAGE_SIZE and
+ * the total size is 256K * PAGE_SIZE.
+ */
+#define DATA_BLOCK_SIZE PAGE_SIZE
+#define DATA_BLOCK_BITS (256 * 1024)
 #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE)
+#define DATA_BLOCK_INIT_BITS 128
 
-/* The ring buffer size is 34M */
+/* The total size of the ring is 8M + 256K * PAGE_SIZE */
 #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
 
+/* Default maximum of the global data blocks(512K * PAGE_SIZE) */
+#define TCMU_GLOBAL_MAX_BLOCKS (512 * 1024)
+
 static struct device *tcmu_root_device;
 
 struct tcmu_hba {
@@ -87,6 +96,8 @@ struct tcmu_hba {
 #define TCMU_CONFIG_LEN 256
 
 struct tcmu_dev {
+   struct list_head node;
+
struct se_device se_dev;
 
char *name;
@@ -98,6 +109,8 @@ struct tcmu_dev {
 
struct uio_info uio_info;
 
+   struct inode *inode;
+
struct tcmu_mailbox *mb_addr;
size_t dev_size;
u32 cmdr_size;
@@ -108,10 +121,11 @@ struct tcmu_dev {
size_t data_size;
 
wait_queue_head_t wait_cmdr;
-   /* TODO should this be a mutex? */
-   spinlock_t cmdr_lock;
+   struct mutex cmdr_lock;
 
+   bool waiting_global;
uint32_t dbi_max;
+   uint32_t dbi_thresh;
DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS);
struct radix_tree_root data_blocks;
 
@@ -146,6 +160,13 @@ struct tcmu_cmd {
unsigned long flags;
 };
 
+static struct task_struct *unmap_thread;
+static wait_queue_head_t unmap_wait;
+static DEFINE_MUTEX(root_udev_mutex);
+static LIST_HEAD(root_udev);
+
+static atomic_t global_db_count = ATOMIC_INIT(0);
+
 static struct kmem_cache *tcmu_cmd_cache;
 
 /* multicast group */
@@ -174,48 +195,79 @@ enum tcmu_multicast_groups {
 #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
 #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
 
-static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd)
+static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
 {
struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
uint32_t i;
 
-   for (i = 0; i < tcmu_cmd->dbi_cnt; i++)
+   for (i = 0; i < len; i++)
clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
 }
 
-static int tcmu_get_empty_block(struct tcmu_dev *udev, void **addr)
+static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
+   struct tcmu_cmd *tcmu_cmd)
 {
-   void *p;
-   uint32_t dbi;
-   int ret;
+   struct page *page;
+   int ret, dbi;
 
-   dbi = find_first_zero_bit(udev->data_bitmap, DATA_BLOCK_BITS);
-   if (dbi > udev->dbi_max)
-   udev->dbi_max = dbi;
+   dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
+   if (dbi ==