Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2006 Oracle.  All rights reserved.
0003  *
0004  * This software is available to you under a choice of one of two
0005  * licenses.  You may choose to be licensed under the terms of the GNU
0006  * General Public License (GPL) Version 2, available from the file
0007  * COPYING in the main directory of this source tree, or the
0008  * OpenIB.org BSD license below:
0009  *
0010  *     Redistribution and use in source and binary forms, with or
0011  *     without modification, are permitted provided that the following
0012  *     conditions are met:
0013  *
0014  *      - Redistributions of source code must retain the above
0015  *        copyright notice, this list of conditions and the following
0016  *        disclaimer.
0017  *
0018  *      - Redistributions in binary form must reproduce the above
0019  *        copyright notice, this list of conditions and the following
0020  *        disclaimer in the documentation and/or other materials
0021  *        provided with the distribution.
0022  *
0023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0024  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0025  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0026  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0027  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0028  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0029  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0030  * SOFTWARE.
0031  *
0032  */
0033 #include <linux/percpu.h>
0034 #include <linux/seq_file.h>
0035 #include <linux/slab.h>
0036 #include <linux/proc_fs.h>
0037 #include <linux/export.h>
0038 
0039 #include "rds.h"
0040 
0041 /*
0042  * This file implements a getsockopt() call which copies a set of fixed
0043  * sized structs into a user-specified buffer as a means of providing
0044  * read-only information about RDS.
0045  *
0046  * For a given information source there are a given number of fixed sized
0047  * structs at a given time.  The structs are only copied if the user-specified
0048  * buffer is big enough.  The destination pages that make up the buffer
0049  * are pinned for the duration of the copy.
0050  *
0051  * This gives us the following benefits:
0052  *
0053  * - simple implementation, no copy "position" across multiple calls
0054  * - consistent snapshot of an info source
0055  * - atomic copy works well with whatever locking info source has
0056  * - one portable tool to get rds info across implementations
0057  * - long-lived tool can get info without allocating
0058  *
0059  * at the following costs:
0060  *
0061  * - info source copy must be pinned, may be "large"
0062  */
0063 
0064 struct rds_info_iterator {
0065     struct page **pages;
0066     void *addr;
0067     unsigned long offset;
0068 };
0069 
0070 static DEFINE_SPINLOCK(rds_info_lock);
0071 static rds_info_func rds_info_funcs[RDS_INFO_LAST - RDS_INFO_FIRST + 1];
0072 
0073 void rds_info_register_func(int optname, rds_info_func func)
0074 {
0075     int offset = optname - RDS_INFO_FIRST;
0076 
0077     BUG_ON(optname < RDS_INFO_FIRST || optname > RDS_INFO_LAST);
0078 
0079     spin_lock(&rds_info_lock);
0080     BUG_ON(rds_info_funcs[offset]);
0081     rds_info_funcs[offset] = func;
0082     spin_unlock(&rds_info_lock);
0083 }
0084 EXPORT_SYMBOL_GPL(rds_info_register_func);
0085 
0086 void rds_info_deregister_func(int optname, rds_info_func func)
0087 {
0088     int offset = optname - RDS_INFO_FIRST;
0089 
0090     BUG_ON(optname < RDS_INFO_FIRST || optname > RDS_INFO_LAST);
0091 
0092     spin_lock(&rds_info_lock);
0093     BUG_ON(rds_info_funcs[offset] != func);
0094     rds_info_funcs[offset] = NULL;
0095     spin_unlock(&rds_info_lock);
0096 }
0097 EXPORT_SYMBOL_GPL(rds_info_deregister_func);
0098 
0099 /*
0100  * Typically we hold an atomic kmap across multiple rds_info_copy() calls
0101  * because the kmap is so expensive.  This must be called before using blocking
0102  * operations while holding the mapping and as the iterator is torn down.
0103  */
0104 void rds_info_iter_unmap(struct rds_info_iterator *iter)
0105 {
0106     if (iter->addr) {
0107         kunmap_atomic(iter->addr);
0108         iter->addr = NULL;
0109     }
0110 }
0111 
0112 /*
0113  * get_user_pages() called flush_dcache_page() on the pages for us.
0114  */
0115 void rds_info_copy(struct rds_info_iterator *iter, void *data,
0116            unsigned long bytes)
0117 {
0118     unsigned long this;
0119 
0120     while (bytes) {
0121         if (!iter->addr)
0122             iter->addr = kmap_atomic(*iter->pages);
0123 
0124         this = min(bytes, PAGE_SIZE - iter->offset);
0125 
0126         rdsdebug("page %p addr %p offset %lu this %lu data %p "
0127               "bytes %lu\n", *iter->pages, iter->addr,
0128               iter->offset, this, data, bytes);
0129 
0130         memcpy(iter->addr + iter->offset, data, this);
0131 
0132         data += this;
0133         bytes -= this;
0134         iter->offset += this;
0135 
0136         if (iter->offset == PAGE_SIZE) {
0137             kunmap_atomic(iter->addr);
0138             iter->addr = NULL;
0139             iter->offset = 0;
0140             iter->pages++;
0141         }
0142     }
0143 }
0144 EXPORT_SYMBOL_GPL(rds_info_copy);
0145 
0146 /*
0147  * @optval points to the userspace buffer that the information snapshot
0148  * will be copied into.
0149  *
0150  * @optlen on input is the size of the buffer in userspace.  @optlen
0151  * on output is the size of the requested snapshot in bytes.
0152  *
0153  * This function returns -errno if there is a failure, particularly -ENOSPC
0154  * if the given userspace buffer was not large enough to fit the snapshot.
0155  * On success it returns the positive number of bytes of each array element
0156  * in the snapshot.
0157  */
0158 int rds_info_getsockopt(struct socket *sock, int optname, char __user *optval,
0159             int __user *optlen)
0160 {
0161     struct rds_info_iterator iter;
0162     struct rds_info_lengths lens;
0163     unsigned long nr_pages = 0;
0164     unsigned long start;
0165     rds_info_func func;
0166     struct page **pages = NULL;
0167     int ret;
0168     int len;
0169     int total;
0170 
0171     if (get_user(len, optlen)) {
0172         ret = -EFAULT;
0173         goto out;
0174     }
0175 
0176     /* check for all kinds of wrapping and the like */
0177     start = (unsigned long)optval;
0178     if (len < 0 || len > INT_MAX - PAGE_SIZE + 1 || start + len < start) {
0179         ret = -EINVAL;
0180         goto out;
0181     }
0182 
0183     /* a 0 len call is just trying to probe its length */
0184     if (len == 0)
0185         goto call_func;
0186 
0187     nr_pages = (PAGE_ALIGN(start + len) - (start & PAGE_MASK))
0188             >> PAGE_SHIFT;
0189 
0190     pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
0191     if (!pages) {
0192         ret = -ENOMEM;
0193         goto out;
0194     }
0195     ret = pin_user_pages_fast(start, nr_pages, FOLL_WRITE, pages);
0196     if (ret != nr_pages) {
0197         if (ret > 0)
0198             nr_pages = ret;
0199         else
0200             nr_pages = 0;
0201         ret = -EAGAIN; /* XXX ? */
0202         goto out;
0203     }
0204 
0205     rdsdebug("len %d nr_pages %lu\n", len, nr_pages);
0206 
0207 call_func:
0208     func = rds_info_funcs[optname - RDS_INFO_FIRST];
0209     if (!func) {
0210         ret = -ENOPROTOOPT;
0211         goto out;
0212     }
0213 
0214     iter.pages = pages;
0215     iter.addr = NULL;
0216     iter.offset = start & (PAGE_SIZE - 1);
0217 
0218     func(sock, len, &iter, &lens);
0219     BUG_ON(lens.each == 0);
0220 
0221     total = lens.nr * lens.each;
0222 
0223     rds_info_iter_unmap(&iter);
0224 
0225     if (total > len) {
0226         len = total;
0227         ret = -ENOSPC;
0228     } else {
0229         len = total;
0230         ret = lens.each;
0231     }
0232 
0233     if (put_user(len, optlen))
0234         ret = -EFAULT;
0235 
0236 out:
0237     if (pages)
0238         unpin_user_pages(pages, nr_pages);
0239     kfree(pages);
0240 
0241     return ret;
0242 }