From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Cyrus-Session-Id: sloti22d1t05-901460-1516770069-2-17903609923232372189 X-Sieve: CMU Sieve 3.0 X-Spam-known-sender: no X-Spam-score: 0.0 X-Spam-hits: BAYES_00 -1.9, HEADER_FROM_DIFFERENT_DOMAINS 0.25, RCVD_IN_DNSWL_HI -5, T_RP_MATCHES_RCVD -0.01, LANGUAGES en, BAYES_USED global, SA_VERSION 3.4.0 X-Spam-source: IP='209.132.180.67', Host='vger.kernel.org', Country='US', FromHeader='com', MailFrom='org', XOriginatingCountry='US' X-Spam-charsets: plain='iso-8859-1' X-Resolved-to: greg@kroah.com X-Delivered-to: greg@kroah.com X-Mail-from: stable-owner@vger.kernel.org ARC-Seal: i=1; a=rsa-sha256; cv=none; d=messagingengine.com; s=arctest; t=1516770068; b=lylBiT9Val/hh5YFipK8ePxDxXnPQebGZOSokhOq+LQ5Hsp AJk3iCJfB1CbgBhJNH1tKADdk33num6i0Yjg+b/xz9zleD/CZFHGS13NrfVFD0e7 5ZfZCjJIg5zGKcEA48I/t763QRWbXaYty4iJCNw8gwCT57UDXQIqdwDEOXggjMsE LesIWa3VfmYl80MwEuIKCI7PcfKwWPVutraA8SCW+CvzuaWS/MkS6/GsPWSKKtKt kvM4rUvB4pXFm/2OlfqR0gllIbgxDHutLGlGnuLRZ31rEvjGnnO893FlBoXojjm/ EZQl5VT+8YXDDwJ9p7fl7bivbeZqR15NQ/Lkfkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=from:to:cc:subject:date:message-id :references:in-reply-to:content-type:content-transfer-encoding :mime-version:sender:list-id; s=arctest; t=1516770068; bh=VTI+FH M2eQCFYpiVpEb0wu6CVPFZLledc0a9mce/gno=; b=dTP0MbzfG/iqlXkOr3uqEj KihbX142n3xNXjfY1xuQQuI/V2gn6zUgsmMUePzPpDDi1XozaSPzWWsMackAqHqG VZWda7ZFwpLCYSGsBUYsXMCSuoEaCakZiccUUJ3RrPBiisu2xpOifG/n8y95cEcb EX1glFkLCthiccGOmovLn9ZQopB5JlIQL82Y132BdrzbEWMwTxl7Q5wvuuHifPAV JBDgKjVPKnzzh+eHI8uFi4DTL4V0jMBgzEJbLxu3iqvgqU290wGIW1bNlseBzcea 4BLSmNNfn7yS564jth+8PZNXDiWlPEUAkN/wiTwq3715buj/fUJ/rCYw2/tW8KXw == ARC-Authentication-Results: i=1; mx4.messagingengine.com; arc=none (no signatures found); dkim=pass (1024-bit rsa key sha256) header.d=microsoft.com header.i=@microsoft.com header.b=mjClr+AK x-bits=1024 x-keytype=rsa x-algorithm=sha256 x-selector=selector1; dmarc=pass (p=reject,has-list-id=yes,d=none) header.from=microsoft.com; iprev=pass policy.iprev=209.132.180.67 (vger.kernel.org); spf=none smtp.mailfrom=stable-owner@vger.kernel.org smtp.helo=vger.kernel.org; x-aligned-from=fail; x-ptr=pass x-ptr-helo=vger.kernel.org x-ptr-lookup=vger.kernel.org; x-return-mx=pass smtp.domain=vger.kernel.org smtp.result=pass smtp_org.domain=kernel.org smtp_org.result=pass smtp_is_org_domain=no header.domain=microsoft.com header.result=pass header_is_org_domain=yes Authentication-Results: mx4.messagingengine.com; arc=none (no signatures found); dkim=pass (1024-bit rsa key sha256) header.d=microsoft.com header.i=@microsoft.com header.b=mjClr+AK x-bits=1024 x-keytype=rsa x-algorithm=sha256 x-selector=selector1; dmarc=pass (p=reject,has-list-id=yes,d=none) header.from=microsoft.com; iprev=pass policy.iprev=209.132.180.67 (vger.kernel.org); spf=none smtp.mailfrom=stable-owner@vger.kernel.org smtp.helo=vger.kernel.org; x-aligned-from=fail; x-ptr=pass x-ptr-helo=vger.kernel.org x-ptr-lookup=vger.kernel.org; x-return-mx=pass smtp.domain=vger.kernel.org smtp.result=pass smtp_org.domain=kernel.org smtp_org.result=pass smtp_is_org_domain=no header.domain=microsoft.com header.result=pass header_is_org_domain=yes Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752502AbeAXFA4 (ORCPT ); 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 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; 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: stable-owner@vger.kernel.org X-Mailing-List: stable@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-Mailing-List: linux-kernel@vger.kernel.org List-ID: 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