/*1* Copyright (c) 2006 Oracle. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*31*/32#include <linux/highmem.h>33#include <linux/gfp.h>3435#include "rds.h"3637struct rds_page_remainder {38struct page *r_page;39unsigned long r_offset;40};4142static DEFINE_PER_CPU_SHARED_ALIGNED(struct rds_page_remainder,43rds_page_remainders);4445/*46* returns 0 on success or -errno on failure.47*48* We don't have to worry about flush_dcache_page() as this only works49* with private pages. If, say, we were to do directed receive to pinned50* user pages we'd have to worry more about cache coherence. (Though51* the flush_dcache_page() in get_user_pages() would probably be enough).52*/53int rds_page_copy_user(struct page *page, unsigned long offset,54void __user *ptr, unsigned long bytes,55int to_user)56{57unsigned long ret;58void *addr;5960addr = kmap(page);61if (to_user) {62rds_stats_add(s_copy_to_user, bytes);63ret = copy_to_user(ptr, addr + offset, bytes);64} else {65rds_stats_add(s_copy_from_user, bytes);66ret = copy_from_user(addr + offset, ptr, bytes);67}68kunmap(page);6970return ret ? -EFAULT : 0;71}72EXPORT_SYMBOL_GPL(rds_page_copy_user);7374/*75* Message allocation uses this to build up regions of a message.76*77* @bytes - the number of bytes needed.78* @gfp - the waiting behaviour of the allocation79*80* @gfp is always ored with __GFP_HIGHMEM. Callers must be prepared to81* kmap the pages, etc.82*83* If @bytes is at least a full page then this just returns a page from84* alloc_page().85*86* If @bytes is a partial page then this stores the unused region of the87* page in a per-cpu structure. Future partial-page allocations may be88* satisfied from that cached region. This lets us waste less memory on89* small allocations with minimal complexity. It works because the transmit90* path passes read-only page regions down to devices. They hold a page91* reference until they are done with the region.92*/93int rds_page_remainder_alloc(struct scatterlist *scat, unsigned long bytes,94gfp_t gfp)95{96struct rds_page_remainder *rem;97unsigned long flags;98struct page *page;99int ret;100101gfp |= __GFP_HIGHMEM;102103/* jump straight to allocation if we're trying for a huge page */104if (bytes >= PAGE_SIZE) {105page = alloc_page(gfp);106if (!page) {107ret = -ENOMEM;108} else {109sg_set_page(scat, page, PAGE_SIZE, 0);110ret = 0;111}112goto out;113}114115rem = &per_cpu(rds_page_remainders, get_cpu());116local_irq_save(flags);117118while (1) {119/* avoid a tiny region getting stuck by tossing it */120if (rem->r_page && bytes > (PAGE_SIZE - rem->r_offset)) {121rds_stats_inc(s_page_remainder_miss);122__free_page(rem->r_page);123rem->r_page = NULL;124}125126/* hand out a fragment from the cached page */127if (rem->r_page && bytes <= (PAGE_SIZE - rem->r_offset)) {128sg_set_page(scat, rem->r_page, bytes, rem->r_offset);129get_page(sg_page(scat));130131if (rem->r_offset != 0)132rds_stats_inc(s_page_remainder_hit);133134rem->r_offset += bytes;135if (rem->r_offset == PAGE_SIZE) {136__free_page(rem->r_page);137rem->r_page = NULL;138}139ret = 0;140break;141}142143/* alloc if there is nothing for us to use */144local_irq_restore(flags);145put_cpu();146147page = alloc_page(gfp);148149rem = &per_cpu(rds_page_remainders, get_cpu());150local_irq_save(flags);151152if (!page) {153ret = -ENOMEM;154break;155}156157/* did someone race to fill the remainder before us? */158if (rem->r_page) {159__free_page(page);160continue;161}162163/* otherwise install our page and loop around to alloc */164rem->r_page = page;165rem->r_offset = 0;166}167168local_irq_restore(flags);169put_cpu();170out:171rdsdebug("bytes %lu ret %d %p %u %u\n", bytes, ret,172ret ? NULL : sg_page(scat), ret ? 0 : scat->offset,173ret ? 0 : scat->length);174return ret;175}176EXPORT_SYMBOL_GPL(rds_page_remainder_alloc);177178static int rds_page_remainder_cpu_notify(struct notifier_block *self,179unsigned long action, void *hcpu)180{181struct rds_page_remainder *rem;182long cpu = (long)hcpu;183184rem = &per_cpu(rds_page_remainders, cpu);185186rdsdebug("cpu %ld action 0x%lx\n", cpu, action);187188switch (action) {189case CPU_DEAD:190if (rem->r_page)191__free_page(rem->r_page);192rem->r_page = NULL;193break;194}195196return 0;197}198199static struct notifier_block rds_page_remainder_nb = {200.notifier_call = rds_page_remainder_cpu_notify,201};202203void rds_page_exit(void)204{205int i;206207for_each_possible_cpu(i)208rds_page_remainder_cpu_notify(&rds_page_remainder_nb,209(unsigned long)CPU_DEAD,210(void *)(long)i);211}212213214