Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp978929pxp; Wed, 16 Mar 2022 23:17:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxhn16786iijprk6nj+vEqOuMC4AAzlLxFayBltuLbInaD3SA3msSDfs8631mKV95yXo9y X-Received: by 2002:a63:4041:0:b0:37f:8077:e0de with SMTP id n62-20020a634041000000b0037f8077e0demr2421502pga.138.1647497848309; Wed, 16 Mar 2022 23:17:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647497848; cv=none; d=google.com; s=arc-20160816; b=0JKoZN2NXP6qEzJnyFNHe678+M8OWj5NW2gYe227HD4+Yc758HsojcwW4kvzzw293a MxpDIX/6wvQPeqlD925iXUo6jz/MT81viJ93bNhwDaeJhr6l/twB7UYQR9rzmRoEBEAx 1irl4nhrilf1wtR1KLwyLM7AFMQ3lsNfEZwy3UZxa946T3vqmVvAIxbZdVoKT1js0KDh qnr+wNLPScLT5AzdJfedVS4ElErHzXH3hcneqz8LB93KI6HMwzNfdklldpYypBYboTot q+/oOqfrlWXEGFdSFVgD+axIDKcnaAPBkqTbxmt0qOXr1IjjzJjFdqqtBoCM02Pk54uY FLuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=m18+z5ITkHioop9Yr3gJlYG21fmrBFOP2qxU2Z/yrrk=; b=R05/KjVj7pg/uVp6s2NFR3s/WRrkKj9d51BDGTOdLB0aPPwRNT+3j51lKvhUm/0A+W tXAm4nYkQdbZJs9DlxuS22KREboCnLfAq+Eq2eMhyIQ6BrDePC8nIWi5wVfUsrBYXUs5 /CxsssJugFpc3nmbvD+w3+TRss4Grnxhwqx2KYmRjux0d8PZ4UeSmBPF22Exa0OQMU3i vlFglSmW4zOcahJNwEv0IOVEbpM9d+YABjp1ebci4LgAy45i+qEqqt8pKPB13M4jpF5m 76/m/IJ3SIvYPUNi7hk+AT6vDy0q7IfT29Nbeedw6FcV+AbNfaZ+djQ1em553FnW3Mds d9uw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id t11-20020a63780b000000b003816043ef3esi1166913pgc.307.2022.03.16.23.17.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 23:17:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4484B22E972; Wed, 16 Mar 2022 22:01:44 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353978AbiCPG4d (ORCPT + 99 others); Wed, 16 Mar 2022 02:56:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239264AbiCPG4d (ORCPT ); Wed, 16 Mar 2022 02:56:33 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 736245EBD0 for ; Tue, 15 Mar 2022 23:55:18 -0700 (PDT) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.53]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4KJLXc092Jz9sgQ; Wed, 16 Mar 2022 14:51:28 +0800 (CST) Received: from [10.174.177.76] (10.174.177.76) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Wed, 16 Mar 2022 14:55:16 +0800 Subject: Re: [PATCH v2] mm/mlock: fix potential imbalanced rlimit ucounts adjustment To: "Eric W. Biederman" CC: , , , , Alexey Gladkov References: <20220314064039.62972-1-linmiaohe@huawei.com> <87h78036hl.fsf@email.froward.int.ebiederm.org> <82cf5aa8-a721-3ff3-7b09-54a66da0d506@huawei.com> <87lexbyslf.fsf@email.froward.int.ebiederm.org> From: Miaohe Lin Message-ID: <4803adf1-ba98-badc-6820-0948871b0742@huawei.com> Date: Wed, 16 Mar 2022 14:55:15 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: <87lexbyslf.fsf@email.froward.int.ebiederm.org> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.177.76] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/3/16 2:32, Eric W. Biederman wrote: > Miaohe Lin writes: > >> On 2022/3/14 23:21, Eric W. Biederman wrote: >>> Miaohe Lin writes: >>> >>>> user_shm_lock forgets to set allowed to 0 when get_ucounts fails. So >>>> the later user_shm_unlock might do the extra dec_rlimit_ucounts. Fix >>>> this by resetting allowed to 0. >>> >>> This fix looks correct. But the ability for people to follow and read >>> the code seems questionable. I saw in v1 of this patch Hugh originally >>> misread the logic. >>> >>> Could we instead change the code to leave lock_limit at ULONG_MAX aka >>> RLIM_INFINITY, leave initialized to 0, and not even need a special case >>> of RLIM_INFINITY as nothing can be greater that ULONG_MAX? >>> >> >> Many thanks for your advice. This looks good but it seems this results in different >> behavior: When (memlock == LONG_MAX) && !capable(CAP_IPC_LOCK), we would fail now >> while it will always success without this change. We should avoid this difference. >> Or am I miss something? Maybe the origin patch is more suitable and >> simple? > > Interesting. I think that is an unintended and necessary bug fix. > > When memlock == LONG_MAX that means inc_rlimit_ucounts failed. > > It either failed because at another level the limit was exceeded or > because the counter wrapped. In either case it is not appropriate to > succeed if inc_rlimit_ucounts detects a failure. > > Which is a long way of saying I think we really want the simplification > because it found and fixed another bug as well. > > Without the simplification I don't think I will be confident the code is > correct. Agree with you. This is a potential bug and you just catch it with the code simplification. :) Am I supposed to do this altogether or will you do this simplification part? Many thanks. > > Eric > > >> Thanks. >> >>> Something like this? >>> >>> diff --git a/mm/mlock.c b/mm/mlock.c >>> index 8f584eddd305..e7eabf5193ab 100644 >>> --- a/mm/mlock.c >>> +++ b/mm/mlock.c >>> @@ -827,13 +827,12 @@ int user_shm_lock(size_t size, struct ucounts *ucounts) >>> >>> locked = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; >>> lock_limit = rlimit(RLIMIT_MEMLOCK); >>> - if (lock_limit == RLIM_INFINITY) >>> - allowed = 1; >>> - lock_limit >>= PAGE_SHIFT; >>> + if (lock_limit != RLIM_INFINITY) >>> + lock_limit >>= PAGE_SHIFT; >>> spin_lock(&shmlock_user_lock); >>> memlock = inc_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked); >>> >>> - if (!allowed && (memlock == LONG_MAX || memlock > lock_limit) && !capable(CAP_IPC_LOCK)) { >>> + if ((memlock == LONG_MAX || memlock > lock_limit) && !capable(CAP_IPC_LOCK)) { >>> dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked); >>> goto out; >>> } >>> >>>> >>>> Fixes: d7c9e99aee48 ("Reimplement RLIMIT_MEMLOCK on top of ucounts") >>>> Signed-off-by: Miaohe Lin >>>> Acked-by: Hugh Dickins >>>> --- >>>> v1->v2: >>>> correct Fixes tag and collect Acked-by tag >>>> Thanks Hugh for review! >>>> --- >>>> mm/mlock.c | 1 + >>>> 1 file changed, 1 insertion(+) >>>> >>>> diff --git a/mm/mlock.c b/mm/mlock.c >>>> index 29372c0eebe5..efd2dd2943de 100644 >>>> --- a/mm/mlock.c >>>> +++ b/mm/mlock.c >>>> @@ -733,6 +733,7 @@ int user_shm_lock(size_t size, struct ucounts *ucounts) >>>> } >>>> if (!get_ucounts(ucounts)) { >>>> dec_rlimit_ucounts(ucounts, UCOUNT_RLIMIT_MEMLOCK, locked); >>>> + allowed = 0; >>>> goto out; >>>> } >>>> allowed = 1; >>> >>> Eric >>> . >>> > . >