7979
8080struct gen_estimator
8181{
82- struct gen_estimator * next ;
82+ struct list_head list ;
8383 struct gnet_stats_basic * bstats ;
8484 struct gnet_stats_rate_est * rate_est ;
8585 spinlock_t * stats_lock ;
86- unsigned interval ;
8786 int ewma_log ;
8887 u64 last_bytes ;
8988 u32 last_packets ;
9089 u32 avpps ;
9190 u32 avbps ;
91+ struct rcu_head e_rcu ;
9292};
9393
9494struct gen_estimator_head
9595{
9696 struct timer_list timer ;
97- struct gen_estimator * list ;
97+ struct list_head list ;
9898};
9999
100100static struct gen_estimator_head elist [EST_MAX_INTERVAL + 1 ];
101101
102- /* Estimator array lock */
102+ /* Protects against NULL dereference */
103103static DEFINE_RWLOCK (est_lock );
104104
105105static void est_timer (unsigned long arg )
106106{
107107 int idx = (int )arg ;
108108 struct gen_estimator * e ;
109109
110- read_lock ( & est_lock );
111- for ( e = elist [idx ].list ; e ; e = e -> next ) {
110+ rcu_read_lock ( );
111+ list_for_each_entry_rcu ( e , & elist [idx ].list , list ) {
112112 u64 nbytes ;
113113 u32 npackets ;
114114 u32 rate ;
115115
116116 spin_lock (e -> stats_lock );
117+ read_lock (& est_lock );
118+ if (e -> bstats == NULL )
119+ goto skip ;
120+
117121 nbytes = e -> bstats -> bytes ;
118122 npackets = e -> bstats -> packets ;
119123 rate = (nbytes - e -> last_bytes )<<(7 - idx );
@@ -125,12 +129,14 @@ static void est_timer(unsigned long arg)
125129 e -> last_packets = npackets ;
126130 e -> avpps += ((long )rate - (long )e -> avpps ) >> e -> ewma_log ;
127131 e -> rate_est -> pps = (e -> avpps + 0x1FF )>>10 ;
132+ skip :
133+ read_unlock (& est_lock );
128134 spin_unlock (e -> stats_lock );
129135 }
130136
131- if (elist [idx ].list != NULL )
137+ if (! list_empty ( & elist [idx ].list ) )
132138 mod_timer (& elist [idx ].timer , jiffies + ((HZ <<idx )/4 ));
133- read_unlock ( & est_lock );
139+ rcu_read_unlock ( );
134140}
135141
136142/**
@@ -147,12 +153,17 @@ static void est_timer(unsigned long arg)
147153 * &rate_est with the statistics lock grabed during this period.
148154 *
149155 * Returns 0 on success or a negative error code.
156+ *
157+ * NOTE: Called under rtnl_mutex
150158 */
151159int gen_new_estimator (struct gnet_stats_basic * bstats ,
152- struct gnet_stats_rate_est * rate_est , spinlock_t * stats_lock , struct rtattr * opt )
160+ struct gnet_stats_rate_est * rate_est ,
161+ spinlock_t * stats_lock ,
162+ struct rtattr * opt )
153163{
154164 struct gen_estimator * est ;
155165 struct gnet_estimator * parm = RTA_DATA (opt );
166+ int idx ;
156167
157168 if (RTA_PAYLOAD (opt ) < sizeof (* parm ))
158169 return - EINVAL ;
@@ -164,7 +175,7 @@ int gen_new_estimator(struct gnet_stats_basic *bstats,
164175 if (est == NULL )
165176 return - ENOBUFS ;
166177
167- est -> interval = parm -> interval + 2 ;
178+ idx = parm -> interval + 2 ;
168179 est -> bstats = bstats ;
169180 est -> rate_est = rate_est ;
170181 est -> stats_lock = stats_lock ;
@@ -174,52 +185,58 @@ int gen_new_estimator(struct gnet_stats_basic *bstats,
174185 est -> last_packets = bstats -> packets ;
175186 est -> avpps = rate_est -> pps <<10 ;
176187
177- est -> next = elist [est -> interval ].list ;
178- if (est -> next == NULL ) {
179- init_timer (& elist [est -> interval ].timer );
180- elist [est -> interval ].timer .data = est -> interval ;
181- elist [est -> interval ].timer .expires = jiffies + ((HZ <<est -> interval )/4 );
182- elist [est -> interval ].timer .function = est_timer ;
183- add_timer (& elist [est -> interval ].timer );
188+ if (!elist [idx ].timer .function ) {
189+ INIT_LIST_HEAD (& elist [idx ].list );
190+ setup_timer (& elist [idx ].timer , est_timer , idx );
184191 }
185- write_lock_bh (& est_lock );
186- elist [est -> interval ].list = est ;
187- write_unlock_bh (& est_lock );
192+
193+ if (list_empty (& elist [idx ].list ))
194+ mod_timer (& elist [idx ].timer , jiffies + ((HZ <<idx )/4 ));
195+
196+ list_add_rcu (& est -> list , & elist [idx ].list );
188197 return 0 ;
189198}
190199
200+ static void __gen_kill_estimator (struct rcu_head * head )
201+ {
202+ struct gen_estimator * e = container_of (head ,
203+ struct gen_estimator , e_rcu );
204+ kfree (e );
205+ }
206+
191207/**
192208 * gen_kill_estimator - remove a rate estimator
193209 * @bstats: basic statistics
194210 * @rate_est: rate estimator statistics
195211 *
196212 * Removes the rate estimator specified by &bstats and &rate_est
197213 * and deletes the timer.
214+ *
215+ * NOTE: Called under rtnl_mutex
198216 */
199217void gen_kill_estimator (struct gnet_stats_basic * bstats ,
200218 struct gnet_stats_rate_est * rate_est )
201219{
202220 int idx ;
203- struct gen_estimator * est , * * pest ;
221+ struct gen_estimator * e , * n ;
204222
205223 for (idx = 0 ; idx <= EST_MAX_INTERVAL ; idx ++ ) {
206- int killed = 0 ;
207- pest = & elist [idx ].list ;
208- while ((est = * pest ) != NULL ) {
209- if (est -> rate_est != rate_est || est -> bstats != bstats ) {
210- pest = & est -> next ;
224+
225+ /* Skip non initialized indexes */
226+ if (!elist [idx ].timer .function )
227+ continue ;
228+
229+ list_for_each_entry_safe (e , n , & elist [idx ].list , list ) {
230+ if (e -> rate_est != rate_est || e -> bstats != bstats )
211231 continue ;
212- }
213232
214233 write_lock_bh (& est_lock );
215- * pest = est -> next ;
234+ e -> bstats = NULL ;
216235 write_unlock_bh (& est_lock );
217236
218- kfree ( est );
219- killed ++ ;
237+ list_del_rcu ( & e -> list );
238+ call_rcu ( & e -> e_rcu , __gen_kill_estimator ) ;
220239 }
221- if (killed && elist [idx ].list == NULL )
222- del_timer (& elist [idx ].timer );
223240 }
224241}
225242
0 commit comments