Commit c3484c275db5abe88e01a7e1a80932112c9bbf69

Authored by Richard Cochran
Committed by David S. Miller
1 parent c7ec0badcc

ptp: reduce stack usage when measuring the system time offset

This patch removes the large buffer from the stack of the system
offset ioctl and replaces it with a kmalloced buffer.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Showing 1 changed file with 14 additions and 7 deletions Inline Diff

drivers/ptp/ptp_chardev.c
1 /* 1 /*
2 * PTP 1588 clock support - character device implementation. 2 * PTP 1588 clock support - character device implementation.
3 * 3 *
4 * Copyright (C) 2010 OMICRON electronics GmbH 4 * Copyright (C) 2010 OMICRON electronics GmbH
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 * 10 *
11 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software 17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */ 19 */
20 #include <linux/module.h> 20 #include <linux/module.h>
21 #include <linux/posix-clock.h> 21 #include <linux/posix-clock.h>
22 #include <linux/poll.h> 22 #include <linux/poll.h>
23 #include <linux/sched.h> 23 #include <linux/sched.h>
24 #include <linux/slab.h> 24 #include <linux/slab.h>
25 25
26 #include "ptp_private.h" 26 #include "ptp_private.h"
27 27
28 int ptp_open(struct posix_clock *pc, fmode_t fmode) 28 int ptp_open(struct posix_clock *pc, fmode_t fmode)
29 { 29 {
30 return 0; 30 return 0;
31 } 31 }
32 32
33 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg) 33 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
34 { 34 {
35 struct ptp_clock_caps caps; 35 struct ptp_clock_caps caps;
36 struct ptp_clock_request req; 36 struct ptp_clock_request req;
37 struct ptp_sys_offset sysoff; 37 struct ptp_sys_offset *sysoff = NULL;
38 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); 38 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
39 struct ptp_clock_info *ops = ptp->info; 39 struct ptp_clock_info *ops = ptp->info;
40 struct ptp_clock_time *pct; 40 struct ptp_clock_time *pct;
41 struct timespec ts; 41 struct timespec ts;
42 int enable, err = 0; 42 int enable, err = 0;
43 unsigned int i; 43 unsigned int i;
44 44
45 switch (cmd) { 45 switch (cmd) {
46 46
47 case PTP_CLOCK_GETCAPS: 47 case PTP_CLOCK_GETCAPS:
48 memset(&caps, 0, sizeof(caps)); 48 memset(&caps, 0, sizeof(caps));
49 caps.max_adj = ptp->info->max_adj; 49 caps.max_adj = ptp->info->max_adj;
50 caps.n_alarm = ptp->info->n_alarm; 50 caps.n_alarm = ptp->info->n_alarm;
51 caps.n_ext_ts = ptp->info->n_ext_ts; 51 caps.n_ext_ts = ptp->info->n_ext_ts;
52 caps.n_per_out = ptp->info->n_per_out; 52 caps.n_per_out = ptp->info->n_per_out;
53 caps.pps = ptp->info->pps; 53 caps.pps = ptp->info->pps;
54 if (copy_to_user((void __user *)arg, &caps, sizeof(caps))) 54 if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
55 err = -EFAULT; 55 err = -EFAULT;
56 break; 56 break;
57 57
58 case PTP_EXTTS_REQUEST: 58 case PTP_EXTTS_REQUEST:
59 if (copy_from_user(&req.extts, (void __user *)arg, 59 if (copy_from_user(&req.extts, (void __user *)arg,
60 sizeof(req.extts))) { 60 sizeof(req.extts))) {
61 err = -EFAULT; 61 err = -EFAULT;
62 break; 62 break;
63 } 63 }
64 if (req.extts.index >= ops->n_ext_ts) { 64 if (req.extts.index >= ops->n_ext_ts) {
65 err = -EINVAL; 65 err = -EINVAL;
66 break; 66 break;
67 } 67 }
68 req.type = PTP_CLK_REQ_EXTTS; 68 req.type = PTP_CLK_REQ_EXTTS;
69 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0; 69 enable = req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0;
70 err = ops->enable(ops, &req, enable); 70 err = ops->enable(ops, &req, enable);
71 break; 71 break;
72 72
73 case PTP_PEROUT_REQUEST: 73 case PTP_PEROUT_REQUEST:
74 if (copy_from_user(&req.perout, (void __user *)arg, 74 if (copy_from_user(&req.perout, (void __user *)arg,
75 sizeof(req.perout))) { 75 sizeof(req.perout))) {
76 err = -EFAULT; 76 err = -EFAULT;
77 break; 77 break;
78 } 78 }
79 if (req.perout.index >= ops->n_per_out) { 79 if (req.perout.index >= ops->n_per_out) {
80 err = -EINVAL; 80 err = -EINVAL;
81 break; 81 break;
82 } 82 }
83 req.type = PTP_CLK_REQ_PEROUT; 83 req.type = PTP_CLK_REQ_PEROUT;
84 enable = req.perout.period.sec || req.perout.period.nsec; 84 enable = req.perout.period.sec || req.perout.period.nsec;
85 err = ops->enable(ops, &req, enable); 85 err = ops->enable(ops, &req, enable);
86 break; 86 break;
87 87
88 case PTP_ENABLE_PPS: 88 case PTP_ENABLE_PPS:
89 if (!capable(CAP_SYS_TIME)) 89 if (!capable(CAP_SYS_TIME))
90 return -EPERM; 90 return -EPERM;
91 req.type = PTP_CLK_REQ_PPS; 91 req.type = PTP_CLK_REQ_PPS;
92 enable = arg ? 1 : 0; 92 enable = arg ? 1 : 0;
93 err = ops->enable(ops, &req, enable); 93 err = ops->enable(ops, &req, enable);
94 break; 94 break;
95 95
96 case PTP_SYS_OFFSET: 96 case PTP_SYS_OFFSET:
97 if (copy_from_user(&sysoff, (void __user *)arg, 97 sysoff = kmalloc(sizeof(*sysoff), GFP_KERNEL);
98 sizeof(sysoff))) { 98 if (!sysoff) {
99 err = -ENOMEM;
100 break;
101 }
102 if (copy_from_user(sysoff, (void __user *)arg,
103 sizeof(*sysoff))) {
99 err = -EFAULT; 104 err = -EFAULT;
100 break; 105 break;
101 } 106 }
102 if (sysoff.n_samples > PTP_MAX_SAMPLES) { 107 if (sysoff->n_samples > PTP_MAX_SAMPLES) {
103 err = -EINVAL; 108 err = -EINVAL;
104 break; 109 break;
105 } 110 }
106 pct = &sysoff.ts[0]; 111 pct = &sysoff->ts[0];
107 for (i = 0; i < sysoff.n_samples; i++) { 112 for (i = 0; i < sysoff->n_samples; i++) {
108 getnstimeofday(&ts); 113 getnstimeofday(&ts);
109 pct->sec = ts.tv_sec; 114 pct->sec = ts.tv_sec;
110 pct->nsec = ts.tv_nsec; 115 pct->nsec = ts.tv_nsec;
111 pct++; 116 pct++;
112 ptp->info->gettime(ptp->info, &ts); 117 ptp->info->gettime(ptp->info, &ts);
113 pct->sec = ts.tv_sec; 118 pct->sec = ts.tv_sec;
114 pct->nsec = ts.tv_nsec; 119 pct->nsec = ts.tv_nsec;
115 pct++; 120 pct++;
116 } 121 }
117 getnstimeofday(&ts); 122 getnstimeofday(&ts);
118 pct->sec = ts.tv_sec; 123 pct->sec = ts.tv_sec;
119 pct->nsec = ts.tv_nsec; 124 pct->nsec = ts.tv_nsec;
120 if (copy_to_user((void __user *)arg, &sysoff, sizeof(sysoff))) 125 if (copy_to_user((void __user *)arg, sysoff, sizeof(*sysoff)))
121 err = -EFAULT; 126 err = -EFAULT;
122 break; 127 break;
123 128
124 default: 129 default:
125 err = -ENOTTY; 130 err = -ENOTTY;
126 break; 131 break;
127 } 132 }
133
134 kfree(sysoff);
128 return err; 135 return err;
129 } 136 }
130 137
131 unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait) 138 unsigned int ptp_poll(struct posix_clock *pc, struct file *fp, poll_table *wait)
132 { 139 {
133 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); 140 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
134 141
135 poll_wait(fp, &ptp->tsev_wq, wait); 142 poll_wait(fp, &ptp->tsev_wq, wait);
136 143
137 return queue_cnt(&ptp->tsevq) ? POLLIN : 0; 144 return queue_cnt(&ptp->tsevq) ? POLLIN : 0;
138 } 145 }
139 146
140 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event)) 147 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event))
141 148
142 ssize_t ptp_read(struct posix_clock *pc, 149 ssize_t ptp_read(struct posix_clock *pc,
143 uint rdflags, char __user *buf, size_t cnt) 150 uint rdflags, char __user *buf, size_t cnt)
144 { 151 {
145 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock); 152 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
146 struct timestamp_event_queue *queue = &ptp->tsevq; 153 struct timestamp_event_queue *queue = &ptp->tsevq;
147 struct ptp_extts_event *event; 154 struct ptp_extts_event *event;
148 unsigned long flags; 155 unsigned long flags;
149 size_t qcnt, i; 156 size_t qcnt, i;
150 int result; 157 int result;
151 158
152 if (cnt % sizeof(struct ptp_extts_event) != 0) 159 if (cnt % sizeof(struct ptp_extts_event) != 0)
153 return -EINVAL; 160 return -EINVAL;
154 161
155 if (cnt > EXTTS_BUFSIZE) 162 if (cnt > EXTTS_BUFSIZE)
156 cnt = EXTTS_BUFSIZE; 163 cnt = EXTTS_BUFSIZE;
157 164
158 cnt = cnt / sizeof(struct ptp_extts_event); 165 cnt = cnt / sizeof(struct ptp_extts_event);
159 166
160 if (mutex_lock_interruptible(&ptp->tsevq_mux)) 167 if (mutex_lock_interruptible(&ptp->tsevq_mux))
161 return -ERESTARTSYS; 168 return -ERESTARTSYS;
162 169
163 if (wait_event_interruptible(ptp->tsev_wq, 170 if (wait_event_interruptible(ptp->tsev_wq,
164 ptp->defunct || queue_cnt(queue))) { 171 ptp->defunct || queue_cnt(queue))) {
165 mutex_unlock(&ptp->tsevq_mux); 172 mutex_unlock(&ptp->tsevq_mux);
166 return -ERESTARTSYS; 173 return -ERESTARTSYS;
167 } 174 }
168 175
169 if (ptp->defunct) { 176 if (ptp->defunct) {
170 mutex_unlock(&ptp->tsevq_mux); 177 mutex_unlock(&ptp->tsevq_mux);
171 return -ENODEV; 178 return -ENODEV;
172 } 179 }
173 180
174 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); 181 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL);
175 if (!event) { 182 if (!event) {
176 mutex_unlock(&ptp->tsevq_mux); 183 mutex_unlock(&ptp->tsevq_mux);
177 return -ENOMEM; 184 return -ENOMEM;
178 } 185 }
179 186
180 spin_lock_irqsave(&queue->lock, flags); 187 spin_lock_irqsave(&queue->lock, flags);
181 188
182 qcnt = queue_cnt(queue); 189 qcnt = queue_cnt(queue);
183 190
184 if (cnt > qcnt) 191 if (cnt > qcnt)
185 cnt = qcnt; 192 cnt = qcnt;
186 193
187 for (i = 0; i < cnt; i++) { 194 for (i = 0; i < cnt; i++) {
188 event[i] = queue->buf[queue->head]; 195 event[i] = queue->buf[queue->head];
189 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS; 196 queue->head = (queue->head + 1) % PTP_MAX_TIMESTAMPS;
190 } 197 }
191 198
192 spin_unlock_irqrestore(&queue->lock, flags); 199 spin_unlock_irqrestore(&queue->lock, flags);
193 200
194 cnt = cnt * sizeof(struct ptp_extts_event); 201 cnt = cnt * sizeof(struct ptp_extts_event);
195 202
196 mutex_unlock(&ptp->tsevq_mux); 203 mutex_unlock(&ptp->tsevq_mux);
197 204
198 result = cnt; 205 result = cnt;
199 if (copy_to_user(buf, event, cnt)) 206 if (copy_to_user(buf, event, cnt))
200 result = -EFAULT; 207 result = -EFAULT;
201 208
202 kfree(event); 209 kfree(event);
203 return result; 210 return result;
204 } 211 }
205 212