Received: by 10.223.176.46 with SMTP id f43csp283103wra; Tue, 23 Jan 2018 21:01:32 -0800 (PST) X-Google-Smtp-Source: AH8x227MXCA5rIP5P9KPKEv8v1kMyMo/c3lEpF0d6UZo1NvB+tJI0XbmeipKwEG6XEQhwpFTEAO3 X-Received: by 10.98.137.75 with SMTP id v72mr12006234pfd.189.1516770092860; Tue, 23 Jan 2018 21:01:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516770092; cv=none; d=google.com; s=arc-20160816; b=KCO6z5wbAHwQBYb7FTOZYJzHFFUJvtNnOp/X8kdLSVd1KaB+F71Zp66WHeGKSxa3hS IbcLqVtuV0fZDUOYq/oEs69wTqyNk906I0l15oJeJNgU6xvodj16bsLlgg/6nijldTx9 /8coQJUe9XmXllEuKkzXDghrCBX3gUQnfRjx3DYeEGiF++rKifUeayzPcVl2Hyb3LDXZ 3V0F3mJLdnAkaLf3u4LRV7AWgJX8kKCMhd8L4WFjyQ1oUKCkaSypCfOtiGOdJnke26jo /0aPuTwwChurNIoxx/ynVD6FuJ/0e6tuvupheHo8Y+t4ikSRe/iZCcfWURz1H8yunOvf Jicg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=VTI+FHM2eQCFYpiVpEb0wu6CVPFZLledc0a9mce/gno=; b=S5hFbzot0SCAPMWgKFlMOKyTiNvvzO3/Ti2l1TC+3wDRbCMCRmAFjo+asEkReBec1S KG3FMhFXDOx30xs4NtBxn0Iu4ooanjy15+7Xh2cOSUvaZB7VaVVPpttjAVQtiL5ZRD72 9xTY6MeoAQZAxDvYIdDQHM/bpANuYZRRFO3hvznIcx/L3Sdf/aF8KjYOJ3PBMMhLu/SA IrdEPMq2L1ZPn6xi0h1PIEwKVxVdJ3iG+kZWHYfxTZJdH06AKiYuAn4r/vHgEgbtZZy8 5HT9L507EbA5I7VP4XdbOM2gg5zrPkpIFxwEyAblw85pnf46N6CQuNmvxJx5sg8h9wsb H4Kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=mjClr+AK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 87si2251244pfl.300.2018.01.23.21.01.19; Tue, 23 Jan 2018 21:01:32 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=mjClr+AK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752652AbeAXFA4 (ORCPT + 99 others); Wed, 24 Jan 2018 00:00:56 -0500 Received: from mail-sn1nam01on0111.outbound.protection.outlook.com ([104.47.32.111]:45037 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752812AbeAXEPL (ORCPT ); Tue, 23 Jan 2018 23:15:11 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=VTI+FHM2eQCFYpiVpEb0wu6CVPFZLledc0a9mce/gno=; b=mjClr+AKXA6if84z3Vwh4La8pNc8BK4vW8JKN2cL0u1FMmokMAe8kiSfxQOYfHGnZ6jsWwCEtF7GdX0a4KhX02/2rDeLyJrw3GB+AGlZpMGMwsyUT8enJZvLcNVXnhJVgSdPJ0TCFckyZKMi4ZCvvexdvUgTFPmNv6z7vsMxNHw= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB0998.namprd21.prod.outlook.com (52.132.133.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.464.0; Wed, 24 Jan 2018 04:15:09 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::6485:b98:d15e:9da7]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::6485:b98:d15e:9da7%2]) with mapi id 15.20.0464.000; Wed, 24 Jan 2018 04:15:09 +0000 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: David Howells , Sasha Levin Subject: [PATCH AUTOSEL for 4.14 031/100] rxrpc: Fix service endpoint expiry Thread-Topic: [PATCH AUTOSEL for 4.14 031/100] rxrpc: Fix service endpoint expiry Thread-Index: AQHTlMnZNd6q2fdtM0GfgoMPRpM/7Q== Date: Wed, 24 Jan 2018 04:14:38 +0000 Message-ID: <20180124041414.32065-31-alexander.levin@microsoft.com> References: <20180124041414.32065-1-alexander.levin@microsoft.com> In-Reply-To: <20180124041414.32065-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB0998;7:lH0H3TtuDL6bHWrJZ9ygWllP3IQyRg+UiAcT8/ei7+Bp8wRnUl471Cp1BU9WHZ3xKKfHoWhRAM0vE1enwamv4Vagwloma1PT+C61OpImzbGZxRbeDShXIPaZ6chf9+cjT/Sw/FV9hjgUni0ye8bzY3OJ2QheEYfRQsZ56mzj2NOyzUwBCyIEkWH97wbUvnkGGYHi0jwSsup6FRC9bT2BHVeU/WHQx/ISlMPg2G8OCqk14eKEviGod984LUaO1RKV x-ms-office365-filtering-correlation-id: bbe9c3d9-b271-4a48-714e-08d562e10e5a x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(48565401081)(2017052603307)(7193020);SRVR:DM5PR2101MB0998; x-ms-traffictypediagnostic: DM5PR2101MB0998: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(61425038)(6040501)(2401047)(8121501046)(5005006)(93006095)(93001095)(3231046)(2400081)(944501161)(10201501046)(3002001)(6055026)(61426038)(61427038)(6041288)(20161123558120)(20161123562045)(20161123564045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:DM5PR2101MB0998;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB0998; x-forefront-prvs: 056297E276 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(366004)(39380400002)(346002)(39860400002)(396003)(376002)(199004)(189003)(14454004)(3660700001)(6346003)(10290500003)(105586002)(66066001)(6506007)(8676002)(22452003)(86612001)(54906003)(10090500001)(81166006)(81156014)(76176011)(110136005)(86362001)(68736007)(106356001)(59450400001)(8936002)(3280700002)(36756003)(25786009)(107886003)(2501003)(6666003)(99286004)(2906002)(26005)(2950100002)(72206003)(102836004)(305945005)(6116002)(3846002)(5660300001)(4326008)(478600001)(316002)(53936002)(97736004)(575784001)(2900100001)(5250100002)(6512007)(6436002)(6486002)(1076002)(7736002)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB0998;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-message-info: 1esclWKpOYy8FJ8P1Nh//4JbBYnXYDg9CG+CU90RAyrTJRtb4OMmN1wDF1SrCHNFfZAG/Bo6nIKTyJSAuNuYrA== spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: bbe9c3d9-b271-4a48-714e-08d562e10e5a X-MS-Exchange-CrossTenant-originalarrivaltime: 24 Jan 2018 04:14:38.9061 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0998 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Howells [ Upstream commit f859ab61875978eeaa539740ff7f7d91f5d60006 ] RxRPC service endpoints expire like they're supposed to by the following means: (1) Mark dead rxrpc_net structs (with ->live) rather than twiddling the global service conn timeout, otherwise the first rxrpc_net struct to die will cause connections on all others to expire immediately from then on. (2) Mark local service endpoints for which the socket has been closed (->service_closed) so that the expiration timeout can be much shortened for service and client connections going through that endpoint. (3) rxrpc_put_service_conn() needs to schedule the reaper when the usage count reaches 1, not 0, as idle conns have a 1 count. (4) The accumulator for the earliest time we might want to schedule for should be initialised to jiffies + MAX_JIFFY_OFFSET, not ULONG_MAX as the comparison functions use signed arithmetic. (5) Simplify the expiration handling, adding the expiration value to the idle timestamp each time rather than keeping track of the time in the past before which the idle timestamp must go to be expired. This is much easier to read. (6) Ignore the timeouts if the net namespace is dead. (7) Restart the service reaper work item rather the client reaper. Signed-off-by: David Howells Signed-off-by: Sasha Levin --- include/trace/events/rxrpc.h | 2 ++ net/rxrpc/af_rxrpc.c | 13 +++++++++++++ net/rxrpc/ar-internal.h | 3 +++ net/rxrpc/conn_client.c | 2 ++ net/rxrpc/conn_object.c | 42 ++++++++++++++++++++++++----------------= -- net/rxrpc/net_ns.c | 3 +++ 6 files changed, 47 insertions(+), 18 deletions(-) diff --git a/include/trace/events/rxrpc.h b/include/trace/events/rxrpc.h index ebe96796027a..a58caf5807ff 100644 --- a/include/trace/events/rxrpc.h +++ b/include/trace/events/rxrpc.h @@ -49,6 +49,7 @@ enum rxrpc_conn_trace { rxrpc_conn_put_client, rxrpc_conn_put_service, rxrpc_conn_queued, + rxrpc_conn_reap_service, rxrpc_conn_seen, }; =20 @@ -206,6 +207,7 @@ enum rxrpc_congest_change { EM(rxrpc_conn_put_client, "PTc") \ EM(rxrpc_conn_put_service, "PTs") \ EM(rxrpc_conn_queued, "QUE") \ + EM(rxrpc_conn_reap_service, "RPs") \ E_(rxrpc_conn_seen, "SEE") =20 #define rxrpc_client_traces \ diff --git a/net/rxrpc/af_rxrpc.c b/net/rxrpc/af_rxrpc.c index 4b0a8288c98a..7c1cb08874d5 100644 --- a/net/rxrpc/af_rxrpc.c +++ b/net/rxrpc/af_rxrpc.c @@ -823,6 +823,19 @@ static int rxrpc_release_sock(struct sock *sk) sock_orphan(sk); sk->sk_shutdown =3D SHUTDOWN_MASK; =20 + /* We want to kill off all connections from a service socket + * as fast as possible because we can't share these; client + * sockets, on the other hand, can share an endpoint. + */ + switch (sk->sk_state) { + case RXRPC_SERVER_BOUND: + case RXRPC_SERVER_BOUND2: + case RXRPC_SERVER_LISTENING: + case RXRPC_SERVER_LISTEN_DISABLED: + rx->local->service_closed =3D true; + break; + } + spin_lock_bh(&sk->sk_receive_queue.lock); sk->sk_state =3D RXRPC_CLOSE; spin_unlock_bh(&sk->sk_receive_queue.lock); diff --git a/net/rxrpc/ar-internal.h b/net/rxrpc/ar-internal.h index 2b3992c5060e..e6c2c4f56fb1 100644 --- a/net/rxrpc/ar-internal.h +++ b/net/rxrpc/ar-internal.h @@ -84,6 +84,7 @@ struct rxrpc_net { unsigned int nr_client_conns; unsigned int nr_active_client_conns; bool kill_all_client_conns; + bool live; spinlock_t client_conn_cache_lock; /* Lock for ->*_client_conns */ spinlock_t client_conn_discard_lock; /* Prevent multiple discarders */ struct list_head waiting_client_conns; @@ -265,6 +266,7 @@ struct rxrpc_local { rwlock_t services_lock; /* lock for services list */ int debug_id; /* debug ID for printks */ bool dead; + bool service_closed; /* Service socket closed */ struct sockaddr_rxrpc srx; /* local address */ }; =20 @@ -824,6 +826,7 @@ void rxrpc_process_connection(struct work_struct *); * conn_object.c */ extern unsigned int rxrpc_connection_expiry; +extern unsigned int rxrpc_closed_conn_expiry; =20 struct rxrpc_connection *rxrpc_alloc_connection(gfp_t); struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *, diff --git a/net/rxrpc/conn_client.c b/net/rxrpc/conn_client.c index 5f9624bd311c..78a154173d90 100644 --- a/net/rxrpc/conn_client.c +++ b/net/rxrpc/conn_client.c @@ -1061,6 +1061,8 @@ next: expiry =3D rxrpc_conn_idle_client_expiry; if (nr_conns > rxrpc_reap_client_connections) expiry =3D rxrpc_conn_idle_client_fast_expiry; + if (conn->params.local->service_closed) + expiry =3D rxrpc_closed_conn_expiry * HZ; =20 conn_expires_at =3D conn->idle_timestamp + expiry; =20 diff --git a/net/rxrpc/conn_object.c b/net/rxrpc/conn_object.c index fe575798592f..a48c817b792b 100644 --- a/net/rxrpc/conn_object.c +++ b/net/rxrpc/conn_object.c @@ -20,7 +20,8 @@ /* * Time till a connection expires after last use (in seconds). */ -unsigned int rxrpc_connection_expiry =3D 10 * 60; +unsigned int __read_mostly rxrpc_connection_expiry =3D 10 * 60; +unsigned int __read_mostly rxrpc_closed_conn_expiry =3D 10; =20 static void rxrpc_destroy_connection(struct rcu_head *); =20 @@ -312,7 +313,7 @@ void rxrpc_put_service_conn(struct rxrpc_connection *co= nn) n =3D atomic_dec_return(&conn->usage); trace_rxrpc_conn(conn, rxrpc_conn_put_service, n, here); ASSERTCMP(n, >=3D, 0); - if (n =3D=3D 0) { + if (n =3D=3D 1) { rxnet =3D conn->params.local->rxnet; rxrpc_queue_delayed_work(&rxnet->service_conn_reaper, 0); } @@ -353,15 +354,14 @@ void rxrpc_service_connection_reaper(struct work_stru= ct *work) struct rxrpc_net *rxnet =3D container_of(to_delayed_work(work), struct rxrpc_net, service_conn_reaper); - unsigned long reap_older_than, earliest, idle_timestamp, now; + unsigned long expire_at, earliest, idle_timestamp, now; =20 LIST_HEAD(graveyard); =20 _enter(""); =20 now =3D jiffies; - reap_older_than =3D now - rxrpc_connection_expiry * HZ; - earliest =3D ULONG_MAX; + earliest =3D now + MAX_JIFFY_OFFSET; =20 write_lock(&rxnet->conn_lock); list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) { @@ -371,15 +371,21 @@ void rxrpc_service_connection_reaper(struct work_stru= ct *work) if (conn->state =3D=3D RXRPC_CONN_SERVICE_PREALLOC) continue; =20 - idle_timestamp =3D READ_ONCE(conn->idle_timestamp); - _debug("reap CONN %d { u=3D%d,t=3D%ld }", - conn->debug_id, atomic_read(&conn->usage), - (long)reap_older_than - (long)idle_timestamp); - - if (time_after(idle_timestamp, reap_older_than)) { - if (time_before(idle_timestamp, earliest)) - earliest =3D idle_timestamp; - continue; + if (rxnet->live) { + idle_timestamp =3D READ_ONCE(conn->idle_timestamp); + expire_at =3D idle_timestamp + rxrpc_connection_expiry * HZ; + if (conn->params.local->service_closed) + expire_at =3D idle_timestamp + rxrpc_closed_conn_expiry * HZ; + + _debug("reap CONN %d { u=3D%d,t=3D%ld }", + conn->debug_id, atomic_read(&conn->usage), + (long)expire_at - (long)now); + + if (time_before(now, expire_at)) { + if (time_before(expire_at, earliest)) + earliest =3D expire_at; + continue; + } } =20 /* The usage count sits at 1 whilst the object is unused on the @@ -387,6 +393,7 @@ void rxrpc_service_connection_reaper(struct work_struct= *work) */ if (atomic_cmpxchg(&conn->usage, 1, 0) !=3D 1) continue; + trace_rxrpc_conn(conn, rxrpc_conn_reap_service, 0, 0); =20 if (rxrpc_conn_is_client(conn)) BUG(); @@ -397,10 +404,10 @@ void rxrpc_service_connection_reaper(struct work_stru= ct *work) } write_unlock(&rxnet->conn_lock); =20 - if (earliest !=3D ULONG_MAX) { - _debug("reschedule reaper %ld", (long) earliest - now); + if (earliest !=3D now + MAX_JIFFY_OFFSET) { + _debug("reschedule reaper %ld", (long)earliest - (long)now); ASSERT(time_after(earliest, now)); - rxrpc_queue_delayed_work(&rxnet->client_conn_reaper, + rxrpc_queue_delayed_work(&rxnet->service_conn_reaper, earliest - now); } =20 @@ -429,7 +436,6 @@ void rxrpc_destroy_all_connections(struct rxrpc_net *rx= net) =20 rxrpc_destroy_all_client_connections(rxnet); =20 - rxrpc_connection_expiry =3D 0; cancel_delayed_work(&rxnet->client_conn_reaper); rxrpc_queue_delayed_work(&rxnet->client_conn_reaper, 0); flush_workqueue(rxrpc_workqueue); diff --git a/net/rxrpc/net_ns.c b/net/rxrpc/net_ns.c index 7edceb8522f5..684c51d600c7 100644 --- a/net/rxrpc/net_ns.c +++ b/net/rxrpc/net_ns.c @@ -22,6 +22,7 @@ static __net_init int rxrpc_init_net(struct net *net) struct rxrpc_net *rxnet =3D rxrpc_net(net); int ret; =20 + rxnet->live =3D true; get_random_bytes(&rxnet->epoch, sizeof(rxnet->epoch)); rxnet->epoch |=3D RXRPC_RANDOM_EPOCH; =20 @@ -60,6 +61,7 @@ static __net_init int rxrpc_init_net(struct net *net) return 0; =20 err_proc: + rxnet->live =3D false; return ret; } =20 @@ -70,6 +72,7 @@ static __net_exit void rxrpc_exit_net(struct net *net) { struct rxrpc_net *rxnet =3D rxrpc_net(net); =20 + rxnet->live =3D false; rxrpc_destroy_all_calls(rxnet); rxrpc_destroy_all_connections(rxnet); rxrpc_destroy_all_locals(rxnet); --=20 2.11.0