From patchwork Tue Sep 12 10:36:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 138451 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp697919vqx; Tue, 12 Sep 2023 14:47:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGOi28cv0Ixva6dUjSWFHURKYid2BvSzcHeX2p9JbQL2NRquzNlsHChrO42fN2LnGcg3Dbq X-Received: by 2002:a05:6870:8a23:b0:1c4:ee87:d3ea with SMTP id p35-20020a0568708a2300b001c4ee87d3eamr1001050oaq.36.1694555241307; Tue, 12 Sep 2023 14:47:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694555241; cv=none; d=google.com; s=arc-20160816; b=HUy0WgoEpWCHlK1bN/+4kavjvDSsJKg+t/7B2BpVo6HyXe67xlRXUb6am5eZ9eQaue InwfwWAfUV89zekdK8duHUSZZIVfOvOSXeIqYCyp7a7Dsq57Yfa2+t7uCYPYil+Mnr3y 18P9BWO/MnDQ8Fr26OVhKRCTb3Uo+8KvFprVhNjbmjdu355GYstFphsyW9zRBvXtIdd6 JM5avwFaO6UWZ16RINHg8WRH5/rkRXrw6i+Tw546KdcULc8zSuZeQ8VLgQjYrCl4tFh1 2Q9t499TvFYe5YwEfBFdNPUnq6XzGoQ7WlotIUzigHL5nt1y40vUyMP1heGQy8GoXJ1k 1+Ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=am+Un2OqmW2Loepn8y4t8ZkGjNdfoKTvcEiRMqelg0M=; fh=H2MkSknZKy/XcUE0SNWlqZifRHvGVZQUr5cmv0Cbtz8=; b=OmTRTDciAU87f8ggDMW0RlQ9rmsnZEG2kOowu43Ptxsp7xIrbvA05YiN/RNqTPqaJ4 Jm16A5U8PXJLcmzNY6a3I6ktYfxy70ybvMocR/jJqY5MvOPWECFImqT2X8e3JDBRfex6 2400BmtKXBjXeLWvBjFSHXzbTNKvOX04ApV+CAQKyjMEB4HjRUU7HgCC5TyCgEZlyFJX LZo9F/WMTxh/BJ/KpXVKUqmTsGDMP2La/B6Iujr54WgNO0KjSgMMis8BN8Q4Z0A2/hMD T1lb5b+1YSHv54kwY77AksWtDjm7uUKChQF4Yyj6Qrf26Rr5QWIpw3mJfU0H+2+ft0sQ sqnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=fhTkufp6; dkim=neutral (no key) header.i=@linutronix.de header.b=j4gQluEx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id p21-20020a634f55000000b0057404ce2fc8si8380698pgl.529.2023.09.12.14.47.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 14:47:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=fhTkufp6; dkim=neutral (no key) header.i=@linutronix.de header.b=j4gQluEx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id A975C834F76A; Tue, 12 Sep 2023 03:37:37 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234377AbjILKhf (ORCPT + 37 others); Tue, 12 Sep 2023 06:37:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233873AbjILKhK (ORCPT ); Tue, 12 Sep 2023 06:37:10 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 158FD26A9; Tue, 12 Sep 2023 03:36:34 -0700 (PDT) Date: Tue, 12 Sep 2023 10:36:32 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694514992; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=am+Un2OqmW2Loepn8y4t8ZkGjNdfoKTvcEiRMqelg0M=; b=fhTkufp6UE56eD+60KBBsTVgb9JUQNGf+SJe0nsedTCsm38j5UX2b4wc6AV3bLv51eyD7x 118fyEm3wAkR6pE9p9gIPpiC6xHvRNaQDwYP2LrKGfKUB1iR8RpbJGA8JM4PdGmdiMtNev 4SpwwsesGrLetor2GTvYYRPf1Uuk+PvKczoqRth/PrUKYzyZf002yiKh+RB8AewTM1cVj9 uNlhnulLE3j163cl2VwD8yfCx4+4r/JfNHw5DT0bEAe5NIqAwMIiYUhtUmXZJHwoaAzyLn 0CLDvXK1GGwfDxQhJysE/evaBGJ72G04IJ9Ghi23wNl6geVzyUU5NEUxXo6Ong== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694514992; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=am+Un2OqmW2Loepn8y4t8ZkGjNdfoKTvcEiRMqelg0M=; b=j4gQluExe9atJWySglxGy9jOpSOZD0VHYZgc3rWi8ImNgRu6MZTE0ZjPrDhnGJKO4sf7vc 9zmKe8XilTn1XICg== From: "tip-bot2 for Peter Zijlstra" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: locking/core] cleanup: Make no_free_ptr() __must_check Cc: "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230816103102.GF980931@hirez.programming.kicks-ass.net> References: <20230816103102.GF980931@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Message-ID: <169451499208.27769.5856056754166699857.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails 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 (howler.vger.email [0.0.0.0]); Tue, 12 Sep 2023 03:37:37 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776869956742199549 X-GMAIL-MSGID: 1776869956742199549 The following commit has been merged into the locking/core branch of tip: Commit-ID: 68373ebb9d61985e05574313a356f751ef9911ab Gitweb: https://git.kernel.org/tip/68373ebb9d61985e05574313a356f751ef9911ab Author: Peter Zijlstra AuthorDate: Tue, 15 Aug 2023 12:52:04 +02:00 Committer: root CommitterDate: Sat, 09 Sep 2023 15:10:27 +02:00 cleanup: Make no_free_ptr() __must_check recent discussion brought about the realization that it makes sense for no_free_ptr() to have __must_check semantics in order to avoid leaking the resource. Additionally, add a few comments to clarify why/how things work. All credit to Linus on how to combine __must_check and the stmt-expression. Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20230816103102.GF980931@hirez.programming.kicks-ass.net --- include/linux/cleanup.h | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h index 53f1a7a..9f1a9c4 100644 --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -7,8 +7,9 @@ /* * DEFINE_FREE(name, type, free): * simple helper macro that defines the required wrapper for a __free() - * based cleanup function. @free is an expression using '_T' to access - * the variable. + * based cleanup function. @free is an expression using '_T' to access the + * variable. @free should typically include a NULL test before calling a + * function, see the example below. * * __free(name): * variable attribute to add a scoped based cleanup to the variable. @@ -17,6 +18,9 @@ * like a non-atomic xchg(var, NULL), such that the cleanup function will * be inhibited -- provided it sanely deals with a NULL value. * + * NOTE: this has __must_check semantics so that it is harder to accidentally + * leak the resource. + * * return_ptr(p): * returns p while inhibiting the __free(). * @@ -24,6 +28,8 @@ * * DEFINE_FREE(kfree, void *, if (_T) kfree(_T)) * + * void *alloc_obj(...) + * { * struct obj *p __free(kfree) = kmalloc(...); * if (!p) * return NULL; @@ -32,6 +38,24 @@ * return NULL; * * return_ptr(p); + * } + * + * NOTE: the DEFINE_FREE()'s @free expression includes a NULL test even though + * kfree() is fine to be called with a NULL value. This is on purpose. This way + * the compiler sees the end of our alloc_obj() function as: + * + * tmp = p; + * p = NULL; + * if (p) + * kfree(p); + * return tmp; + * + * And through the magic of value-propagation and dead-code-elimination, it + * eliminates the actual cleanup call and compiles into: + * + * return p; + * + * Without the NULL test it turns into a mess and the compiler can't help us. */ #define DEFINE_FREE(_name, _type, _free) \ @@ -39,8 +63,17 @@ #define __free(_name) __cleanup(__free_##_name) +#define __get_and_null_ptr(p) \ + ({ __auto_type __ptr = &(p); \ + __auto_type __val = *__ptr; \ + *__ptr = NULL; __val; }) + +static inline __must_check +const volatile void * __must_check_fn(const volatile void *val) +{ return val; } + #define no_free_ptr(p) \ - ({ __auto_type __ptr = (p); (p) = NULL; __ptr; }) + ((typeof(p)) __must_check_fn(__get_and_null_ptr(p))) #define return_ptr(p) return no_free_ptr(p)