From patchwork Fri Jun 28 09:05:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715781 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7DEFC41513 for ; Fri, 28 Jun 2024 09:06:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 422DA6B00A4; Fri, 28 Jun 2024 05:06:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3D2E26B00A5; Fri, 28 Jun 2024 05:06:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 273666B00A6; Fri, 28 Jun 2024 05:06:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 029696B00A4 for ; Fri, 28 Jun 2024 05:06:04 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B38CAC16EA for ; Fri, 28 Jun 2024 09:06:04 +0000 (UTC) X-FDA: 82279715448.13.1F756FF Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf24.hostedemail.com (Postfix) with ESMTP id BD856180007 for ; Fri, 28 Jun 2024 09:06:02 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=frX9xi9J; spf=pass (imf24.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719565539; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=CwFCqiOnde+yMILbB/0A5gJ4DcnMztbiDMHTCTXoNxK7lQRzsjnpo3RIU+k74B0xH4hb1K ajyddZvAx89HfrbHd+7rdS8AePJ9mpUmvsfMnTBbxFrD4hGiZzzYSuIPo/mL2f2SxF5mYY iRFk0baG444XVTDbYgVp2lyCpboh+Sg= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=frX9xi9J; spf=pass (imf24.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719565539; a=rsa-sha256; cv=none; b=mQYxpehhf8qqoerRdoGNa0muP/d2s0FiNsc+Iv802vDzFZXd55CjRGep1vV0Axq1WzT6Gw eKCSDpnRRu6TThoCt4H8QE9HTnF1mldZvInmoXCFhGTmKhLiIHgq4K2f3neFYGItf69n2w 2G5uSCDBj/I7PbUCrzO00reb+SH3Xww= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1fa9ecfb321so2083705ad.0 for ; Fri, 28 Jun 2024 02:06:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565562; x=1720170362; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=frX9xi9J9mkTbky9Vg9+n98cJm4Nv2MHzd/g/oikuvCvnkU3NNQx//BSHzludExsGe H65fWsLJoYrS84dOHfc5iJWVgC+Ai5DL8R/qwO0SUlKx6Xn4a8fN+pUbXLv0RbQCEHRk nRv/Dx1mjj/8DJDTfHIepCPeWbYB+N5MqGguJbG9mZ3z6UxW5t95waJZ0m8jvKIBRVgK yHKtNYEVFEzo6tx/6sf8d5xdWpuy5af7wD7mA4yWPYPUrciea0c4BV7eFl5Ro94SypMK UYZ3e0tvy5VvtOZgJsXGLH52anYCKJnOgMBWPrs7+NZBMishFWnd+vensar2BIMw6Gcl UE+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565562; x=1720170362; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=A0hQSy3rSiKBDz3jPJJxz0ehg3FYiojR8AJ1ZoqPz6Nh65q+GJQY4ULGy8UWl2lPaE 5RNgAH03/stELWFV+WtFq94Fg605X8rBYR0IGSmGyk+RzZpL68uXoD/a7M54GTPbLV8R HYUGI2oJNyezTzNhrRRvMPg+DjVtxrX6hH/pvBP3qK3P/IJfDuM7QZyOcg16PJG125HK BRDN2m7WC2FKI9fHsy9seCTGYUCrfPCJJcSaFQkdxlqb9Nrhsnw9EA15OOzmRTLWbP0i vTFy0SwY1dDcOxN2uc1lTEiC99EKrNG/QauofBASvrAax9mWDkoY41dcjEAzyOKImUMf sHVw== X-Forwarded-Encrypted: i=1; AJvYcCV2MWIFIrth0rHPizzBal0wTCCBGTQsXfHlPKd1OwXq1UmFURiCdh2hO4bx+ezznke81XgioFo5Fye3kyU75ncSOk0= X-Gm-Message-State: AOJu0YxhMKrT07bTV3zdYUCBQ9JmRt6JHKQeha4vAS9/QfoVm38Pc3Ry poGFKk5V5+dDPexvTgCki/r6srr3ZYfjgxloGuvO3xmkts71SVFQ X-Google-Smtp-Source: AGHT+IF41G2qRww4DdF/h1fHsQyMDqy+U0zSGeUJQI71N0qwJDXNmrrmWplbD4mTRfu3C38uIRvv6A== X-Received: by 2002:a17:903:18d:b0:1fa:2001:d8ff with SMTP id d9443c01a7336-1fa2001dad7mr177652065ad.52.1719565561588; Fri, 28 Jun 2024 02:06:01 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:00 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Simon Horman , Matthew Wilcox Subject: [PATCH v4 06/11] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Fri, 28 Jun 2024 17:05:12 +0800 Message-Id: <20240628090517.17994-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: BD856180007 X-Stat-Signature: wewrtgdedkobae1ow8aiyjp7nniwcbp8 X-Rspam-User: X-HE-Tag: 1719565562-95911 X-HE-Meta: U2FsdGVkX19ZhoBqC7BC7AFI7N51e8l4bc7/pzxCuqrwEWDgCEhbJQjYKSwFv1EVg5Msvb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CJ5FKlojDQvEttkwm+mXQ9jPual6Bq8/TRoIcC82gtLpYHCFFfmLg0Q0xZwIzjEErdA5TM+q1CIfvkYVf8+8oBH3zj28yA1t+kZ0LzfGYRzzKdw9o1cnISB+WUbqSyfVO0Vx3Lb9eJNces9iWm1N0vpjmgsPZEOu9EnXLkMe6yD9TevBDUKRLGrg0VROG53yGrxzmNindq3puuACc4898+usxU+rKWdpfYR9U2PaXL2jj8K9Tbf6gCvdKUFfVda5HRC3joOD84Rb0XAfxjpdnNKP7hEmp3aIO/Gjc2B44mnK4S4V3oH6Vh2/TIRi+6BzV9cdkf+S4vjyF96K4E80tbvvdl6z69fx//St0vptrMbTttf0vyadjsqxwFbYs4MRCJxTRKZtfJKWN2BEgezBNYvpznjtr2VXnj1s34RsotvuO+wb2K2BjolCs391OwchEjYsp7i2EusuzpQlZs2QyrR6o87ZnXgIxgLnWCDKb4nXKu9B/H1/5axqvMKbq9AlKBWvv99QTBo= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: These three functions follow the same pattern. To deduplicate the code, let's introduce a common helper __kmemdup_nul(). Suggested-by: Andrew Morton Signed-off-by: Yafang Shao Cc: Simon Horman Cc: Matthew Wilcox --- mm/util.c | 67 +++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/mm/util.c b/mm/util.c index 41c7875572ed..62a4686352b9 100644 --- a/mm/util.c +++ b/mm/util.c @@ -43,33 +43,40 @@ void kfree_const(const void *x) EXPORT_SYMBOL(kfree_const); /** - * kstrdup - allocate space for and copy an existing string - * @s: the string to duplicate + * __kmemdup_nul - Create a NUL-terminated string from @s, which might be unterminated. + * @s: The data to copy + * @len: The size of the data, including the null terminator * @gfp: the GFP mask used in the kmalloc() call when allocating memory * - * Return: newly allocated copy of @s or %NULL in case of error + * Return: newly allocated copy of @s with NUL-termination or %NULL in + * case of error */ -noinline -char *kstrdup(const char *s, gfp_t gfp) +static __always_inline char *__kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - size_t len; char *buf; - if (!s) + buf = kmalloc_track_caller(len, gfp); + if (!buf) return NULL; - len = strlen(s) + 1; - buf = kmalloc_track_caller(len, gfp); - if (buf) { - memcpy(buf, s, len); - /* During memcpy(), the string might be updated to a new value, - * which could be longer than the string when strlen() is - * called. Therefore, we need to add a null termimator. - */ - buf[len - 1] = '\0'; - } + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len - 1] = '\0'; return buf; } + +/** + * kstrdup - allocate space for and copy an existing string + * @s: the string to duplicate + * @gfp: the GFP mask used in the kmalloc() call when allocating memory + * + * Return: newly allocated copy of @s or %NULL in case of error + */ +noinline +char *kstrdup(const char *s, gfp_t gfp) +{ + return s ? __kmemdup_nul(s, strlen(s) + 1, gfp) : NULL; +} EXPORT_SYMBOL(kstrdup); /** @@ -104,19 +111,7 @@ EXPORT_SYMBOL(kstrdup_const); */ char *kstrndup(const char *s, size_t max, gfp_t gfp) { - size_t len; - char *buf; - - if (!s) - return NULL; - - len = strnlen(s, max); - buf = kmalloc_track_caller(len+1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, strnlen(s, max) + 1, gfp) : NULL; } EXPORT_SYMBOL(kstrndup); @@ -190,17 +185,7 @@ EXPORT_SYMBOL(kvmemdup); */ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - char *buf; - - if (!s) - return NULL; - - buf = kmalloc_track_caller(len + 1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, len + 1, gfp) : NULL; } EXPORT_SYMBOL(kmemdup_nul);