Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp496313pxb; Wed, 13 Jan 2021 08:35:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJwd30TiYOb1DVqTnTkaLZ6KS/V4Aqu+uZ7l9DZgGWzbkfR5so4KLtLvIF6ju7o5dUadOf/A X-Received: by 2002:aa7:c60c:: with SMTP id h12mr2451306edq.145.1610555728298; Wed, 13 Jan 2021 08:35:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610555728; cv=none; d=google.com; s=arc-20160816; b=deSr8o9bqB4/DxB1QyTdoDi6VZvM0xsHQ0rSnZT4FSoNeS7NHopcYLabPb/ebOuM3J NMTUcNlSd2II7bvEscmHNJrDakU07c1q2v5W7OEr7aw+Cv4O7dvCtvuz+g6X9TBTFe1u qMKD4ZVI5FIHP8NABBVm/mR3fIija/vtDG0NXjh6OQKLepzoKhS0b2lYXvzWv1pNsvrH ZakndDMNl76DlHeMbF4pXUqMxvBVUlI/Pymgtpe4mZqi0FPKOk52LJQdx14hXLKufiqb QaS4knVQGjPu/snJNuRRwdB4Yn73l3uTu3Qc1ZhLzMjWpLE9Oe5vLMUVrj38be8Yzgb2 1cAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:mime-version:user-agent:message-id :in-reply-to:date:references:cc:to:from; bh=EUKMDne0hQ7p2XZPkkD4cDcpIA2b7cPszAaWeC2vF+w=; b=UQBIFdq5jsCTjf9o/6iwytkwGWv5UduRBc0d6O05nYr65nd3HMRQay2FFHcEtDpvmm TbB09KCst2YCZ/B4axgzO055Z773/nIqFwp32pGdIysT4owZjxdbV3a+MXTkqwQdJ2+J Q2N8r8ksxn8Ws2BITAKF5qAnxeIgAooCJGFiLR96XImXULLBcCyAHayFNx8KQIsIZtWE XpmJOcUFQ7WaxfOZtH87ExmcPmCb7yzRHafgWnQzV98lw7XQFZ0uocYvQhV15y/ZmMnz x0FTgwpC8UbjystBciNUQgQkmIkMsiW/948+Tp6lu61Eo3An5lDG+J+ktULgS1OP07SK CfPA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e26si1373173edv.153.2021.01.13.08.35.03; Wed, 13 Jan 2021 08:35:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726003AbhAMQd0 (ORCPT + 99 others); Wed, 13 Jan 2021 11:33:26 -0500 Received: from out02.mta.xmission.com ([166.70.13.232]:41676 "EHLO out02.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725747AbhAMQd0 (ORCPT ); Wed, 13 Jan 2021 11:33:26 -0500 Received: from in02.mta.xmission.com ([166.70.13.52]) by out02.mta.xmission.com with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1kzj4e-006oSo-Ji; Wed, 13 Jan 2021 09:32:44 -0700 Received: from ip68-227-160-95.om.om.cox.net ([68.227.160.95] helo=x220.xmission.com) by in02.mta.xmission.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1kzj4d-009Vz0-HQ; Wed, 13 Jan 2021 09:32:44 -0700 From: ebiederm@xmission.com (Eric W. Biederman) To: Alexey Gladkov Cc: LKML , Linux Containers , Kernel Hardening , Alexey Gladkov , Kees Cook , Christian Brauner , Linus Torvalds References: <447547b12bba1894d3f1f79d6408dfc60b219b0c.1610299857.git.gladkov.alexey@gmail.com> Date: Wed, 13 Jan 2021 10:31:40 -0600 In-Reply-To: <447547b12bba1894d3f1f79d6408dfc60b219b0c.1610299857.git.gladkov.alexey@gmail.com> (Alexey Gladkov's message of "Sun, 10 Jan 2021 18:33:40 +0100") Message-ID: <878s8wdcib.fsf@x220.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1kzj4d-009Vz0-HQ;;;mid=<878s8wdcib.fsf@x220.int.ebiederm.org>;;;hst=in02.mta.xmission.com;;;ip=68.227.160.95;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX195fKSBqSZ+lYzEZIXlkNCqt38tQhw2MoY= X-SA-Exim-Connect-IP: 68.227.160.95 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on sa08.xmission.com X-Spam-Level: X-Spam-Status: No, score=0.5 required=8.0 tests=ALL_TRUSTED,BAYES_50, DCC_CHECK_NEGATIVE,T_TM2_M_HEADER_IN_MSG,XMSubLong autolearn=disabled version=3.4.2 X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.4984] * 0.7 XMSubLong Long Subject * 0.0 T_TM2_M_HEADER_IN_MSG BODY: No description available. * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa08 1397; Body=1 Fuz1=1 Fuz2=1] X-Spam-DCC: XMission; sa08 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;Alexey Gladkov X-Spam-Relay-Country: X-Spam-Timing: total 510 ms - load_scoreonly_sql: 0.03 (0.0%), signal_user_changed: 12 (2.3%), b_tie_ro: 10 (2.0%), parse: 0.77 (0.2%), extract_message_metadata: 2.7 (0.5%), get_uri_detail_list: 0.96 (0.2%), tests_pri_-1000: 3.4 (0.7%), tests_pri_-950: 1.38 (0.3%), tests_pri_-900: 1.13 (0.2%), tests_pri_-90: 240 (47.1%), check_bayes: 239 (46.8%), b_tokenize: 6 (1.1%), b_tok_get_all: 6 (1.2%), b_comp_prob: 1.52 (0.3%), b_tok_touch_all: 221 (43.4%), b_finish: 1.10 (0.2%), tests_pri_0: 233 (45.7%), check_dkim_signature: 0.50 (0.1%), check_dkim_adsp: 5 (1.1%), poll_dns_idle: 4.0 (0.8%), tests_pri_10: 2.2 (0.4%), tests_pri_500: 7 (1.3%), rewrite_mail: 0.00 (0.0%) Subject: Re: [RFC PATCH v2 1/8] Use atomic type for ucounts reference counting X-SA-Exim-Version: 4.2.1 (built Sat, 08 Feb 2020 21:53:50 +0000) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Alexey Gladkov writes: We might want to use refcount_t instead of atomic_t. Not a big deal either way. > Signed-off-by: Alexey Gladkov > --- > include/linux/user_namespace.h | 2 +- > kernel/ucount.c | 10 +++++----- > 2 files changed, 6 insertions(+), 6 deletions(-) > > diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h > index 64cf8ebdc4ec..84fefa9247c4 100644 > --- a/include/linux/user_namespace.h > +++ b/include/linux/user_namespace.h > @@ -92,7 +92,7 @@ struct ucounts { > struct hlist_node node; > struct user_namespace *ns; > kuid_t uid; > - int count; > + atomic_t count; > atomic_t ucount[UCOUNT_COUNTS]; > }; > > diff --git a/kernel/ucount.c b/kernel/ucount.c > index 11b1596e2542..0f2c7c11df19 100644 > --- a/kernel/ucount.c > +++ b/kernel/ucount.c > @@ -141,7 +141,8 @@ static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid) > > new->ns = ns; > new->uid = uid; > - new->count = 0; > + > + atomic_set(&new->count, 0); > > spin_lock_irq(&ucounts_lock); > ucounts = find_ucounts(ns, uid, hashent); > @@ -152,10 +153,10 @@ static struct ucounts *get_ucounts(struct user_namespace *ns, kuid_t uid) > ucounts = new; > } > } > - if (ucounts->count == INT_MAX) > + if (atomic_read(&ucounts->count) == INT_MAX) > ucounts = NULL; > else > - ucounts->count += 1; > + atomic_inc(&ucounts->count); > spin_unlock_irq(&ucounts_lock); > return ucounts; > } > @@ -165,8 +166,7 @@ static void put_ucounts(struct ucounts *ucounts) > unsigned long flags; > > spin_lock_irqsave(&ucounts_lock, flags); > - ucounts->count -= 1; > - if (!ucounts->count) > + if (atomic_dec_and_test(&ucounts->count)) > hlist_del_init(&ucounts->node); > else > ucounts = NULL; This can become: static void put_ucounts(struct ucounts *ucounts) { unsigned long flags; if (atomic_dec_and_lock_irqsave(&ucounts->count, &ucounts_lock, flags)) { hlist_del_init(&ucounts->node); spin_unlock_irqrestore(&ucounts_lock); kfree(ucounts); } }