@@ -3095,9 +3095,8 @@ int tcp_gro_complete(struct sk_buff *skb)
30953095EXPORT_SYMBOL (tcp_gro_complete );
30963096
30973097#ifdef CONFIG_TCP_MD5SIG
3098- static unsigned long tcp_md5sig_users ;
3099- static struct tcp_md5sig_pool __percpu * tcp_md5sig_pool ;
3100- static DEFINE_SPINLOCK (tcp_md5sig_pool_lock );
3098+ static struct tcp_md5sig_pool __percpu * tcp_md5sig_pool __read_mostly ;
3099+ static DEFINE_MUTEX (tcp_md5sig_mutex );
31013100
31023101static void __tcp_free_md5sig_pool (struct tcp_md5sig_pool __percpu * pool )
31033102{
@@ -3112,30 +3111,14 @@ static void __tcp_free_md5sig_pool(struct tcp_md5sig_pool __percpu *pool)
31123111 free_percpu (pool );
31133112}
31143113
3115- void tcp_free_md5sig_pool (void )
3116- {
3117- struct tcp_md5sig_pool __percpu * pool = NULL ;
3118-
3119- spin_lock_bh (& tcp_md5sig_pool_lock );
3120- if (-- tcp_md5sig_users == 0 ) {
3121- pool = tcp_md5sig_pool ;
3122- tcp_md5sig_pool = NULL ;
3123- }
3124- spin_unlock_bh (& tcp_md5sig_pool_lock );
3125- if (pool )
3126- __tcp_free_md5sig_pool (pool );
3127- }
3128- EXPORT_SYMBOL (tcp_free_md5sig_pool );
3129-
3130- static struct tcp_md5sig_pool __percpu *
3131- __tcp_alloc_md5sig_pool (struct sock * sk )
3114+ static void __tcp_alloc_md5sig_pool (void )
31323115{
31333116 int cpu ;
31343117 struct tcp_md5sig_pool __percpu * pool ;
31353118
31363119 pool = alloc_percpu (struct tcp_md5sig_pool );
31373120 if (!pool )
3138- return NULL ;
3121+ return ;
31393122
31403123 for_each_possible_cpu (cpu ) {
31413124 struct crypto_hash * hash ;
@@ -3146,53 +3129,27 @@ __tcp_alloc_md5sig_pool(struct sock *sk)
31463129
31473130 per_cpu_ptr (pool , cpu )-> md5_desc .tfm = hash ;
31483131 }
3149- return pool ;
3132+ /* before setting tcp_md5sig_pool, we must commit all writes
3133+ * to memory. See ACCESS_ONCE() in tcp_get_md5sig_pool()
3134+ */
3135+ smp_wmb ();
3136+ tcp_md5sig_pool = pool ;
3137+ return ;
31503138out_free :
31513139 __tcp_free_md5sig_pool (pool );
3152- return NULL ;
31533140}
31543141
3155- struct tcp_md5sig_pool __percpu * tcp_alloc_md5sig_pool (struct sock * sk )
3142+ bool tcp_alloc_md5sig_pool (void )
31563143{
3157- struct tcp_md5sig_pool __percpu * pool ;
3158- bool alloc = false;
3159-
3160- retry :
3161- spin_lock_bh (& tcp_md5sig_pool_lock );
3162- pool = tcp_md5sig_pool ;
3163- if (tcp_md5sig_users ++ == 0 ) {
3164- alloc = true;
3165- spin_unlock_bh (& tcp_md5sig_pool_lock );
3166- } else if (!pool ) {
3167- tcp_md5sig_users -- ;
3168- spin_unlock_bh (& tcp_md5sig_pool_lock );
3169- cpu_relax ();
3170- goto retry ;
3171- } else
3172- spin_unlock_bh (& tcp_md5sig_pool_lock );
3173-
3174- if (alloc ) {
3175- /* we cannot hold spinlock here because this may sleep. */
3176- struct tcp_md5sig_pool __percpu * p ;
3177-
3178- p = __tcp_alloc_md5sig_pool (sk );
3179- spin_lock_bh (& tcp_md5sig_pool_lock );
3180- if (!p ) {
3181- tcp_md5sig_users -- ;
3182- spin_unlock_bh (& tcp_md5sig_pool_lock );
3183- return NULL ;
3184- }
3185- pool = tcp_md5sig_pool ;
3186- if (pool ) {
3187- /* oops, it has already been assigned. */
3188- spin_unlock_bh (& tcp_md5sig_pool_lock );
3189- __tcp_free_md5sig_pool (p );
3190- } else {
3191- tcp_md5sig_pool = pool = p ;
3192- spin_unlock_bh (& tcp_md5sig_pool_lock );
3193- }
3144+ if (unlikely (!tcp_md5sig_pool )) {
3145+ mutex_lock (& tcp_md5sig_mutex );
3146+
3147+ if (!tcp_md5sig_pool )
3148+ __tcp_alloc_md5sig_pool ();
3149+
3150+ mutex_unlock (& tcp_md5sig_mutex );
31943151 }
3195- return pool ;
3152+ return tcp_md5sig_pool != NULL ;
31963153}
31973154EXPORT_SYMBOL (tcp_alloc_md5sig_pool );
31983155
@@ -3209,28 +3166,15 @@ struct tcp_md5sig_pool *tcp_get_md5sig_pool(void)
32093166 struct tcp_md5sig_pool __percpu * p ;
32103167
32113168 local_bh_disable ();
3212-
3213- spin_lock (& tcp_md5sig_pool_lock );
3214- p = tcp_md5sig_pool ;
3215- if (p )
3216- tcp_md5sig_users ++ ;
3217- spin_unlock (& tcp_md5sig_pool_lock );
3218-
3169+ p = ACCESS_ONCE (tcp_md5sig_pool );
32193170 if (p )
3220- return this_cpu_ptr (p );
3171+ return __this_cpu_ptr (p );
32213172
32223173 local_bh_enable ();
32233174 return NULL ;
32243175}
32253176EXPORT_SYMBOL (tcp_get_md5sig_pool );
32263177
3227- void tcp_put_md5sig_pool (void )
3228- {
3229- local_bh_enable ();
3230- tcp_free_md5sig_pool ();
3231- }
3232- EXPORT_SYMBOL (tcp_put_md5sig_pool );
3233-
32343178int tcp_md5_hash_header (struct tcp_md5sig_pool * hp ,
32353179 const struct tcphdr * th )
32363180{
0 commit comments