Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3032023rdg; Tue, 17 Oct 2023 02:24:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFPB4Ef4HhYoGCEg+VZ8pGUlireujxINeEZsG8SvBBJUoWfhzVOjzLKTXv6GK2cjjd1cfJM X-Received: by 2002:a17:903:11cc:b0:1c9:e48c:7266 with SMTP id q12-20020a17090311cc00b001c9e48c7266mr2206576plh.57.1697534678786; Tue, 17 Oct 2023 02:24:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697534678; cv=none; d=google.com; s=arc-20160816; b=HHi/WbvW9DFouzVVFPyth0BMCWhRUA8oF92xtLBfo3qr0J7521M2pKSxQnIztOzenL muPpT/CagBK6ahSvOqvxR9HUgC4njHa/u2fCMVjXWB3Ih5lbzP7MOckK3HmAHCwFFSst /66zoLG3we+YRBXppPCB069DGIIHQ3xxCQ5w5AFbiGCMk3d3D44xEkmV5CMcml18cggv 8USPoW5BW58drP32vAc75fltzv7Rqj5In1SjSTd1mK27BdHJOM9VBKiskz9wtBmBVwbJ QIfHKoqycghhmvrETfoQbNqHCJCi1/ENmKck/H+PrC5gBZ4RHcCYP2xTpzefeQbPhAqC trZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:autocrypt:from:references:cc:to:content-language :subject:user-agent:mime-version:date:message-id:dkim-signature; bh=dmxcJ31LESJX8T+oi/bH2ZYeaLP6Js74v6cCMivygmo=; fh=QAs9Z7ASEGiquGZOl6tUH28CaCcVKv5s8HKdiAFHzEQ=; b=G/L4uo0RdywZzNpihBiFssPdpWvELbUPNQhXs+Ad8KkMb4q2/U9JaT9+kZ9FMdlrMC GNbfPWPfuHe/P8tzB24MRqHxeiW04wL+puLF6tUbRavEnS8DTY4u1hwyDG7fOik/GjYZ i/qh2DdqOQVxD/WsxU/QIAXTnZTwHBGW11ycUHo5C5qdBb+YSsB8ilZDUUHnhhdcuUOs 6d4ZtFEYJGHqde4qGq8WOr43gMNB3gVIUEigRuA6ZCAf4OHedtDaIJsCVYndVPV06XuQ nmol1kl6XnXg9BTha/FmaCOXWrxPwe6sVu/IkO6rzT0FlB0iNlo55CwCdSw7wWQUcvWJ SaFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=SRCSrF1H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id e8-20020a170902744800b001c7218c4db8si1402751plt.119.2023.10.17.02.24.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 02:24:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=SRCSrF1H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id A2904807E003; Tue, 17 Oct 2023 02:24:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234796AbjJQJYa (ORCPT + 99 others); Tue, 17 Oct 2023 05:24:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234679AbjJQJY3 (ORCPT ); Tue, 17 Oct 2023 05:24:29 -0400 Received: from smtp-relay-canonical-0.canonical.com (smtp-relay-canonical-0.canonical.com [185.125.188.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBEBB8E; Tue, 17 Oct 2023 02:24:27 -0700 (PDT) Received: from [192.168.192.84] (unknown [50.39.103.33]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-canonical-0.canonical.com (Postfix) with ESMTPSA id 8258D3F6DB; Tue, 17 Oct 2023 09:24:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1697534664; bh=dmxcJ31LESJX8T+oi/bH2ZYeaLP6Js74v6cCMivygmo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=SRCSrF1HZ4Y9iAjrS5JLWSa/soSxelmcCKWVbPiDe+ckf2xrHrTzTIzQ7+QC8ZSAJ TaCq8S8ReyZw+2wBRf57HU0LASlui730I7pn4rrQ7Vnkoqdp90XGdcZu43+EY+VLT4 NFsO7avLFzn+N2VIrhQ2Gglx/GV9wsG34LcsTTdvJ44yZd+jHdM9WLYAXwrmE8fUCo WTVMWqCIx41xh4LeZWfXwJewWVmvY9ZZ+eujRuPXj0KzuRDSj7jnB3hFWeq96/6ojl Pkgwwf73ApEoZQtDdRiCOiJ8P1poaLjf+yZ40QzrYIErKL5pTPzfzJagauICWNOGag rouraJwkmrfhQ== Message-ID: <6c472b12-6957-4542-8ef4-7762e750a56d@canonical.com> Date: Tue, 17 Oct 2023 02:24:21 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 2/4] apparmor: exponential backoff on cache buffer contention Content-Language: en-US To: Sergey Senozhatsky Cc: Anil Altinay , Sebastian Andrzej Siewior , LKLM , Peter Zijlstra , Tomasz Figa , linux-security-module@vger.kernel.org, John Johansen References: <4595e7b4-ea31-5b01-f636-259e84737dfc@canonical.com> <31d6e8d6-0747-a282-746b-5c144a9970bb@canonical.com> <20231006041837.GA17924@google.com> From: John Johansen Autocrypt: addr=john.johansen@canonical.com; keydata= xsFNBE5mrPoBEADAk19PsgVgBKkImmR2isPQ6o7KJhTTKjJdwVbkWSnNn+o6Up5knKP1f49E BQlceWg1yp/NwbR8ad+eSEO/uma/K+PqWvBptKC9SWD97FG4uB4/caomLEU97sLQMtnvGWdx rxVRGM4anzWYMgzz5TZmIiVTZ43Ou5VpaS1Vz1ZSxP3h/xKNZr/TcW5WQai8u3PWVnbkjhSZ PHv1BghN69qxEPomrJBm1gmtx3ZiVmFXluwTmTgJOkpFol7nbJ0ilnYHrA7SX3CtR1upeUpM a/WIanVO96WdTjHHIa43fbhmQube4txS3FcQLOJVqQsx6lE9B7qAppm9hQ10qPWwdfPy/+0W 6AWtNu5ASiGVCInWzl2HBqYd/Zll93zUq+NIoCn8sDAM9iH+wtaGDcJywIGIn+edKNtK72AM gChTg/j1ZoWH6ZeWPjuUfubVzZto1FMoGJ/SF4MmdQG1iQNtf4sFZbEgXuy9cGi2bomF0zvy BJSANpxlKNBDYKzN6Kz09HUAkjlFMNgomL/cjqgABtAx59L+dVIZfaF281pIcUZzwvh5+JoG eOW5uBSMbE7L38nszooykIJ5XrAchkJxNfz7k+FnQeKEkNzEd2LWc3QF4BQZYRT6PHHga3Rg ykW5+1wTMqJILdmtaPbXrF3FvnV0LRPcv4xKx7B3fGm7ygdoowARAQABzStKb2huIEpvaGFu c2VuIDxqb2huLmpvaGFuc2VuQGNhbm9uaWNhbC5jb20+wsF3BBMBCgAhBQJOjRdaAhsDBQsJ CAcDBRUKCQgLBRYCAwEAAh4BAheAAAoJEAUvNnAY1cPYi0wP/2PJtzzt0zi4AeTrI0w3Rj8E Waa1NZWw4GGo6ehviLfwGsM7YLWFAI8JB7gsuzX/im16i9C3w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kZHHSCQfdP4XSc0Obk1olSxqzCAm uoVmRQZ3YyubWqcrBeIC3xIhwQ12rfdHQoopELzReDCPwmffS9ctIb407UYfRQxwDEzDL+m+ TotTkkaNlHvcnlQtWEfgwtsOCAPeY9qIbz5+i1OslQ+qqGD2HJQQ+lgbuyq3vhefv34IRlyM sfPKXq8AUTZbSTGUu1C1RlQc7fpp8W/yoak7dmo++MFS5q1cXq29RALB/cfpcwARAQABwsFf BBgBCgAJBQJOZqz6AhsMAAoJEAUvNnAY1cPYP9cP/R10z/hqLVv5OXWPOcpqNfeQb4x4Rh4j h/jS9yjes4uudEYU5xvLJ9UXr0wp6mJ7g7CgjWNxNTQAN5ydtacM0emvRJzPEEyujduesuGy a+O6dNgi+ywFm0HhpUmO4sgs9SWeEWprt9tWrRlCNuJX+u3aMEQ12b2lslnoaOelghwBs8IJ r998vj9JBFJgdeiEaKJLjLmMFOYrmW197As7DTZ+R7Ef4gkWusYFcNKDqfZKDGef740Xfh9d yb2mJrDeYqwgKb7SF02Hhp8ZnohZXw8ba16ihUOnh1iKH77Ff9dLzMEJzU73DifOU/aArOWp JZuGJamJ9EkEVrha0B4lN1dh3fuP8EjhFZaGfLDtoA80aPffK0Yc1R/pGjb+O2Pi0XXL9AVe qMkb/AaOl21F9u1SOosciy98800mr/3nynvid0AKJ2VZIfOP46nboqlsWebA07SmyJSyeG8c XA87+8BuXdGxHn7RGj6G+zZwSZC6/2v9sOUJ+nOna3dwr6uHFSqKw7HwNl/PUGeRqgJEVu++ +T7sv9+iY+e0Y+SolyJgTxMYeRnDWE6S77g6gzYYHmcQOWP7ZMX+MtD4SKlf0+Q8li/F9GUL p0rw8op9f0p1+YAhyAd+dXWNKf7zIfZ2ME+0qKpbQnr1oizLHuJX/Telo8KMmHter28DPJ03 lT9Q Organization: Canonical In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 17 Oct 2023 02:24:36 -0700 (PDT) Reduce contention on the global buffers lock by using an exponential back off strategy where the amount tries to hold is doubled when contention is encoutered and backed off linearly when there isn't contention. Signed-off-by: John Johansen --- security/apparmor/lsm.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c index ce4f3e7a784d..fd6779ff0da4 100644 --- a/security/apparmor/lsm.c +++ b/security/apparmor/lsm.c @@ -50,6 +50,7 @@ union aa_buffer { }; struct aa_local_cache { + unsigned int contention; unsigned int hold; unsigned int count; struct list_head head; @@ -1793,6 +1794,14 @@ static int param_set_mode(const char *val, const struct kernel_param *kp) return 0; } +static void update_contention(struct aa_local_cache *cache) +{ + cache->contention += 1; + if (cache->contention > 9) + cache->contention = 9; + cache->hold += 1 << cache->contention; /* 2, 4, 8, ... */ +} + char *aa_get_buffer(bool in_atomic) { union aa_buffer *aa_buf; @@ -1814,11 +1823,13 @@ char *aa_get_buffer(bool in_atomic) if (!spin_trylock(&aa_buffers_lock)) { cache = get_cpu_ptr(&aa_local_buffers); - cache->hold += 1; + update_contention(cache); put_cpu_ptr(&aa_local_buffers); spin_lock(&aa_buffers_lock); } else { cache = get_cpu_ptr(&aa_local_buffers); + if (cache->contention) + cache->contention--; put_cpu_ptr(&aa_local_buffers); } retry: @@ -1875,12 +1886,14 @@ void aa_put_buffer(char *buf) buffer_count++; spin_unlock(&aa_buffers_lock); cache = get_cpu_ptr(&aa_local_buffers); + if (cache->contention) + cache->contention--; put_cpu_ptr(&aa_local_buffers); return; } /* contention on global list, fallback to percpu */ cache = get_cpu_ptr(&aa_local_buffers); - cache->hold += 1; + update_contention(cache); } /* cache in percpu list */ @@ -1933,6 +1946,7 @@ static int __init alloc_buffers(void) * lock contention */ for_each_possible_cpu(i) { + per_cpu(aa_local_buffers, i).contention = 0; per_cpu(aa_local_buffers, i).hold = 0; per_cpu(aa_local_buffers, i).count = 0; INIT_LIST_HEAD(&per_cpu(aa_local_buffers, i).head); -- 2.34.1