Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp4961577imb; Thu, 7 Mar 2019 04:48:57 -0800 (PST) X-Google-Smtp-Source: APXvYqwKIqgzo6uFnn8GJtEXHKGbcXjkVC/oltMotywk271BB2qUi9oZctaQw9y09QiprGl4jvZj X-Received: by 2002:a63:20a:: with SMTP id 10mr11237448pgc.346.1551962937347; Thu, 07 Mar 2019 04:48:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551962937; cv=none; d=google.com; s=arc-20160816; b=t9Lz9PMQ90kre4Ot1Og1zhaeC6A1f3CNFtdi5XwfZpDO2EKFfwcGwtqY2z5G8mjOII YQGlPiarzkqCFqMTWxA3XOUIvBi8gM3Mx5wwT96QnGc+4+Xc64ldRZK9nWSX16m9VFvY EHofEeKOjsFcXfTJWReqJZp0HJCTJj6RyZq7eP2x8wrNH+2UJC6Mnk3sbiCCynDCfEOh XTvy9Mgu+9OByY9geA7jCUueRbWXnVD203ozUy+SPkiLlaKEspltxHm9+S3yCnsLz88U WuNE5bFLh4uRg26nLnD7xaqSyxPjR5O2jBtQINZydA5KG2UnGwvn0d9dDuxuTWBxo6JN fXpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=dOtWjFZq01U/8PDkXc5mo9dz58CkJjmkXbnJTRTwgVk=; b=RCGCyZEVQhxKETSvXxw2qIMyncRFgUflkTyMfBlyd7WL2qlBbfMYUWqz6ehZNMseHb OyNkdFi4RecKsZkRdkwnmuqTxHh22hUBHfn2ArxRxcnB3EmBTBgwkaY9Yv5GSUTnEvP3 h4dtrQglixr+1me8EOi086wDLG1Y57lF/mz7j3DBM/uHth4uK6XScFdMOg6rEa6hEHV5 /1qyfMJMjG+F7GEDaql5AUasSd27h1V2CYZFN+Z6DvloqPy6NOivPqP/Lqzcp7i3OJxV KGt7fNO9RB7XHGPaInQHQLhoySL2ZWpjwVDDUrw6jE4mx7bwHMTfc5UP+rC6ndOr70lz q5Tg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=k5rddsSa; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j70si3856819pgd.65.2019.03.07.04.48.40; Thu, 07 Mar 2019 04:48:57 -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; dkim=pass header.i=@chromium.org header.s=google header.b=k5rddsSa; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726196AbfCGMsF (ORCPT + 99 others); Thu, 7 Mar 2019 07:48:05 -0500 Received: from mail-ed1-f68.google.com ([209.85.208.68]:41214 "EHLO mail-ed1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726101AbfCGMsF (ORCPT ); Thu, 7 Mar 2019 07:48:05 -0500 Received: by mail-ed1-f68.google.com with SMTP id x7so13333096eds.8 for ; Thu, 07 Mar 2019 04:48:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=dOtWjFZq01U/8PDkXc5mo9dz58CkJjmkXbnJTRTwgVk=; b=k5rddsSai+sjEyMsVU1uPaFRfRdsxKnB6OGFgRdic/TUo6aMhSjZujbZ1XwYWr3Qi0 dXgxone6DBfzBwm8S5UfE8DSxkweWcRNsGfuTDgo0uUBog9Po1UsfwJxEUKk/pCYMA0m WtvSKJAilggzab6VYXyxNnclToAsTEEGk0T9Q= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=dOtWjFZq01U/8PDkXc5mo9dz58CkJjmkXbnJTRTwgVk=; b=A6vEV42kH8ARwU/NMaNOB25ow11ZjKLdRWQNKpE/IY72A/kBR4a2hpp7jvKODo1VVl cMHRFUQ02FKOyBa2qfsvVsWX7oWRLmrdU+uDHBGD0XDtz3SaFHs3rTRde/G9IZvWp1NH ZwYA+eFz71wS7TYhaGnDDB5DmbCIMn0m+Vd4smZrik9NLKW1BKZ73z3x1BbDhK+O9nHO B3J2G1GOQRvPO5RDJhM7dS+aCNlX486SOtr7q9aAuFoDgbqbBqwmBDkjnpXxEEsakn+n +/NyWSQBrkXqRkRJp6yHueENyD/bEQdryA7nVqw0T1env/ubIDTTGAR9LylffIsgUZJi cQOg== X-Gm-Message-State: APjAAAX/cLieuFSSiZsAkpoSqWQDXChAi/12WmrVnckuBv5uBlh6Sozz PUjW5sDhQNYWBiCdpTw5ln8HHMDxiimxtluU4KinKw== X-Received: by 2002:a50:bb21:: with SMTP id y30mr27058696ede.156.1551962883220; Thu, 07 Mar 2019 04:48:03 -0800 (PST) MIME-Version: 1.0 References: <20190307094653.119425-1-pihsun@chromium.org> <20190307122313.GA30234@dhcp22.suse.cz> In-Reply-To: <20190307122313.GA30234@dhcp22.suse.cz> From: Pi-Hsun Shih Date: Thu, 7 Mar 2019 20:47:52 +0800 Message-ID: Subject: Re: [PATCH] mm/swap: Avoid undefined behavior in __swapoffset To: Michal Hocko Cc: Andrew Morton , Matthew Wilcox , Johannes Weiner , Vlastimil Babka , Minchan Kim , Omar Sandoval , Huang Ying , Aaron Lu , Tejun Heo , Wei Yang , open list Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 7, 2019 at 8:23 PM Michal Hocko wrote: > > 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? > The undefined behavior is from trying to accessing a member of NULL, even not using it value but only use the address. Since the compiler variant for offsetof is used for recent compiler (GCC >= 4 has support for it), ubsan would not warn if the compiler variant is used. For old compiler, I guess ubsan would complain on all offsetof uses.