From patchwork Fri Jun 21 02:29:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706697 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 C391DC2BA18 for ; Fri, 21 Jun 2024 02:31:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A3CB6B039C; Thu, 20 Jun 2024 22:31:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 553AE6B039E; Thu, 20 Jun 2024 22:31:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F4526B039F; Thu, 20 Jun 2024 22:31:44 -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 21E6B6B039C for ; Thu, 20 Jun 2024 22:31:44 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D4194A2212 for ; Fri, 21 Jun 2024 02:31:43 +0000 (UTC) X-FDA: 82253320086.11.14E9ADE Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf28.hostedemail.com (Postfix) with ESMTP id 0C1F1C0017 for ; Fri, 21 Jun 2024 02:31:41 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fMjF904q; spf=pass (imf28.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.171 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=1718937097; 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=gIxIpX3fSDij30wx/6pUEm1wGM6JrlT5xZ3d0lBG2nM=; b=f97TpSbFRQO9fwcr9XNtVEvZ74HjO23zBExKqIB/Yi9aR1oUn0mfwo6fSeFLW/4Qj3zVIn G6chTGHycADFB33FprLlPRc1SWImf5oX6n6qyhXo2RgQ8gOgV4jaMgVoxh3Xc13BvkFbXs PwPrWubV+sgXag6XCR8SYJuS/tJ/JOc= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fMjF904q; spf=pass (imf28.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.171 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=1718937097; a=rsa-sha256; cv=none; b=dCFzV8k8/bW6oDPHilixLK7my/gtgzJaLCs4G+iHEYVrD3Z1baw3Blhx/6Yxi95lZs9ZoD /3cAX0eyroMpM5wTJ67mZ+kNREopGm8a0MqZS4W0Ns2wxLcVPELaAPgUqRULLRgjd4RSSU yDiCMjRQO3AM/AD/Tg0VkXSWUueOoJ4= Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-705d9f4cd7bso1409227b3a.1 for ; Thu, 20 Jun 2024 19:31:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937101; x=1719541901; 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=gIxIpX3fSDij30wx/6pUEm1wGM6JrlT5xZ3d0lBG2nM=; b=fMjF904qm7Zp6br20HkRR/HOJ/z9EuxW/QAmUwkSj9FlkGzD8jWkgMSLZUnfXNPBGz gTGcUBoOjf9pTwrHfl/k7XwY0H+2TnF9WgNBhHiGl0qsk4zHLp2Vh+B5iZrJquHgQVGR ccIxdYuwAyS5x9uoeKoBsZHNz/eWCS4gngSS2h0NNzph2cotie4kHAGVclahEOXTAfVF jqVESJHi9upWw1JgRdFLFZvX7U5l8rQYODnZJc+suDMxpcTrCGHnuTZJ/yDl57+RWlVa va8yYA2GsMasq/A+xmxoYes3gqgdCElu7nvopzegTyuLON3ujj3l9ffC12X0wnFRiXX/ nzow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937101; x=1719541901; 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=gIxIpX3fSDij30wx/6pUEm1wGM6JrlT5xZ3d0lBG2nM=; b=EWpJmFitJGazyNxLN0g19RT+fBK9ip8+7MeWLOYdrss9twRa6KVruH0H+0nQXK2JVU bvMJLzkPi4rN4bDd1Hx0rg9ptfxZvs2JGSKNBDsMkrIKTP8ebOChHBThX1e00utCTC24 FCdXE7msjWpvQS8vXkrj5hraXoQD2iO3Nw1+S4sOzJMe8pUKG76bkx22F8l14rnZ5b0j 21z1J5mglRpqYBoI6uTEVhssqINeL9qLgzAomPhZkvaaeBiZmOiB+hSao1BvdcvWCW/0 SJLBzhpajE494/aHWyQSVWweXZYyOTJrBrdmaR/FrCZG09eHEs1jzVVZ6t6jBgZv5bUo /6qQ== X-Forwarded-Encrypted: i=1; AJvYcCXkZj1XbYc0EZHR+we3P0U2njhWskHPge4t/nY9VKpAX7zxpG8PaDoSQ3TBmgGNpJgdNNda9/DTq+1alymPDfNvtlA= X-Gm-Message-State: AOJu0YyFNTfByPdgUVlCcuvaGgkWMoyyqGmAR6BT8Adnqup4k4LMISw1 Y1MrkqsDC8mWLnX3CvlezhJLdiiWRMzHtnbhUbUrwdSxU65WqgIA X-Google-Smtp-Source: AGHT+IGYNoV/DB/o6zRof24rJ6xmZEwxYI9LpBEDyg5OiYjfuVgaYmaCgemL5OUn4y/KnIdaoKGjig== X-Received: by 2002:a05:6a00:2f43:b0:704:2d64:747 with SMTP id d2e1a72fcca58-70629c1dcdbmr6929306b3a.7.1718937100840; Thu, 20 Jun 2024 19:31:40 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.31.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:31:40 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 Subject: [PATCH v3 06/11] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Fri, 21 Jun 2024 10:29:54 +0800 Message-Id: <20240621022959.9124-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 0C1F1C0017 X-Stat-Signature: g895t6cofmswzr8ndbh4xc9717w383t9 X-HE-Tag: 1718937101-161768 X-HE-Meta: U2FsdGVkX18Dp7IUlmp8Epy/Z81O5is7u/R+Tjq925/1voV6a7LHJbMh1E8EqegFwENmPTuu6UIG+Nn79SuR7fjV54ljOl+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0vwKRtr8RolQ/3ahQJ8KFsZgC2Pa13F2fMmi9xyKtMTEBErTK6b8uywFrCyq3iRRNdDGdvxnsX4wAYpmO+y8kTNdcXD8xDYZTEQyBU9g7ykFPaoqieq++9of+92ZebCR2di2hecGiOrai+h6sjTPQaFKOGDV+9Qohfd6KJHFX3SSoE4m/OZKimDS3EOZ3KJCds4qh02K0NwR4BW7JuI1vW8d2pvRhYDBdIzsQN3ngjtNN9/PqumBobInto/h06PNuZja/hDAGeusTnV2Xeu2sS2+6xGDrwcTFJimdZNPu402lw2FgWryxK5m5GEwbbuCczWRu1z8cUbzKqbftOfVLW1bdzhYXgWvWI9UDlEfL9JJzHYUckWrqSxyTw== 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 help __kstrndup(). Suggested-by: Andrew Morton Signed-off-by: Yafang Shao --- mm/internal.h | 24 ++++++++++++++++++++++++ mm/util.c | 27 ++++----------------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index b2c75b12014e..fd87f685739b 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1521,4 +1521,28 @@ static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry, void workingset_update_node(struct xa_node *node); extern struct list_lru shadow_nodes; +/** + * __kstrndup - Create a NUL-terminated string from @s, which might be unterminated. + * @s: The data to stringify + * @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 with NUL-termination or %NULL in + * case of error + */ +static __always_inline char *__kstrndup(const char *s, size_t len, gfp_t gfp) +{ + char *buf; + + buf = kmalloc_track_caller(len, gfp); + if (!buf) + return NULL; + + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len - 1] = '\0'; + return buf; +} + + #endif /* __MM_INTERNAL_H */ diff --git a/mm/util.c b/mm/util.c index 41c7875572ed..d9135c5fdf7f 100644 --- a/mm/util.c +++ b/mm/util.c @@ -58,17 +58,8 @@ char *kstrdup(const char *s, gfp_t gfp) if (!s) 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'; - } - return buf; + len = strlen(s); + return __kstrndup(s, len + 1, gfp); } EXPORT_SYMBOL(kstrdup); @@ -111,12 +102,7 @@ char *kstrndup(const char *s, size_t max, gfp_t gfp) 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 __kstrndup(s, len + 1, gfp); } EXPORT_SYMBOL(kstrndup); @@ -195,12 +181,7 @@ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) if (!s) return NULL; - buf = kmalloc_track_caller(len + 1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return __kstrndup(s, len + 1, gfp); } EXPORT_SYMBOL(kmemdup_nul);