From patchwork Tue Jun 8 18:27:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 455832 Delivered-To: patch@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp4031962jae; Tue, 8 Jun 2021 12:13:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyNIcjHMPMflkE4KCS2ceS3feNMbCN6j1y0JSCG9dV5Kfu06J54SJKpDep4Cb/6Sx1bZ3tY X-Received: by 2002:aa7:c0cb:: with SMTP id j11mr27372868edp.177.1623179624673; Tue, 08 Jun 2021 12:13:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623179624; cv=none; d=google.com; s=arc-20160816; b=Z6C+SyXuMQHWGFOq4Bprz9ajzjPHVY9qApyi72E2wy8gWuqP3Yw1jJg2rpFCWT83i+ Xjc4fJPltft5ZHXTtw3JAtEPvsLRelA1jUR4zl0n8pomZ8C9csyXjJEpjGIJUOldBeD1 aqM08CZ1PYJVWDlkwAJPOI95Ftwp43qfjdTU6XAOoRKwciFkuwWeoMPghL7e62c4MrfF dnbWsAZ+ebFkoq57KH1X9p6tt9MltRgZ17j89YiRpplrPlsfnabNTxvI0Z4EYJRWy1eL J3SDTR8r0bAR5sNnF+eTpqUeKqSWzY8JYed52SJnaeu3fXx185LHRbOrTGkR9U044B90 srqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=riEegmiKlq8Or1/3RE/pv08UbQszVdZlqJpzeOrWjHY=; b=DFWUEbKgP2pvb6T4flTF6HllOj8HwNsN/uj0wPDH5+OPg5Ej6w+QqaucmNeZXbzuIp 9YAeoWIDTRHPuo2nYN+aCFXDX252D0nHxWddUaSaLtVUOQ9OW+D4etmruFAV/hkw76Nk i2hdhI2KgDA5+s6rKvsCakfeKXn6Rsfdxptwcyb3dAGIzthUNRiEcabluOBDYseSqS6I niOG2iqiNqeciTxGNNEkCgQGq7m1ZfZLpYC9IBsj07TbhPj8gc3zA8uZcm7lqdiyNO67 JjMThmSz/g4zmbZFpkm5+69J5Kl0AUyinU9a22CkPBBOsscKQKQWBWaS5QHL3NQwq6em 8k/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="A/9NY1JI"; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l23si509825ejb.573.2021.06.08.12.13.44; Tue, 08 Jun 2021 12:13:44 -0700 (PDT) Received-SPF: pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="A/9NY1JI"; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236176AbhFHTNy (ORCPT + 12 others); Tue, 8 Jun 2021 15:13:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:59084 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236983AbhFHTLx (ORCPT ); Tue, 8 Jun 2021 15:11:53 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 93E746194E; Tue, 8 Jun 2021 18:49:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1623178147; bh=OiJ8InZe1zfbwKimXOsHeqZpnG2u2FCzoVBR8jR4wEs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A/9NY1JI43nExloe5QYD1aK6JBkWh8zEIs8xECbhnpAgzJ+xgKV8jNIBhOVgHxs+u W9ZZytIXuf9iM7pDkFvqXxinOnDdk/z+WJLECX+j99xf7ZKI+lCwYm/f+Ro6aUyaVO +a7MBzSqrWTO9dtc1mbvpdn7BUNRmVOTwkgZ2F+k= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Arnd Bergmann , Matthew Wilcox , "Jason A. Donenfeld" , "David S. Miller" Subject: [PATCH 5.12 095/161] wireguard: peer: allocate in kmem_cache Date: Tue, 8 Jun 2021 20:27:05 +0200 Message-Id: <20210608175948.657761951@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210608175945.476074951@linuxfoundation.org> References: <20210608175945.476074951@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Jason A. Donenfeld commit a4e9f8e3287c9eb6bf70df982870980dd3341863 upstream. With deployments having upwards of 600k peers now, this somewhat heavy structure could benefit from more fine-grained allocations. Specifically, instead of using a 2048-byte slab for a 1544-byte object, we can now use 1544-byte objects directly, thus saving almost 25% per-peer, or with 600k peers, that's a savings of 303 MiB. This also makes wireguard's memory usage more transparent in tools like slabtop and /proc/slabinfo. Fixes: 8b5553ace83c ("wireguard: queueing: get rid of per-peer ring buffers") Suggested-by: Arnd Bergmann Suggested-by: Matthew Wilcox Cc: stable@vger.kernel.org Signed-off-by: Jason A. Donenfeld Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- drivers/net/wireguard/main.c | 7 +++++++ drivers/net/wireguard/peer.c | 21 +++++++++++++++++---- drivers/net/wireguard/peer.h | 3 +++ 3 files changed, 27 insertions(+), 4 deletions(-) --- a/drivers/net/wireguard/main.c +++ b/drivers/net/wireguard/main.c @@ -28,6 +28,10 @@ static int __init mod_init(void) #endif wg_noise_init(); + ret = wg_peer_init(); + if (ret < 0) + goto err_peer; + ret = wg_device_init(); if (ret < 0) goto err_device; @@ -44,6 +48,8 @@ static int __init mod_init(void) err_netlink: wg_device_uninit(); err_device: + wg_peer_uninit(); +err_peer: return ret; } @@ -51,6 +57,7 @@ static void __exit mod_exit(void) { wg_genetlink_uninit(); wg_device_uninit(); + wg_peer_uninit(); } module_init(mod_init); --- a/drivers/net/wireguard/peer.c +++ b/drivers/net/wireguard/peer.c @@ -15,6 +15,7 @@ #include #include +static struct kmem_cache *peer_cache; static atomic64_t peer_counter = ATOMIC64_INIT(0); struct wg_peer *wg_peer_create(struct wg_device *wg, @@ -29,10 +30,10 @@ struct wg_peer *wg_peer_create(struct wg if (wg->num_peers >= MAX_PEERS_PER_DEVICE) return ERR_PTR(ret); - peer = kzalloc(sizeof(*peer), GFP_KERNEL); + peer = kmem_cache_zalloc(peer_cache, GFP_KERNEL); if (unlikely(!peer)) return ERR_PTR(ret); - if (dst_cache_init(&peer->endpoint_cache, GFP_KERNEL)) + if (unlikely(dst_cache_init(&peer->endpoint_cache, GFP_KERNEL))) goto err; peer->device = wg; @@ -64,7 +65,7 @@ struct wg_peer *wg_peer_create(struct wg return peer; err: - kfree(peer); + kmem_cache_free(peer_cache, peer); return ERR_PTR(ret); } @@ -193,7 +194,8 @@ static void rcu_release(struct rcu_head /* The final zeroing takes care of clearing any remaining handshake key * material and other potentially sensitive information. */ - kfree_sensitive(peer); + memzero_explicit(peer, sizeof(*peer)); + kmem_cache_free(peer_cache, peer); } static void kref_release(struct kref *refcount) @@ -225,3 +227,14 @@ void wg_peer_put(struct wg_peer *peer) return; kref_put(&peer->refcount, kref_release); } + +int __init wg_peer_init(void) +{ + peer_cache = KMEM_CACHE(wg_peer, 0); + return peer_cache ? 0 : -ENOMEM; +} + +void wg_peer_uninit(void) +{ + kmem_cache_destroy(peer_cache); +} --- a/drivers/net/wireguard/peer.h +++ b/drivers/net/wireguard/peer.h @@ -80,4 +80,7 @@ void wg_peer_put(struct wg_peer *peer); void wg_peer_remove(struct wg_peer *peer); void wg_peer_remove_all(struct wg_device *wg); +int wg_peer_init(void); +void wg_peer_uninit(void); + #endif /* _WG_PEER_H */