Received: by 2002:a05:6a10:8a4f:0:0:0:0 with SMTP id dn15csp2600450pxb; Mon, 31 Jan 2022 03:40:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJxbaqq1SAC52nv3Npql0DBo6AnT9zqHT/2/7v53vLkuRwn3suu0DK1GyhYcUa+JVJVxHmTF X-Received: by 2002:a17:90a:fe93:: with SMTP id co19mr23964559pjb.147.1643629227900; Mon, 31 Jan 2022 03:40:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643629227; cv=none; d=google.com; s=arc-20160816; b=vcUOQunRjx3dpiELeA2aS1okCqNSJiPZUL8FjQVnkuZ9flKIBwdsCCxSfnmFIfiRqG 0wvQp+nyttNafd5IUVdTNeSaNA6ALurK4dGu1rvkHWAOWA6042K8KsSTNTINC5DaWqE+ 9Ccaef5w0Ky4esfs/74EvEL4k3EcttTFxaNOK8TtKxAdNVX023JVwtYGodDd30/jK1Xn OJImkC04yFfLwpqB6ajUbzhNmVj1e4d6fmzlNM+YzoB6gq2Fspdu5U6mls53CVEB2J4e 0rfcL66fh8tf12eZbhkAAVaCJ/CbYV9jx0jIdWB0Vvf8/bl8Q4wJEm5fwPFghGZAT3US 84Fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=Hvrb/vy89ZNYuwBLWpFtg9jTrt+DtAEe9H5nZb7p2Ts=; b=rcgVTbq+DvwID0c5/Yuuzs+NOVO2IlidBzjcrcwb4Fiyv+XqsKCNkTF2W5OhtToMaD 5CUmnRFcP2KCB57FLYWGN4KusJ6U9ed+2nL4eeg3rdtnJ495ZqnszhKzOPoLnJFBSzkM 3MQ+KaFt9a8i9+OZY3lu6b54rptgh5Pfb3QBpV0I9tXUsJDHS+oDGRxrSQt0GjopWbcs ZwOfaO0vwlq2SJauBtz3GDYwCPU8SrEFdxyvPeniDhpcSddUIfYJpFJLtlkrxLvYMH1b Gy8pDgW++mEs9PmmgFs0WSgA+g6TkHNwGNDpBHBkB1xBlIljx9FVfsu+5f3Ipj2epzl2 TuAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=G997xVP6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u3si12486068pgk.198.2022.01.31.03.40.17; Mon, 31 Jan 2022 03:40:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=G997xVP6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350554AbiA1VXE (ORCPT + 99 others); Fri, 28 Jan 2022 16:23:04 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:58676 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243744AbiA1VXD (ORCPT ); Fri, 28 Jan 2022 16:23:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643404982; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Hvrb/vy89ZNYuwBLWpFtg9jTrt+DtAEe9H5nZb7p2Ts=; b=G997xVP6dOHFI3Dqtez/fBYGuFLDUojcU/gzOgVu/XU651vIcG5fPhwhWd0llKaUlvFOeQ SQoUiTT1r/F7JG6qQUdoyGwR0H6IsjEEMH3uYGIhzu7qgCwMm7xHRfDHnTvQILTy9O6v30 m8De1S8kTzscTnx+8xkHKQkGw4TVeHI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-97-8F_YfrERNuiFEiWvU914Kg-1; Fri, 28 Jan 2022 16:22:59 -0500 X-MC-Unique: 8F_YfrERNuiFEiWvU914Kg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 32C901006AA8; Fri, 28 Jan 2022 21:22:57 +0000 (UTC) Received: from [10.22.33.1] (unknown [10.22.33.1]) by smtp.corp.redhat.com (Postfix) with ESMTP id F01564E2A1; Fri, 28 Jan 2022 21:22:55 +0000 (UTC) Message-ID: <2b68eb66-db17-a8a2-2a07-48b8584da841@redhat.com> Date: Fri, 28 Jan 2022 16:22:55 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: [PATCH 1/2] mm/page_owner: Introduce SNPRINTF() macro that includes length error check Content-Language: en-US To: Ira Weiny Cc: Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Rafael Aquini References: <20220128195642.416743-1-longman@redhat.com> <20220128195642.416743-2-longman@redhat.com> <20220128211848.GH785175@iweiny-DESK2.sc.intel.com> From: Waiman Long In-Reply-To: <20220128211848.GH785175@iweiny-DESK2.sc.intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 1/28/22 16:18, Ira Weiny wrote: > On Fri, Jan 28, 2022 at 02:56:41PM -0500, Waiman Long wrote: >> In print_page_owner(), there is a repetitive check after each snprintf() >> to make sure that the final length is less than the buffer size. Simplify >> the code and make it easier to read by embedding the buffer length >> check and increment inside the macro. > This does not follow the kernel coding style of not putting control flow > statements in macros.[1] OK, fair enough. I can remove the macro and repost the patch. Cheers, Longman >> Signed-off-by: Waiman Long >> --- >> mm/page_owner.c | 50 +++++++++++++++++++++++-------------------------- >> 1 file changed, 23 insertions(+), 27 deletions(-) > And in the end you only saved 4 lines of code to read... Not worth it IMO. > > Ira > > [1] https://www.kernel.org/doc/html/v4.17/process/coding-style.html > >> diff --git a/mm/page_owner.c b/mm/page_owner.c >> index 99e360df9465..c52ce9d6bc3b 100644 >> --- a/mm/page_owner.c >> +++ b/mm/page_owner.c >> @@ -325,12 +325,20 @@ void pagetypeinfo_showmixedcount_print(struct seq_file *m, >> seq_putc(m, '\n'); >> } >> >> +#define SNPRINTF(_buf, _size, _len, _err, _fmt, ...) \ >> + do { \ >> + _len += snprintf(_buf + _len, _size - _len, _fmt, \ >> + ##__VA_ARGS__); \ >> + if (_len >= _size) \ >> + goto _err; \ >> + } while (0) >> + >> static ssize_t >> print_page_owner(char __user *buf, size_t count, unsigned long pfn, >> struct page *page, struct page_owner *page_owner, >> depot_stack_handle_t handle) >> { >> - int ret, pageblock_mt, page_mt; >> + int ret = 0, pageblock_mt, page_mt; >> char *kbuf; >> >> count = min_t(size_t, count, PAGE_SIZE); >> @@ -338,44 +346,32 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn, >> if (!kbuf) >> return -ENOMEM; >> >> - ret = snprintf(kbuf, count, >> - "Page allocated via order %u, mask %#x(%pGg), pid %d, ts %llu ns, free_ts %llu ns\n", >> - page_owner->order, page_owner->gfp_mask, >> - &page_owner->gfp_mask, page_owner->pid, >> - page_owner->ts_nsec, page_owner->free_ts_nsec); >> - >> - if (ret >= count) >> - goto err; >> + SNPRINTF(kbuf, count, ret, err, >> + "Page allocated via order %u, mask %#x(%pGg), pid %d, ts %llu ns, free_ts %llu ns\n", >> + page_owner->order, page_owner->gfp_mask, >> + &page_owner->gfp_mask, page_owner->pid, >> + page_owner->ts_nsec, page_owner->free_ts_nsec); >> >> /* Print information relevant to grouping pages by mobility */ >> pageblock_mt = get_pageblock_migratetype(page); >> page_mt = gfp_migratetype(page_owner->gfp_mask); >> - ret += snprintf(kbuf + ret, count - ret, >> - "PFN %lu type %s Block %lu type %s Flags %pGp\n", >> - pfn, >> - migratetype_names[page_mt], >> - pfn >> pageblock_order, >> - migratetype_names[pageblock_mt], >> - &page->flags); >> - >> - if (ret >= count) >> - goto err; >> + SNPRINTF(kbuf, count, ret, err, >> + "PFN %lu type %s Block %lu type %s Flags %pGp\n", >> + pfn, migratetype_names[page_mt], >> + pfn >> pageblock_order, >> + migratetype_names[pageblock_mt], >> + &page->flags); >> >> ret += stack_depot_snprint(handle, kbuf + ret, count - ret, 0); >> if (ret >= count) >> goto err; >> >> - if (page_owner->last_migrate_reason != -1) { >> - ret += snprintf(kbuf + ret, count - ret, >> + if (page_owner->last_migrate_reason != -1) >> + SNPRINTF(kbuf, count, ret, err, >> "Page has been migrated, last migrate reason: %s\n", >> migrate_reason_names[page_owner->last_migrate_reason]); >> - if (ret >= count) >> - goto err; >> - } >> >> - ret += snprintf(kbuf + ret, count - ret, "\n"); >> - if (ret >= count) >> - goto err; >> + SNPRINTF(kbuf, count, ret, err, "\n"); >> >> if (copy_to_user(buf, kbuf, ret)) >> ret = -EFAULT; >> -- >> 2.27.0 >> >>