Commit 7b13354e authored by Tejun Heo's avatar Tejun Heo Committed by Jens Axboe

ide-tape: use single continuous buffer

Impact: simpler buffer allocation and handling, kills OOM, fix DMA transfers

ide-tape has its own multiple buffer mechanism using struct
idetape_bh.  It allocates buffer with decreasing order-of-two
allocations so that it results in minimum number of segments.
However, the implementation is quite complex and works in a way that
no other block or ide driver works necessitating a lot of special case
handling.

The benefit this complex allocation scheme brings is questionable as
PIO or DMA the number of segments (16 maximum) doesn't make any
noticeable difference and it also doesn't negate the need for multiple
order allocation which can fail under memory pressure or high
fragmentation although it does lower the highest order necessary by
one when the buffer size isn't power of two.

As the first step to remove the custom buffer management, this patch
makes ide-tape allocate single continous buffer.  The maximum order is
four.  I doubt the change would cause any trouble but if it ever
matters, it should be converted to regular sg mechanism like everyone
else and even in that case dropping custom buffer handling and moving
to standard mechanism first make sense as an intermediate step.

This patch makes the first bh to contain the whole buffer and drops
multi bh handling code.  Following patches will make further changes.

This patch has the side effect of killing OOM triggered by allocation
path and fixing DMA transfers.  Previously, bug in alloc path
triggered OOM on command issue and commands were passed to DMA engine
without DMA-mapping all the segments.
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
parent eb6a61bb
...@@ -134,7 +134,6 @@ enum { ...@@ -134,7 +134,6 @@ enum {
struct idetape_bh { struct idetape_bh {
u32 b_size; u32 b_size;
atomic_t b_count; atomic_t b_count;
struct idetape_bh *b_reqnext;
char *b_data; char *b_data;
}; };
...@@ -228,10 +227,6 @@ typedef struct ide_tape_obj { ...@@ -228,10 +227,6 @@ typedef struct ide_tape_obj {
char *b_data; char *b_data;
int b_count; int b_count;
int pages_per_buffer;
/* Wasted space in each stage */
int excess_bh_size;
/* Measures average tape speed */ /* Measures average tape speed */
unsigned long avg_time; unsigned long avg_time;
int avg_size; int avg_size;
...@@ -303,9 +298,7 @@ static int idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, ...@@ -303,9 +298,7 @@ static int idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
struct idetape_bh *bh = pc->bh; struct idetape_bh *bh = pc->bh;
int count; int count;
while (bcount) { if (bcount && bh) {
if (bh == NULL)
break;
count = min( count = min(
(unsigned int)(bh->b_size - atomic_read(&bh->b_count)), (unsigned int)(bh->b_size - atomic_read(&bh->b_count)),
bcount); bcount);
...@@ -313,15 +306,10 @@ static int idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, ...@@ -313,15 +306,10 @@ static int idetape_input_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
atomic_read(&bh->b_count), count); atomic_read(&bh->b_count), count);
bcount -= count; bcount -= count;
atomic_add(count, &bh->b_count); atomic_add(count, &bh->b_count);
if (atomic_read(&bh->b_count) == bh->b_size) { if (atomic_read(&bh->b_count) == bh->b_size)
bh = bh->b_reqnext; pc->bh = NULL;
if (bh)
atomic_set(&bh->b_count, 0);
}
} }
pc->bh = bh;
return bcount; return bcount;
} }
...@@ -331,22 +319,14 @@ static int idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, ...@@ -331,22 +319,14 @@ static int idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
struct idetape_bh *bh = pc->bh; struct idetape_bh *bh = pc->bh;
int count; int count;
while (bcount) { if (bcount && bh) {
if (bh == NULL)
break;
count = min((unsigned int)pc->b_count, (unsigned int)bcount); count = min((unsigned int)pc->b_count, (unsigned int)bcount);
drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count); drive->hwif->tp_ops->output_data(drive, NULL, pc->b_data, count);
bcount -= count; bcount -= count;
pc->b_data += count; pc->b_data += count;
pc->b_count -= count; pc->b_count -= count;
if (!pc->b_count) { if (!pc->b_count)
bh = bh->b_reqnext; pc->bh = NULL;
pc->bh = bh;
if (bh) {
pc->b_data = bh->b_data;
pc->b_count = atomic_read(&bh->b_count);
}
}
} }
return bcount; return bcount;
...@@ -355,24 +335,20 @@ static int idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc, ...@@ -355,24 +335,20 @@ static int idetape_output_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc) static void idetape_update_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc)
{ {
struct idetape_bh *bh = pc->bh; struct idetape_bh *bh = pc->bh;
int count;
unsigned int bcount = pc->xferred; unsigned int bcount = pc->xferred;
if (pc->flags & PC_FLAG_WRITING) if (pc->flags & PC_FLAG_WRITING)
return; return;
while (bcount) { if (bcount) {
if (bh == NULL) { if (bh == NULL || bcount > bh->b_size) {
printk(KERN_ERR "ide-tape: bh == NULL in %s\n", printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
__func__); __func__);
return; return;
} }
count = min((unsigned int)bh->b_size, (unsigned int)bcount); atomic_set(&bh->b_count, bcount);
atomic_set(&bh->b_count, count);
if (atomic_read(&bh->b_count) == bh->b_size) if (atomic_read(&bh->b_count) == bh->b_size)
bh = bh->b_reqnext; pc->bh = NULL;
bcount -= count;
} }
pc->bh = bh;
} }
/* /*
...@@ -439,24 +415,10 @@ static void idetape_analyze_error(ide_drive_t *drive, u8 *sense) ...@@ -439,24 +415,10 @@ static void idetape_analyze_error(ide_drive_t *drive, u8 *sense)
/* Free data buffers completely. */ /* Free data buffers completely. */
static void ide_tape_kfree_buffer(idetape_tape_t *tape) static void ide_tape_kfree_buffer(idetape_tape_t *tape)
{ {
struct idetape_bh *prev_bh, *bh = tape->merge_bh; struct idetape_bh *bh = tape->merge_bh;
while (bh) {
u32 size = bh->b_size;
while (size) {
unsigned int order = fls(size >> PAGE_SHIFT)-1;
if (bh->b_data)
free_pages((unsigned long)bh->b_data, order);
size &= (order-1); kfree(bh->b_data);
bh->b_data += (1 << order) * PAGE_SIZE; kfree(bh);
}
prev_bh = bh;
bh = bh->b_reqnext;
kfree(prev_bh);
}
} }
static void ide_tape_handle_dsc(ide_drive_t *); static void ide_tape_handle_dsc(ide_drive_t *);
...@@ -861,117 +823,50 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive, ...@@ -861,117 +823,50 @@ static ide_startstop_t idetape_do_request(ide_drive_t *drive,
} }
/* /*
* The function below uses __get_free_pages to allocate a data buffer of size * It returns a pointer to the newly allocated buffer, or NULL in case
* tape->buffer_size (or a bit more). We attempt to combine sequential pages as * of failure.
* much as possible.
*
* It returns a pointer to the newly allocated buffer, or NULL in case of
* failure.
*/ */
static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape, static struct idetape_bh *ide_tape_kmalloc_buffer(idetape_tape_t *tape,
int full, int clear) int full)
{ {
struct idetape_bh *prev_bh, *bh, *merge_bh; struct idetape_bh *bh;
int pages = tape->pages_per_buffer;
unsigned int order, b_allocd;
char *b_data = NULL;
merge_bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
bh = merge_bh;
if (bh == NULL)
goto abort;
order = fls(pages) - 1;
bh->b_data = (char *) __get_free_pages(GFP_KERNEL, order);
if (!bh->b_data)
goto abort;
b_allocd = (1 << order) * PAGE_SIZE;
pages &= (order-1);
if (clear)
memset(bh->b_data, 0, b_allocd);
bh->b_reqnext = NULL;
bh->b_size = b_allocd;
atomic_set(&bh->b_count, full ? bh->b_size : 0);
while (pages) {
order = fls(pages) - 1;
b_data = (char *) __get_free_pages(GFP_KERNEL, order);
if (!b_data)
goto abort;
b_allocd = (1 << order) * PAGE_SIZE;
if (clear)
memset(b_data, 0, b_allocd);
/* newly allocated page frames below buffer header or ...*/
if (bh->b_data == b_data + b_allocd) {
bh->b_size += b_allocd;
bh->b_data -= b_allocd;
if (full)
atomic_add(b_allocd, &bh->b_count);
continue;
}
/* they are above the header */
if (b_data == bh->b_data + bh->b_size) {
bh->b_size += b_allocd;
if (full)
atomic_add(b_allocd, &bh->b_count);
continue;
}
prev_bh = bh;
bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL); bh = kmalloc(sizeof(struct idetape_bh), GFP_KERNEL);
if (!bh) { if (!bh)
free_pages((unsigned long) b_data, order); return NULL;
goto abort;
}
bh->b_reqnext = NULL;
bh->b_data = b_data;
bh->b_size = b_allocd;
atomic_set(&bh->b_count, full ? bh->b_size : 0);
prev_bh->b_reqnext = bh;
pages &= (order-1); bh->b_data = kmalloc(tape->buffer_size, GFP_KERNEL);
if (!bh->b_data) {
kfree(bh);
return NULL;
} }
bh->b_size -= tape->excess_bh_size; bh->b_size = tape->buffer_size;
if (full) atomic_set(&bh->b_count, full ? bh->b_size : 0);
atomic_sub(tape->excess_bh_size, &bh->b_count);
return merge_bh; return bh;
abort:
ide_tape_kfree_buffer(tape);
return NULL;
} }
static int idetape_copy_stage_from_user(idetape_tape_t *tape, static int idetape_copy_stage_from_user(idetape_tape_t *tape,
const char __user *buf, int n) const char __user *buf, int n)
{ {
struct idetape_bh *bh = tape->bh; struct idetape_bh *bh = tape->bh;
int count;
int ret = 0; int ret = 0;
while (n) { if (n) {
if (bh == NULL) { if (bh == NULL || n > bh->b_size - atomic_read(&bh->b_count)) {
printk(KERN_ERR "ide-tape: bh == NULL in %s\n", printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
__func__); __func__);
return 1; return 1;
} }
count = min((unsigned int)
(bh->b_size - atomic_read(&bh->b_count)),
(unsigned int)n);
if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf, if (copy_from_user(bh->b_data + atomic_read(&bh->b_count), buf,
count)) n))
ret = 1; ret = 1;
n -= count; atomic_add(n, &bh->b_count);
atomic_add(count, &bh->b_count); if (atomic_read(&bh->b_count) == bh->b_size)
buf += count; tape->bh = NULL;
if (atomic_read(&bh->b_count) == bh->b_size) {
bh = bh->b_reqnext;
if (bh)
atomic_set(&bh->b_count, 0);
}
} }
tape->bh = bh;
return ret; return ret;
} }
...@@ -979,30 +874,20 @@ static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf, ...@@ -979,30 +874,20 @@ static int idetape_copy_stage_to_user(idetape_tape_t *tape, char __user *buf,
int n) int n)
{ {
struct idetape_bh *bh = tape->bh; struct idetape_bh *bh = tape->bh;
int count;
int ret = 0; int ret = 0;
while (n) { if (n) {
if (bh == NULL) { if (bh == NULL || n > tape->b_count) {
printk(KERN_ERR "ide-tape: bh == NULL in %s\n", printk(KERN_ERR "ide-tape: bh == NULL in %s\n",
__func__); __func__);
return 1; return 1;
} }
count = min(tape->b_count, n); if (copy_to_user(buf, tape->b_data, n))
if (copy_to_user(buf, tape->b_data, count))
ret = 1; ret = 1;
n -= count; tape->b_data += n;
tape->b_data += count; tape->b_count -= n;
tape->b_count -= count; if (!tape->b_count)
buf += count; tape->bh = NULL;
if (!tape->b_count) {
bh = bh->b_reqnext;
tape->bh = bh;
if (bh) {
tape->b_data = bh->b_data;
tape->b_count = atomic_read(&bh->b_count);
}
}
} }
return ret; return ret;
} }
...@@ -1254,7 +1139,7 @@ static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks) ...@@ -1254,7 +1139,7 @@ static int idetape_add_chrdev_write_request(ide_drive_t *drive, int blocks)
static void ide_tape_flush_merge_buffer(ide_drive_t *drive) static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
{ {
idetape_tape_t *tape = drive->driver_data; idetape_tape_t *tape = drive->driver_data;
int blocks, min; int blocks;
struct idetape_bh *bh; struct idetape_bh *bh;
if (tape->chrdev_dir != IDETAPE_DIR_WRITE) { if (tape->chrdev_dir != IDETAPE_DIR_WRITE) {
...@@ -1269,31 +1154,16 @@ static void ide_tape_flush_merge_buffer(ide_drive_t *drive) ...@@ -1269,31 +1154,16 @@ static void ide_tape_flush_merge_buffer(ide_drive_t *drive)
if (tape->merge_bh_size) { if (tape->merge_bh_size) {
blocks = tape->merge_bh_size / tape->blk_size; blocks = tape->merge_bh_size / tape->blk_size;
if (tape->merge_bh_size % tape->blk_size) { if (tape->merge_bh_size % tape->blk_size) {
unsigned int i; unsigned int i = tape->blk_size -
tape->merge_bh_size % tape->blk_size;
blocks++; blocks++;
i = tape->blk_size - tape->merge_bh_size %
tape->blk_size;
bh = tape->bh->b_reqnext;
while (bh) {
atomic_set(&bh->b_count, 0);
bh = bh->b_reqnext;
}
bh = tape->bh; bh = tape->bh;
while (i) { if (bh) {
if (bh == NULL) {
printk(KERN_INFO "ide-tape: bug,"
" bh NULL\n");
break;
}
min = min(i, (unsigned int)(bh->b_size -
atomic_read(&bh->b_count)));
memset(bh->b_data + atomic_read(&bh->b_count), memset(bh->b_data + atomic_read(&bh->b_count),
0, min); 0, i);
atomic_add(min, &bh->b_count); atomic_add(i, &bh->b_count);
i -= min; } else
bh = bh->b_reqnext; printk(KERN_INFO "ide-tape: bug, bh NULL\n");
}
} }
(void) idetape_add_chrdev_write_request(drive, blocks); (void) idetape_add_chrdev_write_request(drive, blocks);
tape->merge_bh_size = 0; tape->merge_bh_size = 0;
...@@ -1321,7 +1191,7 @@ static int idetape_init_read(ide_drive_t *drive) ...@@ -1321,7 +1191,7 @@ static int idetape_init_read(ide_drive_t *drive)
" 0 now\n"); " 0 now\n");
tape->merge_bh_size = 0; tape->merge_bh_size = 0;
} }
tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0); tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0);
if (!tape->merge_bh) if (!tape->merge_bh)
return -ENOMEM; return -ENOMEM;
tape->chrdev_dir = IDETAPE_DIR_READ; tape->chrdev_dir = IDETAPE_DIR_READ;
...@@ -1368,23 +1238,18 @@ static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks) ...@@ -1368,23 +1238,18 @@ static int idetape_add_chrdev_read_request(ide_drive_t *drive, int blocks)
static void idetape_pad_zeros(ide_drive_t *drive, int bcount) static void idetape_pad_zeros(ide_drive_t *drive, int bcount)
{ {
idetape_tape_t *tape = drive->driver_data; idetape_tape_t *tape = drive->driver_data;
struct idetape_bh *bh; struct idetape_bh *bh = tape->merge_bh;
int blocks; int blocks;
while (bcount) { while (bcount) {
unsigned int count; unsigned int count;
bh = tape->merge_bh;
count = min(tape->buffer_size, bcount); count = min(tape->buffer_size, bcount);
bcount -= count; bcount -= count;
blocks = count / tape->blk_size; blocks = count / tape->blk_size;
while (count) { atomic_set(&bh->b_count, count);
atomic_set(&bh->b_count,
min(count, (unsigned int)bh->b_size));
memset(bh->b_data, 0, atomic_read(&bh->b_count)); memset(bh->b_data, 0, atomic_read(&bh->b_count));
count -= atomic_read(&bh->b_count);
bh = bh->b_reqnext;
}
idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks, idetape_queue_rw_tail(drive, REQ_IDETAPE_WRITE, blocks,
tape->merge_bh); tape->merge_bh);
} }
...@@ -1596,7 +1461,7 @@ static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf, ...@@ -1596,7 +1461,7 @@ static ssize_t idetape_chrdev_write(struct file *file, const char __user *buf,
"should be 0 now\n"); "should be 0 now\n");
tape->merge_bh_size = 0; tape->merge_bh_size = 0;
} }
tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0, 0); tape->merge_bh = ide_tape_kmalloc_buffer(tape, 0);
if (!tape->merge_bh) if (!tape->merge_bh)
return -ENOMEM; return -ENOMEM;
tape->chrdev_dir = IDETAPE_DIR_WRITE; tape->chrdev_dir = IDETAPE_DIR_WRITE;
...@@ -1970,7 +1835,7 @@ static void idetape_write_release(ide_drive_t *drive, unsigned int minor) ...@@ -1970,7 +1835,7 @@ static void idetape_write_release(ide_drive_t *drive, unsigned int minor)
idetape_tape_t *tape = drive->driver_data; idetape_tape_t *tape = drive->driver_data;
ide_tape_flush_merge_buffer(drive); ide_tape_flush_merge_buffer(drive);
tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1, 0); tape->merge_bh = ide_tape_kmalloc_buffer(tape, 1);
if (tape->merge_bh != NULL) { if (tape->merge_bh != NULL) {
idetape_pad_zeros(drive, tape->blk_size * idetape_pad_zeros(drive, tape->blk_size *
(tape->user_bs_factor - 1)); (tape->user_bs_factor - 1));
...@@ -2201,11 +2066,6 @@ static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor) ...@@ -2201,11 +2066,6 @@ static void idetape_setup(ide_drive_t *drive, idetape_tape_t *tape, int minor)
tape->buffer_size = *ctl * tape->blk_size; tape->buffer_size = *ctl * tape->blk_size;
} }
buffer_size = tape->buffer_size; buffer_size = tape->buffer_size;
tape->pages_per_buffer = buffer_size / PAGE_SIZE;
if (buffer_size % PAGE_SIZE) {
tape->pages_per_buffer++;
tape->excess_bh_size = PAGE_SIZE - buffer_size % PAGE_SIZE;
}
/* select the "best" DSC read/write polling freq */ /* select the "best" DSC read/write polling freq */
speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]); speed = max(*(u16 *)&tape->caps[14], *(u16 *)&tape->caps[8]);
......
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