linux/fs/orangefs/orangefs-bufmap.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * (C) 2001 Clemson University and The University of Chicago
   4 *
   5 * See COPYING in top-level directory.
   6 */
   7#include "protocol.h"
   8#include "orangefs-kernel.h"
   9#include "orangefs-bufmap.h"
  10
  11struct slot_map {
  12        int c;
  13        wait_queue_head_t q;
  14        int count;
  15        unsigned long *map;
  16};
  17
  18static struct slot_map rw_map = {
  19        .c = -1,
  20        .q = __WAIT_QUEUE_HEAD_INITIALIZER(rw_map.q)
  21};
  22static struct slot_map readdir_map = {
  23        .c = -1,
  24        .q = __WAIT_QUEUE_HEAD_INITIALIZER(readdir_map.q)
  25};
  26
  27
  28static void install(struct slot_map *m, int count, unsigned long *map)
  29{
  30        spin_lock(&m->q.lock);
  31        m->c = m->count = count;
  32        m->map = map;
  33        wake_up_all_locked(&m->q);
  34        spin_unlock(&m->q.lock);
  35}
  36
  37static void mark_killed(struct slot_map *m)
  38{
  39        spin_lock(&m->q.lock);
  40        m->c -= m->count + 1;
  41        spin_unlock(&m->q.lock);
  42}
  43
  44static void run_down(struct slot_map *m)
  45{
  46        DEFINE_WAIT(wait);
  47        spin_lock(&m->q.lock);
  48        if (m->c != -1) {
  49                for (;;) {
  50                        if (likely(list_empty(&wait.entry)))
  51                                __add_wait_queue_entry_tail(&m->q, &wait);
  52                        set_current_state(TASK_UNINTERRUPTIBLE);
  53
  54                        if (m->c == -1)
  55                                break;
  56
  57                        spin_unlock(&m->q.lock);
  58                        schedule();
  59                        spin_lock(&m->q.lock);
  60                }
  61                __remove_wait_queue(&m->q, &wait);
  62                __set_current_state(TASK_RUNNING);
  63        }
  64        m->map = NULL;
  65        spin_unlock(&m->q.lock);
  66}
  67
  68static void put(struct slot_map *m, int slot)
  69{
  70        int v;
  71        spin_lock(&m->q.lock);
  72        __clear_bit(slot, m->map);
  73        v = ++m->c;
  74        if (v > 0)
  75                wake_up_locked(&m->q);
  76        if (unlikely(v == -1))     /* finished dying */
  77                wake_up_all_locked(&m->q);
  78        spin_unlock(&m->q.lock);
  79}
  80
  81static int wait_for_free(struct slot_map *m)
  82{
  83        long left = slot_timeout_secs * HZ;
  84        DEFINE_WAIT(wait);
  85
  86        do {
  87                long n = left, t;
  88                if (likely(list_empty(&wait.entry)))
  89                        __add_wait_queue_entry_tail_exclusive(&m->q, &wait);
  90                set_current_state(TASK_INTERRUPTIBLE);
  91
  92                if (m->c > 0)
  93                        break;
  94
  95                if (m->c < 0) {
  96                        /* we are waiting for map to be installed */
  97                        /* it would better be there soon, or we go away */
  98                        if (n > ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ)
  99                                n = ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ;
 100                }
 101                spin_unlock(&m->q.lock);
 102                t = schedule_timeout(n);
 103                spin_lock(&m->q.lock);
 104                if (unlikely(!t) && n != left && m->c < 0)
 105                        left = t;
 106                else
 107                        left = t + (left - n);
 108                if (signal_pending(current))
 109                        left = -EINTR;
 110        } while (left > 0);
 111
 112        if (!list_empty(&wait.entry))
 113                list_del(&wait.entry);
 114        else if (left <= 0 && waitqueue_active(&m->q))
 115                __wake_up_locked_key(&m->q, TASK_INTERRUPTIBLE, NULL);
 116        __set_current_state(TASK_RUNNING);
 117
 118        if (likely(left > 0))
 119                return 0;
 120
 121        return left < 0 ? -EINTR : -ETIMEDOUT;
 122}
 123
 124static int get(struct slot_map *m)
 125{
 126        int res = 0;
 127        spin_lock(&m->q.lock);
 128        if (unlikely(m->c <= 0))
 129                res = wait_for_free(m);
 130        if (likely(!res)) {
 131                m->c--;
 132                res = find_first_zero_bit(m->map, m->count);
 133                __set_bit(res, m->map);
 134        }
 135        spin_unlock(&m->q.lock);
 136        return res;
 137}
 138
 139/* used to describe mapped buffers */
 140struct orangefs_bufmap_desc {
 141        void __user *uaddr;             /* user space address pointer */
 142        struct page **page_array;       /* array of mapped pages */
 143        int array_count;                /* size of above arrays */
 144        struct list_head list_link;
 145};
 146
 147static struct orangefs_bufmap {
 148        int desc_size;
 149        int desc_shift;
 150        int desc_count;
 151        int total_size;
 152        int page_count;
 153
 154        struct page **page_array;
 155        struct orangefs_bufmap_desc *desc_array;
 156
 157        /* array to track usage of buffer descriptors */
 158        unsigned long *buffer_index_array;
 159
 160        /* array to track usage of buffer descriptors for readdir */
 161#define N DIV_ROUND_UP(ORANGEFS_READDIR_DEFAULT_DESC_COUNT, BITS_PER_LONG)
 162        unsigned long readdir_index_array[N];
 163#undef N
 164} *__orangefs_bufmap;
 165
 166static DEFINE_SPINLOCK(orangefs_bufmap_lock);
 167
 168static void
 169orangefs_bufmap_unmap(struct orangefs_bufmap *bufmap)
 170{
 171        unpin_user_pages(bufmap->page_array, bufmap->page_count);
 172}
 173
 174static void
 175orangefs_bufmap_free(struct orangefs_bufmap *bufmap)
 176{
 177        kfree(bufmap->page_array);
 178        kfree(bufmap->desc_array);
 179        kfree(bufmap->buffer_index_array);
 180        kfree(bufmap);
 181}
 182
 183/*
 184 * XXX: Can the size and shift change while the caller gives up the
 185 * XXX: lock between calling this and doing something useful?
 186 */
 187
 188int orangefs_bufmap_size_query(void)
 189{
 190        struct orangefs_bufmap *bufmap;
 191        int size = 0;
 192        spin_lock(&orangefs_bufmap_lock);
 193        bufmap = __orangefs_bufmap;
 194        if (bufmap)
 195                size = bufmap->desc_size;
 196        spin_unlock(&orangefs_bufmap_lock);
 197        return size;
 198}
 199
 200int orangefs_bufmap_shift_query(void)
 201{
 202        struct orangefs_bufmap *bufmap;
 203        int shift = 0;
 204        spin_lock(&orangefs_bufmap_lock);
 205        bufmap = __orangefs_bufmap;
 206        if (bufmap)
 207                shift = bufmap->desc_shift;
 208        spin_unlock(&orangefs_bufmap_lock);
 209        return shift;
 210}
 211
 212static DECLARE_WAIT_QUEUE_HEAD(bufmap_waitq);
 213static DECLARE_WAIT_QUEUE_HEAD(readdir_waitq);
 214
 215static struct orangefs_bufmap *
 216orangefs_bufmap_alloc(struct ORANGEFS_dev_map_desc *user_desc)
 217{
 218        struct orangefs_bufmap *bufmap;
 219
 220        bufmap = kzalloc(sizeof(*bufmap), GFP_KERNEL);
 221        if (!bufmap)
 222                goto out;
 223
 224        bufmap->total_size = user_desc->total_size;
 225        bufmap->desc_count = user_desc->count;
 226        bufmap->desc_size = user_desc->size;
 227        bufmap->desc_shift = ilog2(bufmap->desc_size);
 228
 229        bufmap->buffer_index_array =
 230                kzalloc(DIV_ROUND_UP(bufmap->desc_count, BITS_PER_LONG), GFP_KERNEL);
 231        if (!bufmap->buffer_index_array)
 232                goto out_free_bufmap;
 233
 234        bufmap->desc_array =
 235                kcalloc(bufmap->desc_count, sizeof(struct orangefs_bufmap_desc),
 236                        GFP_KERNEL);
 237        if (!bufmap->desc_array)
 238                goto out_free_index_array;
 239
 240        bufmap->page_count = bufmap->total_size / PAGE_SIZE;
 241
 242        /* allocate storage to track our page mappings */
 243        bufmap->page_array =
 244                kcalloc(bufmap->page_count, sizeof(struct page *), GFP_KERNEL);
 245        if (!bufmap->page_array)
 246                goto out_free_desc_array;
 247
 248        return bufmap;
 249
 250out_free_desc_array:
 251        kfree(bufmap->desc_array);
 252out_free_index_array:
 253        kfree(bufmap->buffer_index_array);
 254out_free_bufmap:
 255        kfree(bufmap);
 256out:
 257        return NULL;
 258}
 259
 260static int
 261orangefs_bufmap_map(struct orangefs_bufmap *bufmap,
 262                struct ORANGEFS_dev_map_desc *user_desc)
 263{
 264        int pages_per_desc = bufmap->desc_size / PAGE_SIZE;
 265        int offset = 0, ret, i;
 266
 267        /* map the pages */
 268        ret = pin_user_pages_fast((unsigned long)user_desc->ptr,
 269                             bufmap->page_count, FOLL_WRITE, bufmap->page_array);
 270
 271        if (ret < 0)
 272                return ret;
 273
 274        if (ret != bufmap->page_count) {
 275                gossip_err("orangefs error: asked for %d pages, only got %d.\n",
 276                                bufmap->page_count, ret);
 277
 278                for (i = 0; i < ret; i++) {
 279                        SetPageError(bufmap->page_array[i]);
 280                        unpin_user_page(bufmap->page_array[i]);
 281                }
 282                return -ENOMEM;
 283        }
 284
 285        /*
 286         * ideally we want to get kernel space pointers for each page, but
 287         * we can't kmap that many pages at once if highmem is being used.
 288         * so instead, we just kmap/kunmap the page address each time the
 289         * kaddr is needed.
 290         */
 291        for (i = 0; i < bufmap->page_count; i++)
 292                flush_dcache_page(bufmap->page_array[i]);
 293
 294        /* build a list of available descriptors */
 295        for (offset = 0, i = 0; i < bufmap->desc_count; i++) {
 296                bufmap->desc_array[i].page_array = &bufmap->page_array[offset];
 297                bufmap->desc_array[i].array_count = pages_per_desc;
 298                bufmap->desc_array[i].uaddr =
 299                    (user_desc->ptr + (i * pages_per_desc * PAGE_SIZE));
 300                offset += pages_per_desc;
 301        }
 302
 303        return 0;
 304}
 305
 306/*
 307 * orangefs_bufmap_initialize()
 308 *
 309 * initializes the mapped buffer interface
 310 *
 311 * returns 0 on success, -errno on failure
 312 */
 313int orangefs_bufmap_initialize(struct ORANGEFS_dev_map_desc *user_desc)
 314{
 315        struct orangefs_bufmap *bufmap;
 316        int ret = -EINVAL;
 317
 318        gossip_debug(GOSSIP_BUFMAP_DEBUG,
 319                     "orangefs_bufmap_initialize: called (ptr ("
 320                     "%p) sz (%d) cnt(%d).\n",
 321                     user_desc->ptr,
 322                     user_desc->size,
 323                     user_desc->count);
 324
 325        if (user_desc->total_size < 0 ||
 326            user_desc->size < 0 ||
 327            user_desc->count < 0)
 328                goto out;
 329
 330        /*
 331         * sanity check alignment and size of buffer that caller wants to
 332         * work with
 333         */
 334        if (PAGE_ALIGN((unsigned long)user_desc->ptr) !=
 335            (unsigned long)user_desc->ptr) {
 336                gossip_err("orangefs error: memory alignment (front). %p\n",
 337                           user_desc->ptr);
 338                goto out;
 339        }
 340
 341        if (PAGE_ALIGN(((unsigned long)user_desc->ptr + user_desc->total_size))
 342            != (unsigned long)(user_desc->ptr + user_desc->total_size)) {
 343                gossip_err("orangefs error: memory alignment (back).(%p + %d)\n",
 344                           user_desc->ptr,
 345                           user_desc->total_size);
 346                goto out;
 347        }
 348
 349        if (user_desc->total_size != (user_desc->size * user_desc->count)) {
 350                gossip_err("orangefs error: user provided an oddly sized buffer: (%d, %d, %d)\n",
 351                           user_desc->total_size,
 352                           user_desc->size,
 353                           user_desc->count);
 354                goto out;
 355        }
 356
 357        if ((user_desc->size % PAGE_SIZE) != 0) {
 358                gossip_err("orangefs error: bufmap size not page size divisible (%d).\n",
 359                           user_desc->size);
 360                goto out;
 361        }
 362
 363        ret = -ENOMEM;
 364        bufmap = orangefs_bufmap_alloc(user_desc);
 365        if (!bufmap)
 366                goto out;
 367
 368        ret = orangefs_bufmap_map(bufmap, user_desc);
 369        if (ret)
 370                goto out_free_bufmap;
 371
 372
 373        spin_lock(&orangefs_bufmap_lock);
 374        if (__orangefs_bufmap) {
 375                spin_unlock(&orangefs_bufmap_lock);
 376                gossip_err("orangefs: error: bufmap already initialized.\n");
 377                ret = -EINVAL;
 378                goto out_unmap_bufmap;
 379        }
 380        __orangefs_bufmap = bufmap;
 381        install(&rw_map,
 382                bufmap->desc_count,
 383                bufmap->buffer_index_array);
 384        install(&readdir_map,
 385                ORANGEFS_READDIR_DEFAULT_DESC_COUNT,
 386                bufmap->readdir_index_array);
 387        spin_unlock(&orangefs_bufmap_lock);
 388
 389        gossip_debug(GOSSIP_BUFMAP_DEBUG,
 390                     "orangefs_bufmap_initialize: exiting normally\n");
 391        return 0;
 392
 393out_unmap_bufmap:
 394        orangefs_bufmap_unmap(bufmap);
 395out_free_bufmap:
 396        orangefs_bufmap_free(bufmap);
 397out:
 398        return ret;
 399}
 400
 401/*
 402 * orangefs_bufmap_finalize()
 403 *
 404 * shuts down the mapped buffer interface and releases any resources
 405 * associated with it
 406 *
 407 * no return value
 408 */
 409void orangefs_bufmap_finalize(void)
 410{
 411        struct orangefs_bufmap *bufmap = __orangefs_bufmap;
 412        if (!bufmap)
 413                return;
 414        gossip_debug(GOSSIP_BUFMAP_DEBUG, "orangefs_bufmap_finalize: called\n");
 415        mark_killed(&rw_map);
 416        mark_killed(&readdir_map);
 417        gossip_debug(GOSSIP_BUFMAP_DEBUG,
 418                     "orangefs_bufmap_finalize: exiting normally\n");
 419}
 420
 421void orangefs_bufmap_run_down(void)
 422{
 423        struct orangefs_bufmap *bufmap = __orangefs_bufmap;
 424        if (!bufmap)
 425                return;
 426        run_down(&rw_map);
 427        run_down(&readdir_map);
 428        spin_lock(&orangefs_bufmap_lock);
 429        __orangefs_bufmap = NULL;
 430        spin_unlock(&orangefs_bufmap_lock);
 431        orangefs_bufmap_unmap(bufmap);
 432        orangefs_bufmap_free(bufmap);
 433}
 434
 435/*
 436 * orangefs_bufmap_get()
 437 *
 438 * gets a free mapped buffer descriptor, will sleep until one becomes
 439 * available if necessary
 440 *
 441 * returns slot on success, -errno on failure
 442 */
 443int orangefs_bufmap_get(void)
 444{
 445        return get(&rw_map);
 446}
 447
 448/*
 449 * orangefs_bufmap_put()
 450 *
 451 * returns a mapped buffer descriptor to the collection
 452 *
 453 * no return value
 454 */
 455void orangefs_bufmap_put(int buffer_index)
 456{
 457        put(&rw_map, buffer_index);
 458}
 459
 460/*
 461 * orangefs_readdir_index_get()
 462 *
 463 * gets a free descriptor, will sleep until one becomes
 464 * available if necessary.
 465 * Although the readdir buffers are not mapped into kernel space
 466 * we could do that at a later point of time. Regardless, these
 467 * indices are used by the client-core.
 468 *
 469 * returns slot on success, -errno on failure
 470 */
 471int orangefs_readdir_index_get(void)
 472{
 473        return get(&readdir_map);
 474}
 475
 476void orangefs_readdir_index_put(int buffer_index)
 477{
 478        put(&readdir_map, buffer_index);
 479}
 480
 481/*
 482 * we've been handed an iovec, we need to copy it to
 483 * the shared memory descriptor at "buffer_index".
 484 */
 485int orangefs_bufmap_copy_from_iovec(struct iov_iter *iter,
 486                                int buffer_index,
 487                                size_t size)
 488{
 489        struct orangefs_bufmap_desc *to;
 490        int i;
 491
 492        gossip_debug(GOSSIP_BUFMAP_DEBUG,
 493                     "%s: buffer_index:%d: size:%zu:\n",
 494                     __func__, buffer_index, size);
 495
 496        to = &__orangefs_bufmap->desc_array[buffer_index];
 497        for (i = 0; size; i++) {
 498                struct page *page = to->page_array[i];
 499                size_t n = size;
 500                if (n > PAGE_SIZE)
 501                        n = PAGE_SIZE;
 502                if (copy_page_from_iter(page, 0, n, iter) != n)
 503                        return -EFAULT;
 504                size -= n;
 505        }
 506        return 0;
 507}
 508
 509/*
 510 * we've been handed an iovec, we need to fill it from
 511 * the shared memory descriptor at "buffer_index".
 512 */
 513int orangefs_bufmap_copy_to_iovec(struct iov_iter *iter,
 514                                    int buffer_index,
 515                                    size_t size)
 516{
 517        struct orangefs_bufmap_desc *from;
 518        int i;
 519
 520        from = &__orangefs_bufmap->desc_array[buffer_index];
 521        gossip_debug(GOSSIP_BUFMAP_DEBUG,
 522                     "%s: buffer_index:%d: size:%zu:\n",
 523                     __func__, buffer_index, size);
 524
 525
 526        for (i = 0; size; i++) {
 527                struct page *page = from->page_array[i];
 528                size_t n = size;
 529                if (n > PAGE_SIZE)
 530                        n = PAGE_SIZE;
 531                n = copy_page_to_iter(page, 0, n, iter);
 532                if (!n)
 533                        return -EFAULT;
 534                size -= n;
 535        }
 536        return 0;
 537}
 538
 539void orangefs_bufmap_page_fill(void *page_to,
 540                                int buffer_index,
 541                                int slot_index)
 542{
 543        struct orangefs_bufmap_desc *from;
 544        void *page_from;
 545
 546        from = &__orangefs_bufmap->desc_array[buffer_index];
 547        page_from = kmap_atomic(from->page_array[slot_index]);
 548        memcpy(page_to, page_from, PAGE_SIZE);
 549        kunmap_atomic(page_from);
 550}
 551