From mboxrd@z Thu Jan 1 00:00:00 1970 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 CFEECC433EF for ; Fri, 15 Jul 2022 08:29:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B2129401D9; Fri, 15 Jul 2022 04:29:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 261C59401A5; Fri, 15 Jul 2022 04:29:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12AC49401D9; Fri, 15 Jul 2022 04:29:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 04A609401A5 for ; Fri, 15 Jul 2022 04:29:27 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AF1D6215A5 for ; Fri, 15 Jul 2022 08:29:26 +0000 (UTC) X-FDA: 79688659932.05.12EF5C9 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf18.hostedemail.com (Postfix) with ESMTP id 83F3D1C007B for ; Fri, 15 Jul 2022 08:29:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1657873765; x=1689409765; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=unf53x4o1mo9OvqXE99tnKwyqyec5nc6m4hit56NSFw=; b=FQfL7GACpHrHwhMeJdwzgZqEC9G5cLfm/yWsep6ZHE0RXqVivtEZoTeU t0ObI6csNcAn4vVqRHe3AtjHjgAh4WVZZtNseheV7oFbVMCk1gdx07Rbo Sab7JMqSJBfsIHB0j6j6XhgCvkk2veEpGXanCk5/bWeKk6qVpKxHXktC4 SoVcDxWgdWDKMDWKCKUl2zUs1dn6MTjhPanbnkKHvlqX9XpxAevd/LHgk tjkHtGme6LL7a7/KSJzN+OuEvI1tit4E1TR6UIu1d7wFAaRd0mnW7Yn5q q6sPUzE+QF+XeeiViHvcp8I6OpdZQfEZVUWExW14tYF4WJUJSKrxGiHtW w==; X-IronPort-AV: E=McAfee;i="6400,9594,10408"; a="286874503" X-IronPort-AV: E=Sophos;i="5.92,273,1650956400"; d="scan'208";a="286874503" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jul 2022 01:29:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,273,1650956400"; d="scan'208";a="571438868" Received: from shbuild999.sh.intel.com (HELO localhost) ([10.239.146.138]) by orsmga006.jf.intel.com with ESMTP; 15 Jul 2022 01:29:20 -0700 Date: Fri, 15 Jul 2022 16:29:22 +0800 From: Feng Tang To: Vlastimil Babka Cc: Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, linux-kernel@vger.kernel.org, dave.hansen@intel.com, Robin Murphy , John Garry Subject: Re: [PATCH v1] mm/slub: enable debugging memory wasting of kmalloc Message-ID: <20220715082922.GA88035@shbuild999.sh.intel.com> References: <20220701135954.45045-1-feng.tang@intel.com> <41763154-f923-ae99-55c0-0f3717636779@suse.cz> <20220713073642.GA69088@shbuild999.sh.intel.com> <45906408-34ce-4b79-fbe4-768335ffbf96@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <45906408-34ce-4b79-fbe4-768335ffbf96@suse.cz> ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657873766; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KvajyPuTDLOeeHgrQPEqGBAQDvOiwS/dke6lI7s53/4=; b=yZyR/lQWbY9B2HC2TBLyANgY/tfm4oD0JxkeptAQc00rshAoE4DRBDkatlnkCxfMOESTQr Creu+W+ITcvyJI1yqQ51PWrzA22yW+E+2H1L0npc4DhZfAUvjoJaIVAFeZOrz0ZQoT1+nS fyTd15xQtsgkVhCDohfC7qnygx3pfqM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657873766; a=rsa-sha256; cv=none; b=wuV7+Vl1zjxUGRvwKP7fs+wrOK33x9DIsnUwDaTEZvmAHTNJeocB6T7qFFTagSI7ZrKLGk iyC40Z+8q9csGNkjayMeCA352n8UJxFIkG/QIoL7uyNrT9WPAgXLERzqGnEoOcUyi/3rPr KZzL5kic1guWiHM8xzUKYpneTJO+x9g= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FQfL7GAC; spf=none (imf18.hostedemail.com: domain of feng.tang@intel.com has no SPF policy when checking 134.134.136.65) smtp.mailfrom=feng.tang@intel.com; dmarc=pass (policy=none) header.from=intel.com Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=FQfL7GAC; spf=none (imf18.hostedemail.com: domain of feng.tang@intel.com has no SPF policy when checking 134.134.136.65) smtp.mailfrom=feng.tang@intel.com; dmarc=pass (policy=none) header.from=intel.com X-Rspamd-Queue-Id: 83F3D1C007B X-Stat-Signature: c1p18tsz83hpck8ndme9qqpjg8azjpxb X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1657873765-59260 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: On Thu, Jul 14, 2022 at 10:11:32PM +0200, Vlastimil Babka wrote: > On 7/13/22 09:36, Feng Tang wrote: > > Hi Vlastimil, > > > > On Mon, Jul 11, 2022 at 10:15:21AM +0200, Vlastimil Babka wrote: > >> On 7/1/22 15:59, Feng Tang wrote: > >> > kmalloc's API family is critical for mm, with one shortcoming that > >> > its object size is fixed to be power of 2. When user requests memory > >> > for '2^n + 1' bytes, actually 2^(n+1) bytes will be allocated, so > >> > in worst case, there is around 50% memory space waste. > >> > > >> > We've met a kernel boot OOM panic (v5.10), and from the dumped slab info: > >> > > >> > [ 26.062145] kmalloc-2k 814056KB 814056KB > >> > > >> > From debug we found there are huge number of 'struct iova_magazine', > >> > whose size is 1032 bytes (1024 + 8), so each allocation will waste > >> > 1016 bytes. Though the issue was solved by giving the right (bigger) > >> > size of RAM, it is still nice to optimize the size (either use a > >> > kmalloc friendly size or create a dedicated slab for it). > > [...] > >> > >> Hi and thanks. > >> I would suggest some improvements to consider: > >> > >> - don't use the struct track to store orig_size, although it's an obvious > >> first choice. It's unused waste for the free_track, and also for any > >> non-kmalloc caches. I'd carve out an extra int next to the struct tracks. > >> Only for kmalloc caches (probably a new kmem cache flag set on creation will > >> be needed to easily distinguish them). > >> Besides the saved space, you can then set the field from ___slab_alloc() > >> directly and not need to pass the orig_size also to alloc_debug_processing() > >> etc. > > > > Here is a draft patch fowlling your suggestion, please check if I missed > > anything? (Quick test showed it achived similar effect as v1 patch). Thanks! > > Thanks, overal it looks at first glance! Thanks! > > --- > > diff --git a/include/linux/slab.h b/include/linux/slab.h > > index 0fefdf528e0d..d3dacb0f013f 100644 > > --- a/include/linux/slab.h > > +++ b/include/linux/slab.h > > @@ -29,6 +29,8 @@ > > #define SLAB_RED_ZONE ((slab_flags_t __force)0x00000400U) > > /* DEBUG: Poison objects */ > > #define SLAB_POISON ((slab_flags_t __force)0x00000800U) > > +/* Indicate a slab of kmalloc */ > > "Indicate a kmalloc cache" would be more precise. Will use this in next version. > > +#define SLAB_KMALLOC ((slab_flags_t __force)0x00001000U) > > /* Align objs on cache lines */ > > #define SLAB_HWCACHE_ALIGN ((slab_flags_t __force)0x00002000U) > > /* Use GFP_DMA memory */ > > diff --git a/mm/slub.c b/mm/slub.c > > index 26b00951aad1..3b0f80927817 100644 > > > > > > >> - the knowledge of actual size could be used to improve poisoning checks as > >> well, detect cases when there's buffer overrun over the orig_size but not > >> cache's size. e.g. if you kmalloc(48) and overrun up to 64 we won't detect > >> it now, but with orig_size stored we could? > > > > The above patch doesn't touch this. As I have a question, for the > > [orib_size, object_size) area, shall we fill it with POISON_XXX no matter > > REDZONE flag is set or not? > > Ah, looks like we use redzoning, not poisoning, for padding from > s->object_size to word boundary. So it would be more consistent to use the > redzone pattern (RED_ACTIVE) and check with the dynamic orig_size. Probably > no change for RED_INACTIVE handling is needed though. Thanks for clarifying, will go this way and do more test. Also I'd make it a separate patch, as it is logically different from the space wastage. Thanks, Feng