@@ -460,7 +460,7 @@ static inline void
460460__ip_vs_bind_svc (struct ip_vs_dest * dest , struct ip_vs_service * svc )
461461{
462462 atomic_inc (& svc -> refcnt );
463- dest -> svc = svc ;
463+ rcu_assign_pointer ( dest -> svc , svc ) ;
464464}
465465
466466static void ip_vs_service_free (struct ip_vs_service * svc )
@@ -470,18 +470,25 @@ static void ip_vs_service_free(struct ip_vs_service *svc)
470470 kfree (svc );
471471}
472472
473- static void
474- __ip_vs_unbind_svc (struct ip_vs_dest * dest )
473+ static void ip_vs_service_rcu_free (struct rcu_head * head )
475474{
476- struct ip_vs_service * svc = dest -> svc ;
475+ struct ip_vs_service * svc ;
476+
477+ svc = container_of (head , struct ip_vs_service , rcu_head );
478+ ip_vs_service_free (svc );
479+ }
477480
478- dest -> svc = NULL ;
481+ static void __ip_vs_svc_put (struct ip_vs_service * svc , bool do_delay )
482+ {
479483 if (atomic_dec_and_test (& svc -> refcnt )) {
480484 IP_VS_DBG_BUF (3 , "Removing service %u/%s:%u\n" ,
481485 svc -> fwmark ,
482486 IP_VS_DBG_ADDR (svc -> af , & svc -> addr ),
483487 ntohs (svc -> port ));
484- ip_vs_service_free (svc );
488+ if (do_delay )
489+ call_rcu (& svc -> rcu_head , ip_vs_service_rcu_free );
490+ else
491+ ip_vs_service_free (svc );
485492 }
486493}
487494
@@ -667,11 +674,6 @@ ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
667674 IP_VS_DBG_ADDR (svc -> af , & dest -> addr ),
668675 ntohs (dest -> port ),
669676 atomic_read (& dest -> refcnt ));
670- /* We can not reuse dest while in grace period
671- * because conns still can use dest->svc
672- */
673- if (test_bit (IP_VS_DEST_STATE_REMOVING , & dest -> state ))
674- continue ;
675677 if (dest -> af == svc -> af &&
676678 ip_vs_addr_equal (svc -> af , & dest -> addr , daddr ) &&
677679 dest -> port == dport &&
@@ -697,8 +699,10 @@ ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,
697699
698700static void ip_vs_dest_free (struct ip_vs_dest * dest )
699701{
702+ struct ip_vs_service * svc = rcu_dereference_protected (dest -> svc , 1 );
703+
700704 __ip_vs_dst_cache_reset (dest );
701- __ip_vs_unbind_svc ( dest );
705+ __ip_vs_svc_put ( svc , false );
702706 free_percpu (dest -> stats .cpustats );
703707 kfree (dest );
704708}
@@ -771,6 +775,7 @@ __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
771775 struct ip_vs_dest_user_kern * udest , int add )
772776{
773777 struct netns_ipvs * ipvs = net_ipvs (svc -> net );
778+ struct ip_vs_service * old_svc ;
774779 struct ip_vs_scheduler * sched ;
775780 int conn_flags ;
776781
@@ -792,13 +797,14 @@ __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
792797 atomic_set (& dest -> conn_flags , conn_flags );
793798
794799 /* bind the service */
795- if (!dest -> svc ) {
800+ old_svc = rcu_dereference_protected (dest -> svc , 1 );
801+ if (!old_svc ) {
796802 __ip_vs_bind_svc (dest , svc );
797803 } else {
798- if (dest -> svc != svc ) {
799- __ip_vs_unbind_svc (dest );
804+ if (old_svc != svc ) {
800805 ip_vs_zero_stats (& dest -> stats );
801806 __ip_vs_bind_svc (dest , svc );
807+ __ip_vs_svc_put (old_svc , true);
802808 }
803809 }
804810
@@ -998,16 +1004,6 @@ ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)
9981004 return 0 ;
9991005}
10001006
1001- static void ip_vs_dest_wait_readers (struct rcu_head * head )
1002- {
1003- struct ip_vs_dest * dest = container_of (head , struct ip_vs_dest ,
1004- rcu_head );
1005-
1006- /* End of grace period after unlinking */
1007- clear_bit (IP_VS_DEST_STATE_REMOVING , & dest -> state );
1008- }
1009-
1010-
10111007/*
10121008 * Delete a destination (must be already unlinked from the service)
10131009 */
@@ -1023,20 +1019,16 @@ static void __ip_vs_del_dest(struct net *net, struct ip_vs_dest *dest,
10231019 */
10241020 ip_vs_rs_unhash (dest );
10251021
1026- if (!cleanup ) {
1027- set_bit (IP_VS_DEST_STATE_REMOVING , & dest -> state );
1028- call_rcu (& dest -> rcu_head , ip_vs_dest_wait_readers );
1029- }
1030-
10311022 spin_lock_bh (& ipvs -> dest_trash_lock );
10321023 IP_VS_DBG_BUF (3 , "Moving dest %s:%u into trash, dest->refcnt=%d\n" ,
10331024 IP_VS_DBG_ADDR (dest -> af , & dest -> addr ), ntohs (dest -> port ),
10341025 atomic_read (& dest -> refcnt ));
10351026 if (list_empty (& ipvs -> dest_trash ) && !cleanup )
10361027 mod_timer (& ipvs -> dest_trash_timer ,
1037- jiffies + IP_VS_DEST_TRASH_PERIOD );
1028+ jiffies + ( IP_VS_DEST_TRASH_PERIOD >> 1 ) );
10381029 /* dest lives in trash without reference */
10391030 list_add (& dest -> t_list , & ipvs -> dest_trash );
1031+ dest -> idle_start = 0 ;
10401032 spin_unlock_bh (& ipvs -> dest_trash_lock );
10411033 ip_vs_dest_put (dest );
10421034}
@@ -1108,24 +1100,30 @@ static void ip_vs_dest_trash_expire(unsigned long data)
11081100 struct net * net = (struct net * ) data ;
11091101 struct netns_ipvs * ipvs = net_ipvs (net );
11101102 struct ip_vs_dest * dest , * next ;
1103+ unsigned long now = jiffies ;
11111104
11121105 spin_lock (& ipvs -> dest_trash_lock );
11131106 list_for_each_entry_safe (dest , next , & ipvs -> dest_trash , t_list ) {
1114- /* Skip if dest is in grace period */
1115- if (test_bit (IP_VS_DEST_STATE_REMOVING , & dest -> state ))
1116- continue ;
11171107 if (atomic_read (& dest -> refcnt ) > 0 )
11181108 continue ;
1109+ if (dest -> idle_start ) {
1110+ if (time_before (now , dest -> idle_start +
1111+ IP_VS_DEST_TRASH_PERIOD ))
1112+ continue ;
1113+ } else {
1114+ dest -> idle_start = max (1UL , now );
1115+ continue ;
1116+ }
11191117 IP_VS_DBG_BUF (3 , "Removing destination %u/%s:%u from trash\n" ,
11201118 dest -> vfwmark ,
1121- IP_VS_DBG_ADDR (dest -> svc -> af , & dest -> addr ),
1119+ IP_VS_DBG_ADDR (dest -> af , & dest -> addr ),
11221120 ntohs (dest -> port ));
11231121 list_del (& dest -> t_list );
11241122 ip_vs_dest_free (dest );
11251123 }
11261124 if (!list_empty (& ipvs -> dest_trash ))
11271125 mod_timer (& ipvs -> dest_trash_timer ,
1128- jiffies + IP_VS_DEST_TRASH_PERIOD );
1126+ jiffies + ( IP_VS_DEST_TRASH_PERIOD >> 1 ) );
11291127 spin_unlock (& ipvs -> dest_trash_lock );
11301128}
11311129
@@ -1320,14 +1318,6 @@ ip_vs_edit_service(struct ip_vs_service *svc, struct ip_vs_service_user_kern *u)
13201318 return ret ;
13211319}
13221320
1323- static void ip_vs_service_rcu_free (struct rcu_head * head )
1324- {
1325- struct ip_vs_service * svc ;
1326-
1327- svc = container_of (head , struct ip_vs_service , rcu_head );
1328- ip_vs_service_free (svc );
1329- }
1330-
13311321/*
13321322 * Delete a service from the service list
13331323 * - The service must be unlinked, unlocked and not referenced!
@@ -1376,13 +1366,7 @@ static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)
13761366 /*
13771367 * Free the service if nobody refers to it
13781368 */
1379- if (atomic_dec_and_test (& svc -> refcnt )) {
1380- IP_VS_DBG_BUF (3 , "Removing service %u/%s:%u\n" ,
1381- svc -> fwmark ,
1382- IP_VS_DBG_ADDR (svc -> af , & svc -> addr ),
1383- ntohs (svc -> port ));
1384- call_rcu (& svc -> rcu_head , ip_vs_service_rcu_free );
1385- }
1369+ __ip_vs_svc_put (svc , true);
13861370
13871371 /* decrease the module use count */
13881372 ip_vs_use_count_dec ();
0 commit comments