From patchwork Wed Mar 23 23:42:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 8655361 Return-Path: X-Original-To: patchwork-linux-sparse@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 73C66C0553 for ; Wed, 23 Mar 2016 23:42:53 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 70F572035B for ; Wed, 23 Mar 2016 23:42:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A0E44201C8 for ; Wed, 23 Mar 2016 23:42:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752123AbcCWXmu (ORCPT ); Wed, 23 Mar 2016 19:42:50 -0400 Received: from mail-wm0-f44.google.com ([74.125.82.44]:37972 "EHLO mail-wm0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751649AbcCWXmt (ORCPT ); Wed, 23 Mar 2016 19:42:49 -0400 Received: by mail-wm0-f44.google.com with SMTP id l68so44084616wml.1 for ; Wed, 23 Mar 2016 16:42:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id; bh=CJ5dQHi7h2ejLRWXcmee91XmCNw8U6zW971xFLc4DzM=; b=gYR30gYPR8ZtSXWX7IoI9RU4XRSsE+7JbOEvO1YRCuLbmq5POFjvHJyD/0S/fiH9Id YtJukJJDCZISQsuvVUbOKnSBg2Vss7ibVpQlvv0G7Suli+3Q5QYjLiNl2vmAs8mY1g7T e9mt8W6sA0JSxo8niRZjs1xq9HvfdJewk5h9s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=CJ5dQHi7h2ejLRWXcmee91XmCNw8U6zW971xFLc4DzM=; b=LeJzMmvzB6m6jnPGJo8gMANAhV90CvYngkyfQgYJ0guTNwxKoZI67/oWOPZSyvwtDo f4UKIfVRxrn+K9/r7YzfFXSEIQ/xGbbaIhg01vCadacIfKsPLSOHONloDO3aVWrfG+Kq jX5GiBqPWHeFL7HwbhzDfsFa/HjG7MtTQubGqRoaVFBhN2L/9UJxv4zoE/KNo1UiW6K3 CRYIE73c2DsjsMQ51XQSUaxvUiO9HMjX2C3Ke29vszC8pPweADwy4fp/Gp5PmI7GlWKm gb7z+4CdEHhypiFWovm/foHpwZeCxWnm7a9FFcJ2fehv57321xTaW0WwiSAJyrN/EB3y YyQQ== X-Gm-Message-State: AD7BkJKHyPvZPr4pUBDcLdN3bjUoQI+qshNSdCA6RANNBmJreOHMDHMepS+PjuCLYukHig== X-Received: by 10.194.184.139 with SMTP id eu11mr6913303wjc.169.1458776567674; Wed, 23 Mar 2016 16:42:47 -0700 (PDT) Received: from localhost.localdomain (lvps87-230-87-209.dedicated.hosteurope.de. [87.230.87.209]) by smtp.gmail.com with ESMTPSA id i5sm4750771wjx.15.2016.03.23.16.42.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 23 Mar 2016 16:42:47 -0700 (PDT) From: Rasmus Villemoes To: Andrew Morton , Christopher Li Cc: Andi Kleen , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-sparse@vger.kernel.org Subject: [PATCH 1/2] compiler.h: add support for malloc attribute Date: Thu, 24 Mar 2016 00:42:31 +0100 Message-Id: <1458776553-9033-1-git-send-email-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.1.4 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID,T_RP_MATCHES_RCVD,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP gcc as far back as at least 3.04 documents the function attribute __malloc__. Add a shorthand for attaching that to a function declaration. This was also suggested by Andi Kleen way back in 2002 [1], but didn't get applied, perhaps because gcc at that time generated the exact same code with and without this attribute. This attribute tells the compiler that the return value (if non-NULL) can be assumed not to alias any other valid pointers at the time of the call. Please note that the documentation for a range of gcc versions (starting from around 4.7) contained a somewhat confusing and self-contradicting text: The malloc attribute is used to tell the compiler that a function may be treated as if any non-NULL pointer it returns cannot alias any other pointer valid when the function returns and *that the memory has undefined content*. [...] Standard functions with this property include malloc and *calloc*. (emphasis mine). The intended meaning has later been clarified [2]: This tells the compiler that a function is malloc-like, i.e., that the pointer P returned by the function cannot alias any other pointer valid when the function returns, and moreover no pointers to valid objects occur in any storage addressed by P. What this means is that we can apply the attribute to kmalloc and friends, and it is ok for the returned memory to have well-defined contents (__GFP_ZERO). But it is not ok to apply it to kmemdup(), nor to other functions which both allocate and possibly initialize the memory with existing pointers. So unless someone is doing something pretty perverted kstrdup() should also be a fine candidate. [1] http://thread.gmane.org/gmane.linux.kernel/57172 [2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56955 Signed-off-by: Rasmus Villemoes --- include/linux/compiler-gcc.h | 1 + include/linux/compiler.h | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h index 22ab246feed3..9004f153c5ab 100644 --- a/include/linux/compiler-gcc.h +++ b/include/linux/compiler-gcc.h @@ -142,6 +142,7 @@ #if GCC_VERSION >= 30400 #define __must_check __attribute__((warn_unused_result)) +#define __malloc __attribute__((__malloc__)) #endif #if GCC_VERSION >= 40000 diff --git a/include/linux/compiler.h b/include/linux/compiler.h index b5ff9881bef8..793c0829e3a3 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -357,6 +357,10 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s #define __deprecated_for_modules #endif +#ifndef __malloc +#define __malloc +#endif + /* * Allow us to avoid 'defined but not used' warnings on functions and data, * as well as force them to be emitted to the assembly file.