Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp4944426imb; Thu, 7 Mar 2019 04:24:27 -0800 (PST) X-Google-Smtp-Source: APXvYqyZ9yEJv0xAQIW+DsVdZrUJrU5FNi0Lx7FHf5Q22FMtNrQ8sGfLovKblY5qvKhTsmA6wUg1 X-Received: by 2002:a63:4e1a:: with SMTP id c26mr11088689pgb.175.1551961467470; Thu, 07 Mar 2019 04:24:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551961467; cv=none; d=google.com; s=arc-20160816; b=LtMSxDK53ViDTn+dV1BgKHGjEsAO+lKrRGRs0xLWURwCQpTyHHxlQODD9RdwGkRP5/ XVsESOD2/zn+U2NzUr9mppjnqKMsTf8QkF8tU4IQityTi2kY4+MBO5mb3cnaGOVcUbqp dOZFlalM8YEJsSqlkU/0zso9gx83U9ZcV+IZxyBTjQLWPNL4mbIqF+NHMO6Drz1lkYfO Bm1b3JDcQyD8poybX+kc4SRUw1i6rGq4XzGXH9NmtB7TJoP2S11KXrF4My9N9067CSOW i5K3JDCaCc681MZAytxCC32GvbUCwKfS4mTpuuRC3b9hXE1oHkDV9BaRkUIdHbmKSu99 oT3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=zSWm04rXGBPGSYGdHAsTOmeMDKXKZIjRH2V1sEFUJmk=; b=aXXnPFVZ+QmzXz8Xkp+XZRd+dGXhNm7JGhJUspAPBajyPu0dKC+9kV1B9G0XACy4n8 j+mCaMhE/+1enp0St8w41es97GR01tUbUZ/VMjyOs48qp67Ai/RUzbaBlJE6OZqpN4PE pFJKWwMkQdgb2ruDDd5Ti7NSTFnrj0J0eXoo8rtgpuULQDmO4/Yys42xvIGS4GmYkI9Q xHxIqv1FRat3CEKUd0a38eH+MoOcbMzTQcea8zkN0i5lxspypFpdA+vnsRDuRuoWgwLz cm2Xs8nxlZ1hRN6toqndKzhPE/YWoZ5QHb6jtHcfRRdlO59inDGgO6Ad5E7DuMXB0kto sC4A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id cb3si4231253plb.254.2019.03.07.04.24.11; Thu, 07 Mar 2019 04:24:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726297AbfCGMXQ (ORCPT + 99 others); Thu, 7 Mar 2019 07:23:16 -0500 Received: from mx2.suse.de ([195.135.220.15]:43586 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726127AbfCGMXP (ORCPT ); Thu, 7 Mar 2019 07:23:15 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 7DE58BE21; Thu, 7 Mar 2019 12:23:14 +0000 (UTC) Date: Thu, 7 Mar 2019 13:23:13 +0100 From: Michal Hocko To: Pi-Hsun Shih Cc: Andrew Morton , Matthew Wilcox , Johannes Weiner , Vlastimil Babka , Minchan Kim , Omar Sandoval , Huang Ying , Aaron Lu , Tejun Heo , Wei Yang , open list Subject: Re: [PATCH] mm/swap: Avoid undefined behavior in __swapoffset Message-ID: <20190307122313.GA30234@dhcp22.suse.cz> References: <20190307094653.119425-1-pihsun@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190307094653.119425-1-pihsun@chromium.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu 07-03-19 17:46:50, Pi-Hsun Shih wrote: > Use offsetof to calculate offset of a field to avoid UBSAN warning like: > > =================================================================== > UBSAN: Undefined behaviour in mm/swapfile.c:3010:38 > member access within null pointer of type 'union swap_header' > CPU: 6 PID: 1833 Comm: swapon Tainted: G S 4.19.23 #43 > Call trace: > dump_backtrace+0x0/0x194 > show_stack+0x20/0x2c > __dump_stack+0x20/0x28 > dump_stack+0x70/0x94 > ubsan_epilogue+0x14/0x44 > ubsan_type_mismatch_common+0xf4/0xfc > __ubsan_handle_type_mismatch_v1+0x34/0x54 > __se_sys_swapon+0x654/0x1084 > __arm64_sys_swapon+0x1c/0x24 > el0_svc_common+0xa8/0x150 > el0_svc_compat_handler+0x2c/0x38 > el0_svc_compat+0x8/0x18 > ================================================================== Could you be more specific about what exactly is undefined here and why offsetof is any better. AFAIR it uses the same construct unless a compiler defines a built in. I do not object the change itself because it is cleaner to use the existing helper but I am wondering why this is fixing ubsan. Is ubsan defining the compiler variant and consider it safe? > > Signed-off-by: Pi-Hsun Shih > --- > include/linux/swap.h | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/include/linux/swap.h b/include/linux/swap.h > index fc50e21b3b88..4bfb5c4ac108 100644 > --- a/include/linux/swap.h > +++ b/include/linux/swap.h > @@ -157,9 +157,9 @@ struct swap_extent { > /* > * Max bad pages in the new format.. > */ > -#define __swapoffset(x) ((unsigned long)&((union swap_header *)0)->x) > #define MAX_SWAP_BADPAGES \ > - ((__swapoffset(magic.magic) - __swapoffset(info.badpages)) / sizeof(int)) > + ((offsetof(union swap_header, magic.magic) - \ > + offsetof(union swap_header, info.badpages)) / sizeof(int)) > > enum { > SWP_USED = (1 << 0), /* is slot in swap_info[] used? */ > -- > 2.21.0.352.gf09ad66450-goog > -- Michal Hocko SUSE Labs