From owner-freebsd-current@freebsd.org Tue Aug 18 13:49:09 2020 Return-Path: Delivered-To: freebsd-current@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 69AA93BCBBF for ; Tue, 18 Aug 2020 13:49:09 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: from mailman.nyi.freebsd.org (unknown [127.0.1.3]) by mx1.freebsd.org (Postfix) with ESMTP id 4BWC1x1MKxz45DS for ; Tue, 18 Aug 2020 13:49:09 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: by mailman.nyi.freebsd.org (Postfix) id 2EACC3BCBBE; Tue, 18 Aug 2020 13:49:09 +0000 (UTC) Delivered-To: current@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 2E7353BCC49 for ; Tue, 18 Aug 2020 13:49:09 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BWC1v5Q2Rz45Vn for ; Tue, 18 Aug 2020 13:49:07 +0000 (UTC) (envelope-from markjdb@gmail.com) Received: by mail-qt1-x843.google.com with SMTP id h21so15093094qtp.11 for ; Tue, 18 Aug 2020 06:49:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to; bh=SQeASj8bDHt5R3W899mCyccOaslhXtSFBfIVLc81yKE=; b=f+s9qUNuSeV7WBMSWOz9zmXRL1BoMwpdjNY5aadHBfdy4k0oDTMh9HDjK6PHsUju+X M9/oNuZmH4qopnT/TOIa44As8xB6BXmIoBg6+3wdtRZi32YXqknkXkrD0WfoBXbzfn+N E+GyC9+3CpFbo9+mfbKou5IQGh/o+Bs05GAz1jaJr/geOWcW8QAlIvPS1L35WQVM/J7l YujmoTz6Arb66sHsSBEKfOD2SJ0bijBqT2Mwan5i9KcbzcGG4j1aZpjpkHtWGXBoE67a bEcYm77Lex/TWM5N2vCtPbuDgVvsyyzP+kX+TW4Dt5NQ6/3WDa7BKZLuz9+vfxmBrhhe XRNQ== X-Gm-Message-State: AOAM530S/AFHLPMiYCs3qRMVJWAZM7WKPIVo6CTtoxZPefHe5EN5Zkh1 WO//D/OCN8WSkdq5+eli3IE= X-Google-Smtp-Source: ABdhPJweKLpwvT1L8I2yGU6qx+dHeMY7ciB+0iUhgH8Tm2LpIfoJt89aEZc6WFLUWFXv0mLN9nJNLw== X-Received: by 2002:ac8:4511:: with SMTP id q17mr17671552qtn.117.1597758546295; Tue, 18 Aug 2020 06:49:06 -0700 (PDT) Received: from raichu (toroon0560w-lp130-08-67-71-176-35.dsl.bell.ca. [67.71.176.35]) by smtp.gmail.com with ESMTPSA id a21sm20618203qkg.54.2020.08.18.06.49.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Aug 2020 06:49:05 -0700 (PDT) Sender: Mark Johnston Date: Tue, 18 Aug 2020 09:49:00 -0400 From: Mark Johnston To: Mateusz Guzik Cc: current@freebsd.org Subject: Re: "panic: malloc(M_WAITOK) in non-sleepable context" after r364296 -> r364341 Message-ID: <20200818134900.GA28906@raichu> References: <20200818124419.GO1394@albert.catwhisker.org> <20200818125825.GP1394@albert.catwhisker.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 4BWC1v5Q2Rz45Vn X-Spamd-Bar: / X-Spamd-Result: default: False [0.42 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2607:f8b0:4000::/36]; NEURAL_SPAM_SHORT(0.12)[0.119]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[current@freebsd.org]; DMARC_NA(0.00)[freebsd.org]; RCVD_COUNT_THREE(0.00)[3]; TO_MATCH_ENVRCPT_SOME(0.00)[]; DKIM_TRACE(0.00)[gmail.com:+]; RCPT_COUNT_TWO(0.00)[2]; RCVD_IN_DNSWL_NONE(0.00)[2607:f8b0:4864:20::843:from]; MID_RHS_NOT_FQDN(0.50)[]; FREEMAIL_TO(0.00)[gmail.com]; FORGED_SENDER(0.30)[markj@freebsd.org,markjdb@gmail.com]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; FROM_NEQ_ENVFROM(0.00)[markj@freebsd.org,markjdb@gmail.com]; RCVD_TLS_ALL(0.00)[]; MAILMAN_DEST(0.00)[current] X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Aug 2020 13:49:09 -0000 On Tue, Aug 18, 2020 at 03:24:52PM +0200, Mateusz Guzik wrote: > Try this: > > diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c > index 46eb1c4347c..7b94ca7b880 100644 > --- a/sys/kern/kern_malloc.c > +++ b/sys/kern/kern_malloc.c > @@ -618,8 +618,8 @@ void * > unsigned long osize = size; > #endif > > - KASSERT((flags & M_WAITOK) == 0 || THREAD_CAN_SLEEP(), > - ("malloc(M_WAITOK) in non-sleepable context")); > + if ((flags & M_WAITOK) != 0) > + WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); > > #ifdef MALLOC_DEBUG > va = NULL; > diff --git a/sys/vm/uma_core.c b/sys/vm/uma_core.c > index 37d78354200..2e1267ec02f 100644 > --- a/sys/vm/uma_core.c > +++ b/sys/vm/uma_core.c > @@ -3355,8 +3355,8 @@ uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) > uma_cache_bucket_t bucket; > uma_cache_t cache; > > - KASSERT((flags & M_WAITOK) == 0 || THREAD_CAN_SLEEP(), > - ("uma_zalloc(M_WAITOK) in non-sleepable context")); > + if ((flags & M_WAITOK) != 0) > + WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); > > /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ > random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA); Doesn't it only print a warning if non-sleepable locks are held? THREAD_CAN_SLEEP() catches other cases, like epoch sections and worker threads that are not allowed to sleep (CAM doneq threads in this case). Otherwise uma_zalloc_debug() already checks with WITNESS. I'm inclined to simply revert the commit for now. Alternately, disk_alloc() could be modified to handle M_NOWAIT/M_WAITOK flags, and that could be used in daregister() and other CAM periph drivers. daregister() already uses M_NOWAIT to allocate the driver softc itself. diff --git a/sys/cam/scsi/scsi_da.c b/sys/cam/scsi/scsi_da.c index d648d511b1a2..d940e7db24e0 100644 --- a/sys/cam/scsi/scsi_da.c +++ b/sys/cam/scsi/scsi_da.c @@ -2767,18 +2767,23 @@ daregister(struct cam_periph *periph, void *arg) return(CAM_REQ_CMP_ERR); } - softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF, - M_NOWAIT|M_ZERO); - + softc = malloc(sizeof(*softc), M_DEVBUF, M_NOWAIT | M_ZERO); if (softc == NULL) { printf("daregister: Unable to probe new device. " "Unable to allocate softc\n"); return(CAM_REQ_CMP_ERR); } + softc->disk = disk_alloc_flags(M_NOWAIT); + if (softc->disk == NULL) { + printf("daregister: Unable to probe new device. " + "Unable to allocate disk structure\n"); + return (CAM_REQ_CMP_ERR); + } if (cam_iosched_init(&softc->cam_iosched, periph) != 0) { printf("daregister: Unable to probe new device. " "Unable to allocate iosched memory\n"); + disk_destroy(softc->disk); free(softc, M_DEVBUF); return(CAM_REQ_CMP_ERR); } @@ -2898,7 +2903,6 @@ daregister(struct cam_periph *periph, void *arg) /* * Register this media as a disk. */ - softc->disk = disk_alloc(); softc->disk->d_devstat = devstat_new_entry(periph->periph_name, periph->unit_number, 0, DEVSTAT_BS_UNAVAILABLE, diff --git a/sys/geom/geom_disk.c b/sys/geom/geom_disk.c index eaba770828d0..c07494ab4ec0 100644 --- a/sys/geom/geom_disk.c +++ b/sys/geom/geom_disk.c @@ -850,15 +850,22 @@ g_disk_ident_adjust(char *ident, size_t size) } struct disk * -disk_alloc(void) +disk_alloc_flags(int mflag) { struct disk *dp; - dp = g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO); - LIST_INIT(&dp->d_aliases); + dp = g_malloc(sizeof(struct disk), mflag | M_ZERO); + if (dp != NULL) + LIST_INIT(&dp->d_aliases); return (dp); } +struct disk * +disk_alloc(void) +{ + return (disk_alloc_flags(M_WAITOK)); +} + void disk_create(struct disk *dp, int version) { diff --git a/sys/geom/geom_disk.h b/sys/geom/geom_disk.h index 8e2282a09a3a..794ce2cc38bd 100644 --- a/sys/geom/geom_disk.h +++ b/sys/geom/geom_disk.h @@ -137,6 +137,7 @@ struct disk { #define DISKFLAG_WRITE_PROTECT 0x0100 struct disk *disk_alloc(void); +struct disk *disk_alloc_flags(int mflag); void disk_create(struct disk *disk, int version); void disk_destroy(struct disk *disk); void disk_gone(struct disk *disk);