From patchwork Mon Oct 7 14:49:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13824830 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 A5141CFB443 for ; Mon, 7 Oct 2024 14:50:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41E4B6B00A2; Mon, 7 Oct 2024 10:50:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A7AC6B00A3; Mon, 7 Oct 2024 10:50:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1FA256B00A4; Mon, 7 Oct 2024 10:50:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id F062F6B00A2 for ; Mon, 7 Oct 2024 10:50:44 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A2026ABE6D for ; Mon, 7 Oct 2024 14:50:44 +0000 (UTC) X-FDA: 82647092808.10.A26F57B Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf09.hostedemail.com (Postfix) with ESMTP id BBD2E140004 for ; Mon, 7 Oct 2024 14:50:42 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=TVww5mTh; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.181 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=1728312465; 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=fBFkTCCgw4nKzS/UQclQt52OlK7datrkk2Sl/mMl9SE=; b=1sZXduMiraRnfPcmMNmIQsDpcNi5j9JyqtXx+w1xKvsz8i/ZGsbUGDMlR0zZNEd3teAozB j4jH3D2nPLtl4BKLhXrgh7fmq9iJNV3AaU5zdQ9z5W7pEb6eMM0ccg7S+9rfcWHTg9sRxa +KWYu/1+QN+wpt6ouNdHho83upe6FTw= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=TVww5mTh; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.181 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=1728312465; a=rsa-sha256; cv=none; b=4Z0rD/dC5f2PAgYErZ4C36Zuek0wP+IT5KSqlPhZCdZobKsq5rzNXa2B8BPqR5CdmUasSq uUokSt2svyxvm/xWPiJKlFlaB33nZyryTkFILFsarr0eiwHHh+QbPaQbAS7tITILdo5D0t U+X3WCB9QEnmuIxrVEgkaEc+wBr3ra0= Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-71df67c6881so1727686b3a.3 for ; Mon, 07 Oct 2024 07:50:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728312641; x=1728917441; 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=fBFkTCCgw4nKzS/UQclQt52OlK7datrkk2Sl/mMl9SE=; b=TVww5mTh6AL05q1SKqW9VrY+0XmX4uOWdLPrgoR8D9WnLLwyboTy2eA0Zr4I36r1HM e47tms/macbSVoZdg1bxtQSMt1emUnfdSuZ/NS8JPNDAcAF9ja6hSEgnr2gXrVZbY0uD 0VgoIx3L0VoVtfS1t4nsJmk34jNs7tN1v02BQ95VElG/+8YAEfaSTP+lGJlVkIAqadt5 Eutj4gAQSCRCiZJXR87/Fs7ha/BblbvWtQvQhFyVgEUrCYOdcSK05xEJ9Cwn77Dph8i/ pSzjtZT/CpQg3bGY+6J8t8MHuf+ktqwRortDCuwSvjr/41LqevqF1Uc3HuuoL+e/3FZ5 hPqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728312641; x=1728917441; 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=fBFkTCCgw4nKzS/UQclQt52OlK7datrkk2Sl/mMl9SE=; b=JTxxu6qw+EYJG5eotVV7fdZDIoy4gibKkHyHN8Gl5YHe2flj+whfLKSPnneDDv49/C XX00PiBjD94DCsL7QeIAZc9TkGwsA8hoIEAK5IimKgb68oAf3zZ6PLighbNxpMZrTA6U eClR6HmeA6gEhPDcvFVnYa2rlmoxKfTzBNz7Coy2/vkgIADNW4EnvlK5ooUYj/DOsytU T3MWGOz90vnGAK+fu9z6XcXjP+rzMpbpxIovkIsNqdYC4hMQnOnEIOJUeWG1qCpbvDTy vtww63RvVSIa2B48oWn5a89I59qVjhjEYlKMgoKn0tdZlwh3iEdjb7XDrvpZTen6Egum pXug== X-Forwarded-Encrypted: i=1; AJvYcCVFkj3/AEGetUwTxBRa5ONMrJrLMb4hyVQpOPwzu8jIgpGT35SJfvYTuCLuZKCVZDv1YzPM77zVcQ==@kvack.org X-Gm-Message-State: AOJu0YwFnGz7eWb8CHccOAtme3icEaFY1Y781OOdYcHVWF3jeQLuvlM3 M8z5KRvq1yOgVgkQEJ/YBnvdiZj4TncGwzqdqp9SiKsw/SmUvM34 X-Google-Smtp-Source: AGHT+IGsy7J8upAtizdqT7cwdExa0Ot6GSfKRh917J35OaiwbZMAM2iFolVRA0oRI0wvfYtn8vWlEw== X-Received: by 2002:a05:6a20:9f8f:b0:1d3:e4e:ff55 with SMTP id adf61e73a8af0-1d6dfa22f17mr20436900637.7.1728312641423; Mon, 07 Oct 2024 07:50:41 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.43]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0d7cf82sm4466432b3a.200.2024.10.07.07.50.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Oct 2024 07:50:40 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, keescook@chromium.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Simon Horman , Matthew Wilcox Subject: [PATCH v9 6/7] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Mon, 7 Oct 2024 22:49:10 +0800 Message-Id: <20241007144911.27693-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20241007144911.27693-1-laoar.shao@gmail.com> References: <20241007144911.27693-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: BBD2E140004 X-Stat-Signature: 8is9yjwf3a5jexc7s9umwgdo4y86n6hg X-Rspam-User: X-HE-Tag: 1728312642-977272 X-HE-Meta: U2FsdGVkX19RgqlphE5uzo+IpSohQ8pOdeQhM7PMdbsqZl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klOBN1vKMwJZ4D/a2hjp/h8fCVkBH65kE2YW9KqoOgZERCYmLd9HWDRqvTv6ilQJyWeC/gSIFJISGbi6N9Opbkn3PLBrJtSlMzXnn19ZZGQ1QYibvjJl0Bm52oJB7p16+nNnTBH36dSlIvPeYHeKZEW3sq32ifaEPpZvP+XWAytQEP5sQ8tYxnuobExJoPwLkc37hKrwC8arsegijmjxwOs49bGALgGX4TJXqh7fvjYH1zFaEScKWuo8AjCKFOXSb09PPGdVcBBc9yvqwsGcDWu4Cfb7Kf2X5HkRmvdZ1gnftxzI5eyIt2jO5wOAJCj3j9BfvbHdkzLra21KmBH80dSpP/LUz3B1vRSV1O3jz4T+rd40at1y2xvCBBziMlXuy3n17wjEwKBEvoypbSZ83el4AJc4CZKgzoUuwF4sHvXBfwIHV9+kQdzDsmlc0xqthDMdGAMHBuqzIhlqB0AJ2I6cpkdqlUJQsKKiLKuQtS6u0S6WlaVvOaT8RmGWArDMt3E9w3REBsRZT9aUiOVWR35auvxX 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 Cc: Alejandro Colomar --- mm/util.c | 69 ++++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 42 deletions(-) diff --git a/mm/util.c b/mm/util.c index 858a9a2f57e7..c7d851c40843 100644 --- a/mm/util.c +++ b/mm/util.c @@ -45,34 +45,41 @@ 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, not including the NUL 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) + /* '+1' for the NUL terminator */ + buf = kmalloc_track_caller(len + 1, 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 NUL terminator. - */ - buf[len - 1] = '\0'; - } + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len] = '\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), gfp) : NULL; +} EXPORT_SYMBOL(kstrdup); /** @@ -107,19 +114,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), gfp) : NULL; } EXPORT_SYMBOL(kstrndup); @@ -193,17 +188,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, gfp) : NULL; } EXPORT_SYMBOL(kmemdup_nul);