xref: /linux/net/rds/page.c (revision 1b98f357dadd6ea613a435fbaef1a5dd7b35fd21)
1 /*
2  * Copyright (c) 2006 Oracle.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33 #include <linux/highmem.h>
34 #include <linux/gfp.h>
35 #include <linux/cpu.h>
36 #include <linux/export.h>
37 
38 #include "rds.h"
39 
40 struct rds_page_remainder {
41 	struct page	*r_page;
42 	unsigned long	r_offset;
43 	local_lock_t	bh_lock;
44 };
45 
46 static DEFINE_PER_CPU_SHARED_ALIGNED(struct rds_page_remainder, rds_page_remainders) = {
47 	.bh_lock = INIT_LOCAL_LOCK(bh_lock),
48 };
49 
50 /**
51  * rds_page_remainder_alloc - build up regions of a message.
52  *
53  * @scat: Scatter list for message
54  * @bytes: the number of bytes needed.
55  * @gfp: the waiting behaviour of the allocation
56  *
57  * @gfp is always ored with __GFP_HIGHMEM.  Callers must be prepared to
58  * kmap the pages, etc.
59  *
60  * If @bytes is at least a full page then this just returns a page from
61  * alloc_page().
62  *
63  * If @bytes is a partial page then this stores the unused region of the
64  * page in a per-cpu structure.  Future partial-page allocations may be
65  * satisfied from that cached region.  This lets us waste less memory on
66  * small allocations with minimal complexity.  It works because the transmit
67  * path passes read-only page regions down to devices.  They hold a page
68  * reference until they are done with the region.
69  */
70 int rds_page_remainder_alloc(struct scatterlist *scat, unsigned long bytes,
71 			     gfp_t gfp)
72 {
73 	struct rds_page_remainder *rem;
74 	struct page *page;
75 	int ret;
76 
77 	gfp |= __GFP_HIGHMEM;
78 
79 	/* jump straight to allocation if we're trying for a huge page */
80 	if (bytes >= PAGE_SIZE) {
81 		page = alloc_page(gfp);
82 		if (!page) {
83 			ret = -ENOMEM;
84 		} else {
85 			sg_set_page(scat, page, PAGE_SIZE, 0);
86 			ret = 0;
87 		}
88 		goto out;
89 	}
90 
91 	local_bh_disable();
92 	local_lock_nested_bh(&rds_page_remainders.bh_lock);
93 	rem = this_cpu_ptr(&rds_page_remainders);
94 
95 	while (1) {
96 		/* avoid a tiny region getting stuck by tossing it */
97 		if (rem->r_page && bytes > (PAGE_SIZE - rem->r_offset)) {
98 			rds_stats_inc(s_page_remainder_miss);
99 			__free_page(rem->r_page);
100 			rem->r_page = NULL;
101 		}
102 
103 		/* hand out a fragment from the cached page */
104 		if (rem->r_page && bytes <= (PAGE_SIZE - rem->r_offset)) {
105 			sg_set_page(scat, rem->r_page, bytes, rem->r_offset);
106 			get_page(sg_page(scat));
107 
108 			if (rem->r_offset != 0)
109 				rds_stats_inc(s_page_remainder_hit);
110 
111 			rem->r_offset += ALIGN(bytes, 8);
112 			if (rem->r_offset >= PAGE_SIZE) {
113 				__free_page(rem->r_page);
114 				rem->r_page = NULL;
115 			}
116 			ret = 0;
117 			break;
118 		}
119 
120 		/* alloc if there is nothing for us to use */
121 		local_unlock_nested_bh(&rds_page_remainders.bh_lock);
122 		local_bh_enable();
123 
124 		page = alloc_page(gfp);
125 
126 		local_bh_disable();
127 		local_lock_nested_bh(&rds_page_remainders.bh_lock);
128 		rem = this_cpu_ptr(&rds_page_remainders);
129 
130 		if (!page) {
131 			ret = -ENOMEM;
132 			break;
133 		}
134 
135 		/* did someone race to fill the remainder before us? */
136 		if (rem->r_page) {
137 			__free_page(page);
138 			continue;
139 		}
140 
141 		/* otherwise install our page and loop around to alloc */
142 		rem->r_page = page;
143 		rem->r_offset = 0;
144 	}
145 
146 	local_unlock_nested_bh(&rds_page_remainders.bh_lock);
147 	local_bh_enable();
148 out:
149 	rdsdebug("bytes %lu ret %d %p %u %u\n", bytes, ret,
150 		 ret ? NULL : sg_page(scat), ret ? 0 : scat->offset,
151 		 ret ? 0 : scat->length);
152 	return ret;
153 }
154 EXPORT_SYMBOL_GPL(rds_page_remainder_alloc);
155 
156 void rds_page_exit(void)
157 {
158 	unsigned int cpu;
159 
160 	for_each_possible_cpu(cpu) {
161 		struct rds_page_remainder *rem;
162 
163 		rem = &per_cpu(rds_page_remainders, cpu);
164 		rdsdebug("cpu %u\n", cpu);
165 
166 		if (rem->r_page)
167 			__free_page(rem->r_page);
168 		rem->r_page = NULL;
169 	}
170 }
171