Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp591152ybg; Fri, 12 Jun 2020 09:21:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJySR662hVV2ezNtH2vZGP8tq4ccpnxN+gUXqfGOGHKUhFMJoGoTDlOIW9IL8vcwyBTS2KPl X-Received: by 2002:a17:906:63c9:: with SMTP id u9mr14327833ejk.439.1591978881661; Fri, 12 Jun 2020 09:21:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591978881; cv=none; d=google.com; s=arc-20160816; b=v9P2j+DLJr7YsEpEQh0eXw9PWdI1sdKaCJnzYI5y/ACaXPyeuEfhkQC8HVcvGh+Hjt QW729PaLGvwXEycteu9oZB9o9O5CRZE+0V2pgdB/TtnC0EuXhIi7W6amMg9m419/b7ba 36hY9ctHOb1c+fYwBbLO7+OSD9Jw/vv3uPOnffijkf13KKQtdLfWtaJVuZG+NtqgsHvv ucBkKz8vmyXP+AnV4r/ssyzfCPHMmsyMmoxXMviL67uscB0yrX7tcVPF6lewtUwT68kD B1ynL2Sfe5cquIFe7e6pwxzLu3QUKwfvb1gK9I2ZvO/ojgVWdtYxgCo11woQspfobx3S szKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from; bh=H50lWriwhIceMny9Dm3N+38ZsDPFhZMGadvckUtitWM=; b=lVAd++domg7ncZfsa1WAVghJi+AmRmwm44DDdLDCUgmnGO+RsGaEDEDByNmficSC0W oIwDJXIXZNCHYWHf2MY9kp4+dGfy/oToY830cIAY7/GWcNIlm5noXalpNxeJrlKITa2d 139an0eHdHVAuzOIF34BeYnJiPzZWGamgwL9fNM/8fjKb359/T3LiDoGLGJGbxyJWBJQ FR442vALwt0vRu4q6MMLTdjYcEVIuaq367qlUOKzvu/V1O7Nz44WbqSgjG22kveCRNUj VmT5IzV+J5/YFsmDAyO/bnDdvYPLfsDi15EuGKHYC+/aKT/WH/g+G+8zYNDzBDkvsixP 5+sg== 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=aculab.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lx2si4124537ejb.284.2020.06.12.09.20.58; Fri, 12 Jun 2020 09:21:21 -0700 (PDT) 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=aculab.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726314AbgFLQRD convert rfc822-to-8bit (ORCPT + 99 others); Fri, 12 Jun 2020 12:17:03 -0400 Received: from eu-smtp-delivery-151.mimecast.com ([146.101.78.151]:42211 "EHLO eu-smtp-delivery-151.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726268AbgFLQRC (ORCPT ); Fri, 12 Jun 2020 12:17:02 -0400 Received: from AcuMS.aculab.com (156.67.243.126 [156.67.243.126]) (Using TLS) by relay.mimecast.com with ESMTP id uk-mta-275-Rg5Ewbm4N1GXENH-TTDXpQ-1; Fri, 12 Jun 2020 17:16:58 +0100 X-MC-Unique: Rg5Ewbm4N1GXENH-TTDXpQ-1 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:43c:695e:880f:8750) by AcuMS.aculab.com (fd9f:af1c:a25b:0:43c:695e:880f:8750) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Fri, 12 Jun 2020 17:16:56 +0100 Received: from AcuMS.Aculab.com ([fe80::43c:695e:880f:8750]) by AcuMS.aculab.com ([fe80::43c:695e:880f:8750%12]) with mapi id 15.00.1347.000; Fri, 12 Jun 2020 17:16:56 +0100 From: David Laight To: 'Xiaoming Ni' , "paul@paul-moore.com" , "edumazet@google.com" , "peterz@infradead.org" , "paulmck@kernel.org" , "dhowells@redhat.com" , "keescook@chromium.org" , "shakeelb@google.com" , "jamorris@linux.microsoft.com" CC: "alex.huangjianhui@huawei.com" , "dylix.dailei@huawei.com" , "chenzefeng2@huawei.com" , "linux-kernel@vger.kernel.org" Subject: RE: [PATCH RFC] cred: Add WARN to detect wrong use of get/put_cred Thread-Topic: [PATCH RFC] cred: Add WARN to detect wrong use of get/put_cred Thread-Index: AQHWQKQ4FhLI5egRjUmWWYYpyV8csKjVJp0w Date: Fri, 12 Jun 2020 16:16:56 +0000 Message-ID: <9a680489a44b44d397f8a3e77a6503e7@AcuMS.aculab.com> References: <1591957695-118312-1-git-send-email-nixiaoming@huawei.com> In-Reply-To: <1591957695-118312-1-git-send-email-nixiaoming@huawei.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Xiaoming Ni > Sent: 12 June 2020 11:28 > Cred release and usage check code flow: > 1. put_cred() > if (atomic_dec_and_test(&(cred)->usage)) > __put_cred(cred); > > 2. __put_cred() > BUG_ON(atomic_read(&cred->usage) != 0); > call_rcu(&cred->rcu, put_cred_rcu); > > 3. put_cred_rcu() > if (atomic_read(&cred->usage) != 0) > panic("CRED: put_cred_rcu() sees %p with usage %d\n", > cred, atomic_read(&cred->usage)); > kmem_cache_free(cred_jar, cred); > > If panic is triggered on put_cred_rcu(), there are two possibilities > 1. Call get_cred() after __put_cred(), usage > 0 > 2. Call put_cred() after __put_cred(), usage < 0 > Since put_cred_rcu is an asynchronous behavior, it is no longer the first > scene when panic, there is no information about the murderer in the panic > call stack... > > So, add WARN() in get_cred()/put_cred(), and pray to catch the murderer > at the first scene. > > Signed-off-by: Xiaoming Ni > --- > include/linux/cred.h | 14 +++++++++++--- > 1 file changed, 11 insertions(+), 3 deletions(-) > > diff --git a/include/linux/cred.h b/include/linux/cred.h > index 18639c0..c00d5a1 100644 > --- a/include/linux/cred.h > +++ b/include/linux/cred.h > @@ -224,11 +224,16 @@ static inline bool cap_ambient_invariant_ok(const struct cred *cred) > * > * Get a reference on the specified set of new credentials. The caller must > * release the reference. > + * > + * Initialize usage to 1 during cred resource allocation, > + * so when calling get_cred, usage cannot be 0. > */ > static inline struct cred *get_new_cred(struct cred *cred) > { > - atomic_inc(&cred->usage); > - return cred; > + if (atomic_inc_not_zero(&cred->usage)) > + return cred; > + WARN(1, "get_new_cred after __put_cred"); > + return NULL; > } > > /** > @@ -280,11 +285,14 @@ static inline const struct cred *get_cred_rcu(const struct cred *cred) > static inline void put_cred(const struct cred *_cred) > { > struct cred *cred = (struct cred *) _cred; > + int usage; > > if (cred) { > validate_creds(cred); > - if (atomic_dec_and_test(&(cred)->usage)) > + usage = atomic_dec_return(&(cred)->usage); > + if (usage == 0) > __put_cred(cred); > + WARN(usage < 0, "put_cred after __put_cred"); > } > } You really don't want to add WARN() to static inline functions. It will bloat horribly. It might be possible to the message into a called function. One thing I've thought about for reference counts is for the code that allocates and frees the item to add a big number and code that only borrows a reference just adds 1. If the counter is large enough you can separately detect double frees and missing frees for the two different types of allocation. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)